8000 Throwing an exception if the class is not found · symfony/symfony@e85bcc9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e85bcc9

Browse files
weaverryannicolas-grekas
authored andcommitted
Throwing an exception if the class is not found
1 parent b118226 commit e85bcc9

File tree

3 files changed

+16
-9
lines changed
  • Loader
  • 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)
    0