Skip to content

Commit 45df2e6

Browse files
committed
[Config] updated resources API to be more explicit
1 parent 092b5dd commit 45df2e6

File tree

5 files changed

+102
-8
lines changed

5 files changed

+102
-8
lines changed

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

+33-6
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public function getPattern()
5959
}
6060

6161
/**
62-
* Returns true if the resource has not been updated since the given timestamp.
63-
*
64-
* @param integer $timestamp The last time the resource was loaded
62+
* Returns resource mtime.
6563
*
66-
* @return Boolean true if the resource has not been updated, false otherwise
64+
* @return integer
6765
*/
68-
public function isFresh($timestamp)
66+
public function getModificationTime()
6967
{
68+
clearstatcache(true, $this->resource);
69+
7070
if (!is_dir($this->resource)) {
7171
return false;
7272
}
@@ -84,10 +84,37 @@ public function isFresh($timestamp)
8484
continue;
8585
}
8686

87+
clearstatcache(true, (string) $file);
8788
$newestMTime = max($file->getMTime(), $newestMTime);
8889
}
8990

90-
return $newestMTime < $timestamp;
91+
return $newestMTime;
92+
}
93+
94+
/**
95+
* Returns true if the resource has not been updated since the given timestamp.
96+
*
97+
* @param integer $timestamp The last time the resource was loaded
98+
*
99+
* @return Boolean true if the resource has not been updated, false otherwise
100+
*/
101+
public function isFresh($timestamp)
102+
{
103+
if (!$this->exists()) {
104+
return false;
105+
}
106+
107+
return $this->getModificationTime() <= $timestamp;
108+
}
109+
110+
/**
111+
* Returns true if the resource exists in the filesystem.
112+
*
113+
* @return Boolean
114+
*/
115+
public function exists()
116+
{
117+
return file_exists($this->resource);
91118
}
92119

93120
public function serialize()

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ 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+
clearstatcache(true, $this->resource);
63+
64+
return filemtime($this->resource);
65+
}
66+
5567
/**
5668
* Returns true if the resource has not been updated since the given timestamp.
5769
*
@@ -61,11 +73,21 @@ public function getResource()
6173
*/
6274
public function isFresh($timestamp)
6375
{
64-
if (!file_exists($this->resource)) {
76+
if (!$this->exists()) {
6577
return false;
6678
}
6779

68-
return filemtime($this->resource) < $timestamp;
80+
return $this->getModificationTime() <= $timestamp;
81+
}
82+
83+
/**
84+
* Returns true if the resource exists in the filesystem.
85+
*
86+
* @return Boolean
87+
*/
88+
public function exists()
89+
{
90+
return file_exists($this->resource);
6991
}
7092

7193
public function serialize()

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

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ 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
*

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

+12
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,20 @@ public function testIsFreshDeleteDirectory()
118118
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
119119
}
120120

121+
/**
122+
* @covers Symfony\Component\Config\Resource\DirectoryResource::exists
123+
*/
124+
public function testExists()
125+
{
126+
$this->assertTrue($this->resource->exists(), '->exists() returns true if the directory still exist');
127+
128+
$this->removeDirectory($this->directory);
129+
$this->assertFalse($this->resource->exists(), '->exists() returns false if the directory does not exist');
130+
}
131+
121132
/**
122133
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
134+
* @covers Symfony\Component\Config\Resource\DirectoryResource::getModificationTime
123135
*/
124136
public function testIsFreshCreateFileInSubdirectory()
125137
{

src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,23 @@ public function testIsFresh()
4949
$resource = new FileResource('/____foo/foobar'.rand(1, 999999));
5050
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
5151
}
52+
53+
/**
54+
* @covers Symfony\Component\Config\Resource\FileResource::exists
55+
*/
56+
public function testExists()
57+
{
58+
$this->assertTrue($this->resource->exists(), '->exists() returns true if the resource does exist');
59+
60+
$resource = new FileResource('/____foo/foobar'.rand(1, 999999));
61+
$this->assertFalse($resource->exists(), '->exists() returns false if the resource does not exist');
62+
}
63+
64+
/**
65+
* @covers Symfony\Component\Config\Resource\FileResource::getModificationTime
66+
*/
67+
public function testGetModificationTime()
68+
{
69+
$this->assertSame(filemtime($this->resource->getResource()), $this->resource->getModificationTime());
70+
}
5271
}

0 commit comments

Comments
 (0)