Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ObjectManagerProvider.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Model;
8 
9 use Symfony\Component\Console\Application;
12 use Zend\ServiceManager\ServiceLocatorInterface;
14 
23 {
27  private $serviceLocator;
28 
32  private $objectManager;
33 
37  private $bootstrap;
38 
43  public function __construct(
44  ServiceLocatorInterface $serviceLocator,
45  Bootstrap $bootstrap
46  ) {
47  $this->serviceLocator = $serviceLocator;
48  $this->bootstrap = $bootstrap;
49  }
50 
57  public function get()
58  {
59  if (null === $this->objectManager) {
60  $initParams = $this->serviceLocator->get(InitParamListener::BOOTSTRAP_PARAM);
61  $factory = $this->getObjectManagerFactory($initParams);
62  $this->objectManager = $factory->create($initParams);
63  if (PHP_SAPI == 'cli') {
64  $this->createCliCommands();
65  }
66  }
67  return $this->objectManager;
68  }
69 
75  private function createCliCommands()
76  {
78  $commandList = $this->objectManager->create(CommandListInterface::class);
79  foreach ($commandList->getCommands() as $command) {
80  $command->setApplication(
81  $this->serviceLocator->get(Application::class)
82  );
83  }
84  }
85 
91  public function reset()
92  {
93  $this->objectManager = null;
94  }
95 
102  public function setObjectManager(ObjectManagerInterface $objectManager)
103  {
104  $this->objectManager = $objectManager;
105  }
106 
113  public function getObjectManagerFactory($initParams = [])
114  {
115  return $this->bootstrap->createObjectManagerFactory(
116  BP,
117  $initParams
118  );
119  }
120 }
__construct(ServiceLocatorInterface $serviceLocator, Bootstrap $bootstrap)
$objectManager
Definition: bootstrap.php:17
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
const BP
Definition: autoload.php:14
setObjectManager(ObjectManagerInterface $objectManager)