Bootstrap.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\AppInterface;
  9. use Magento\Framework\Autoload\AutoloaderRegistry;
  10. use Magento\Framework\Autoload\Populator;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. use Magento\Framework\Filesystem\DriverPool;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * A bootstrap of Magento application
  16. *
  17. * Performs basic initialization root function: injects init parameters and creates object manager
  18. * Can create/run applications
  19. *
  20. * @api
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. * @since 100.0.2
  23. */
  24. class Bootstrap
  25. {
  26. /**#@+
  27. * Possible errors that can be triggered by the bootstrap
  28. */
  29. const ERR_MAINTENANCE = 901;
  30. const ERR_IS_INSTALLED = 902;
  31. /**#@- */
  32. /**#@+
  33. * Initialization parameters that allow control bootstrap behavior of asserting maintenance mode or is installed
  34. *
  35. * Possible values:
  36. * - true -- set expectation that it is required
  37. * - false -- set expectation that is required not to
  38. * - null -- bypass the assertion completely
  39. *
  40. * If key is absent in the parameters array, the default behavior will be used
  41. * @see DEFAULT_REQUIRE_MAINTENANCE
  42. * @see DEFAULT_REQUIRE_IS_INSTALLED
  43. */
  44. const PARAM_REQUIRE_MAINTENANCE = 'MAGE_REQUIRE_MAINTENANCE';
  45. const PARAM_REQUIRE_IS_INSTALLED = 'MAGE_REQUIRE_IS_INSTALLED';
  46. /**#@- */
  47. /**#@+
  48. * Default behavior of bootstrap assertions
  49. */
  50. const DEFAULT_REQUIRE_MAINTENANCE = false;
  51. const DEFAULT_REQUIRE_IS_INSTALLED = true;
  52. /**#@- */
  53. /**
  54. * Initialization parameter for custom directory paths
  55. */
  56. const INIT_PARAM_FILESYSTEM_DIR_PATHS = 'MAGE_DIRS';
  57. /**
  58. * Initialization parameter for additional filesystem drivers
  59. */
  60. const INIT_PARAM_FILESYSTEM_DRIVERS = 'MAGE_FILESYSTEM_DRIVERS';
  61. /**
  62. * The initialization parameters (normally come from the $_SERVER)
  63. *
  64. * @var array
  65. */
  66. private $server;
  67. /**
  68. * Root directory
  69. *
  70. * @var string
  71. */
  72. private $rootDir;
  73. /**
  74. * Object manager
  75. *
  76. * @var \Magento\Framework\ObjectManagerInterface
  77. */
  78. private $objectManager;
  79. /**
  80. * Maintenance mode manager
  81. *
  82. * @var \Magento\Framework\App\MaintenanceMode
  83. */
  84. private $maintenance;
  85. /**
  86. * Bootstrap-specific error code that may have been set in runtime
  87. *
  88. * @var int
  89. */
  90. private $errorCode = 0;
  91. /**
  92. * Attribute for creating object manager
  93. *
  94. * @var ObjectManagerFactory
  95. */
  96. private $factory;
  97. /**
  98. * Static method so that client code does not have to create Object Manager Factory every time Bootstrap is called
  99. *
  100. * @param string $rootDir
  101. * @param array $initParams
  102. * @param ObjectManagerFactory $factory
  103. * @return Bootstrap
  104. */
  105. public static function create($rootDir, array $initParams, ObjectManagerFactory $factory = null)
  106. {
  107. self::populateAutoloader($rootDir, $initParams);
  108. if ($factory === null) {
  109. $factory = self::createObjectManagerFactory($rootDir, $initParams);
  110. }
  111. return new self($factory, $rootDir, $initParams);
  112. }
  113. /**
  114. * Populates autoloader with mapping info
  115. *
  116. * @param string $rootDir
  117. * @param array $initParams
  118. * @return void
  119. */
  120. public static function populateAutoloader($rootDir, $initParams)
  121. {
  122. $dirList = self::createFilesystemDirectoryList($rootDir, $initParams);
  123. $autoloadWrapper = AutoloaderRegistry::getAutoloader();
  124. Populator::populateMappings($autoloadWrapper, $dirList);
  125. }
  126. /**
  127. * Creates instance of object manager factory
  128. *
  129. * @param string $rootDir
  130. * @param array $initParams
  131. * @return ObjectManagerFactory
  132. */
  133. public static function createObjectManagerFactory($rootDir, array $initParams)
  134. {
  135. $dirList = self::createFilesystemDirectoryList($rootDir, $initParams);
  136. $driverPool = self::createFilesystemDriverPool($initParams);
  137. $configFilePool = self::createConfigFilePool();
  138. return new ObjectManagerFactory($dirList, $driverPool, $configFilePool);
  139. }
  140. /**
  141. * Creates instance of filesystem directory list
  142. *
  143. * @param string $rootDir
  144. * @param array $initParams
  145. * @return DirectoryList
  146. */
  147. public static function createFilesystemDirectoryList($rootDir, array $initParams)
  148. {
  149. $customDirs = [];
  150. if (isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS])) {
  151. $customDirs = $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
  152. }
  153. return new DirectoryList($rootDir, $customDirs);
  154. }
  155. /**
  156. * Creates instance of filesystem driver pool
  157. *
  158. * @param array $initParams
  159. * @return DriverPool
  160. */
  161. public static function createFilesystemDriverPool(array $initParams)
  162. {
  163. $extraDrivers = [];
  164. if (isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS])) {
  165. $extraDrivers = $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS];
  166. }
  167. return new DriverPool($extraDrivers);
  168. }
  169. /**
  170. * Creates instance of configuration files pool
  171. *
  172. * @return DriverPool
  173. */
  174. public static function createConfigFilePool()
  175. {
  176. return new ConfigFilePool();
  177. }
  178. /**
  179. * Constructor
  180. *
  181. * @param ObjectManagerFactory $factory
  182. * @param string $rootDir
  183. * @param array $initParams
  184. */
  185. public function __construct(ObjectManagerFactory $factory, $rootDir, array $initParams)
  186. {
  187. $this->factory = $factory;
  188. $this->rootDir = $rootDir;
  189. $this->server = $initParams;
  190. $this->objectManager = $this->factory->create($this->server);
  191. }
  192. /**
  193. * Gets the current parameters
  194. *
  195. * @return array
  196. */
  197. public function getParams()
  198. {
  199. return $this->server;
  200. }
  201. /**
  202. * Factory method for creating application instances
  203. *
  204. * @param string $type
  205. * @param array $arguments
  206. * @return \Magento\Framework\AppInterface
  207. * @throws \InvalidArgumentException
  208. */
  209. public function createApplication($type, $arguments = [])
  210. {
  211. try {
  212. $application = $this->objectManager->create($type, $arguments);
  213. if (!($application instanceof AppInterface)) {
  214. throw new \InvalidArgumentException("The provided class doesn't implement AppInterface: {$type}");
  215. }
  216. return $application;
  217. } catch (\Exception $e) {
  218. $this->terminate($e);
  219. }
  220. }
  221. /**
  222. * Runs an application
  223. *
  224. * @param \Magento\Framework\AppInterface $application
  225. * @return void
  226. */
  227. public function run(AppInterface $application)
  228. {
  229. try {
  230. try {
  231. \Magento\Framework\Profiler::start('magento');
  232. $this->initErrorHandler();
  233. $this->assertMaintenance();
  234. $this->assertInstalled();
  235. $response = $application->launch();
  236. $response->sendResponse();
  237. \Magento\Framework\Profiler::stop('magento');
  238. } catch (\Exception $e) {
  239. \Magento\Framework\Profiler::stop('magento');
  240. $this->objectManager->get(LoggerInterface::class)->error($e->getMessage());
  241. if (!$application->catchException($this, $e)) {
  242. throw $e;
  243. }
  244. }
  245. } catch (\Exception $e) {
  246. $this->terminate($e);
  247. }
  248. }
  249. /**
  250. * Asserts maintenance mode
  251. *
  252. * @return void
  253. * @throws \Exception
  254. */
  255. protected function assertMaintenance()
  256. {
  257. $isExpected = $this->getIsExpected(self::PARAM_REQUIRE_MAINTENANCE, self::DEFAULT_REQUIRE_MAINTENANCE);
  258. if (null === $isExpected) {
  259. return;
  260. }
  261. /** @var \Magento\Framework\App\MaintenanceMode $maintenance */
  262. $this->maintenance = $this->objectManager->get(\Magento\Framework\App\MaintenanceMode::class);
  263. /** @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $phpRemoteAddressEnvironment */
  264. $phpRemoteAddressEnvironment = $this->objectManager->get(
  265. \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class
  266. );
  267. $remoteAddress = $phpRemoteAddressEnvironment->getRemoteAddress();
  268. $isOn = $this->maintenance->isOn($remoteAddress ? $remoteAddress : '');
  269. if ($isOn && !$isExpected) {
  270. $this->errorCode = self::ERR_MAINTENANCE;
  271. throw new \Exception('Unable to proceed: the maintenance mode is enabled. ');
  272. }
  273. if (!$isOn && $isExpected) {
  274. $this->errorCode = self::ERR_MAINTENANCE;
  275. throw new \Exception('Unable to proceed: the maintenance mode must be enabled first. ');
  276. }
  277. }
  278. /**
  279. * Asserts whether application is installed
  280. *
  281. * @return void
  282. * @throws \Exception
  283. */
  284. protected function assertInstalled()
  285. {
  286. $isExpected = $this->getIsExpected(self::PARAM_REQUIRE_IS_INSTALLED, self::DEFAULT_REQUIRE_IS_INSTALLED);
  287. if (null === $isExpected) {
  288. return;
  289. }
  290. $isInstalled = $this->isInstalled();
  291. if (!$isInstalled && $isExpected) {
  292. $this->errorCode = self::ERR_IS_INSTALLED;
  293. throw new \Exception('Error: Application is not installed yet. ');
  294. }
  295. if ($isInstalled && !$isExpected) {
  296. $this->errorCode = self::ERR_IS_INSTALLED;
  297. throw new \Exception('Error: Application is already installed. ');
  298. }
  299. }
  300. /**
  301. * Analyze a key in the initialization parameters as "is expected" parameter
  302. *
  303. * If there is no such key, returns default value. Otherwise casts it to boolean, unless it is null
  304. *
  305. * @param string $key
  306. * @param bool $default
  307. * @return bool|null
  308. */
  309. private function getIsExpected($key, $default)
  310. {
  311. if (array_key_exists($key, $this->server)) {
  312. if (isset($this->server[$key])) {
  313. return (bool) (int) $this->server[$key];
  314. }
  315. return null;
  316. }
  317. return $default;
  318. }
  319. /**
  320. * Determines whether application is installed
  321. *
  322. * @return bool
  323. */
  324. private function isInstalled()
  325. {
  326. /** @var \Magento\Framework\App\DeploymentConfig $deploymentConfig */
  327. $deploymentConfig = $this->objectManager->get(\Magento\Framework\App\DeploymentConfig::class);
  328. return $deploymentConfig->isAvailable();
  329. }
  330. /**
  331. * Gets the object manager instance
  332. *
  333. * @return \Magento\Framework\ObjectManagerInterface
  334. */
  335. public function getObjectManager()
  336. {
  337. return $this->objectManager;
  338. }
  339. /**
  340. * Sets a custom error handler
  341. *
  342. * @return void
  343. */
  344. private function initErrorHandler()
  345. {
  346. $handler = new ErrorHandler();
  347. set_error_handler([$handler, 'handler']);
  348. }
  349. /**
  350. * Getter for error code
  351. *
  352. * @return int
  353. */
  354. public function getErrorCode()
  355. {
  356. return $this->errorCode;
  357. }
  358. /**
  359. * Checks whether developer mode is set in the initialization parameters
  360. *
  361. * @return bool
  362. */
  363. public function isDeveloperMode()
  364. {
  365. $mode = 'default';
  366. if (isset($this->server[State::PARAM_MODE])) {
  367. $mode = $this->server[State::PARAM_MODE];
  368. } else {
  369. $deploymentConfig = $this->getObjectManager()->get(DeploymentConfig::class);
  370. $configMode = $deploymentConfig->get(State::PARAM_MODE);
  371. if ($configMode) {
  372. $mode = $configMode;
  373. }
  374. }
  375. return $mode == State::MODE_DEVELOPER;
  376. }
  377. /**
  378. * Display an exception and terminate program execution
  379. *
  380. * @param \Exception $e
  381. * @return void
  382. * @SuppressWarnings(PHPMD.ExitExpression)
  383. */
  384. protected function terminate(\Exception $e)
  385. {
  386. if ($this->isDeveloperMode()) {
  387. echo $e;
  388. } else {
  389. $message = "An error has happened during application run. See exception log for details.\n";
  390. try {
  391. if (!$this->objectManager) {
  392. throw new \DomainException();
  393. }
  394. $this->objectManager->get(LoggerInterface::class)->critical($e);
  395. } catch (\Exception $e) {
  396. $message .= "Could not write error message to log. Please use developer mode to see the message.\n";
  397. }
  398. echo $message;
  399. }
  400. exit(1);
  401. }
  402. }