VarnishPlugin.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Model\App\FrontController;
  7. use Magento\PageCache\Model\Config;
  8. use Magento\Framework\App\PageCache\Version;
  9. use Magento\Framework\App\State as AppState;
  10. use Magento\Framework\App\FrontControllerInterface;
  11. use Magento\Framework\App\ResponseInterface;
  12. use Magento\Framework\App\Response\Http as ResponseHttp;
  13. use Magento\Framework\Controller\ResultInterface;
  14. /**
  15. * Varnish for processing builtin cache
  16. */
  17. class VarnishPlugin
  18. {
  19. /**
  20. * @var Config
  21. */
  22. private $config;
  23. /**
  24. * @var Version
  25. */
  26. private $version;
  27. /**
  28. * @var AppState
  29. */
  30. private $state;
  31. /**
  32. * @param Config $config
  33. * @param Version $version
  34. * @param AppState $state
  35. */
  36. public function __construct(Config $config, Version $version, AppState $state)
  37. {
  38. $this->config = $config;
  39. $this->version = $version;
  40. $this->state = $state;
  41. }
  42. /**
  43. * Perform response postprocessing
  44. *
  45. * @param FrontControllerInterface $subject
  46. * @param ResponseInterface|ResultInterface $result
  47. * @return ResponseHttp|ResultInterface
  48. *
  49. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  50. */
  51. public function afterDispatch(FrontControllerInterface $subject, $result)
  52. {
  53. if ($this->config->getType() == Config::VARNISH && $this->config->isEnabled()
  54. && $result instanceof ResponseHttp
  55. ) {
  56. $this->version->process();
  57. if ($this->state->getMode() == AppState::MODE_DEVELOPER) {
  58. $result->setHeader('X-Magento-Debug', 1);
  59. }
  60. }
  61. return $result;
  62. }
  63. }