Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions
Bootstrap Class Reference

Public Member Functions

 __construct (ObjectManagerFactory $factory, $rootDir, array $initParams)
 
 getParams ()
 
 createApplication ($type, $arguments=[])
 
 run (AppInterface $application)
 
 getObjectManager ()
 
 getErrorCode ()
 
 isDeveloperMode ()
 

Static Public Member Functions

static create ($rootDir, array $initParams, ObjectManagerFactory $factory=null)
 
static populateAutoloader ($rootDir, $initParams)
 
static createObjectManagerFactory ($rootDir, array $initParams)
 
static createFilesystemDirectoryList ($rootDir, array $initParams)
 
static createFilesystemDriverPool (array $initParams)
 
static createConfigFilePool ()
 

Data Fields

const ERR_MAINTENANCE = 901
 
const ERR_IS_INSTALLED = 902
 
const PARAM_REQUIRE_MAINTENANCE = 'MAGE_REQUIRE_MAINTENANCE'
 
const PARAM_REQUIRE_IS_INSTALLED = 'MAGE_REQUIRE_IS_INSTALLED'
 
const DEFAULT_REQUIRE_MAINTENANCE = false
 
const DEFAULT_REQUIRE_IS_INSTALLED = true
 
const INIT_PARAM_FILESYSTEM_DIR_PATHS = 'MAGE_DIRS'
 
const INIT_PARAM_FILESYSTEM_DRIVERS = 'MAGE_FILESYSTEM_DRIVERS'
 

Protected Member Functions

 assertInstalled ()
 
 terminate (\Exception $e)
 

Detailed Description

A bootstrap of Magento application

Performs basic initialization root function: injects init parameters and creates object manager Can create/run applications

@api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 27 of file Bootstrap.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ObjectManagerFactory  $factory,
  $rootDir,
array  $initParams 
)

Constructor

Parameters
ObjectManagerFactory$factory
string$rootDir
array$initParams

Definition at line 205 of file Bootstrap.php.

206  {
207  $this->factory = $factory;
208  $this->rootDir = $rootDir;
209  $this->server = $initParams;
210  $this->objectManager = $this->factory->create($this->server);
211  }

Member Function Documentation

◆ assertInstalled()

assertInstalled ( )
protected

Asserts whether application is installed

Returns
void
Exceptions

Definition at line 311 of file Bootstrap.php.

312  {
313  $isExpected = $this->getIsExpected(self::PARAM_REQUIRE_IS_INSTALLED, self::DEFAULT_REQUIRE_IS_INSTALLED);
314  if (null === $isExpected) {
315  return;
316  }
317  $isInstalled = $this->isInstalled();
318  if (!$isInstalled && $isExpected) {
319  $this->errorCode = self::ERR_IS_INSTALLED;
320  throw new \Exception('Error: Application is not installed yet. ');
321  }
322  if ($isInstalled && !$isExpected) {
323  $this->errorCode = self::ERR_IS_INSTALLED;
324  throw new \Exception('Error: Application is already installed. ');
325  }
326  }

◆ create()

static create (   $rootDir,
array  $initParams,
ObjectManagerFactory  $factory = null 
)
static

Static method so that client code does not have to create Object Manager Factory every time Bootstrap is called

Parameters
string$rootDir
array$initParams
ObjectManagerFactory$factory
Returns
Bootstrap

Definition at line 119 of file Bootstrap.php.

120  {
121  self::populateAutoloader($rootDir, $initParams);
122  if ($factory === null) {
123  $factory = self::createObjectManagerFactory($rootDir, $initParams);
124  }
125  return new self($factory, $rootDir, $initParams);
126  }
static createObjectManagerFactory($rootDir, array $initParams)
Definition: Bootstrap.php:149
static populateAutoloader($rootDir, $initParams)
Definition: Bootstrap.php:135

◆ createApplication()

createApplication (   $type,
  $arguments = [] 
)

Factory method for creating application instances

Parameters
string$type
array$arguments
Returns
\Magento\Framework\AppInterface
Exceptions

Definition at line 231 of file Bootstrap.php.

