diff --git a/src/Storage/AbstractCache.php b/src/Storage/AbstractCache.php index 141b468..dd58880 100644 --- a/src/Storage/AbstractCache.php +++ b/src/Storage/AbstractCache.php @@ -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 @@ -22,6 +23,11 @@ abstract class AbstractCache implements CacheInterface */ protected $complete = []; + /** + * @var Config + */ + protected $config; + /** * Destructor. */ @@ -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. * @@ -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']; @@ -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(); @@ -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(); } @@ -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; } } @@ -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)) { @@ -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; @@ -179,14 +214,20 @@ 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(); } /** @@ -194,11 +235,14 @@ public function rename($path, $newpath) */ 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); } /** @@ -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]); } } @@ -230,18 +278,19 @@ 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]; } /** @@ -249,8 +298,10 @@ public function getMimetype($path) */ 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; @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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; } /** @@ -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); } } @@ -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; } } @@ -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; } } diff --git a/src/Storage/Adapter.php b/src/Storage/Adapter.php index 3aa8b1a..edce7c1 100644 --- a/src/Storage/Adapter.php +++ b/src/Storage/Adapter.php @@ -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); } /** diff --git a/src/Storage/Memcached.php b/src/Storage/Memcached.php index f67d271..951fb9f 100644 --- a/src/Storage/Memcached.php +++ b/src/Storage/Memcached.php @@ -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); } /** diff --git a/src/Storage/PhpRedis.php b/src/Storage/PhpRedis.php index 4530b14..b57368f 100644 --- a/src/Storage/PhpRedis.php +++ b/src/Storage/PhpRedis.php @@ -24,15 +24,17 @@ class PhpRedis extends AbstractCache /** * Constructor. * - * @param Redis|null $client phpredis client - * @param string $key storage key - * @param int|null $expire seconds until cache expiration + * @param Redis|null $client phpredis client + * @param string $key storage key + * @param int|null $expire seconds until cache expiration + * @param Config|array|null $config settings values */ - public function __construct(Redis $client = null, $key = 'flysystem', $expire = null) + public function __construct(Redis $client = null, $key = 'flysystem', $expire = null, $config = []) { $this->client = $client ?: new Redis(); $this->key = $key; $this->expire = $expire; + $this->setConfig($config); } /** diff --git a/src/Storage/Predis.php b/src/Storage/Predis.php index 8a29574..d567551 100644 --- a/src/Storage/Predis.php +++ b/src/Storage/Predis.php @@ -24,15 +24,17 @@ class Predis extends AbstractCache /** * Constructor. * - * @param \Predis\Client $client predis client - * @param string $key storage key - * @param int|null $expire seconds until cache expiration + * @param \Predis\Client $client predis client + * @param string $key storage key + * @param int|null $expire seconds until cache expiration + * @param Config|array|null $config settings values */ - public function __construct(Client $client = null, $key = 'flysystem', $expire = null) + public function __construct(Client $client = null, $key = 'flysystem', $expire = null, $config = []) { $this->client = $client ?: new Client(); $this->key = $key; $this->expire = $expire; + $this->setConfig($config); } /** diff --git a/src/Storage/Psr6Cache.php b/src/Storage/Psr6Cache.php index 43be87e..230482b 100644 --- a/src/Storage/Psr6Cache.php +++ b/src/Storage/Psr6Cache.php @@ -27,12 +27,14 @@ class Psr6Cache extends AbstractCache * @param CacheItemPoolInterface $pool * @param string $key storage key * @param int|null $expire seconds until cache expiration + * @param Config|array|null $config settings values */ - public function __construct(CacheItemPoolInterface $pool, $key = 'flysystem', $expire = null) + public function __construct(CacheItemPoolInterface $pool, $key = 'flysystem', $expire = null, $config = []) { $this->pool = $pool; $this->key = $key; $this->expire = $expire; + $this->setConfig($config); } /** diff --git a/src/Storage/Stash.php b/src/Storage/Stash.php index e05b832..0bedff3 100644 --- a/src/Storage/Stash.php +++ b/src/Storage/Stash.php @@ -24,15 +24,17 @@ class Stash extends AbstractCache /** * Constructor. * - * @param \Stash\Pool $pool - * @param string $key storage key - * @param int|null $expire seconds until cache expiration + * @param \Stash\Pool $pool + * @param string $key storage key + * @param int|null $expire seconds until cache expiration + * @param Config|array|null $config settings values */ - public function __construct(Pool $pool, $key = 'flysystem', $expire = null) + public function __construct(Pool $pool, $key = 'flysystem', $expire = null, $config = []) { $this->key = $key; $this->expire = $expire; $this->pool = $pool; + $this->setConfig($config); } /**