Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / src / FileMetadataManager.php
1 <?php
2
3 namespace Drupal\file_mdm;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\File\FileSystemInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\file_mdm\Plugin\FileMetadataPluginManager;
10 use Psr\Log\LoggerInterface;
11
12 /**
13  * A service class to provide file metadata.
14  */
15 class FileMetadataManager implements FileMetadataManagerInterface {
16
17   use StringTranslationTrait;
18
19   /**
20    * The FileMetadata plugin manager.
21    *
22    * @var \Drupal\file_mdm\Plugin\FileMetadataPluginManager
23    */
24   protected $pluginManager;
25
26   /**
27    * The file_mdm logger.
28    *
29    * @var \Psr\Log\LoggerInterface
30    */
31   protected $logger;
32
33   /**
34    * The config factory service.
35    *
36    * @var \Drupal\Core\Config\ConfigFactoryInterface
37    */
38   protected $configFactory;
39
40   /**
41    * The file system service.
42    *
43    * @var \Drupal\Core\File\FileSystemInterface
44    */
45   protected $fileSystem;
46
47   /**
48    * The cache service.
49    *
50    * @var \Drupal\Core\Cache\CacheBackendInterface
51    */
52   protected $cache;
53
54   /**
55    * The array of FileMetadata objects currently in use.
56    *
57    * @var \Drupal\file_mdm\FileMetadataInterface[]
58    */
59   protected $files = [];
60
61   /**
62    * Constructs a FileMetadataManager object.
63    *
64    * @param \Drupal\file_mdm\Plugin\FileMetadataPluginManager $plugin_manager
65    *   The FileMetadata plugin manager.
66    * @param \Psr\Log\LoggerInterface $logger
67    *   The file_mdm logger.
68    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
69    *   The config factory.
70    * @param \Drupal\Core\File\FileSystemInterface $file_system
71    *   The file system service.
72    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_service
73    *   The cache service.
74    */
75   public function __construct(FileMetadataPluginManager $plugin_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, FileSystemInterface $file_system, CacheBackendInterface $cache_service) {
76     $this->pluginManager = $plugin_manager;
77     $this->logger = $logger;
78     $this->configFactory = $config_factory;
79     $this->fileSystem = $file_system;
80     $this->cache = $cache_service;
81   }
82
83   /**
84    * Returns an hash for the URI, used internally by the manager.
85    *
86    * @param string $uri
87    *   The URI to a file.
88    *
89    * @return string
90    *   An hash string.
91    */
92   protected function calculateHash($uri) {
93     // Sanitize URI removing duplicate slashes, if any.
94     // @see http://stackoverflow.com/questions/12494515/remove-unnecessary-slashes-from-path
95     $uri = preg_replace('/([^:])(\/{2,})/', '$1/', $uri);
96     // If URI is invalid and no local file path exists, return NULL.
97     if (!file_valid_uri($uri) && !$this->fileSystem->realpath($uri)) {
98       return NULL;
99     }
100     // Return a hash of the URI.
101     return hash('sha256', $uri);
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function has($uri) {
108     $hash = $this->calculateHash($uri);
109     return $hash ? isset($this->files[$hash]) : NULL;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function uri($uri) {
116     if (!$hash = $this->calculateHash($uri)) {
117       return NULL;
118     }
119     if (!isset($this->files[$hash])) {
120       $this->files[$hash] = new FileMetadata($this->pluginManager, $this->logger, $this->fileSystem, $uri, $hash);
121     }
122     return $this->files[$hash];
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function deleteCachedMetadata($uri) {
129     if (!$hash = $this->calculateHash($uri)) {
130       return FALSE;
131     }
132     foreach (array_keys($this->pluginManager->getDefinitions()) as $plugin_id) {
133       $this->cache->delete("hash:{$plugin_id}:{$hash}");
134     }
135     return TRUE;
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function release($uri) {
142     if (!$hash = $this->calculateHash($uri)) {
143       return FALSE;
144     }
145     if (isset($this->files[$hash])) {
146       unset($this->files[$hash]);
147       return TRUE;
148     }
149     return FALSE;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function count() {
156     return count($this->files);
157   }
158
159 }