Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InitParamListener.php
Go to the documentation of this file.
1 <?php
7 
8 use Interop\Container\ContainerInterface;
9 use Interop\Container\Exception\ContainerException;
10 use Magento\Framework\App\Bootstrap as AppBootstrap;
16 use Zend\Console\Request;
17 use Zend\EventManager\EventManagerInterface;
18 use Zend\EventManager\ListenerAggregateInterface;
19 use Zend\Mvc\Application;
20 use Zend\Mvc\MvcEvent;
21 use Zend\Router\Http\RouteMatch;
22 use Zend\ServiceManager\Exception\ServiceNotCreatedException;
23 use Zend\ServiceManager\Exception\ServiceNotFoundException;
24 use Zend\ServiceManager\FactoryInterface;
25 use Zend\ServiceManager\ServiceLocatorInterface;
26 use Zend\Stdlib\RequestInterface;
27 
33 class InitParamListener implements ListenerAggregateInterface, FactoryInterface
34 {
38  const BOOTSTRAP_PARAM = 'magento-init-params';
39 
43  private $listeners = [];
44 
50  private $controllersToSkip = [
51  \Magento\Setup\Controller\Session::class => ['index', 'unlogin'],
52  \Magento\Setup\Controller\Success::class => ['index']
53  ];
54 
62  public function attach(EventManagerInterface $events, $priority = 1)
63  {
64  $sharedEvents = $events->getSharedManager();
65  $this->listeners[] = $sharedEvents->attach(
66  Application::class,
67  MvcEvent::EVENT_BOOTSTRAP,
68  [$this, 'onBootstrap'],
69  $priority
70  );
71  }
72 
76  public function detach(EventManagerInterface $events)
77  {
78  foreach ($this->listeners as $index => $listener) {
79  if ($events->detach($listener)) {
80  unset($this->listeners[$index]);
81  }
82  }
83  }
84 
91  public function onBootstrap(MvcEvent $e)
92  {
94  $application = $e->getApplication();
95  $initParams = $application->getServiceManager()->get(self::BOOTSTRAP_PARAM);
96  $directoryList = $this->createDirectoryList($initParams);
97  $serviceManager = $application->getServiceManager();
98  $serviceManager->setService(\Magento\Framework\App\Filesystem\DirectoryList::class, $directoryList);
99  $serviceManager->setService(\Magento\Framework\Filesystem::class, $this->createFilesystem($directoryList));
100 
101  if (!($application->getRequest() instanceof Request)) {
102  $eventManager = $application->getEventManager();
103  $eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'authPreDispatch'], 100);
104  }
105  }
106 
114  public function authPreDispatch($event)
115  {
117  $routeMatch = $event->getRouteMatch();
118  $controller = $routeMatch->getParam('controller');
119  $action = $routeMatch->getParam('action');
120 
121  $skipCheck = array_key_exists($controller, $this->controllersToSkip)
122  && in_array($action, $this->controllersToSkip[$controller]);
123 
124  if (!$skipCheck) {
126  $application = $event->getApplication();
127  $serviceManager = $application->getServiceManager();
128 
129  if ($serviceManager->get(\Magento\Framework\App\DeploymentConfig::class)->isAvailable()) {
131  $objectManagerProvider = $serviceManager->get(\Magento\Setup\Model\ObjectManagerProvider::class);
133  $objectManager = $objectManagerProvider->get();
135  $adminAppState = $objectManager->get(\Magento\Framework\App\State::class);
136  $adminAppState->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
138  $sessionConfig = $objectManager->get(\Magento\Backend\Model\Session\AdminConfig::class);
139  $cookiePath = $this->getSetupCookiePath($objectManager);
140  $sessionConfig->setCookiePath($cookiePath);
142  $adminSession = $objectManager->create(
143  \Magento\Backend\Model\Auth\Session::class,
144  [
145  'sessionConfig' => $sessionConfig,
146  'appState' => $adminAppState
147  ]
148  );
150  $authentication = $objectManager->get(\Magento\Backend\Model\Auth::class);
151 
152  if (!$authentication->isLoggedIn() ||
153  !$adminSession->isAllowed('Magento_Backend::setup_wizard')
154  ) {
155  $adminSession->destroy();
157  $response = $event->getResponse();
158  $baseUrl = Http::getDistroBaseUrlPath($_SERVER);
159  $response->getHeaders()->addHeaderLine('Location', $baseUrl . 'index.php/session/unlogin');
160  $response->setStatusCode(302);
161  $event->stopPropagation();
162 
163  return $response;
164  }
165  }
166  }
167 
168  return false;
169  }
170 
177  private function getSetupCookiePath(\Magento\Framework\ObjectManagerInterface $objectManager)
178  {
180  $backendAppList = $objectManager->get(\Magento\Backend\App\BackendAppList::class);
181  $backendApp = $backendAppList->getBackendApp('setup');
183  $url = $objectManager->create(\Magento\Backend\Model\Url::class);
184  $baseUrl = parse_url($url->getBaseUrl(), PHP_URL_PATH);
186  $cookiePath = $baseUrl . $backendApp->getCookiePath();
187  return $cookiePath;
188  }
189 
193  public function createService(ServiceLocatorInterface $serviceLocator)
194  {
195  return $this->extractInitParameters($serviceLocator->get('Application'));
196  }
197 
209  private function extractInitParameters(Application $application)
210  {
211  $result = [];
212  $config = $application->getConfig();
213  if (isset($config[self::BOOTSTRAP_PARAM])) {
215  }
216  foreach ([State::PARAM_MODE, AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] as $initKey) {
217  if (isset($_SERVER[$initKey])) {
218  $result[$initKey] = $_SERVER[$initKey];
219  }
220  }
221  $result = array_replace_recursive($result, $this->extractFromCli($application->getRequest()));
222  return $result;
223  }
224 
233  private function extractFromCli(RequestInterface $request)
234  {
235  if (!($request instanceof Request)) {
236  return [];
237  }
238  $bootstrapParam = new ComplexParameter(self::BOOTSTRAP_PARAM);
239  foreach ($request->getContent() as $paramStr) {
240  $result = $bootstrapParam->getFromString($paramStr);
241  if (!empty($result)) {
242  return $result;
243  }
244  }
245  return [];
246  }
247 
255  public function createDirectoryList($initParams)
256  {
257  if (!isset($initParams[AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS][DirectoryList::ROOT])) {
258  throw new \LogicException('Magento root directory is not specified.');
259  }
260  $config = $initParams[AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
262  return new DirectoryList($rootDir, $config);
263  }
264 
271  public function createFilesystem(DirectoryList $directoryList)
272  {
273  $driverPool = new Filesystem\DriverPool();
274  return new Filesystem(
275  $directoryList,
276  new Filesystem\Directory\ReadFactory($driverPool),
277  new Filesystem\Directory\WriteFactory($driverPool)
278  );
279  }
280 }
$response
Definition: 404.php:11
static getDistroBaseUrlPath($server)
Definition: Http.php:362
$objectManager
Definition: bootstrap.php:17
$config
Definition: fraud_order.php:17
attach(EventManagerInterface $events, $priority=1)
$application
Definition: bootstrap.php:58
createFilesystem(DirectoryList $directoryList)
createService(ServiceLocatorInterface $serviceLocator)
$rootDir
Definition: website.php:12
$controller
Definition: info.phtml:14
$index
Definition: list.phtml:44