ObjectManagerFactory.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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\Filesystem\DriverPool;
  9. use Magento\Framework\Interception\ObjectManager\ConfigInterface;
  10. use Magento\Framework\App\ObjectManager\Environment;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. use Magento\Framework\Code\GeneratedFiles;
  13. /**
  14. * Initialization of object manager is a complex operation.
  15. * To abstract away this complexity, this class was introduced.
  16. * Objects of this class create fully initialized instance of object manager with "global" configuration loaded.
  17. *
  18. * @api
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. * @since 100.0.2
  21. */
  22. class ObjectManagerFactory
  23. {
  24. /**
  25. * Initialization parameter for a custom deployment configuration file
  26. */
  27. const INIT_PARAM_DEPLOYMENT_CONFIG_FILE = 'MAGE_CONFIG_FILE';
  28. /**
  29. * Initialization parameter for custom deployment configuration data
  30. */
  31. const INIT_PARAM_DEPLOYMENT_CONFIG = 'MAGE_CONFIG';
  32. /**
  33. * Locator class name
  34. *
  35. * @var string
  36. */
  37. protected $_locatorClassName = \Magento\Framework\App\ObjectManager::class;
  38. /**
  39. * Config class name
  40. *
  41. * @var string
  42. */
  43. protected $_configClassName = \Magento\Framework\Interception\ObjectManager\ConfigInterface::class;
  44. /**
  45. * Environment factory class name
  46. *
  47. * @var string
  48. */
  49. protected $envFactoryClassName = \Magento\Framework\App\EnvironmentFactory::class;
  50. /**
  51. * Filesystem directory list
  52. *
  53. * @var DirectoryList
  54. */
  55. protected $directoryList;
  56. /**
  57. * Filesystem driver pool
  58. *
  59. * @var DriverPool
  60. */
  61. protected $driverPool;
  62. /**
  63. * Configuration file pool
  64. *
  65. * @var ConfigFilePool
  66. */
  67. protected $configFilePool;
  68. /**
  69. * Factory
  70. *
  71. * @var \Magento\Framework\ObjectManager\FactoryInterface
  72. */
  73. protected $factory;
  74. /**
  75. * Constructor
  76. *
  77. * @param DirectoryList $directoryList
  78. * @param DriverPool $driverPool
  79. * @param ConfigFilePool $configFilePool
  80. */
  81. public function __construct(DirectoryList $directoryList, DriverPool $driverPool, ConfigFilePool $configFilePool)
  82. {
  83. $this->directoryList = $directoryList;
  84. $this->driverPool = $driverPool;
  85. $this->configFilePool = $configFilePool;
  86. }
  87. /**
  88. * Create ObjectManager
  89. *
  90. * @param array $arguments
  91. * @return \Magento\Framework\ObjectManagerInterface
  92. *
  93. * @SuppressWarnings(PHPMD.NPathComplexity)
  94. */
  95. public function create(array $arguments)
  96. {
  97. $writeFactory = new \Magento\Framework\Filesystem\Directory\WriteFactory($this->driverPool);
  98. $generatedFiles = new GeneratedFiles($this->directoryList, $writeFactory);
  99. $generatedFiles->cleanGeneratedFiles();
  100. $deploymentConfig = $this->createDeploymentConfig($this->directoryList, $this->configFilePool, $arguments);
  101. $arguments = array_merge($deploymentConfig->get(), $arguments);
  102. $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory(
  103. $this->driverPool->getDriver(DriverPool::FILE),
  104. $this->directoryList->getPath(DirectoryList::GENERATED_CODE)
  105. );
  106. $definitions = $definitionFactory->createClassDefinition();
  107. $relations = $definitionFactory->createRelations();
  108. /** @var EnvironmentFactory $envFactory */
  109. $envFactory = new $this->envFactoryClassName($relations, $definitions);
  110. /** @var EnvironmentInterface $env */
  111. $env = $envFactory->createEnvironment();
  112. /** @var ConfigInterface $diConfig */
  113. $diConfig = $env->getDiConfig();
  114. $appMode = isset($arguments[State::PARAM_MODE]) ? $arguments[State::PARAM_MODE] : State::MODE_DEFAULT;
  115. $booleanUtils = new \Magento\Framework\Stdlib\BooleanUtils();
  116. $argInterpreter = $this->createArgumentInterpreter($booleanUtils);
  117. $argumentMapper = new \Magento\Framework\ObjectManager\Config\Mapper\Dom($argInterpreter);
  118. if ($env->getMode() != Environment\Compiled::MODE) {
  119. $configData = $this->_loadPrimaryConfig($this->directoryList, $this->driverPool, $argumentMapper, $appMode);
  120. if ($configData) {
  121. $diConfig->extend($configData);
  122. }
  123. }
  124. // set cache profiler decorator if enabled
  125. if (\Magento\Framework\Profiler::isEnabled()) {
  126. $cacheFactoryArguments = $diConfig->getArguments(\Magento\Framework\App\Cache\Frontend\Factory::class);
  127. $cacheFactoryArguments['decorators'][] = [
  128. 'class' => \Magento\Framework\Cache\Frontend\Decorator\Profiler::class,
  129. 'parameters' => ['backendPrefixes' => ['Zend_Cache_Backend_', 'Cm_Cache_Backend_']],
  130. ];
  131. $cacheFactoryConfig = [
  132. \Magento\Framework\App\Cache\Frontend\Factory::class => ['arguments' => $cacheFactoryArguments]
  133. ];
  134. $diConfig->extend($cacheFactoryConfig);
  135. }
  136. $sharedInstances = [
  137. \Magento\Framework\App\DeploymentConfig::class => $deploymentConfig,
  138. \Magento\Framework\App\Filesystem\DirectoryList::class => $this->directoryList,
  139. \Magento\Framework\Filesystem\DirectoryList::class => $this->directoryList,
  140. \Magento\Framework\Filesystem\DriverPool::class => $this->driverPool,
  141. \Magento\Framework\ObjectManager\RelationsInterface::class => $relations,
  142. \Magento\Framework\Interception\DefinitionInterface::class => $definitionFactory->createPluginDefinition(),
  143. \Magento\Framework\ObjectManager\ConfigInterface::class => $diConfig,
  144. \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => $diConfig,
  145. \Magento\Framework\ObjectManager\DefinitionInterface::class => $definitions,
  146. \Magento\Framework\Stdlib\BooleanUtils::class => $booleanUtils,
  147. \Magento\Framework\ObjectManager\Config\Mapper\Dom::class => $argumentMapper,
  148. \Magento\Framework\ObjectManager\ConfigLoaderInterface::class => $env->getObjectManagerConfigLoader(),
  149. $this->_configClassName => $diConfig,
  150. ];
  151. $arguments['shared_instances'] = &$sharedInstances;
  152. $this->factory = $env->getObjectManagerFactory($arguments);
  153. /** @var \Magento\Framework\ObjectManagerInterface $objectManager */
  154. $objectManager = new $this->_locatorClassName($this->factory, $diConfig, $sharedInstances);
  155. $this->factory->setObjectManager($objectManager);
  156. $generatorParams = $diConfig->getArguments(\Magento\Framework\Code\Generator::class);
  157. /** Arguments are stored in different format when DI config is compiled, thus require custom processing */
  158. $generatedEntities = isset($generatorParams['generatedEntities']['_v_'])
  159. ? $generatorParams['generatedEntities']['_v_']
  160. : (isset($generatorParams['generatedEntities']) ? $generatorParams['generatedEntities'] : []);
  161. $definitionFactory->getCodeGenerator()
  162. ->setObjectManager($objectManager)
  163. ->setGeneratedEntities($generatedEntities);
  164. $env->configureObjectManager($diConfig, $sharedInstances);
  165. return $objectManager;
  166. }
  167. /**
  168. * Creates deployment configuration object
  169. *
  170. * @param DirectoryList $directoryList
  171. * @param ConfigFilePool $configFilePool
  172. * @param array $arguments
  173. * @return DeploymentConfig
  174. */
  175. protected function createDeploymentConfig(
  176. DirectoryList $directoryList,
  177. ConfigFilePool $configFilePool,
  178. array $arguments
  179. ) {
  180. $customFile = isset($arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG_FILE])
  181. ? $arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG_FILE]
  182. : null;
  183. $customData = isset($arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG])
  184. ? $arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG]
  185. : [];
  186. $reader = new DeploymentConfig\Reader($directoryList, $this->driverPool, $configFilePool, $customFile);
  187. return new DeploymentConfig($reader, $customData);
  188. }
  189. /**
  190. * Return newly created instance on an argument interpreter, suitable for processing DI arguments
  191. *
  192. * @param \Magento\Framework\Stdlib\BooleanUtils $booleanUtils
  193. * @return \Magento\Framework\Data\Argument\InterpreterInterface
  194. */
  195. protected function createArgumentInterpreter(
  196. \Magento\Framework\Stdlib\BooleanUtils $booleanUtils
  197. ) {
  198. $constInterpreter = new \Magento\Framework\Data\Argument\Interpreter\Constant();
  199. $result = new \Magento\Framework\Data\Argument\Interpreter\Composite(
  200. [
  201. 'boolean' => new \Magento\Framework\Data\Argument\Interpreter\Boolean($booleanUtils),
  202. 'string' => new \Magento\Framework\Data\Argument\Interpreter\BaseStringUtils($booleanUtils),
  203. 'number' => new \Magento\Framework\Data\Argument\Interpreter\Number(),
  204. 'null' => new \Magento\Framework\Data\Argument\Interpreter\NullType(),
  205. 'object' => new \Magento\Framework\Data\Argument\Interpreter\DataObject($booleanUtils),
  206. 'const' => $constInterpreter,
  207. 'init_parameter' => new \Magento\Framework\App\Arguments\ArgumentInterpreter($constInterpreter),
  208. ],
  209. \Magento\Framework\ObjectManager\Config\Reader\Dom::TYPE_ATTRIBUTE
  210. );
  211. // Add interpreters that reference the composite
  212. $result->addInterpreter('array', new \Magento\Framework\Data\Argument\Interpreter\ArrayType($result));
  213. return $result;
  214. }
  215. /**
  216. * Load primary config
  217. *
  218. * @param DirectoryList $directoryList
  219. * @param DriverPool $driverPool
  220. * @param mixed $argumentMapper
  221. * @param string $appMode
  222. * @return array
  223. * @throws \Magento\Framework\Exception\State\InitException
  224. */
  225. protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, $argumentMapper, $appMode)
  226. {
  227. $configData = null;
  228. try {
  229. $fileResolver = new \Magento\Framework\App\Arguments\FileResolver\Primary(
  230. new \Magento\Framework\Filesystem(
  231. $directoryList,
  232. new \Magento\Framework\Filesystem\Directory\ReadFactory($driverPool),
  233. new \Magento\Framework\Filesystem\Directory\WriteFactory($driverPool)
  234. ),
  235. new \Magento\Framework\Config\FileIteratorFactory(
  236. new \Magento\Framework\Filesystem\File\ReadFactory($driverPool)
  237. )
  238. );
  239. $schemaLocator = new \Magento\Framework\ObjectManager\Config\SchemaLocator();
  240. $validationState = new \Magento\Framework\App\Arguments\ValidationState($appMode);
  241. $reader = new \Magento\Framework\ObjectManager\Config\Reader\Dom(
  242. $fileResolver,
  243. $argumentMapper,
  244. $schemaLocator,
  245. $validationState
  246. );
  247. $configData = $reader->read('primary');
  248. } catch (\Exception $e) {
  249. throw new \Magento\Framework\Exception\State\InitException(
  250. new \Magento\Framework\Phrase($e->getMessage()),
  251. $e
  252. );
  253. }
  254. return $configData;
  255. }
  256. /**
  257. * Crete plugin list object
  258. *
  259. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  260. * @param \Magento\Framework\ObjectManager\RelationsInterface $relations
  261. * @param \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory
  262. * @param \Magento\Framework\ObjectManager\Config\Config $diConfig
  263. * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
  264. * @return \Magento\Framework\Interception\PluginList\PluginList
  265. * @deprecated 101.0.0
  266. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  267. */
  268. protected function _createPluginList(
  269. \Magento\Framework\ObjectManagerInterface $objectManager,
  270. \Magento\Framework\ObjectManager\RelationsInterface $relations,
  271. \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory,
  272. \Magento\Framework\ObjectManager\Config\Config $diConfig,
  273. \Magento\Framework\ObjectManager\DefinitionInterface $definitions
  274. ) {
  275. return $objectManager->create(
  276. \Magento\Framework\Interception\PluginList\PluginList::class,
  277. [
  278. 'relations' => $relations,
  279. 'definitions' => $definitionFactory->createPluginDefinition(),
  280. 'omConfig' => $diConfig,
  281. 'classDefinitions' => null
  282. ]
  283. );
  284. }
  285. }