More information about Magento Framework¶
Object Manager¶
- Object Manager Devdocs
- Object manager used to create new object instance and retrieve cached object instance all object in Magento at anywhere. View class
vendor/magento/framework/ObjectManagerInterface.php.interface ObjectManagerInterface { /** * Create new object instance * * @param string $type * @param array $arguments * @return mixed */ public function create($type, array $arguments = []); /** * Retrieve cached object instance * * @param string $type * @return mixed */ public function get($type); /** * Configure object manager * * @param array $configuration * @return void */ public function configure(array $configuration); } -
Detail more than about Object manager, how Magento initiation Object manager.
- at
index.php,$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); -
at
vendor/magento/framework/App/Bootstrap.phpMagento init ObjectManagerFactory, and from ObjectManagerFactory init ObjectManagerInterface.public static function create($rootDir, array $initParams, ObjectManagerFactory $factory = null) { self::populateAutoloader($rootDir, $initParams); if ($factory === null) { $factory = self::createObjectManagerFactory($rootDir, $initParams); } return new self($factory, $rootDir, $initParams); } ... public static function createObjectManagerFactory($rootDir, array $initParams) { $dirList = self::createFilesystemDirectoryList($rootDir, $initParams); $driverPool = self::createFilesystemDriverPool($initParams); $configFilePool = self::createConfigFilePool(); return new ObjectManagerFactory($dirList, $driverPool, $configFilePool); } ... public function __construct(ObjectManagerFactory $factory, $rootDir, array $initParams) { $this->factory = $factory; $this->rootDir = $rootDir; $this->server = $initParams; $this->objectManager = $this->factory->create($this->server); } -
at
vendor/magento/framework/App/ObjectManagerFactory.phpMagento EnvironmentInterface distinguish mode:public function create(array $arguments) { /** @var EnvironmentFactory $envFactory */ $envFactory = new $this->envFactoryClassName($relations, $definitions); /** @var EnvironmentInterface $env */ $env = $envFactory->createEnvironment(); /** @var ConfigInterface $diConfig */ $diConfig = $env->getDiConfig(); if ($env->getMode() != Environment\Compiled::MODE) { $configData = $this->_loadPrimaryConfig($this->directoryList, $this->driverPool, $argumentMapper, $appMode); if ($configData) { $diConfig->extend($configData); } } $this->factory = $env->getObjectManagerFactory($arguments); /** @var \Magento\Framework\ObjectManagerInterface $objectManager */ $objectManager = new $this->_locatorClassName($this->factory, $diConfig, $sharedInstances); $this->factory->setObjectManager($objectManager); $env->configureObjectManager($diConfig, $sharedInstances); return $objectManager; }Environment\Compiled::MODEor else. Atvendor/magento/framework/App/EnvironmentFactory.phpMode Compiled after run commandpublic function createEnvironment() { switch ($this->getMode()) { case Compiled::MODE: return new Compiled($this); break; default: return new Developer($this); } }php bin/magento setup:di:compile, Magento will compile all config to one file php contain config at foldergenerated/metadatawith every area: global, .... It usually map with Magento application mode as production. In Magento application mode as developer, foldergenerated/metadatawill be deleted.In mode Compiled,
$diConfig = $env->getDiConfig();will get config from file php after compiled. Else in mode Developer,$configData = $this->_loadPrimaryConfig($this->directoryList, $this->driverPool, $argumentMapper, $appMode);will help magento read config from file xmlapp/etc/di.xml.At line
$this->factory = $env->getObjectManagerFactory($arguments);, Magento init factory from$envin classAbstractEnvironment.php. This factory used to init$objectManagerand this object be set to this factory$this->factory->setObjectManager($objectManager); -
at
index.phpIn$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class); $bootstrap->run($app);Bootstrap.php, object manager used to create application.
- at
-
Magento initiation ObjectManager follow design pattern Singleton
-
Use object manger to create and retrieve object at anywhere in Magento.
$objectClassCreateNew = \Magento\Framework\App\ObjectManager::getInstance() ->create($typeClassName);$objectClassRetrieve = \Magento\Framework\App\ObjectManager::getInstance() ->get($typeClassName);private $_objectManager; public function __construct( \Magento\Framework\ObjectManagerInterface $objectmanager ){ $this->_objectManager = $objectmanager; } public function getExample() { $customerSession = $this->_objectManager->create("Magento\Customer\Model\Session"); if ($customerSession->isLoggedIn()) { $customerData = $customerSession->getCustomer()->getData(); /*Your logic*/ } } -
Detail more than how Object manager create and retrieve object, how inject interface to
__constructof one class but Magento still inject extract object implement this interface.- When call
\Magento\Framework\App\ObjectManager::getInstance()->create($typeClassName), Magento create object (view filevendor/magento/framework/ObjectManager/ObjectManager.php)public function create($type, array $arguments = []) { return $this->_factory->create($this->_config->getPreference($type), $arguments); }$this->_factorycreated when Object manager created and it isnew Developer($this);created in fileApp/EnvironmentFactory.php -
in file
vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.phppublic function create($requestedType, array $arguments = []) { $type = $this->config->getInstanceType($requestedType); $parameters = $this->definitions->getParameters($type); if ($parameters == null) { return new $type(); } if (isset($this->creationStack[$requestedType])) { $lastFound = end($this->creationStack); $this->creationStack = []; throw new \LogicException("Circular dependency: {$requestedType} depends on {$lastFound} and vice versa."); } $this->creationStack[$requestedType] = $requestedType; try { $args = $this->_resolveArguments($requestedType, $parameters, $arguments); unset($this->creationStack[$requestedType]); } catch (\Exception $e) { unset($this->creationStack[$requestedType]); throw $e; } return $this->createObject($type, $args); }$typeis instance set up in filedi.xml.$parametersis list of method parameters of$type.$argsis recursive to resolve constructor arguments.
- When call
Magento 2 request flow overview¶
- Some documents:
CLI Command¶
Events and observers¶
- Events and observers
- Event and observers in Magento is synchronize. When you call dispatch event with a object, magento will read all observers in all file
events.xml, call observers listen of this event and execute functionexecutein observers.