Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI\object()->constructorParameter() doesn't work with wildcards definitions #561

Open
jhuet opened this issue Dec 4, 2017 · 2 comments
Open
Labels

Comments

@jhuet
Copy link

jhuet commented Dec 4, 2017

I'm testing with 3 different definitions.

This first one works as expected :

<?php
    Rbac\ArticleRouteGuard::class => DI\object(Rbac\ArticleRouteGuard::class)
        ->constructorParameter('roles', DI\get('settings.rbac.runtime.roles'))
        ->constructorParameter('rules', DI\get('settings.rbac.runtime.rules'))
    ,

For convenience, i wanted to try the wildcard approach to not have to repeat the definition :

<?php
    'Rbac\*RouteGuard' => DI\object('Rbac\*RouteGuard')
        ->constructorParameter('roles', DI\get('settings.rbac.runtime.roles'))
        ->constructorParameter('rules', DI\get('settings.rbac.runtime.rules'))
    ,

Unfortunatly, i'm then getting this exception :

DI\Definition\Exception\DefinitionException: Parameter with name 'roles' could not be found. Class Rbac\*RouteGuard does not exist. Full definition: Object ( class = #UNKNOWN# Rbac\*RouteGuard scope = singleton lazy = false ) in /var/www/cms/vendor/php-di/php-di/src/DI/Definition/Exception/DefinitionException.php on line 16

Now, if i keep trying with a wildcard but using the constructor() method instead of constructorParameter() everything works again :

    'Rbac\*RouteGuard' => DI\object('Rbac\*RouteGuard')
        ->constructor(
            DI\get('Rbac\Rbac'),
            DI\get('settings.rbac.runtime.roles'),
            DI\get('settings.rbac.runtime.rules')
        )
    ,

For the record, here's the contructor definition of Rbac\ArticleRouteGuard :

<?php
    /**
     * @param Rbac                                      $rbac
     * @param RoleInterface|RoleInterface[]|Traversable $roles
     * @param array                                     $rules
     */
    public function __construct(Rbac $rbac, $roles, $rules)
    {
        // ....
        parent::__construct($rbac, $roles, $rules, $assertion);
    }

Is there something i'm missing here or is wildcard + constructorParameter() not supported ?

Thanks.

@mnapoli mnapoli added the bug label Dec 4, 2017
@mnapoli
Copy link
Member

mnapoli commented Dec 4, 2017

Oh right, I think this is a real bug caused in here: https://github.com/PHP-DI/PHP-DI/blob/master/src/Definition/Helper/CreateDefinitionHelper.php#L189

The parameter is resolved early, before we get the real class name. So it's trying to look for the parameter in the Rbac\*RouteGuard class (as it says in the error message), and the class doesn't exist so it fails.

🤔 I'm not sure how to fix that right now, I'll try to have a look for v6.

@jhuet
Copy link
Author

jhuet commented Dec 4, 2017

Alright, thanks for the quick answer, i'll try to work around that for now.

@jhuet jhuet changed the title DI\object()->constructorParameter() doesn't seem to work with wildcards definitions DI\object()->constructorParameter() doesn't work with wildcards definitions Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants