Skip to content

Commit d9696fc

Browse files
committed
Merge branch '3.3' into 3.4
* 3.3: [Config] Always protect ClassExistenceResource against bad parents
2 parents e08d7f2 + 35716d4 commit d9696fc

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

Resource/ClassExistenceResource.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,21 @@
2121
*/
2222
class ClassExistenceResource implements SelfCheckingResourceInterface, \Serializable
2323
{
24-
const EXISTS_OK = 1;
25-
const EXISTS_KO = 0;
26-
const EXISTS_KO_WITH_THROWING_AUTOLOADER = -1;
27-
2824
private $resource;
29-
private $existsStatus;
25+
private $exists;
3026

3127
private static $autoloadLevel = 0;
3228
private static $existsCache = array();
3329

3430
/**
35-
* @param string $resource The fully-qualified class name
36-
* @param int|null $existsStatus One of the self::EXISTS_* const if the existency check has already been done
31+
* @param string $resource The fully-qualified class name
32+
* @param bool|null $exists Boolean when the existency check has already been done
3733
*/
38-
public function __construct($resource, $existsStatus = null)
34+
public function __construct($resource, $exists = null)
3935
{
4036
$this->resource = $resource;
41-
if (null !== $existsStatus) {
42-
$this->existsStatus = (int) $existsStatus;
37+
if (null !== $exists) {
38+
$this->exists = (bool) $exists;
4339
}
4440
}
4541

@@ -64,11 +60,13 @@ public function getResource()
6460
*/
6561
public function isFresh($timestamp)
6662
{
63+
$loaded = class_exists($this->resource, false) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
64+
6765
if (null !== $exists = &self::$existsCache[$this->resource]) {
68-
$exists = $exists || class_exists($this->resource, false) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
69-
} elseif (self::EXISTS_KO_WITH_THROWING_AUTOLOADER === $this->existsStatus) {
66+
$exists = $exists || $loaded;
67+
} elseif (!$exists = $loaded) {
7068
if (!self::$autoloadLevel++) {
71-
spl_autoload_register('Symfony\Component\Config\Resource\ClassExistenceResource::throwOnRequiredClass');
69+
spl_autoload_register(__CLASS__.'::throwOnRequiredClass');
7270
}
7371

7472
try {
@@ -77,38 +75,36 @@ public function isFresh($timestamp)
7775
$exists = false;
7876
} finally {
7977
if (!--self::$autoloadLevel) {
80-
spl_autoload_unregister('Symfony\Component\Config\Resource\ClassExistenceResource::throwOnRequiredClass');
78+
spl_autoload_unregister(__CLASS__.'::throwOnRequiredClass');
8179
}
8280
}
83-
} else {
84-
$exists = class_exists($this->resource) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
8581
}
8682

87-
if (null === $this->existsStatus) {
88-
$this->existsStatus = $exists ? self::EXISTS_OK : self::EXISTS_KO;
83+
if (null === $this->exists) {
84+
$this->exists = $exists;
8985
}
9086

91-
return self::EXISTS_OK === $this->existsStatus xor !$exists;
87+
return $this->exists xor !$exists;
9288
}
9389

9490
/**
9591
* {@inheritdoc}
9692
*/
9793
public function serialize()
9894
{
99-
if (null === $this->existsStatus) {
95+
if (null === $this->exists) {
10096
$this->isFresh(0);
10197
}
10298

103-
return serialize(array($this->resource, $this->existsStatus));
99+
return serialize(array($this->resource, $this->exists));
104100
}
105101

106102
/**
107103
* {@inheritdoc}
108104
*/
109105
public function unserialize($serialized)
110106
{
111-
list($this->resource, $this->existsStatus) = unserialize($serialized);
107+
list($this->resource, $this->exists) = unserialize($serialized);
112108
}
113109

114110
/**

Tests/Resource/ClassExistenceResourceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testExistsKo()
6666

6767
$loadedClass = 123;
6868

69-
$res = new ClassExistenceResource('MissingFooClass', ClassExistenceResource::EXISTS_KO);
69+
$res = new ClassExistenceResource('MissingFooClass', false);
7070

7171
$this->assertSame(123, $loadedClass);
7272
} finally {
@@ -76,7 +76,7 @@ public function testExistsKo()
7676

7777
public function testConditionalClass()
7878
{
79-
$res = new ClassExistenceResource(ConditionalClass::class, ClassExistenceResource::EXISTS_KO_WITH_THROWING_AUTOLOADER);
79+
$res = new ClassExistenceResource(ConditionalClass::class, false);
8080

8181
$this->assertFalse($res->isFresh(0));
8282
}

0 commit comments

Comments
 (0)