Skip to content

Commit

Permalink
[Config] simplified DirectoryResource to only allow one regex
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed Apr 1, 2011
1 parent c9c66db commit c51b716
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 63 deletions.
40 changes: 8 additions & 32 deletions src/Symfony/Component/Config/Resource/DirectoryResource.php
Expand Up @@ -19,36 +19,18 @@
class DirectoryResource implements ResourceInterface
{
private $resource;
private $filterRegexList;
private $pattern;

/**
* Constructor.
*
* @param string $resource The file path to the resource
* @param string $pattern A pattern to restrict monitored files
*/
public function __construct($resource)
public function __construct($resource, $pattern = null)
{
$this->resource = $resource;
}

/**
* Set a list of filter regex (to restrict the list of monitored files)
*
* @param array $filterRegexList An array of regular expressions
*/
public function setFilterRegexList(array $filterRegexList)
{
$this->filterRegexList = $filterRegexList;
}

/**
* Returns the list of filter regex
*
* @return array An array of regular expressions
*/
public function getFilterRegexList()
{
return $this->filterRegexList;
$this->pattern = $pattern;
}

/**
Expand Down Expand Up @@ -87,22 +69,16 @@ public function isFresh($timestamp)
$newestMTime = filemtime($this->resource);
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
// if regex filtering is enabled only check matching files
if (isset($this->filterRegexList) && $file->isFile()) {
$regexMatched = false;
foreach ($this->filterRegexList as $regex) {
if (preg_match($regex, $file->__toString())) {
$regexMatched = true;
}
}
if (!$regexMatched) {
continue;
}
if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
continue;
}

// always monitor directories for changes, except the .. entries
// (otherwise deleted files wouldn't get detected)
if ($file->isDir() && '/..' === substr($file, -3)) {
continue;
}

$newestMTime = max($file->getMTime(), $newestMTime);
}

Expand Down
Expand Up @@ -15,7 +15,6 @@

class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
{
protected $resource;
protected $directory;

protected function setUp()
Expand All @@ -25,7 +24,6 @@ protected function setUp()
mkdir($this->directory);
}
touch($this->directory.'/tmp.xml');
$this->resource = new DirectoryResource($this->directory);
}

protected function tearDown()
Expand Down Expand Up @@ -56,16 +54,18 @@ protected function removeDirectory($directory) {
*/
public function testGetResource()
{
$this->assertEquals($this->directory, $this->resource->getResource(), '->getResource() returns the path to the resource');
$resource = new DirectoryResource($this->directory);
$this->assertEquals($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFresh()
{
$this->assertTrue($this->resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
$this->assertFalse($this->resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
$resource = new DirectoryResource($this->directory);
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
$this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');

$resource = new DirectoryResource('/____foo/foobar'.rand(1, 999999));
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
Expand All @@ -76,35 +76,39 @@ public function testIsFresh()
*/
public function testIsFreshUpdateFile()
{
$resource = new DirectoryResource($this->directory);
touch($this->directory.'/tmp.xml', time() + 20);
$this->assertFalse($this->resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshNewFile()
{
$resource = new DirectoryResource($this->directory);
touch($this->directory.'/new.xml', time() + 20);
$this->assertFalse($this->resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshDeleteFile()
{
$resource = new DirectoryResource($this->directory);
unlink($this->directory.'/tmp.xml');
$this->assertFalse($this->resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshDeleteDirectory()
{
$resource = new DirectoryResource($this->directory);
$this->removeDirectory($this->directory);
$this->assertFalse($this->resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
}

/**
Expand All @@ -115,58 +119,46 @@ public function testIsFreshCreateFileInSubdirectory()
$subdirectory = $this->directory.'/subdirectory';
mkdir($subdirectory);

$this->assertTrue($this->resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
$resource = new DirectoryResource($this->directory);
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');

touch($subdirectory.'/newfile.xml', time() + 20);
$this->assertFalse($this->resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshModifySubdirectory()
{
$resource = new DirectoryResource($this->directory);

$subdirectory = $this->directory.'/subdirectory';
mkdir($subdirectory);

touch($subdirectory, time() + 20);
$this->assertFalse($this->resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::setFilterRegexList
* @covers Symfony\Component\Config\Resource\DirectoryResource::getFilterRegexList
*/
public function testSetFilterRegexList()
{
$regexes = array('#\.foo$#', '#\.xml$#');
$this->resource->setFilterRegexList($regexes);

$this->assertEquals($regexes, $this->resource->getFilterRegexList(), '->getFilterRegexList() returns the previously defined list of filter regexes');
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testFilterRegexListNoMatch()
{
$regexes = array('#\.foo$#', '#\.xml$#');
$this->resource->setFilterRegexList($regexes);
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');

touch($this->directory.'/new.bar', time() + 20);
$this->assertTrue($this->resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testFilterRegexListMatch()
{
$regexes = array('#\.foo$#', '#\.xml$#');
$this->resource->setFilterRegexList($regexes);
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');

touch($this->directory.'/new.xml', time() + 20);
$this->assertFalse($this->resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
}

}

0 comments on commit c51b716

Please sign in to comment.