8000 bug #23041 [Config] Always protected ClassExistenceResource against b… · symfony/symfony@c21e825 · GitHub
[go: up one dir, main page]

Skip to content

Commit c21e825

Browse files
committed
bug #23041 [Config] Always protected ClassExistenceResource against bad parents (nicolas-grekas)
This PR was merged into the 3.3 branch. Discussion ---------- [Config] Always protected ClassExistenceResource against bad parents | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | (new hidden feat) | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Let's always protected against bad or missing parents. The protection was conditional because the protection adds some perf overhead, but this was a micro optim really... BC break is for a new feat, better not maintain this. Commits ------- 86bf26c [Config] Always protect ClassExistenceResource against bad parents
2 parents 349e5fe + 86bf26c commit c21e825

File tree

5 files changed

+28
-36
lines changed

5 files changed

+28
-36
lines changed

src/Symfony/Component/Config/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
/**

src/Symfony/Component/Config/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
}

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private function populateAvailableType($id, Definition $definition)
388388
unset($this->ambiguousServiceTypes[$type]);
389389
}
390390

391-
if ($definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), true)) {
391+
if ($definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass())) {
392392
return;
393393
}
394394

@@ -444,7 +444,7 @@ private function set($type, $id)
444444
*/
445445
private function createAutowiredDefinition($type)
446446
{
447-
if (!($typeHint = $this->container->getReflectionClass($type, true)) || !$typeHint->isInstantiable()) {
447+
if (!($typeHint = $this->container->getReflectionClass($type)) || !$typeHint->isInstantiable()) {
448448
return;
449449
}
450450

@@ -478,7 +478,7 @@ private function createAutowiredDefinition($type)
478478

479479
private function createTypeNotFoundMessage(TypedReference $reference, $label)
480480
{
481-
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), true)) {
481+
if (!$r = $this->container->getReflectionClass($type = $reference->getType())) {
482482
$message = sprintf('has type "%s" but this class does not exist.', $type);
483483
} else {
484484
$message = $this->container->has($type) ? 'this service is abstract' : 'no such service exists';

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,12 @@ public function addClassResource(\ReflectionClass $class)
337337
* Retrieves the requested reflection class and registers it for resource tracking.
338338
*
339339
* @param string $class
340-
* @param bool $koWithThrowingAutoloader Whether autoload should be protected against bad parents or not
341340
*
342341
* @return \ReflectionClass|null
343342
*
344343
* @final
345344
*/
346-
public function getReflectionClass($class, $koWithThrowingAutoloader = false)
345+
public function getReflectionClass($class)
347346
{
348347
if (!$class = $this->getParameterBag()->resolveValue($class)) {
349348
return;
@@ -353,20 +352,17 @@ public function getReflectionClass($class, $koWithThrowingAutoloader = false)
353352
try {
354353
if (isset($this->classReflectors[$class])) {
355354
$classReflector = $this->classReflectors[$class];
356-
} elseif ($koWithThrowingAutoloader) {
357-
$resource = new ClassExistenceResource($class, ClassExistenceResource::EXISTS_KO_WITH_THROWING_AUTOLOADER);
358-
359-
$classReflector = $resource->isFresh(0) ? false : new \ReflectionClass($class);
360355
} else {
361-
$classReflector = new \ReflectionClass($class);
356+
$resource = new ClassExistenceResource($class, false);
357+
$classReflector = $resource->isFresh(0) ? false : new \ReflectionClass($class);
362358
}
363359
} catch (\ReflectionException $e) {
364360
$classReflector = false;
365361
}
366362

367363
if ($this->trackResources) {
368364
if (!$classReflector) {
369-
$this->addResource($resource ?: new ClassExistenceResource($class, ClassExistenceResource::EXISTS_KO));
365+
$this->addResource($resource ?: new ClassExistenceResource($class, false));
370366
} elseif (!$classReflector->isInternal()) {
371367
$path = $classReflector->getFileName();
372368

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3333
},
3434
"conflict": {
35-
"symfony/config": "<3.3",
35+
"symfony/config": "<3.3.1",
3636
"symfony/finder": "<3.3",
3737
"symfony/yaml": "<3.3"
3838
},

0 commit comments

Comments
 (0)
0