Skip to content

Commit c51b716

Browse files
committed
[Config] simplified DirectoryResource to only allow one regex
1 parent c9c66db commit c51b716

File tree

2 files changed

+31
-63
lines changed

2 files changed

+31
-63
lines changed

src/Symfony/Component/Config/Resource/DirectoryResource.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,18 @@
1919
class DirectoryResource implements ResourceInterface
2020
{
2121
private $resource;
22-
private $filterRegexList;
22+
private $pattern;
2323

2424
/**
2525
* Constructor.
2626
*
2727
* @param string $resource The file path to the resource
28+
* @param string $pattern A pattern to restrict monitored files
2829
*/
29-
public function __construct($resource)
30+
public function __construct($resource, $pattern = null)
3031
{
3132
$this->resource = $resource;
32-
}
33-
34-
/**
35-
* Set a list of filter regex (to restrict the list of monitored files)
36-
*
37-
* @param array $filterRegexList An array of regular expressions
38-
*/
39-
public function setFilterRegexList(array $filterRegexList)
40-
{
41-
$this->filterRegexList = $filterRegexList;
42-
}
43-
44-
/**
45-
* Returns the list of filter regex
46-
*
47-
* @return array An array of regular expressions
48-
*/
49-
public function getFilterRegexList()
50-
{
51-
return $this->filterRegexList;
33+
$this->pattern = $pattern;
5234
}
5335

5436
/**
@@ -87,22 +69,16 @@ public function isFresh($timestamp)
8769
$newestMTime = filemtime($this->resource);
8870
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
8971
// if regex filtering is enabled only check matching files
90-
if (isset($this->filterRegexList) && $file->isFile()) {
91-
$regexMatched = false;
92-
foreach ($this->filterRegexList as $regex) {
93-
if (preg_match($regex, $file->__toString())) {
94-
$regexMatched = true;
95-
}
96-
}
97-
if (!$regexMatched) {
98-
continue;
99-
}
72+
if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
73+
continue;
10074
}
75+
10176
// always monitor directories for changes, except the .. entries
10277
// (otherwise deleted files wouldn't get detected)
10378
if ($file->isDir() && '/..' === substr($file, -3)) {
10479
continue;
10580
}
81+
10682
$newestMTime = max($file->getMTime(), $newestMTime);
10783
}
10884

tests/Symfony/Tests/Component/Config/Resource/DirectoryResourceTest.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
1717
{
18-
protected $resource;
1918
protected $directory;
2019

2120
protected function setUp()
@@ -25,7 +24,6 @@ protected function setUp()
2524
mkdir($this->directory);
2625
}
2726
touch($this->directory.'/tmp.xml');
28-
$this->resource = new DirectoryResource($this->directory);
2927
}
3028

3129
protected function tearDown()
@@ -56,16 +54,18 @@ protected function removeDirectory($directory) {
5654
*/
5755
public function testGetResource()
5856
{
59-
$this->assertEquals($this->directory, $this->resource->getResource(), '->getResource() returns the path to the resource');
57+
$resource = new DirectoryResource($this->directory);
58+
$this->assertEquals($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
6059
}
6160

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

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

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

9294
/**
9395
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
9496
*/
9597
public function testIsFreshDeleteFile()
9698
{
99+
$resource = new DirectoryResource($this->directory);
97100
unlink($this->directory.'/tmp.xml');
98-
$this->assertFalse($this->resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
101+
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
99102
}
100103

101104
/**
102105
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
103106
*/
104107
public function testIsFreshDeleteDirectory()
105108
{
109+
$resource = new DirectoryResource($this->directory);
106110
$this->removeDirectory($this->directory);
107-
$this->assertFalse($this->resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
111+
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
108112
}
109113

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

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

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

124129
/**
125130
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
126131
*/
127132
public function testIsFreshModifySubdirectory()
128133
{
134+
$resource = new DirectoryResource($this->directory);
135+
129136
$subdirectory = $this->directory.'/subdirectory';
130137
mkdir($subdirectory);
131-
132138
touch($subdirectory, time() + 20);
133-
$this->assertFalse($this->resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
134-
}
135139

136-
/**
137-
* @covers Symfony\Component\Config\Resource\DirectoryResource::setFilterRegexList
138-
* @covers Symfony\Component\Config\Resource\DirectoryResource::getFilterRegexList
139-
*/
140-
public function testSetFilterRegexList()
141-
{
142-
$regexes = array('#\.foo$#', '#\.xml$#');
143-
$this->resource->setFilterRegexList($regexes);
144-
145-
$this->assertEquals($regexes, $this->resource->getFilterRegexList(), '->getFilterRegexList() returns the previously defined list of filter regexes');
140+
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
146141
}
147142

148143
/**
149144
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
150145
*/
151146
public function testFilterRegexListNoMatch()
152147
{
153-
$regexes = array('#\.foo$#', '#\.xml$#');
154-
$this->resource->setFilterRegexList($regexes);
148+
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
155149

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

160154
/**
161155
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
162156
*/
163157
public function testFilterRegexListMatch()
164158
{
165-
$regexes = array('#\.foo$#', '#\.xml$#');
166-
$this->resource->setFilterRegexList($regexes);
159+
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
167160

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

0 commit comments

Comments
 (0)