Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Http.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\App;
7 
10 use Magento\Framework\App\Request\Http as RequestHttp;
16 
23 {
27  protected $_objectManager;
28 
32  protected $_eventManager;
33 
37  protected $_areaList;
38 
42  protected $_request;
43 
47  protected $_configLoader;
48 
52  protected $_state;
53 
57  protected $_filesystem;
58 
62  protected $_response;
63 
67  protected $registry;
68 
72  private $logger;
73 
85  public function __construct(
87  Event\Manager $eventManager,
88  AreaList $areaList,
89  RequestHttp $request,
91  ConfigLoaderInterface $configLoader,
92  State $state,
94  \Magento\Framework\Registry $registry
95  ) {
96  $this->_objectManager = $objectManager;
97  $this->_eventManager = $eventManager;
98  $this->_areaList = $areaList;
99  $this->_request = $request;
100  $this->_response = $response;
101  $this->_configLoader = $configLoader;
102  $this->_state = $state;
103  $this->_filesystem = $filesystem;
104  $this->registry = $registry;
105  }
106 
114  private function getLogger()
115  {
116  if (!$this->logger instanceof \Psr\Log\LoggerInterface) {
117  $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
118  }
119  return $this->logger;
120  }
121 
128  public function launch()
129  {
130  $areaCode = $this->_areaList->getCodeByFrontName($this->_request->getFrontName());
131  $this->_state->setAreaCode($areaCode);
132  $this->_objectManager->configure($this->_configLoader->load($areaCode));
134  $frontController = $this->_objectManager->get(\Magento\Framework\App\FrontControllerInterface::class);
135  $result = $frontController->dispatch($this->_request);
136  // TODO: Temporary solution until all controllers return ResultInterface (MAGETWO-28359)
137  if ($result instanceof ResultInterface) {
138  $this->registry->register('use_page_cache_plugin', true, true);
139  $result->renderResult($this->_response);
140  } elseif ($result instanceof HttpInterface) {
141  $this->_response = $result;
142  } else {
143  throw new \InvalidArgumentException('Invalid return type');
144  }
145  // This event gives possibility to launch something before sending output (allow cookie setting)
146  $eventParams = ['request' => $this->_request, 'response' => $this->_response];
147  $this->_eventManager->dispatch('controller_front_send_response_before', $eventParams);
148  return $this->_response;
149  }
150 
154  public function catchException(Bootstrap $bootstrap, \Exception $exception)
155  {
156  $result = $this->handleDeveloperMode($bootstrap, $exception)
157  || $this->handleBootstrapErrors($bootstrap, $exception)
158  || $this->handleSessionException($exception)
159  || $this->handleInitException($exception)
160  || $this->handleGenericReport($bootstrap, $exception);
161  return $result;
162  }
163 
171  private function handleDeveloperMode(Bootstrap $bootstrap, \Exception $exception)
172  {
173  if ($bootstrap->isDeveloperMode()) {
174  if (Bootstrap::ERR_IS_INSTALLED == $bootstrap->getErrorCode()) {
175  try {
176  $this->redirectToSetup($bootstrap, $exception);
177  return true;
178  } catch (\Exception $e) {
179  $exception = $e;
180  }
181  }
182  $this->_response->setHttpResponseCode(500);
183  $this->_response->setHeader('Content-Type', 'text/plain');
184  $this->_response->setBody($this->buildContentFromException($exception));
185  $this->_response->sendResponse();
186  return true;
187  }
188  return false;
189  }
190 
197  private function buildContentFromException(\Exception $exception)
198  {
200  $exceptions = [];
201  do {
202  $exceptions[] = $exception;
203  } while ($exception = $exception->getPrevious());
204 
205  $buffer = sprintf("%d exception(s):\n", count($exceptions));
206 
207  foreach ($exceptions as $index => $exception) {
208  $buffer .= sprintf("Exception #%d (%s): %s\n", $index, get_class($exception), $exception->getMessage());
209  }
210 
211  foreach ($exceptions as $index => $exception) {
212  $buffer .= sprintf(
213  "\nException #%d (%s): %s\n%s\n",
214  $index,
215  get_class($exception),
216  $exception->getMessage(),
217  $exception->getTraceAsString()
218  );
219  }
220 
221  return $buffer;
222  }
223 
232  private function redirectToSetup(Bootstrap $bootstrap, \Exception $exception)
233  {
234  $setupInfo = new SetupInfo($bootstrap->getParams());
235  $projectRoot = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
236  if ($setupInfo->isAvailable()) {
237  $this->_response->setRedirect($setupInfo->getUrl());
238  $this->_response->sendHeaders();
239  } else {
240  $newMessage = $exception->getMessage() . "\nNOTE: You cannot install Magento using the Setup Wizard "
241  . "because the Magento setup directory cannot be accessed. \n"
242  . 'You can install Magento using either the command line or you must restore access '
243  . 'to the following directory: ' . $setupInfo->getDir($projectRoot) . "\n";
244 
245  throw new \Exception($newMessage, 0, $exception);
246  }
247  }
248 
256  private function handleBootstrapErrors(Bootstrap $bootstrap, \Exception &$exception)
257  {
258  $bootstrapCode = $bootstrap->getErrorCode();
259  if (Bootstrap::ERR_MAINTENANCE == $bootstrapCode) {
260  require $this->_filesystem->getDirectoryRead(DirectoryList::PUB)->getAbsolutePath('errors/503.php');
261  return true;
262  }
263  if (Bootstrap::ERR_IS_INSTALLED == $bootstrapCode) {
264  try {
265  $this->redirectToSetup($bootstrap, $exception);
266  return true;
267  } catch (\Exception $e) {
268  $exception = $e;
269  }
270  }
271  return false;
272  }
273 
280  private function handleSessionException(\Exception $exception)
281  {
282  if ($exception instanceof \Magento\Framework\Exception\SessionException) {
283  $this->_response->setRedirect($this->_request->getDistroBaseUrl());
284  $this->_response->sendHeaders();
285  return true;
286  }
287  return false;
288  }
289 
296  private function handleInitException(\Exception $exception)
297  {
298  if ($exception instanceof \Magento\Framework\Exception\State\InitException) {
299  $this->getLogger()->critical($exception);
300  require $this->_filesystem->getDirectoryRead(DirectoryList::PUB)->getAbsolutePath('errors/404.php');
301  return true;
302  }
303  return false;
304  }
305 
313  private function handleGenericReport(Bootstrap $bootstrap, \Exception $exception)
314  {
315  $reportData = [$exception->getMessage(), $exception->getTraceAsString()];
316  $params = $bootstrap->getParams();
317  if (isset($params['REQUEST_URI'])) {
318  $reportData['url'] = $params['REQUEST_URI'];
319  }
320  if (isset($params['SCRIPT_NAME'])) {
321  $reportData['script_name'] = $params['SCRIPT_NAME'];
322  }
323  require $this->_filesystem->getDirectoryRead(DirectoryList::PUB)->getAbsolutePath('errors/report.php');
324  return true;
325  }
326 }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, Event\Manager $eventManager, AreaList $areaList, RequestHttp $request, ResponseHttp $response, ConfigLoaderInterface $configLoader, State $state, Filesystem $filesystem, \Magento\Framework\Registry $registry)
Definition: Http.php:85
catchException(Bootstrap $bootstrap, \Exception $exception)
Definition: Http.php:154
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$filesystem
$index
Definition: list.phtml:44