Skip to content

Commit d0f53e3

Browse files
committed
Don't stop PSR-4 service discovery if a parent class is missing.
1 parent 332ad0a commit d0f53e3

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
6060
$interfaces = array();
6161
$singlyImplemented = array();
6262

63-
foreach ($classes as $class) {
63+
foreach ($classes as $class => $errorMessage) {
64+
if (null !== $errorMessage) {
65+
$definition = new Definition($class);
66+
$definition->addError($errorMessage);
67+
68+
$this->setDefinition($class, $definition);
69+
70+
continue;
71+
}
72+
6473
if (interface_exists($class, false)) {
6574
$interfaces[] = $class;
6675
} else {
@@ -96,7 +105,7 @@ protected function setDefinition($id, Definition $definition)
96105
}
97106
}
98107

99-
private function findClasses($namespace, $pattern, $excludePattern)
108+
private function findClasses($namespace, $pattern, $excludePattern): array
100109
{
101110
$parameterBag = $this->container->getParameterBag();
102111

@@ -139,13 +148,20 @@ private function findClasses($namespace, $pattern, $excludePattern)
139148
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
140149
continue;
141150
}
151+
152+
try {
153+
$r = $this->container->getReflectionClass($class);
154+
} catch (\ReflectionException $e) {
155+
$classes[$class] = $e->getMessage();
156+
continue;
157+
}
142158
// check to make sure the expected class exists
143-
if (!$r = $this->container->getReflectionClass($class)) {
159+
if (!$r) {
144160
throw new InvalidArgumentException(sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern));
145161
}
146162

147163
if ($r->isInstantiable() || $r->isInterface()) {
148-
$classes[] = $class;
164+
$classes[$class] = null;
149165
}
150166
}
151167

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses;
4+
5+
class MissingParent extends MissingClass
6+
{
7+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
33
<services>
4-
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{OtherDir}" />
4+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{OtherDir,BadClasses}" />
55
</services>
66
</container>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
services:
22
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
33
resource: ../Prototype
4-
exclude: '../Prototype/{OtherDir}'
4+
exclude: '../Prototype/{OtherDir,BadClasses}'

src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2626
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2727
use Symfony\Component\DependencyInjection\Reference;
28+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
2829
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\AnotherSub\DeeperBaz;
2930
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Baz;
3031
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
@@ -163,6 +164,26 @@ public function testNestedRegisterClasses()
163164
$this->assertFalse($alias->isPrivate());
164165
}
165166

167+
public function testMissingParentClass()
168+
{
169+
$container = new ContainerBuilder();
170+
$container->setParameter('bad_classes_dir', 'BadClasses');
171+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
172+
173+
$loader->registerClasses(
174+
(new Definition())->setPublic(false),
175+
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\',
176+
'Prototype/%bad_classes_dir%/*'
177+
);
178+
179+
$this->assertTrue($container->has(MissingParent::class));
180+
181+
$this->assertSame(
182+
array('Class Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingClass not found'),
183+
$container->getDefinition(MissingParent::class)->getErrors()
184+
);
185+
}
186+
166187
/**
167188
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
168189
* @expectedExceptionMessageRegExp /Expected to find class "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Prototype\\Bar" in file ".+" while importing services from resource "Prototype\/Sub\/\*", but it was not found\! Check the namespace prefix used with the resource/

0 commit comments

Comments
 (0)