nette / di

💎 Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 8 features.
https://doc.nette.org/dependency-injection
Other
869 stars 70 forks source link

Bug with class_alias #156

Closed enumag closed 7 years ago

enumag commented 7 years ago

Description

Nette/DI seems to be incompatible with class aliases. My tests are failing with this error:

[Nette\DI\ServiceCreationException] Service 'arachne.forms.twig.extension.translation': Case mismatch on class name 'Twig\NodeVisitor\NodeVisitorInterface', correct name is 'Twig_NodeVisitorInterface'.

Obviously there is no case mismatch. It's just that Twig\NodeVisitor\NodeVisitorInterface is an alias of Twig_NodeVisitorInterface which confuses ContainerBuilder::checkCase().

Steps To Reproduce

Create a class and an alias to that class. Add it to DIC as service. Try to autowire it into another service using the alias instead of the base class. EDIT: See the test case in my PR.

enumag commented 7 years ago

Not sure if it's possible to fix it otherwise than ignoring it with

    private function checkCase($class)
    {
        if (class_exists($class) || interface_exists($class)) {
            $name = (new ReflectionClass($class))->getName();
            if ($class !== $name && strtolower($class) === strtolower($name)) {
                throw new ServiceCreationException("Case mismatch on class name '$class', correct name is '$name'.");
            }
        }
    }

EDIT: It seems there is no way to get the defined class alias so I guess there is no better fix.

enumag commented 7 years ago

@dg Any estimate 2.4.9 with this fix could be available? This bug is blocking some of my open-source projects.

dg commented 7 years ago

@enumag done

enumag commented 7 years ago

Thank you!