Skip to content

Commit 2b36ac5

Browse files
mpdudefabpot
authored andcommitted
Fix that two DirectoryResources with different patterns would be deduplicated
ResourceInterface::__toString is mainly important because in various places, array_uniqe() is called to perform a de-duplication of resources and will use the string representation for objects. Thus, we need to take care that if DirectoryResources apply different patterns they must be kept after array_unique calls.
1 parent cfd8cc2 commit 2b36ac5

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($resource, $pattern = null)
3838
*/
3939
public function __toString()
4040
{
41-
return (string) $this->resource;
41+
return md5(serialize(array($this->resource, $this->pattern)));
4242
}
4343

4444
/**

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public function testGetResource()
5454
{
5555
$resource = new DirectoryResource($this->directory);
5656
$this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
57-
$this->assertSame($this->directory, (string) $resource, '->__toString() returns the path to the resource');
5857
}
5958

6059
public function testGetPattern()
@@ -87,6 +86,13 @@ public function testIsFreshNewFile()
8786
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
8887
}
8988

89+
public function testIsFreshNewFileWithDifferentPattern()
90+
{
91+
$resource = new DirectoryResource($this->directory, '/.xml$/');
92+
touch($this->directory.'/new.yaml', time() + 20);
93+
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
94+
}
95+
9096
public function testIsFreshDeleteFile()
9197
{
9298
$resource = new DirectoryResource($this->directory);
@@ -149,4 +155,12 @@ public function testSerializeUnserialize()
149155
$this->assertSame($this->directory, $resource->getResource());
150156
$this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
151157
}
158+
159+
public function testResourcesWithDifferentPatternsAreDifferent()
160+
{
161+
$resourceA = new DirectoryResource($this->directory, '/.xml$/');
162+
$resourceB = new DirectoryResource($this->directory, '/.yaml$/');
163+
164+
$this->assertEquals(2, count(array_unique(array($resourceA, $resourceB))));
165+
}
152166
}

0 commit comments

Comments
 (0)