PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/framework/App/ObjectManagerFactory.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 307 lines | 169 code | 29 blank | 109 comment | 4 complexity | ac072a6bc7116a87d54da353fc869ec6 MD5 | raw file
  1. <?php
  2. /**
  3. * Initialize application object manager.
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Filesystem\DriverPool;
  11. use Magento\Framework\Interception\ObjectManager\ConfigInterface;
  12. use Magento\Framework\ObjectManager\Definition\Compiled\Serialized;
  13. use Magento\Framework\App\ObjectManager\Environment;
  14. use Magento\Framework\Config\File\ConfigFilePool;
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * Class ObjectManagerFactory
  18. */
  19. class ObjectManagerFactory
  20. {
  21. /**
  22. * Path to definitions format in deployment configuration
  23. */
  24. const CONFIG_PATH_DEFINITION_FORMAT = 'definition/format';
  25. /**
  26. * Initialization parameter for a custom deployment configuration file
  27. */
  28. const INIT_PARAM_DEPLOYMENT_CONFIG_FILE = 'MAGE_CONFIG_FILE';
  29. /**
  30. * Initialization parameter for custom deployment configuration data
  31. */
  32. const INIT_PARAM_DEPLOYMENT_CONFIG = 'MAGE_CONFIG';
  33. /**
  34. * Locator class name
  35. *
  36. * @var string
  37. */
  38. protected $_locatorClassName = 'Magento\Framework\ObjectManager\ObjectManager';
  39. /**
  40. * Config class name
  41. *
  42. * @var string
  43. */
  44. protected $_configClassName = 'Magento\Framework\Interception\ObjectManager\ConfigInterface';
  45. /**
  46. * Environment factory class name
  47. *
  48. * @var string
  49. */
  50. protected $envFactoryClassName = 'Magento\Framework\App\EnvironmentFactory';
  51. /**
  52. * Filesystem directory list
  53. *
  54. * @var DirectoryList
  55. */
  56. protected $directoryList;
  57. /**
  58. * Filesystem driver pool
  59. *
  60. * @var DriverPool
  61. */
  62. protected $driverPool;
  63. /**
  64. * Configuration file pool
  65. *
  66. * @var ConfigFilePool
  67. */
  68. protected $configFilePool;
  69. /**
  70. * Factory
  71. *
  72. * @var \Magento\Framework\ObjectManager\FactoryInterface
  73. */
  74. protected $factory;
  75. /**
  76. * Constructor
  77. *
  78. * @param DirectoryList $directoryList
  79. * @param DriverPool $driverPool
  80. * @param ConfigFilePool $configFilePool
  81. */
  82. public function __construct(DirectoryList $directoryList, DriverPool $driverPool, ConfigFilePool $configFilePool)
  83. {
  84. $this->directoryList = $directoryList;
  85. $this->driverPool = $driverPool;
  86. $this->configFilePool = $configFilePool;
  87. }
  88. /**
  89. * Create ObjectManager
  90. *
  91. * @param array $arguments
  92. * @return \Magento\Framework\ObjectManagerInterface
  93. *
  94. * @SuppressWarnings(PHPMD.NPathComplexity)
  95. */
  96. public function create(array $arguments)
  97. {
  98. $deploymentConfig = $this->createDeploymentConfig($this->directoryList, $this->configFilePool, $arguments);
  99. $arguments = array_merge($deploymentConfig->get(), $arguments);
  100. $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory(
  101. $this->driverPool->getDriver(DriverPool::FILE),
  102. $this->directoryList->getPath(DirectoryList::DI),
  103. $this->directoryList->getPath(DirectoryList::GENERATION),
  104. $deploymentConfig->get(self::CONFIG_PATH_DEFINITION_FORMAT, Serialized::MODE_NAME)
  105. );
  106. $definitions = $definitionFactory->createClassDefinition($deploymentConfig->get('definitions'));
  107. $relations = $definitionFactory->createRelations();
  108. /** @var EnvironmentFactory $enFactory */
  109. $enFactory = new $this->envFactoryClassName($relations, $definitions);
  110. /** @var EnvironmentInterface $env */
  111. $env = $enFactory->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');
  127. $cacheFactoryArguments['decorators'][] = [
  128. 'class' => 'Magento\Framework\Cache\Frontend\Decorator\Profiler',
  129. 'parameters' => ['backendPrefixes' => ['Zend_Cache_Backend_', 'Cm_Cache_Backend_']],
  130. ];
  131. $cacheFactoryConfig = [
  132. 'Magento\Framework\App\Cache\Frontend\Factory' => ['arguments' => $cacheFactoryArguments]
  133. ];
  134. $diConfig->extend($cacheFactoryConfig);
  135. }
  136. $sharedInstances = [
  137. 'Magento\Framework\App\DeploymentConfig' => $deploymentConfig,
  138. 'Magento\Framework\App\Filesystem\DirectoryList' => $this->directoryList,
  139. 'Magento\Framework\Filesystem\DirectoryList' => $this->directoryList,
  140. 'Magento\Framework\Filesystem\DriverPool' => $this->driverPool,
  141. 'Magento\Framework\ObjectManager\RelationsInterface' => $relations,
  142. 'Magento\Framework\Interception\DefinitionInterface' => $definitionFactory->createPluginDefinition(),
  143. 'Magento\Framework\ObjectManager\ConfigInterface' => $diConfig,
  144. 'Magento\Framework\Interception\ObjectManager\ConfigInterface' => $diConfig,
  145. 'Magento\Framework\ObjectManager\DefinitionInterface' => $definitions,
  146. 'Magento\Framework\Stdlib\BooleanUtils' => $booleanUtils,
  147. 'Magento\Framework\ObjectManager\Config\Mapper\Dom' => $argumentMapper,
  148. 'Magento\Framework\ObjectManager\ConfigLoaderInterface' => $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. ObjectManager::setInstance($objectManager);
  157. $definitionFactory->getCodeGenerator()->setObjectManager($objectManager);
  158. $env->configureObjectManager($diConfig, $sharedInstances);
  159. return $objectManager;
  160. }
  161. /**
  162. * Creates deployment configuration object
  163. *
  164. * @param DirectoryList $directoryList
  165. * @param ConfigFilePool $configFilePool
  166. * @param array $arguments
  167. * @return DeploymentConfig
  168. */
  169. protected function createDeploymentConfig(
  170. DirectoryList $directoryList,
  171. ConfigFilePool $configFilePool,
  172. array $arguments
  173. ) {
  174. $customFile = isset($arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG_FILE])
  175. ? $arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG_FILE]
  176. : null;
  177. $customData = isset($arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG])
  178. ? $arguments[self::INIT_PARAM_DEPLOYMENT_CONFIG]
  179. : [];
  180. $reader = new DeploymentConfig\Reader($directoryList, $this->driverPool, $configFilePool, $customFile);
  181. return new DeploymentConfig($reader, $customData);
  182. }
  183. /**
  184. * Return newly created instance on an argument interpreter, suitable for processing DI arguments
  185. *
  186. * @param \Magento\Framework\Stdlib\BooleanUtils $booleanUtils
  187. * @return \Magento\Framework\Data\Argument\InterpreterInterface
  188. */
  189. protected function createArgumentInterpreter(
  190. \Magento\Framework\Stdlib\BooleanUtils $booleanUtils
  191. ) {
  192. $constInterpreter = new \Magento\Framework\Data\Argument\Interpreter\Constant();
  193. $result = new \Magento\Framework\Data\Argument\Interpreter\Composite(
  194. [
  195. 'boolean' => new \Magento\Framework\Data\Argument\Interpreter\Boolean($booleanUtils),
  196. 'string' => new \Magento\Framework\Data\Argument\Interpreter\StringUtils($booleanUtils),
  197. 'number' => new \Magento\Framework\Data\Argument\Interpreter\Number(),
  198. 'null' => new \Magento\Framework\Data\Argument\Interpreter\NullType(),
  199. 'object' => new \Magento\Framework\Data\Argument\Interpreter\DataObject($booleanUtils),
  200. 'const' => $constInterpreter,
  201. 'init_parameter' => new \Magento\Framework\App\Arguments\ArgumentInterpreter($constInterpreter),
  202. ],
  203. \Magento\Framework\ObjectManager\Config\Reader\Dom::TYPE_ATTRIBUTE
  204. );
  205. // Add interpreters that reference the composite
  206. $result->addInterpreter('array', new \Magento\Framework\Data\Argument\Interpreter\ArrayType($result));
  207. return $result;
  208. }
  209. /**
  210. * Load primary config
  211. *
  212. * @param DirectoryList $directoryList
  213. * @param DriverPool $driverPool
  214. * @param mixed $argumentMapper
  215. * @param string $appMode
  216. * @return array
  217. * @throws \Magento\Framework\Exception\State\InitException
  218. */
  219. protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, $argumentMapper, $appMode)
  220. {
  221. $configData = null;
  222. try {
  223. $fileResolver = new \Magento\Framework\App\Arguments\FileResolver\Primary(
  224. new \Magento\Framework\Filesystem(
  225. $directoryList,
  226. new \Magento\Framework\Filesystem\Directory\ReadFactory($driverPool),
  227. new \Magento\Framework\Filesystem\Directory\WriteFactory($driverPool)
  228. ),
  229. new \Magento\Framework\Config\FileIteratorFactory(
  230. new \Magento\Framework\Filesystem\File\ReadFactory(new \Magento\Framework\Filesystem\DriverPool())
  231. )
  232. );
  233. $schemaLocator = new \Magento\Framework\ObjectManager\Config\SchemaLocator();
  234. $validationState = new \Magento\Framework\App\Arguments\ValidationState($appMode);
  235. $reader = new \Magento\Framework\ObjectManager\Config\Reader\Dom(
  236. $fileResolver,
  237. $argumentMapper,
  238. $schemaLocator,
  239. $validationState
  240. );
  241. $configData = $reader->read('primary');
  242. } catch (\Exception $e) {
  243. throw new \Magento\Framework\Exception\State\InitException(
  244. new \Magento\Framework\Phrase($e->getMessage()),
  245. $e
  246. );
  247. }
  248. return $configData;
  249. }
  250. /**
  251. * Crete plugin list object
  252. *
  253. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  254. * @param \Magento\Framework\ObjectManager\RelationsInterface $relations
  255. * @param \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory
  256. * @param \Magento\Framework\ObjectManager\Config\Config $diConfig
  257. * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
  258. * @return \Magento\Framework\Interception\PluginList\PluginList
  259. */
  260. protected function _createPluginList(
  261. \Magento\Framework\ObjectManagerInterface $objectManager,
  262. \Magento\Framework\ObjectManager\RelationsInterface $relations,
  263. \Magento\Framework\ObjectManager\DefinitionFactory $definitionFactory,
  264. \Magento\Framework\ObjectManager\Config\Config $diConfig,
  265. \Magento\Framework\ObjectManager\DefinitionInterface $definitions
  266. ) {
  267. return $objectManager->create(
  268. 'Magento\Framework\Interception\PluginList\PluginList',
  269. [
  270. 'relations' => $relations,
  271. 'definitions' => $definitionFactory->createPluginDefinition(),
  272. 'omConfig' => $diConfig,
  273. 'classDefinitions' => $definitions instanceof
  274. \Magento\Framework\ObjectManager\Definition\Compiled ? $definitions : null
  275. ]
  276. );
  277. }
  278. }