VarnishPlugin.php 1.9 KB

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