232  {
233  try {
234  $application = $this->objectManager->create($type, $arguments);
235  if (!($application instanceof AppInterface)) {
236  throw new \InvalidArgumentException("The provided class doesn't implement AppInterface: {$type}");
237  }
238  return $application;
239  } catch (\Exception $e) {
240  $this->terminate($e);
241  }
242  }
$type
Definition: item.phtml:13
$application
Definition: bootstrap.php:58
$arguments

◆ createConfigFilePool()

static createConfigFilePool ( )
static

Creates instance of configuration files pool

Returns
DriverPool

Definition at line 193 of file Bootstrap.php.

194  {
195  return new ConfigFilePool();
196  }

◆ createFilesystemDirectoryList()

static createFilesystemDirectoryList (   $rootDir,
array  $initParams 
)
static

Creates instance of filesystem directory list

Parameters
string$rootDir
array$initParams
Returns
DirectoryList

Definition at line 164 of file Bootstrap.php.

165  {
166  $customDirs = [];
167  if (isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS])) {
168  $customDirs = $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
169  }
170  return new DirectoryList($rootDir, $customDirs);
171  }

◆ createFilesystemDriverPool()

static createFilesystemDriverPool ( array  $initParams)
static

Creates instance of filesystem driver pool

Parameters
array$initParams
Returns
DriverPool

Definition at line 179 of file Bootstrap.php.

180  {
181  $extraDrivers = [];
182  if (isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS])) {
183  $extraDrivers = $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS];
184  }
185  return new DriverPool($extraDrivers);
186  }

◆ createObjectManagerFactory()

static createObjectManagerFactory (   $rootDir,
array  $initParams 
)
static

Creates instance of object manager factory

Parameters
string$rootDir
array$initParams
Returns
ObjectManagerFactory

Definition at line 149 of file Bootstrap.php.

150  {
151  $dirList = self::createFilesystemDirectoryList($rootDir, $initParams);
152  $driverPool = self::createFilesystemDriverPool($initParams);
153  $configFilePool = self::createConfigFilePool();
154  return new ObjectManagerFactory($dirList, $driverPool, $configFilePool);
155  }
if(!file_exists($installConfigFile)) $dirList
Definition: bootstrap.php:57
static createFilesystemDirectoryList($rootDir, array $initParams)
Definition: Bootstrap.php:164
static createFilesystemDriverPool(array $initParams)
Definition: Bootstrap.php:179

◆ getErrorCode()

getErrorCode ( )

Getter for error code

Returns
int

Definition at line 386 of file Bootstrap.php.

387  {
388  return $this->errorCode;
389  }

◆ getObjectManager()

getObjectManager ( )

Gets the object manager instance

Returns
\Magento\Framework\ObjectManagerInterface

Definition at line 365 of file Bootstrap.php.

366  {
367  return $this->objectManager;
368  }

◆ getParams()

getParams ( )

Gets the current parameters

Returns
array

Definition at line 218 of file Bootstrap.php.

219  {
220  return $this->server;
221  }

◆ isDeveloperMode()

isDeveloperMode ( )

Checks whether developer mode is set in the initialization parameters

Returns
bool

Definition at line 396 of file Bootstrap.php.

397  {
398  $mode = 'default';
399  if (isset($this->server[State::PARAM_MODE])) {
400  $mode = $this->server[State::PARAM_MODE];
401  } else {
402  $deploymentConfig = $this->getObjectManager()->get(DeploymentConfig::class);
403  $configMode = $deploymentConfig->get(State::PARAM_MODE);
404  if ($configMode) {
405  $mode = $configMode;
406  }
407  }
408 
409  return $mode == State::MODE_DEVELOPER;
410  }
$deploymentConfig
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ populateAutoloader()

static populateAutoloader (   $rootDir,
  $initParams 
)
static

Populates autoloader with mapping info

Parameters
string$rootDir
array$initParams
Returns
void

Definition at line 135 of file Bootstrap.php.

136  {
137  $dirList = self::createFilesystemDirectoryList($rootDir, $initParams);
140  }
if(!file_exists($installConfigFile)) $dirList
Definition: bootstrap.php:57
static populateMappings(AutoloaderInterface $autoloader, DirectoryList $dirList)
Definition: Populator.php:22
$autoloadWrapper
Definition: autoload.php:12
static createFilesystemDirectoryList($rootDir, array $initParams)
Definition: Bootstrap.php:164

