class FileMetadata in File metadata manager 8
Same name in this branch
- 8 src/FileMetadata.php \Drupal\file_mdm\FileMetadata
- 8 src/Plugin/Annotation/FileMetadata.php \Drupal\file_mdm\Plugin\Annotation\FileMetadata
Same name and namespace in other branches
- 8.2 src/FileMetadata.php \Drupal\file_mdm\FileMetadata
A file metadata object.
Hierarchy
- class \Drupal\file_mdm\FileMetadata implements FileMetadataInterface
Expanded class hierarchy of FileMetadata
File
- src/
FileMetadata.php, line 13
Namespace
Drupal\file_mdmView source
class FileMetadata implements FileMetadataInterface {
/**
* The FileMetadata plugin manager.
*
* @var \Drupal\file_mdm\Plugin\FileMetadataPluginManager
*/
protected $pluginManager;
/**
* The file_mdm logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The URI of the file.
*
* @var string
*/
protected $uri = '';
/**
* The hash used to reference the URI.
*
* @var string
*/
protected $hash;
/**
* The local filesystem path to the file.
*
* This is used to allow accessing local copies of files stored remotely, to
* minimise remote calls and allow functions that cannot access remote stream
* wrappers to operate locally.
*
* @var string
*/
protected $localTempPath;
/**
* The array of FileMetadata plugins for this URI.
*
* @var \Drupal\file_mdm\Plugin\FileMetadataPluginInterface[]
*/
protected $plugins = [];
/**
* Constructs a FileMetadata object.
*
* @param \Drupal\file_mdm\Plugin\FileMetadataPluginManager $plugin_manager
* The file metadata plugin manager.
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
* @param string $uri
* The URI of the file.
* @param string $hash
* The hash used to reference the URI by file_mdm.
*/
public function __construct(FileMetadataPluginManager $plugin_manager, LoggerInterface $logger, FileSystemInterface $file_system, $uri, $hash) {
$this->pluginManager = $plugin_manager;
$this->logger = $logger;
$this->fileSystem = $file_system;
$this->uri = $uri;
$this->hash = $hash;
}
/**
* {@inheritdoc}
*/
public function getUri() {
return $this->uri;
}
/**
* {@inheritdoc}
*/
public function getLocalTempPath() {
return $this->localTempPath;
}
/**
* {@inheritdoc}
*/
public function setLocalTempPath($temp_uri) {
$this->localTempPath = $temp_uri;
foreach ($this->plugins as $plugin) {
$plugin
->setLocalTempPath($this->localTempPath);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function copyUriToTemp($temp_uri = NULL) {
if ($temp_uri === NULL) {
$temp_uri = $this->fileSystem
->tempnam('temporary://', 'file_mdm_');
$this->fileSystem
->unlink($temp_uri);
$temp_uri .= '.' . pathinfo($this
->getUri(), PATHINFO_EXTENSION);
}
if ($temp_path = file_unmanaged_copy($this
->getUri(), $this->fileSystem
->realpath($temp_uri), FILE_EXISTS_REPLACE)) {
$this
->setLocalTempPath($temp_path);
}
return (bool) $temp_path;
}
/**
* {@inheritdoc}
*/
public function copyTempToUri() {
if (($temp_path = $this
->getLocalTempPath()) === NULL) {
return FALSE;
}
return (bool) file_unmanaged_copy($temp_path, $this
->getUri(), FILE_EXISTS_REPLACE);
}
/**
* {@inheritdoc}
*/
public function getFileMetadataPlugin($metadata_id) {
if (!isset($this->plugins[$metadata_id])) {
try {
$this->plugins[$metadata_id] = $this->pluginManager
->createInstance($metadata_id);
$this->plugins[$metadata_id]
->setUri($this->uri);
$this->plugins[$metadata_id]
->setLocalTempPath($this->localTempPath ?: $this->uri);
$this->plugins[$metadata_id]
->setHash($this->hash);
} catch (PluginNotFoundException $e) {
return NULL;
}
}
return $this->plugins[$metadata_id];
}
/**
* {@inheritdoc}
*/
public function getSupportedKeys($metadata_id, $options = NULL) {
try {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
$keys = $plugin
->getSupportedKeys($options);
}
else {
$keys = NULL;
}
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
$keys = NULL;
}
return $keys;
}
/**
* {@inheritdoc}
*/
public function getMetadata($metadata_id, $key = NULL) {
try {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
$metadata = $plugin
->getMetadata($key);
}
else {
$metadata = NULL;
}
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
$metadata = NULL;
}
return $metadata;
}
/**
* {@inheritdoc}
*/
public function removeMetadata($metadata_id, $key) {
try {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->removeMetadata($key);
}
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function setMetadata($metadata_id, $key, $value) {
try {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->setMetadata($key, $value);
}
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isMetadataLoaded($metadata_id) {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->isMetadataLoaded();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function loadMetadata($metadata_id, $metadata) {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->loadMetadata($metadata);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function loadMetadataFromCache($metadata_id) {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->loadMetadataFromCache();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function saveMetadataToCache($metadata_id, array $tags = []) {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->saveMetadataToCache($tags);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function saveMetadataToFile($metadata_id) {
if ($plugin = $this
->getFileMetadataPlugin($metadata_id)) {
return $plugin
->saveMetadataToFile();
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileMetadata:: |
protected | property | The file system service. | |
FileMetadata:: |
protected | property | The hash used to reference the URI. | |
FileMetadata:: |
protected | property | The local filesystem path to the file. | |
FileMetadata:: |
protected | property | The file_mdm logger. | |
FileMetadata:: |
protected | property | The FileMetadata plugin manager. | |
FileMetadata:: |
protected | property | The array of FileMetadata plugins for this URI. | |
FileMetadata:: |
protected | property | The URI of the file. | |
FileMetadata:: |
public | function |
Copies the local temporary file to the destination URI. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Copies the file at URI to a local temporary file. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Gets a FileMetadata plugin instance. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Gets the local filesystem URI to the temporary file. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Gets a metadata element. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Returns a list of supported metadata keys. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Gets the URI of the file. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Checks if file metadata has been already loaded. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Loads file metadata. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Loads file metadata from a cache entry. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Removes a metadata element. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Caches metadata for file at URI. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Saves metadata to file at URI. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Sets the local filesystem URI to the temporary file. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function |
Sets a metadata element. Overrides FileMetadataInterface:: |
|
FileMetadata:: |
public | function | Constructs a FileMetadata object. | |
FileMetadataInterface:: |
constant | Metadata loaded by code. | ||
FileMetadataInterface:: |
constant | Metadata loaded from cache. | ||
FileMetadataInterface:: |
constant | Metadata loaded from file. | ||
FileMetadataInterface:: |
constant | Metadata not loaded. |