View source
<?php
namespace Drupal\flysystem\Flysystem\Adapter;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
class DrupalCacheAdapter implements AdapterInterface {
protected $adapter;
protected $cacheItemBackend;
protected $scheme;
public function __construct($scheme, AdapterInterface $adapter, CacheItemBackend $cacheItemBackend) {
$this->scheme = $scheme;
$this->adapter = $adapter;
$this->cacheItemBackend = $cacheItemBackend;
}
public function write($path, $contents, Config $config) {
$metadata = $this->adapter
->write($path, $contents, $config);
return $this
->updateMetadata($path, $metadata);
}
public function writeStream($path, $resource, Config $config) {
$metadata = $this->adapter
->writeStream($path, $resource, $config);
return $this
->updateMetadata($path, $metadata);
}
public function update($path, $contents, Config $config) {
$metadata = $this->adapter
->update($path, $contents, $config);
return $this
->updateMetadata($path, $metadata);
}
public function updateStream($path, $resource, Config $config) {
$metadata = $this->adapter
->updateStream($path, $resource, $config);
return $this
->updateMetadata($path, $metadata);
}
public function rename($path, $newpath) {
$result = $this->adapter
->rename($path, $newpath);
if ($result) {
$item = $this->cacheItemBackend
->load($path);
$newitem = clone $item;
$this->cacheItemBackend
->set($newpath, $newitem);
$this->cacheItemBackend
->delete($path);
}
return $result;
}
public function copy($path, $newpath) {
$result = $this->adapter
->copy($path, $newpath);
if ($result) {
$item = $this->cacheItemBackend
->load($path);
$newitem = clone $item;
$this->cacheItemBackend
->set($newpath, $newitem);
}
return $result;
}
public function delete($path) {
$result = $this->adapter
->delete($path);
if ($result) {
$this->cacheItemBackend
->delete($path);
}
return $result;
}
public function deleteDir($dirname) {
$contents = $this->adapter
->listContents($dirname, TRUE);
$result = $this->adapter
->deleteDir($dirname);
if ($result) {
$paths = array_column($contents, 'path');
$this->cacheItemBackend
->deleteMultiple($paths);
}
return $result;
}
public function createDir($dirname, Config $config) {
$metadata = $this->adapter
->createDir($dirname, $config);
if ($metadata) {
$item = new CacheItem();
$item
->updateMetadata($metadata);
$this->cacheItemBackend
->set($dirname, $item);
}
return $metadata;
}
public function setVisibility($path, $visibility) {
$metadata = $this->adapter
->setVisibility($path, $visibility);
return $this
->updateMetadata($path, $metadata);
}
public function has($path) {
if ($this->cacheItemBackend
->has($path)) {
return TRUE;
}
return $this->adapter
->has($path);
}
public function read($path) {
return $this->adapter
->read($path);
}
public function readStream($path) {
return $this->adapter
->readStream($path);
}
public function listContents($directory = '', $recursive = FALSE) {
return $this->adapter
->listContents($directory, $recursive);
}
public function getMetadata($path) {
$item = $this->cacheItemBackend
->load($path);
if ($metadata = $item
->getMetadata()) {
return $metadata;
}
$metadata = $this->adapter
->getMetadata($path);
$item
->updateMetadata($metadata);
$this->cacheItemBackend
->set($path, $item);
return $metadata;
}
public function getSize($path) {
return $this
->fetchMetadataKey($path, 'size');
}
public function getMimetype($path) {
return $this
->fetchMetadataKey($path, 'mimetype');
}
public function getTimestamp($path) {
return $this
->fetchMetadataKey($path, 'timestamp');
}
public function getVisibility($path) {
return $this
->fetchMetadataKey($path, 'visibility');
}
protected function fetchMetadataKey($path, $key) {
$item = $this->cacheItemBackend
->load($path);
if (($metadata = $item
->getMetadata()) && isset($metadata[$key])) {
return $metadata;
}
$method = 'get' . ucfirst($key);
return $this
->updateMetadata($path, $this->adapter
->{$method}($path));
}
protected function updateMetadata($path, $metadata) {
if (!empty($metadata)) {
$item = $this->cacheItemBackend
->load($path);
$item
->updateMetadata($metadata);
$this->cacheItemBackend
->set($path, $item);
}
return $metadata;
}
}