Skip to content

Commit 9d9f628

Browse files
minor symfony#22531 Throwing an exception if the class is not found (weaverryan)
This PR was squashed before being merged into the 3.3-dev branch (closes symfony#22531). Discussion ---------- Throwing an exception if the class is not found | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a We now throw an exception if the user makes a mistake with their PSR-4 prefix and namespace. For example: ```yml AppBundle\Controller\: resource: '../../src/AppBundle/{Controller}' public: true ``` I should not have the `\Controller` at the end of the key. Previously, it would silently not import any services from the directory. Now it throws: > Expected to find class "AppBundle\Controller\Controller\Admin\BlogController" in file "/path/to/project/src/AppBundle/Controller/Admin/BlogController.php" while importing services from resource "../../src/AppBundle/{Controller}", but it was not found! Check the namespace prefix used with the resource. The only "downside" is that this prevents someone from importing files from a resource that has a file with no class in it (`functions.php`). @nicolas-grekas and I decided today that we can throw an exception now to be safe, and see if anyone has that valid use-case. Cheers! Commits ------- e85bcc9 Throwing an exception if the class is not found
2 parents 8974b52 + e85bcc9 commit 9d9f628

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ private function findClasses($namespace, $resource)
103103
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)) {
104104
continue;
105105
}
106-
if (!$r = $this->container->getReflectionClass($class, true)) {
107-
continue;
106+
// check to make sure the expected class exists
107+
if (!$r = $this->container->getReflectionClass($class)) {
108+
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, $resource));
108109
}
109110
if (!$r->isInterface() && !$r->isTrait()) {
110111
$classes[] = $class;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/MissingParent.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ public function testRegisterClasses()
8686

8787
$this->assertTrue($container->has(Bar::class));
8888
}
89+
90+
/**
91+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
92+
* @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/
93+
*/
94+
public function testRegisterClassesWithBadPrefix()
95+
{
96+
$container = new ContainerBuilder();
97+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
98+
99+
// the Sub is missing from namespace prefix
100+
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/Sub/*');
101+
}
89102
}
90103

91104
class TestFileLoader extends FileLoader

0 commit comments

Comments
 (0)