Identifier.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\PageCache;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Page unique identifier
  11. */
  12. class Identifier
  13. {
  14. /**
  15. * @var \Magento\Framework\App\Request\Http
  16. */
  17. protected $request;
  18. /**
  19. * @var \Magento\Framework\App\Http\Context
  20. */
  21. protected $context;
  22. /**
  23. * @var Json
  24. */
  25. private $serializer;
  26. /**
  27. * @param \Magento\Framework\App\Request\Http $request
  28. * @param \Magento\Framework\App\Http\Context $context
  29. * @param Json|null $serializer
  30. */
  31. public function __construct(
  32. \Magento\Framework\App\Request\Http $request,
  33. \Magento\Framework\App\Http\Context $context,
  34. Json $serializer = null
  35. ) {
  36. $this->request = $request;
  37. $this->context = $context;
  38. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  39. }
  40. /**
  41. * Return unique page identifier
  42. *
  43. * @return string
  44. */
  45. public function getValue()
  46. {
  47. $data = [
  48. $this->request->isSecure(),
  49. $this->request->getUriString(),
  50. $this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
  51. ?: $this->context->getVaryString()
  52. ];
  53. return sha1($this->serializer->serialize($data));
  54. }
  55. }