◆ run()

run ( AppInterface  $application)

Runs an application

Parameters
\Magento\Framework\AppInterface$application
Returns
void

Definition at line 250 of file Bootstrap.php.

251  {
252  try {
253  try {
254  \Magento\Framework\Profiler::start('magento');
255  $this->initErrorHandler();
256  $this->assertMaintenance();
257  $this->assertInstalled();
258  $response = $application->launch();
259  $response->sendResponse();
260  \Magento\Framework\Profiler::stop('magento');
261  } catch (\Exception $e) {
262  \Magento\Framework\Profiler::stop('magento');
263  $this->objectManager->get(LoggerInterface::class)->error($e->getMessage());
264  if (!$application->catchException($this, $e)) {
265  throw $e;
266  }
267  }
268  } catch (\Exception $e) {
269  $this->terminate($e);
270  }
271  }
$response
Definition: 404.php:11
$application
Definition: bootstrap.php:58

◆ terminate()

terminate ( \Exception  $e)
protected

Display an exception and terminate program execution

Parameters
\Exception$e
Returns
void @SuppressWarnings(PHPMD.ExitExpression)

Definition at line 419 of file Bootstrap.php.

420  {
421  if ($this->isDeveloperMode()) {
422  echo $e;
423  } else {
424  $message = "An error has happened during application run. See exception log for details.\n";
425  try {
426  if (!$this->objectManager) {
427  throw new \DomainException();
428  }
429  $this->objectManager->get(LoggerInterface::class)->critical($e);
430  } catch (\Exception $e) {
431  $message .= "Could not write error message to log. Please use developer mode to see the message.\n";
432  }
433  echo $message;
434  }
435  exit(1);
436  }
$message
exit
Definition: redirect.phtml:12

Field Documentation

◆ DEFAULT_REQUIRE_IS_INSTALLED

const DEFAULT_REQUIRE_IS_INSTALLED = true

Definition at line 56 of file Bootstrap.php.

◆ DEFAULT_REQUIRE_MAINTENANCE

const DEFAULT_REQUIRE_MAINTENANCE = false

#- #+ Default behavior of bootstrap assertions

Definition at line 55 of file Bootstrap.php.

◆ ERR_IS_INSTALLED

const ERR_IS_INSTALLED = 902

Definition at line 33 of file Bootstrap.php.

◆ ERR_MAINTENANCE

const ERR_MAINTENANCE = 901

#+ Possible errors that can be triggered by the bootstrap

Definition at line 32 of file Bootstrap.php.

◆ INIT_PARAM_FILESYSTEM_DIR_PATHS

const INIT_PARAM_FILESYSTEM_DIR_PATHS = 'MAGE_DIRS'

#- Initialization parameter for custom directory paths

Definition at line 62 of file Bootstrap.php.

◆ INIT_PARAM_FILESYSTEM_DRIVERS

const INIT_PARAM_FILESYSTEM_DRIVERS = 'MAGE_FILESYSTEM_DRIVERS'

Initialization parameter for additional filesystem drivers

Definition at line 67 of file Bootstrap.php.

◆ PARAM_REQUIRE_IS_INSTALLED

const PARAM_REQUIRE_IS_INSTALLED = 'MAGE_REQUIRE_IS_INSTALLED'

Definition at line 49 of file Bootstrap.php.

◆ PARAM_REQUIRE_MAINTENANCE

const PARAM_REQUIRE_MAINTENANCE = 'MAGE_REQUIRE_MAINTENANCE'

#- #+ Initialization parameters that allow control bootstrap behavior of asserting maintenance mode or is installed

Possible values:

  • true – set expectation that it is required
  • false – set expectation that is required not to
  • null – bypass the assertion completely

If key is absent in the parameters array, the default behavior will be used

See also
DEFAULT_REQUIRE_MAINTENANCE
DEFAULT_REQUIRE_IS_INSTALLED

Definition at line 48 of file Bootstrap.php.


The documentation for this class was generated from the following file: