3 namespace Drupal\file_mdm;
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;
13 * A service class to provide file metadata.
15 class FileMetadataManager implements FileMetadataManagerInterface {
17 use StringTranslationTrait;
20 * The FileMetadata plugin manager.
22 * @var \Drupal\file_mdm\Plugin\FileMetadataPluginManager
24 protected $pluginManager;
27 * The file_mdm logger.
29 * @var \Psr\Log\LoggerInterface
34 * The config factory service.
36 * @var \Drupal\Core\Config\ConfigFactoryInterface
38 protected $configFactory;
41 * The file system service.
43 * @var \Drupal\Core\File\FileSystemInterface
45 protected $fileSystem;
50 * @var \Drupal\Core\Cache\CacheBackendInterface
55 * The array of FileMetadata objects currently in use.
57 * @var \Drupal\file_mdm\FileMetadataInterface[]
59 protected $files = [];
62 * Constructs a FileMetadataManager object.
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
70 * @param \Drupal\Core\File\FileSystemInterface $file_system
71 * The file system service.
72 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_service
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;
84 * Returns an hash for the URI, used internally by the manager.
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)) {
100 // Return a hash of the URI.
101 return hash('sha256', $uri);
107 public function has($uri) {
108 $hash = $this->calculateHash($uri);
109 return $hash ? isset($this->files[$hash]) : NULL;
115 public function uri($uri) {
116 if (!$hash = $this->calculateHash($uri)) {
119 if (!isset($this->files[$hash])) {
120 $this->files[$hash] = new FileMetadata($this->pluginManager, $this->logger, $this->fileSystem, $uri, $hash);
122 return $this->files[$hash];
128 public function deleteCachedMetadata($uri) {
129 if (!$hash = $this->calculateHash($uri)) {
132 foreach (array_keys($this->pluginManager->getDefinitions()) as $plugin_id) {
133 $this->cache->delete("hash:{$plugin_id}:{$hash}");
141 public function release($uri) {
142 if (!$hash = $this->calculateHash($uri)) {
145 if (isset($this->files[$hash])) {
146 unset($this->files[$hash]);
155 public function count() {
156 return count($this->files);