Version.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\View\Deployment;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Config\ConfigOptionsListConstants;
  10. use Psr\Log\LoggerInterface;
  11. /**
  12. * Deployment version of static files
  13. */
  14. class Version
  15. {
  16. /**
  17. * @var \Magento\Framework\App\State
  18. */
  19. private $appState;
  20. /**
  21. * @var \Magento\Framework\App\View\Deployment\Version\StorageInterface
  22. */
  23. private $versionStorage;
  24. /**
  25. * @var string
  26. */
  27. private $cachedValue;
  28. /**
  29. * @var LoggerInterface
  30. */
  31. private $logger;
  32. /**
  33. * @var DeploymentConfig
  34. */
  35. private $deploymentConfig;
  36. /**
  37. * @param \Magento\Framework\App\State $appState
  38. * @param Version\StorageInterface $versionStorage
  39. * @param DeploymentConfig|null $deploymentConfig
  40. */
  41. public function __construct(
  42. \Magento\Framework\App\State $appState,
  43. \Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage,
  44. DeploymentConfig $deploymentConfig = null
  45. ) {
  46. $this->appState = $appState;
  47. $this->versionStorage = $versionStorage;
  48. $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
  49. }
  50. /**
  51. * Retrieve deployment version of static files
  52. *
  53. * @return string
  54. */
  55. public function getValue()
  56. {
  57. if (!$this->cachedValue) {
  58. $this->cachedValue = $this->readValue($this->appState->getMode());
  59. }
  60. return $this->cachedValue;
  61. }
  62. /**
  63. * Load or generate deployment version of static files depending on the application mode
  64. *
  65. * @param string $appMode
  66. * @return string
  67. */
  68. protected function readValue($appMode)
  69. {
  70. $result = $this->versionStorage->load();
  71. if (!$result) {
  72. if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION
  73. && !$this->deploymentConfig->getConfigData(
  74. ConfigOptionsListConstants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION
  75. )
  76. ) {
  77. $this->getLogger()->critical('Can not load static content version.');
  78. throw new \UnexpectedValueException(
  79. "Unable to retrieve deployment version of static files from the file system."
  80. );
  81. }
  82. $result = $this->generateVersion();
  83. $this->versionStorage->save($result);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * Generate version of static content
  89. *
  90. * @return int
  91. */
  92. private function generateVersion()
  93. {
  94. return time();
  95. }
  96. /**
  97. * Get logger
  98. *
  99. * @return LoggerInterface
  100. * @deprecated 101.0.0
  101. */
  102. private function getLogger()
  103. {
  104. if ($this->logger == null) {
  105. $this->logger = \Magento\Framework\App\ObjectManager::getInstance()
  106. ->get(LoggerInterface::class);
  107. }
  108. return $this->logger;
  109. }
  110. }