Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Bootstrap.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework\App;
8 
15 use Psr\Log\LoggerInterface;
16 
27 class Bootstrap
28 {
32  const ERR_MAINTENANCE = 901;
33  const ERR_IS_INSTALLED = 902;
48  const PARAM_REQUIRE_MAINTENANCE = 'MAGE_REQUIRE_MAINTENANCE';
49  const PARAM_REQUIRE_IS_INSTALLED = 'MAGE_REQUIRE_IS_INSTALLED';
62  const INIT_PARAM_FILESYSTEM_DIR_PATHS = 'MAGE_DIRS';
63 
67  const INIT_PARAM_FILESYSTEM_DRIVERS = 'MAGE_FILESYSTEM_DRIVERS';
68 
74  private $server;
75 
81  private $rootDir;
82 
88  private $objectManager;
89 
95  private $maintenance;
96 
102  private $errorCode = 0;
103 
109  private $factory;
110 
119  public static function create($rootDir, array $initParams, ObjectManagerFactory $factory = null)
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  }
127 
135  public static function populateAutoloader($rootDir, $initParams)
136  {
137  $dirList = self::createFilesystemDirectoryList($rootDir, $initParams);
140  }
141 
149  public static function createObjectManagerFactory($rootDir, array $initParams)
150  {
151  $dirList = self::createFilesystemDirectoryList($rootDir, $initParams);
152  $driverPool = self::createFilesystemDriverPool($initParams);
153  $configFilePool = self::createConfigFilePool();
154  return new ObjectManagerFactory($dirList, $driverPool, $configFilePool);
155  }
156 
164  public static function createFilesystemDirectoryList($rootDir, array $initParams)
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  }
172 
179  public static function createFilesystemDriverPool(array $initParams)
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  }
187 
193  public static function createConfigFilePool()
194  {
195  return new ConfigFilePool();
196  }
197 
205  public function __construct(ObjectManagerFactory $factory, $rootDir, array $initParams)
206  {
207  $this->factory = $factory;
208  $this->rootDir = $rootDir;
209  $this->server = $initParams;
210  $this->objectManager = $this->factory->create($this->server);
211  }
212 
218  public function getParams()
219  {
220  return $this->server;
221  }
222 
231  public function createApplication($type, $arguments = [])
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  }
243 
250  public function run(AppInterface $application)
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  }
272 
279  protected function assertMaintenance()
280  {
281  $isExpected = $this->getIsExpected(self::PARAM_REQUIRE_MAINTENANCE, self::DEFAULT_REQUIRE_MAINTENANCE);
282  if (null === $isExpected) {
283  return;
284  }
286  $this->maintenance = $this->objectManager->get(\Magento\Framework\App\MaintenanceMode::class);
287 
289  $phpRemoteAddressEnvironment = $this->objectManager->get(
290  \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class
291  );
292  $remoteAddress = $phpRemoteAddressEnvironment->getRemoteAddress();
293  $isOn = $this->maintenance->isOn($remoteAddress ? $remoteAddress : '');
294 
295  if ($isOn && !$isExpected) {
296  $this->errorCode = self::ERR_MAINTENANCE;
297  throw new \Exception('Unable to proceed: the maintenance mode is enabled. ');
298  }
299  if (!$isOn && $isExpected) {
300  $this->errorCode = self::ERR_MAINTENANCE;
301  throw new \Exception('Unable to proceed: the maintenance mode must be enabled first. ');
302  }
303  }
304 
311  protected function assertInstalled()
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  }
327 
337  private function getIsExpected($key, $default)
338  {
339  if (array_key_exists($key, $this->server)) {
340  if (isset($this->server[$key])) {
341  return (bool) (int) $this->server[$key];
342  }
343  return null;
344  }
345  return $default;
346  }
347 
353  private function isInstalled()
354  {
356  $deploymentConfig = $this->objectManager->get(\Magento\Framework\App\DeploymentConfig::class);
357  return $deploymentConfig->isAvailable();
358  }
359 
365  public function getObjectManager()
366  {
367  return $this->objectManager;
368  }
369 
375  private function initErrorHandler()
376  {
377  $handler = new ErrorHandler();
378  set_error_handler([$handler, 'handler']);
379  }
380 
386  public function getErrorCode()
387  {
388  return $this->errorCode;
389  }
390 
396  public function isDeveloperMode()
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  }
411 
419  protected function terminate(\Exception $e)
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  }
437 }
static create($rootDir, array $initParams, ObjectManagerFactory $factory=null)
Definition: Bootstrap.php:119
$response
Definition: 404.php:11
if(!file_exists($installConfigFile)) $dirList
Definition: bootstrap.php:57
createApplication($type, $arguments=[])
Definition: Bootstrap.php:231
$message
static populateMappings(AutoloaderInterface $autoloader, DirectoryList $dirList)
Definition: Populator.php:22
$autoloadWrapper
Definition: autoload.php:12
$deploymentConfig
$type
Definition: item.phtml:13
$application
Definition: bootstrap.php:58
static createFilesystemDirectoryList($rootDir, array $initParams)
Definition: Bootstrap.php:164
exit
Definition: redirect.phtml:12
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
run(AppInterface $application)
Definition: Bootstrap.php:250
static createFilesystemDriverPool(array $initParams)
Definition: Bootstrap.php:179
__construct(ObjectManagerFactory $factory, $rootDir, array $initParams)
Definition: Bootstrap.php:205
$arguments
static createObjectManagerFactory($rootDir, array $initParams)
Definition: Bootstrap.php:149
static populateAutoloader($rootDir, $initParams)
Definition: Bootstrap.php:135
catch(\Exception $e) $handler
Definition: index.php:30