From a93aa5519967c2bd6aa76c1cb9e49b47863fc1a2 Mon Sep 17 00:00:00 2001 From: develop Date: Fri, 4 Mar 2011 14:17:12 +0100 Subject: [PATCH] If you use an annotation resource pointing to a controller directory in your routing configuration, only existing files in this directory get tracked for changes in this directory. So if you add a new controller file in this directory you'd have to manually clear the cache since the new file gets ignored. This patch adds a DirectoryResource for this case which tracks all changes in the given directory (and files and directories contained within). --- .../Config/Resource/DirectoryResource.php | 72 +++++++++++++++++++ .../Loader/AnnotationDirectoryLoader.php | 4 +- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Config/Resource/DirectoryResource.php diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php new file mode 100644 index 0000000000..190ff5d58e --- /dev/null +++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Resource; + +/** + * DirectoryResource represents a resources stored in a subdirectory tree. + * + * @author Fabien Potencier + */ +class DirectoryResource implements ResourceInterface +{ + protected $resource; + + /** + * Constructor. + * + * @param string $resource The file path to the resource + */ + public function __construct($resource) + { + $this->resource = realpath($resource); + } + + /** + * Returns a string representation of the Resource. + * + * @return string A string representation of the Resource + */ + public function __toString() + { + return (string) $this->resource; + } + + /** + * Returns the resource tied to this Resource. + * + * @return mixed The resource + */ + public function getResource() + { + return $this->resource; + } + + /** + * Returns true if the resource has not been updated since the given timestamp. + * + * @param integer $timestamp The last time the resource was loaded + * + * @return Boolean true if the resource has not been updated, false otherwise + */ + public function isFresh($timestamp) + { + if (!file_exists($this->resource)) { + return false; + } + + $newestMTime = 0; + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) { + $newestMTime = max(filemtime($file), $newestMTime); + } + return $newestMTime < $timestamp; + } +} diff --git a/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php index 4a47d79f2a..325f400be7 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Routing\Loader; use Symfony\Component\Routing\RouteCollection; -use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\Config\Resource\DirectoryResource; /** * AnnotationDirectoryLoader loads routing information from annotations set @@ -37,13 +37,13 @@ class AnnotationDirectoryLoader extends AnnotationFileLoader $dir = $this->locator->locate($path); $collection = new RouteCollection(); + $collection->addResource(new DirectoryResource($dir)); foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) { if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) { continue; } if ($class = $this->findClass($file)) { - $collection->addResource(new FileResource($file)); $collection->addCollection($this->loader->load($class, $type)); } }