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
9 
16 use Magento\Framework\App\Request\Http as HttpRequest;
18 
20 {
22  const COOKIE_VARY_STRING = 'X-Magento-Vary';
23 
25  const EXPIRATION_TIMESTAMP_FORMAT = 'D, d M Y H:i:s T';
26 
28  const HEADER_X_FRAME_OPT = 'X-Frame-Options';
29 
33  protected $request;
34 
38  protected $cookieManager;
39 
44 
48  protected $context;
49 
53  protected $dateTime;
54 
58  private $sessionConfig;
59 
68  public function __construct(
69  HttpRequest $request,
74  ConfigInterface $sessionConfig = null
75  ) {
76  $this->request = $request;
77  $this->cookieManager = $cookieManager;
78  $this->cookieMetadataFactory = $cookieMetadataFactory;
79  $this->context = $context;
80  $this->dateTime = $dateTime;
81  $this->sessionConfig = $sessionConfig ?: ObjectManager::getInstance()->get(ConfigInterface::class);
82  }
83 
90  public function setXFrameOptions($value)
91  {
92  $this->setHeader(self::HEADER_X_FRAME_OPT, $value);
93  }
94 
100  public function sendVary()
101  {
102  $varyString = $this->context->getVaryString();
103  if ($varyString) {
104  $cookieLifeTime = $this->sessionConfig->getCookieLifetime();
105  $sensitiveCookMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata(
106  [CookieMetadata::KEY_DURATION => $cookieLifeTime]
107  )->setPath('/');
108  $this->cookieManager->setSensitiveCookie(self::COOKIE_VARY_STRING, $varyString, $sensitiveCookMetadata);
109  } elseif ($this->request->get(self::COOKIE_VARY_STRING)) {
110  $cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
111  $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata);
112  }
113  }
114 
123  public function setPublicHeaders($ttl)
124  {
125  if ($ttl < 0 || !preg_match('/^[0-9]+$/', $ttl)) {
126  throw new \InvalidArgumentException('Time to live is a mandatory parameter for set public headers');
127  }
128  $this->setHeader('pragma', 'cache', true);
129  $this->setHeader('cache-control', 'public, max-age=' . $ttl . ', s-maxage=' . $ttl, true);
130  $this->setHeader('expires', $this->getExpirationHeader('+' . $ttl . ' seconds'), true);
131  }
132 
140  public function setPrivateHeaders($ttl)
141  {
142  if (!$ttl) {
143  throw new \InvalidArgumentException('Time to live is a mandatory parameter for set private headers');
144  }
145  $this->setHeader('pragma', 'cache', true);
146  $this->setHeader('cache-control', 'private, max-age=' . $ttl, true);
147  $this->setHeader('expires', $this->getExpirationHeader('+' . $ttl . ' seconds'), true);
148  }
149 
156  public function setNoCacheHeaders()
157  {
158  $this->setHeader('pragma', 'no-cache', true);
159  $this->setHeader('cache-control', 'no-store, no-cache, must-revalidate, max-age=0', true);
160  $this->setHeader('expires', $this->getExpirationHeader('-1 year'), true);
161  }
162 
170  public function representJson($content)
171  {
172  $this->setHeader('Content-Type', 'application/json', true);
173  return $this->setContent($content);
174  }
175 
180  public function __sleep()
181  {
182  return ['content', 'isRedirect', 'statusCode', 'context', 'headers'];
183  }
184 
191  public function __wakeup()
192  {
194  $this->cookieManager = $objectManager->create(\Magento\Framework\Stdlib\CookieManagerInterface::class);
195  $this->cookieMetadataFactory = $objectManager->get(
196  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
197  );
198  $this->request = $objectManager->get(\Magento\Framework\App\Request\Http::class);
199  }
200 
208  protected function getExpirationHeader($time)
209  {
210  return $this->dateTime->gmDate(self::EXPIRATION_TIMESTAMP_FORMAT, $this->dateTime->strToTime($time));
211  }
212 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
__construct(HttpRequest $request, CookieManagerInterface $cookieManager, CookieMetadataFactory $cookieMetadataFactory, Context $context, DateTime $dateTime, ConfigInterface $sessionConfig=null)
Definition: Http.php:68
$value
Definition: gender.phtml:16
setHeader($name, $value, $replace=false)
Definition: Response.php:74