Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Version.php
Go to the documentation of this file.
1 <?php
8 
12 use Psr\Log\LoggerInterface;
13 
17 class Version
18 {
22  private $appState;
23 
27  private $versionStorage;
28 
32  private $cachedValue;
33 
37  private $logger;
38 
42  private $deploymentConfig;
43 
49  public function __construct(
50  \Magento\Framework\App\State $appState,
51  \Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage,
52  DeploymentConfig $deploymentConfig = null
53  ) {
54  $this->appState = $appState;
55  $this->versionStorage = $versionStorage;
56  $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
57  }
58 
64  public function getValue()
65  {
66  if (!$this->cachedValue) {
67  $this->cachedValue = $this->readValue($this->appState->getMode());
68  }
69  return $this->cachedValue;
70  }
71 
78  protected function readValue($appMode)
79  {
80  $result = $this->versionStorage->load();
81  if (!$result) {
82  if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION
83  && !$this->deploymentConfig->getConfigData(
85  )
86  ) {
87  $this->getLogger()->critical('Can not load static content version.');
88  throw new \UnexpectedValueException(
89  "Unable to retrieve deployment version of static files from the file system."
90  );
91  }
92  $result = $this->generateVersion();
93  $this->versionStorage->save($result);
94  }
95  return $result;
96  }
97 
103  private function generateVersion()
104  {
105  return time();
106  }
107 
114  private function getLogger()
115  {
116  if ($this->logger == null) {
118  ->get(LoggerInterface::class);
119  }
120  return $this->logger;
121  }
122 }
__construct(\Magento\Framework\App\State $appState, \Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage, DeploymentConfig $deploymentConfig=null)
Definition: Version.php:49