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