Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 12d93fc

Browse filesBrowse files
committed
Don't stop PSR-4 service discovery if a parent class is missing.
1 parent 05e9682 commit 12d93fc
Copy full SHA for 12d93fc

File tree

6 files changed

+48
-8
lines changed
Filter options

6 files changed

+48
-8
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
+17-5Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +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) {
6464
if (interface_exists($class, false)) {
6565
$interfaces[] = $class;
6666
} else {
67-
$this->setDefinition($class, unserialize($serializedPrototype));
67+
$this->setDefinition($class, $definition = unserialize($serializedPrototype));
68+
if (null !== $errorMessage) {
69+
$definition->addError($errorMessage);
70+
71+
continue;
72+
}
6873
foreach (class_implements($class, false) as $interface) {
6974
$singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
7075
}
@@ -96,7 +101,7 @@ protected function setDefinition($id, Definition $definition)
96101
}
97102
}
98103

99-
private function findClasses($namespace, $pattern, $excludePattern)
104+
private function findClasses($namespace, $pattern, $excludePattern): array
100105
{
101106
$parameterBag = $this->container->getParameterBag();
102107

@@ -139,13 +144,20 @@ private function findClasses($namespace, $pattern, $excludePattern)
139144
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)) {
140145
continue;
141146
}
147+
148+
try {
149+
$r = $this->container->getReflectionClass($class);
150+
} catch (\ReflectionException $e) {
151+
$classes[$class] = $e->getMessage();
152+
continue;
153+
}
142154
// check to make sure the expected class exists
143-
if (!$r = $this->container->getReflectionClass($class)) {
155+
if (!$r) {
144156
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));
145157
}
146158

147159
if ($r->isInstantiable() || $r->isInterface()) {
148-
$classes[] = $class;
160+
$classes[$class] = null;
149161
}
150162
}
151163

+7Lines 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+
}

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/prototype.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/prototype.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
->tag('baz');
1010
$di->load(Prototype::class.'\\', '../Prototype')
1111
->autoconfigure()
12-
->exclude('../Prototype/{OtherDir}')
12+
->exclude('../Prototype/{OtherDir,BadClasses}')
1313
->factory('f')
1414
->deprecate('%service_id%')
1515
->args(array(0))
+1-1Lines 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>
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php
+21Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.