Skip to content

Commit

Permalink
Merge aae2812 into 07590ef
Browse files Browse the repository at this point in the history
  • Loading branch information
eusonlito committed May 5, 2020
2 parents 07590ef + aae2812 commit 46fc90a
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 65 deletions.
163 changes: 119 additions & 44 deletions src/Storage/AbstractCache.php
Expand Up @@ -3,6 +3,7 @@
namespace League\Flysystem\Cached\Storage;

use League\Flysystem\Cached\CacheInterface;
use League\Flysystem\Config;
use League\Flysystem\Util;

abstract class AbstractCache implements CacheInterface
Expand All @@ -22,6 +23,11 @@ abstract class AbstractCache implements CacheInterface
*/
protected $complete = [];

/**
* @var Config
*/
protected $config;

/**
* Destructor.
*/
Expand All @@ -32,6 +38,26 @@ public function __destruct()
}
}

/**
* Get the config object or null.
*
* @return Config|null config
*/
public function getConfig()
{
return $this->config;
}

/**
* Set the config.
*
* @param Config|array|null $config
*/
public function setConfig($config)
{
$this->config = Util::ensureConfig($config);
}

/**
* Get the autosave setting.
*
Expand Down Expand Up @@ -67,7 +93,8 @@ public function storeContents($directory, array $contents, $recursive = false)

foreach ($contents as $object) {
$this->updateObject($object['path'], $object);
$object = $this->cache[$object['path']];

$object = $this->cache[$this->pathCase($object['path'])];

if ($recursive && $this->pathIsInDirectory($directory, $object['path'])) {
$directories[] = $object['dirname'];
Expand All @@ -90,11 +117,13 @@ public function storeContents($directory, array $contents, $recursive = false)
*/
public function updateObject($path, array $object, $autosave = false)
{
$key = $this->pathCase($path);

if (! $this->has($path)) {
$this->cache[$path] = Util::pathinfo($path);
$this->cache[$key] = Util::pathinfo($path);
}

$this->cache[$path] = array_merge($this->cache[$path], $object);
$this->cache[$key] = array_merge($this->cache[$key], $object);

if ($autosave) {
$this->autosave();
Expand All @@ -110,7 +139,7 @@ public function updateObject($path, array $object, $autosave = false)
*/
public function storeMiss($path)
{
$this->cache[$path] = false;
$this->cache[$this->pathCase($path)] = false;
$this->autosave();
}

Expand All @@ -124,15 +153,17 @@ public function storeMiss($path)
*/
public function listContents($dirname = '', $recursive = false)
{
$key = $this->pathCase($dirname);
$result = [];

foreach ($this->cache as $object) {
if ($object === false) {
continue;
}
if ($object['dirname'] === $dirname) {

if ($this->pathCase($object['dirname']) === $key) {
$result[] = $object;
} elseif ($recursive && $this->pathIsInDirectory($dirname, $object['path'])) {
} elseif ($recursive && $this->pathIsInDirectory($key, $object['path'])) {
$result[] = $object;
}
}
Expand All @@ -145,8 +176,10 @@ public function listContents($dirname = '', $recursive = false)
*/
public function has($path)
{
if ($path !== false && array_key_exists($path, $this->cache)) {
return $this->cache[$path] !== false;
$key = $this->pathCase($path);

if ($path !== false && array_key_exists($key, $this->cache)) {
return $this->cache[$key] !== false;
}

if ($this->isComplete(Util::dirname($path), false)) {
Expand All @@ -159,8 +192,10 @@ public function has($path)
*/
public function read($path)
{
if (isset($this->cache[$path]['contents']) && $this->cache[$path]['contents'] !== false) {
return $this->cache[$path];
$key = $this->pathCase($path);

if (isset($this->cache[$key]['contents']) && $this->cache[$key]['contents'] !== false) {
return $this->cache[$key];
}

return false;
Expand All @@ -179,26 +214,35 @@ public function readStream($path)
*/
public function rename($path, $newpath)
{
if ($this->has($path)) {
$object = $this->cache[$path];
unset($this->cache[$path]);
$object['path'] = $newpath;
$object = array_merge($object, Util::pathinfo($newpath));
$this->cache[$newpath] = $object;
$this->autosave();
if (!$this->has($path)) {
return;
}

$key = $this->pathCase($path);
$object = $this->cache[$key];

unset($this->cache[$key]);

$object['path'] = $newpath;
$object = array_merge($object, Util::pathinfo($newpath));

$this->cache[$this->pathCase($newpath)] = $object;
$this->autosave();
}

/**
* {@inheritdoc}
*/
public function copy($path, $newpath)
{
if ($this->has($path)) {
$object = $this->cache[$path];
$object = array_merge($object, Util::pathinfo($newpath));
$this->updateObject($newpath, $object, true);
if (!$this->has($path)) {
return;
}

$object = $this->cache[$this->pathCase($path)];
$object = array_merge($object, Util::pathinfo($newpath));

$this->updateObject($newpath, $object, true);
}

/**
Expand All @@ -214,9 +258,13 @@ public function delete($path)
*/
public function deleteDir($dirname)
{
$dirname = $this->pathCase($dirname);

foreach ($this->cache as $path => $object) {
if ($this->pathIsInDirectory($dirname, $path) || $path === $dirname) {
unset($this->cache[$path]);
$key = $this->pathCase($path);

if ($this->pathIsInDirectory($dirname, $path) || $key === $dirname) {
unset($this->cache[$key]);
}
}

Expand All @@ -230,27 +278,30 @@ public function deleteDir($dirname)
*/
public function getMimetype($path)
{
if (isset($this->cache[$path]['mimetype'])) {
return $this->cache[$path];
$key = $this->pathCase($path);

if (isset($this->cache[$key]['mimetype'])) {
return $this->cache[$key];
}

if (! $result = $this->read($path)) {
return false;
}

$mimetype = Util::guessMimeType($path, $result['contents']);
$this->cache[$path]['mimetype'] = $mimetype;
$this->cache[$key]['mimetype'] = Util::guessMimeType($path, $result['contents']);

return $this->cache[$path];
return $this->cache[$key];
}

/**
* {@inheritdoc}
*/
public function getSize($path)
{
if (isset($this->cache[$path]['size'])) {
return $this->cache[$path];
$key = $this->pathCase($path);

if (isset($this->cache[$key]['size'])) {
return $this->cache[$key];
}

return false;
Expand All @@ -261,8 +312,10 @@ public function getSize($path)
*/
public function getTimestamp($path)
{
if (isset($this->cache[$path]['timestamp'])) {
return $this->cache[$path];
$key = $this->pathCase($path);

if (isset($this->cache[$key]['timestamp'])) {
return $this->cache[$key];
}

return false;
Expand All @@ -273,8 +326,10 @@ public function getTimestamp($path)
*/
public function getVisibility($path)
{
if (isset($this->cache[$path]['visibility'])) {
return $this->cache[$path];
$key = $this->pathCase($path);

if (isset($this->cache[$key]['visibility'])) {
return $this->cache[$key];
}

return false;
Expand All @@ -285,8 +340,10 @@ public function getVisibility($path)
*/
public function getMetadata($path)
{
if (isset($this->cache[$path]['type'])) {
return $this->cache[$path];
$key = $this->pathCase($path);

if (isset($this->cache[$key]['type'])) {
return $this->cache[$key];
}

return false;
Expand All @@ -297,11 +354,13 @@ public function getMetadata($path)
*/
public function isComplete($dirname, $recursive)
{
if (! array_key_exists($dirname, $this->complete)) {
$key = $this->pathCase($dirname);

if (! array_key_exists($key, $this->complete)) {
return false;
}

if ($recursive && $this->complete[$dirname] !== 'recursive') {
if ($recursive && $this->complete[$key] !== 'recursive') {
return false;
}

Expand All @@ -313,7 +372,7 @@ public function isComplete($dirname, $recursive)
*/
public function setComplete($dirname, $recursive)
{
$this->complete[$dirname] = $recursive ? 'recursive' : true;
$this->complete[$this->pathCase($dirname)] = $recursive ? 'recursive' : true;
}

/**
Expand All @@ -333,7 +392,7 @@ public function cleanContents(array $contents)

foreach ($contents as $path => $object) {
if (is_array($object)) {
$contents[$path] = array_intersect_key($object, $cachedProperties);
$contents[$this->pathCase($path)] = array_intersect_key($object, $cachedProperties);
}
}

Expand Down Expand Up @@ -394,12 +453,12 @@ public function setFromStorage($json)
*/
public function ensureParentDirectories($path)
{
$object = $this->cache[$path];
$object = $this->cache[$this->pathCase($path)];

while ($object['dirname'] !== '' && ! isset($this->cache[$object['dirname']])) {
while ($object['dirname'] !== '' && ! isset($this->cache[$this->pathCase($object['dirname'])])) {
$object = Util::pathinfo($object['dirname']);
$object['type'] = 'dir';
$this->cache[$object['path']] = $object;
$this->cache[$this->pathCase($object['path'])] = $object;
}
}

Expand All @@ -413,6 +472,22 @@ public function ensureParentDirectories($path)
*/
protected function pathIsInDirectory($directory, $path)
{
return $directory === '' || strpos($path, $directory . '/') === 0;
return $directory === '' || strpos($this->pathCase($path), $this->pathCase($directory) . '/') === 0;
}

/**
* Return the path string checking case_sensitive config value
*
* @param string $path
*
* @return string
*/
protected function pathCase($path)
{
if ($this->config && $this->config->get('case_sensitive', true) === false) {
$path = strtolower($path);
}

return $path;
}
}
10 changes: 6 additions & 4 deletions src/Storage/Adapter.php
Expand Up @@ -25,15 +25,17 @@ class Adapter extends AbstractCache
/**
* Constructor.
*
* @param AdapterInterface $adapter adapter
* @param string $file the file to cache to
* @param int|null $expire seconds until cache expiration
* @param AdapterInterface $adapter adapter
* @param string $file the file to cache to
* @param int|null $expire seconds until cache expiration
* @param Config|array|null $config settings values
*/
public function __construct(AdapterInterface $adapter, $file, $expire = null)
public function __construct(AdapterInterface $adapter, $file, $expire = null, $config = [])
{
$this->adapter = $adapter;
$this->file = $file;
$this->setExpire($expire);
$this->setConfig($config);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Storage/Memcached.php
Expand Up @@ -24,15 +24,17 @@ class Memcached extends AbstractCache
/**
* Constructor.
*
* @param \Memcached $memcached
* @param string $key storage key
* @param int|null $expire seconds until cache expiration
* @param \Memcached $memcached
* @param string $key storage key
* @param int|null $expire seconds until cache expiration
* @param Config|array|null $config settings values
*/
public function __construct(NativeMemcached $memcached, $key = 'flysystem', $expire = null)
public function __construct(NativeMemcached $memcached, $key = 'flysystem', $expire = null, $config = [])
{
$this->key = $key;
$this->expire = $expire;
$this->memcached = $memcached;
$this->setConfig($config);
}

/**
Expand Down

0 comments on commit 46fc90a

Please sign in to comment.