Cookie.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\Js;
  7. use Magento\Framework\Session\Config\ConfigInterface;
  8. use Magento\Framework\View\Element\Template;
  9. use Magento\Framework\View\Element\Template\Context;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Cookie extends Template
  15. {
  16. /**
  17. * Session config
  18. *
  19. * @var ConfigInterface
  20. */
  21. protected $sessionConfig;
  22. /**
  23. * @var \Magento\Framework\Validator\Ip
  24. */
  25. protected $ipValidator;
  26. /**
  27. * Constructor
  28. *
  29. * @param Context $context
  30. * @param ConfigInterface $cookieConfig
  31. * @param \Magento\Framework\Validator\Ip $ipValidator
  32. * @param array $data
  33. */
  34. public function __construct(
  35. Context $context,
  36. ConfigInterface $cookieConfig,
  37. \Magento\Framework\Validator\Ip $ipValidator,
  38. array $data = []
  39. ) {
  40. $this->sessionConfig = $cookieConfig;
  41. $this->ipValidator = $ipValidator;
  42. parent::__construct($context, $data);
  43. }
  44. /**
  45. * Get configured cookie domain
  46. *
  47. * @return string
  48. */
  49. public function getDomain()
  50. {
  51. $domain = $this->sessionConfig->getCookieDomain();
  52. if ($this->ipValidator->isValid($domain)) {
  53. return $domain;
  54. }
  55. if (!empty($domain[0]) && $domain[0] !== '.') {
  56. $domain = '.' . $domain;
  57. }
  58. return $domain;
  59. }
  60. /**
  61. * Get configured cookie path
  62. *
  63. * @return string
  64. */
  65. public function getPath()
  66. {
  67. return $this->sessionConfig->getCookiePath();
  68. }
  69. /**
  70. * @return int
  71. */
  72. public function getLifetime()
  73. {
  74. return $this->sessionConfig->getCookieLifetime();
  75. }
  76. }