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).
This commit is contained in:
parent
73a78d0633
commit
a93aa55199
72
src/Symfony/Component/Config/Resource/DirectoryResource.php
Normal file
72
src/Symfony/Component/Config/Resource/DirectoryResource.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* 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 <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user