8000 merged branch everzet/config-additions-from-rw (PR #4619) · sigues/symfony@041286e · GitHub
[go: up one dir, main page]

Skip to content

Commit 041286e

Browse files
committed
merged branch everzet/config-additions-from-rw (PR symfony#4619)
Commits ------- 241aa92 [Config] added existence check to some resource methods 56b60c8 [Config] use is_file in FileResource::exists() ff9c132 [Config] added type prefixes to resource ids ece489f [Config] skip dots in getFilteredChilds() (fixes test suite on Linux) c9eaa72 [Config] made ResourceInterface extends Serializable d7c24eb [Config] added new methods and their tests to File and Directory resources 9fe0d00 [Config] update FileResourceTest 45a45ba [Config] updated DirectoryResource tests 1f9ba38 [Config] getFilteredChildResources() method added to DirectoryResource 6b39688 [Config] moved DirectoryResource childs retrieving to the special getFilteredChilds method 45df2e6 [Config] updated resources API to be more explicit Discussion ---------- [Config] additions from ResourceWatcher Extracted `Config` component changes from `ResourceWatcher` component. --------------------------------------------------------------------------- by travisbot at 2012-06-20T08:27:30Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1662786) (merged 241aa92 into 092b5dd).
2 parents 4c8ea31 + 241aa92 commit 041286e

File tree

5 files changed

+359
-23
lines changed

5 files changed

+359
-23
lines changed

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

Lines changed: 148 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
*/
19-
class DirectoryResource implements ResourceInterface, \Serializable
19+
class DirectoryResource implements ResourceInterface
2020
{
2121
private $resource;
2222
private $pattern;
@@ -33,6 +33,82 @@ public function __construct($resource, $pattern = null)
3333
$this->pattern = $pattern;
3434
}
3535

36+
/**
37+
* Returns the list of filtered file and directory childs of directory resource.
38+
*
39+
* @return array An array of files
40+
*/
41+
public function getFilteredChilds()
42+
{
43+
if (!$this->exists()) {
44+
return array();
45+
}
46+
47+
$iterator = new \RecursiveIteratorIterator(
48+
new \RecursiveDirectoryIterator($this->resource, \FilesystemIterator::SKIP_DOTS),
49+
\RecursiveIteratorIterator::SELF_FIRST
50+
);
51+
52+
$childs = array();
53+
foreach ($iterator as $file) {
54+
// if regex filtering is enabled only return matching files
55+
if ($file->isFile() && !$this->hasFile($file)) {
56+
continue;
57+
}
58+
59+
// always monitor directories for changes, except the .. entries
60+
// (otherwise deleted files wouldn't get detected)
61+
if ($file->isDir() && '/..' === substr($file, -3)) {
62+
continue;
63+
}
64+
65+
$childs[] = $file;
66+
}
67+
68+
return $childs;
69+
}
70+
71+
/**
72+
* Returns child resources that matches directory filters.
73+
*
74+
* @return array
75+
*/
76+
public function getFilteredResources()
77+
{
78+
if (!$this->exists()) {
79+
return array();
80+
}
81+
82+
$iterator = new \DirectoryIterator($this->resource);
83+
84+
$resources = array();
85+
foreach ($iterator as $file) {
86+
// if regex filtering is enabled only return matching files
87+
if ($file->isFile() && !$this->hasFile($file)) {
88+
continue;
89+
}
90+
91+
// always monitor directories for changes, except the .. entries
92+
// (otherwise deleted files wouldn't get detected)
93+
if ($file->isDir() && '/..' === substr($file, -3)) {
94+
continue;
95+
}
96+
97+
// if file is dot - continue
98+
if ($file->isDot()) {
99+
continue;
100+
}
101+
102+
if ($file->isFile()) {
103+
$resources[] = new FileResource($file->getRealPath());
104+
} elseif ($file->isDir()) {
105+
$resources[] = new DirectoryResource($file->getRealPath());
106+
}
107+
}
108+
109+
return $resources;
110+
}
111+
36112
/**
37113
* Returns a string representation of the Resource.
38114
*
@@ -53,11 +129,62 @@ public function getResource()
53129
return $this->resource;
54130
}
55131

132+
/**
133+
* Returns check pattern.
134+
*
135+
* @return mixed
136+
*/
56137
public function getPattern()
57138
{
58139
return $this->pattern;
59140
}
60141

142+
/**
143+
* Checks that passed file exists in resource and matches resource filters.
144+
*
145+
* @param SplFileInfo|string $file
146+
*
147+
* @return Boolean
148+
*/
149+
public function hasFile($file)
150+
{
151+
if (!$file instanceof \SplFileInfo) {
152+
$file = new \SplFileInfo($file);
153+
}
154+
155+
if (0 !== strpos($file->getRealPath(), realpath($this->resource))) {
156+
return false;
157+
}
158+
159+
if ($this->pattern) {
160+
return (bool) preg_match($this->pattern, $file->getBasename());
161+
}
162+
163+
return true;
164+
}
165+
166+
/**
167+
* Returns resource mtime.
168+
*
169+
* @return integer
170+
*/
171+
public function getModificationTime()
172+
{
173+
if (!$this->exists()) {
174+
return -1;
175+
}
176+
177+
clearstatcache(true, $this->resource);
178+
$newestMTime = filemtime($this->resource);
179+
180+
foreach ($this->getFilteredChilds() as $file) {
181+
clearstatcache(true, (string) $file);
182+
$newestMTime = max($file->getMTime(), $newestMTime);
183+
}
184+
185+
return $newestMTime;
186+
}
187+
61188
/**
62189
* Returns true if the resource has not been updated since the given timestamp.
63190
*
@@ -67,27 +194,31 @@ public function getPattern()
67194
*/
68195
public function isFresh($timestamp)
69196
{
70-
if (!is_dir($this->resource)) {
197+
if (!$this->exists()) {
71198
return false;
72199
}
73200

74-
$newestMTime = filemtime($this->resource);
75-
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
76-
// if regex filtering is enabled only check matching files
77-
if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
78-
continue;
79-
}
80-
81-
// always monitor directories for changes, except the .. entries
82-
// (otherwise deleted files wouldn't get detected)
83-
if ($file->isDir() && '/..' === substr($file, -3)) {
84-
continue;
85-
}
201+
return $this->getModificationTime() < $timestamp;
202+
}
86203

87-
$newestMTime = max($file->getMTime(), $newestMTime);
88-
}
204+
/**
205+
* Returns true if the resource exists in the filesystem.
206+
*
207+
* @return Boolean
208+
*/
209+
public function exists()
210+
{
211+
return is_dir($this->resource);
212+
}
89213

