From c1d7a557bb0bb58e3ddec06d859b2db061b8460d Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 22 Apr 2013 00:27:04 +0200 Subject: [PATCH] Refactor behaviour triggered by a flag into separate classes. --- src/Symfony/Component/Config/ConfigCache.php | 63 +++----------- .../Component/Config/NonvalidatingCache.php | 69 +++++++++++++++ .../Config/ResourceValidatingCache.php | 86 +++++++++++++++++++ 3 files changed, 168 insertions(+), 50 deletions(-) create mode 100644 src/Symfony/Component/Config/NonvalidatingCache.php create mode 100644 src/Symfony/Component/Config/ResourceValidatingCache.php diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index cbb198427260f..07d47917c76d2 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -15,17 +15,15 @@ use Symfony\Component\Filesystem\Filesystem; /** - * ConfigCache manages PHP cache files. - * - * When debug is enabled, it knows when to flush the cache - * thanks to an array of ResourceInterface instances. + * ConfigCache is a backwards-compatible way of using the + * cache implementation classes. * * @author Fabien Potencier + * @author Matthias Pigulla */ class ConfigCache { - private $debug; - private $file; + private $cacheImplementation; /** * Constructor. @@ -35,8 +33,11 @@ class ConfigCache */ public function __construct($file, $debug) { - $this->file = $file; - $this->debug = (Boolean) $debug; + if ($debug) { + $this->cacheImplementation = new ResourceValidatingCache($file); + } else { + $this->cacheImplementation = new NonvalidatingCache($file); + } } /** @@ -46,7 +47,7 @@ public function __construct($file, $debug) */ public function __toString() { - return $this->file; + return $this->cacheImplementation->__toString(); } /** @@ -59,28 +60,7 @@ public function __toString() */ public function isFresh() { - if (!is_file($this->file)) { - return false; - } - - if (!$this->debug) { - return true; - } - - $metadata = $this->getMetaFile(); - if (!is_file($metadata)) { - return false; - } - - $time = filemtime($this->file); - $meta = unserialize(file_get_contents($metadata)); - foreach ($meta as $resource) { - if (!$resource->isFresh($time)) { - return false; - } - } - - return true; + return $this->cacheImplementation->isFresh(); } /** @@ -89,27 +69,10 @@ public function isFresh() * @param string $content The content to write in the cache * @param ResourceInterface[] $metadata An array of ResourceInterface instances * - * @throws \RuntimeException When cache file can't be wrote + * @throws \RuntimeException When the cache file cannot be written. */ public function write($content, array $metadata = null) { - $mode = 0666 & ~umask(); - $filesystem = new Filesystem(); - $filesystem->dumpFile($this->file, $content, $mode); - - if (null !== $metadata && true === $this->debug) { - $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), $mode); - } + $this->cacheImplementation->write($content, $metadata); } - - /** - * Gets the meta file path. - * - * @return string The meta file path - */ - private function getMetaFile() - { - return $this->file.'.meta'; - } - } diff --git a/src/Symfony/Component/Config/NonvalidatingCache.php b/src/Symfony/Component/Config/NonvalidatingCache.php new file mode 100644 index 0000000000000..342d73061ac72 --- /dev/null +++ b/src/Symfony/Component/Config/NonvalidatingCache.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config; + +use Symfony\Component\Config\Resource\ResourceInterface; +use Symfony\Component\Filesystem\Filesystem; + +/** + * A cache that never expires once it has been written. + * + * @author Fabien Potencier + * @author Matthias Pigulla + */ +class NonvalidatingCache +{ + protected $file; + + /** + * Constructor. + * + * @param string $file The absolute cache path + */ + public function __construct($file) + { + $this->file = $file; + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return $this->file; + } + + /** + * By design, this method always returns true once the cache + * has been initialized. + * + * {@inheritdoc} + */ + public function isFresh() + { + if (!is_file($this->file)) { + return false; + } + + return true; + } + + /** + * This implementation ignores the metadata. + * {@inheritdoc} + */ + public function write($content, array $metadata = null) + { + $filesystem = new Filesystem(); + $filesystem->dumpFile($this->file, $content, 0666 & ~umask()); + } +} diff --git a/src/Symfony/Component/Config/ResourceValidatingCache.php b/src/Symfony/Component/Config/ResourceValidatingCache.php new file mode 100644 index 0000000000000..2539ce0938747 --- /dev/null +++ b/src/Symfony/Component/Config/ResourceValidatingCache.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config; + +use Symfony\Component\Config\Resource\ResourceInterface; +use Symfony\Component\Filesystem\Filesystem; + +/** + * ConfigCache manages PHP cache files. + * + * When debug is enabled, it knows when to flush the cache + * thanks to an array of ResourceInterface instances. + * + * @author Fabien Potencier + */ +class ResourceValidatingCache extends NonvalidatingCache +{ + + /** + * Checks if the cache is still fresh. + * + * This method evaluates the resources/metadata passed to the + * write() method. + * + * @return Boolean true if the cache is fresh, false otherwise + */ + public function isFresh() + { + if (!parent::isFresh()) { + return false; + } + + $metadata = $this->getMetaFile(); + if (!is_file($metadata)) { + return false; + } + + $time = filemtime($this->file); + $meta = unserialize(file_get_contents($metadata)); + foreach ($meta as $resource) { + if (!$resource->isFresh($time)) { + return false; + } + } + + return true; + } + + /** + * Writes cache. + * + * @param string $content The content to write in the cache + * @param ResourceInterface[] $metadata An array of ResourceInterface instances + * + * @throws \RuntimeException When cache file can't be wrote + */ + public function write($content, array $metadata = null) + { + if (null !== $metadata) { + $filesystem = new Filesystem(); + $filesystem->dumpFile($this->getMetaFile(), serialize($metadata), 0666 & ~umask()); + } + + parent::write($content); + } + + /** + * Gets the meta file path. + * + * @return string The meta file path + */ + private function getMetaFile() + { + return $this->file.'.meta'; + } + +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy