/** * Perform action and generate response * * @param RequestInterface $request * @return ResponseInterface|\Magento\Framework\Controller\ResultInterface * @throws \LogicException */ public function dispatch(RequestInterface $request) { \Magento\Framework\Profiler::start('routers_match'); $routingCycleCounter = 0; $result = null; while (!$request->isDispatched() && $routingCycleCounter++ < 100) { /** @var \Magento\Framework\App\RouterInterface $router */ foreach ($this->_routerList as $router) { try { $actionInstance = $router->match($request); if ($actionInstance) { $request->setDispatched(true); $this->response->setNoCacheHeaders(); if ($actionInstance instanceof \Magento\Framework\App\Action\AbstractAction) { $result = $actionInstance->dispatch($request); } else { $result = $actionInstance->execute(); } break; } } catch (\Magento\Framework\Exception\NotFoundException $e) { $request->initForward(); $request->setActionName('noroute'); $request->setDispatched(false); break; } } } \Magento\Framework\Profiler::stop('routers_match'); if ($routingCycleCounter > 100) { throw new \LogicException('Front controller reached 100 router match iterations'); } return $result; }
/** * {@inheritdoc} */ public function setNoCacheHeaders() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'setNoCacheHeaders'); if (!$pluginInfo) { return parent::setNoCacheHeaders(); } else { return $this->___callPlugins('setNoCacheHeaders', func_get_args(), $pluginInfo); } }
/** * Test setting public cache headers */ public function testSetNoCacheHeaders() { $pragma = 'no-cache'; $cacheControl = 'no-store, no-cache, must-revalidate, max-age=0'; $expires = gmdate('D, d M Y H:i:s T', strtotime('-1 year')); $this->model->setNoCacheHeaders(); $this->assertEquals($pragma, $this->model->getHeader('Pragma')['value']); $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')['value']); $this->assertEquals($expires, $this->model->getHeader('Expires')['value']); }
/** * Test setting public cache headers */ public function testSetNoCacheHeaders() { $timestamp = 1000000; $pragma = 'no-cache'; $cacheControl = 'max-age=0, must-revalidate, no-cache, no-store'; $expiresResult = 'Thu, 01 Jan 1970 00:00:00 GMT'; $this->dateTimeMock->expects($this->once())->method('strToTime')->with('-1 year')->willReturn($timestamp); $this->dateTimeMock->expects($this->once())->method('gmDate')->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)->willReturn($expiresResult); $this->model->setNoCacheHeaders(); $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue()); $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue()); $this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue()); }
/** * Modify and cache application response * * @param \Magento\Framework\App\Response\Http $response * @return void */ public function process(\Magento\Framework\App\Response\Http $response) { if (preg_match('/public.*s-maxage=(\\d+)/', $response->getHeader('Cache-Control')['value'], $matches)) { $maxAge = $matches[1]; $response->setNoCacheHeaders(); if ($response->getHttpResponseCode() == 200 && ($this->request->isGet() || $this->request->isHead())) { $tagsHeader = $response->getHeader('X-Magento-Tags'); $tags = $tagsHeader ? explode(',', $tagsHeader['value']) : array(); $response->clearHeader('Set-Cookie'); $response->clearHeader('X-Magento-Tags'); if (!headers_sent()) { header_remove('Set-Cookie'); } $this->cache->save(serialize($response), $this->identifier->getValue(), $tags, $maxAge); } } }