90-
return $newestMTime < $timestamp;
214+
/**
215+
* Returns unique resource ID.
216+
*
217+
* @return string
218+
*/
219+
public function getId()
220+
{
221+
return md5('d'.$this->resource.$this->pattern);
91222
}
92223

93224
public function serialize()

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
2020
*/
21-
class FileResource implements ResourceInterface, \Serializable
21+
class FileResource implements ResourceInterface
2222
{
2323
private $resource;
2424

@@ -29,7 +29,7 @@ class FileResource implements ResourceInterface, \Serializable
2929
*/
3030
public function __construct($resource)
3131
{
32-
$this->resource = realpath($resource);
32+
$this->resource = file_exists($resource) ? realpath($resource) : $resource;
3333
}
3434

3535
/**
@@ -52,6 +52,22 @@ public function getResource()
5252
return $this->resource;
5353
}
5454

55+
/**
56+
* Returns resource mtime.
57+
*
58+
* @return integer
59+
*/
60+
public function getModificationTime()
61+
{
62+
if (!$this->exists()) {
63+
return -1;
64+
}
65+
66+
clearstatcache(true, $this->resource);
67+
68+
return filemtime($this->resource);
69+
}
70+
5571
/**
5672
* Returns true if the resource has not been updated since the given timestamp.
5773
*
@@ -61,11 +77,31 @@ public function getResource()
6177
*/
6278
public function isFresh($timestamp)
6379
{
64-
if (!file_exists($this->resource)) {
80+
if (!$this->exists()) {
6581
return false;
6682
}
6783

68-
return filemtime($this->resource) < $timestamp;
84+
return $this->getModificationTime() <= $timestamp;
85+
}
86+
87+
/**
88+
* Returns true if the resource exists in the filesystem.
89+
*
90+
* @return Boolean
91+
*/
92+
public function exists()
93+
{
94+
return is_file($this->resource);
95+
}
96+
97+
/**
98+
* Returns unique resource ID.
99+
*
100+
* @return string
101+
*/
102+
public function getId()
103+
{
104+
return md5('f'.$this->resource);
69105
}
70106

71107
public function serialize()

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
*/
19-
interface ResourceInterface
19+
interface ResourceInterface extends \Serializable
2020
{
2121
/**
2222
* Returns a string representation of the Resource.
@@ -34,10 +34,31 @@ function __toString();
3434
*/
3535
function isFresh($timestamp);
3636

37+
/**
38+
* Returns resource mtime.
39+
*
40+
* @return integer
41+
*/
42+
function getModificationTime();
43+
44+
/**
45+
* Returns true if the resource exists in the filesystem.
46+
*
47+
* @return Boolean
48+
*/
49+
function exists();
50+
3751
/**
3852
* Returns the resource tied to this Resource.
3953
*
4054
* @return mixed The resource
4155
*/
4256
function getResource();
57+
58+
/**
59+
* Returns unique resource ID.
60+
*
61+
* @return string
62+
*/
63+
function getId();
4364
}

0 commit comments

Comments
 (0)
0