okapi-web / php-aop

PHP-AOP is a library that adds aspect oriented programming capabilities to PHP.
https://github.com/okapi-web/php-aop
MIT License
30 stars 3 forks source link

DI\DependencyException for Proxied Class #71

Open djschilling opened 11 months ago

djschilling commented 11 months ago

I have a Class GroupMemberService which is Proxied by php-aop. In its constructor the class GroupPolicy is passed which is also proxied.

I use DI to inject the constructor params.

This error occurs when i try to get GroupMemberService from the DI Service:

DI\DependencyException : Error while injecting dependencies into Churchtools\Domain\Group\Member\GroupMemberService: No entry or class found for 'Churchtools\Domain\Group\GroupPolicy__AopProxied'
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ObjectCreator.php:133
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ObjectCreator.php:56
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php:60
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Container.php:353
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Container.php:136
 /Users/david/projects/churchtools/tests/php/integration/Domain/Group/Member/GroupMemberWaitingListTest.php:52

 Caused by
 DI\NotFoundException: No entry or class found for 'Churchtools\Domain\Group\GroupPolicy__AopProxied'

 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Container.php:133
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Reference.php:44
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php:55
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ParameterResolver.php:73
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ObjectCreator.php:124
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ObjectCreator.php:56
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php:60
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Container.php:353
 /Users/david/projects/churchtools/system/composer/php-di/php-di/src/Container.php:136
 /Users/david/projects/churchtools/tests/php/integration/Domain/Group/Member/GroupMemberWaitingListTest.php:52
djschilling commented 10 months ago

I had today the same issue with an other Class: GroupService

I think i now know the reason for this issue. Most of our dependencies can be resolved automatically by DI but not all. For some of them we define them in our code like this:

    GroupService::class => static function (ContainerInterface $container) {
        return new GroupService(
            $container->get(EntityManager::class),
            ...
        );
    },

The Problem gets resolved if i define both GroupService and GroupService__AopProxied like this:

    GroupService::class => static function (ContainerInterface $container) {
        return new GroupService(
            $container->get(EntityManager::class),
            ...
        );
    },
    GroupService::class . '__AopProxied' => static function (ContainerInterface $container) {
        return $container->get(GroupService::class);
    },

It seems to work but its not a great solution because now i have to do this each time i use an Attribute in one of the classes i defined the definitions for in code.

djschilling commented 9 months ago

I had again a similar case but a little different.

I have a class SignUpService with a function signUpMultipleForms. This function has a annotation which is handled by this framework and so a proxy class is created for it. This is the start of the function:

Bildschirmfoto 2024-01-21 um 20 04 37

When calling the constructor of SignUpConditions i get the error: Churchtools\\Domain\\GroupHomepage\\SignUpConditions::__construct(): Argument #3 ($groupPolicy) must be of type Churchtools\\Domain\\Group\\GroupPolicy, Churchtools\\Domain\\Group\\GroupPolicy__AopProxied given, called in /private/var/tmp/ct-aop-cache/32167/proxies/src/Domain/GroupHomepage/SignUpService.php on line 159

My guess is that it has something to do with the SignUpConditions not being Proxied, but i am not sure.

@WalterWoshid do you have an idea about these issues?

WalterWoshid commented 5 months ago

Hi there, it was difficult trying to reproduce it, but I found some other errors. After a lot of thought and stupid luck, I removed all the type replacement logic and what a wonder, every test still worked. I think that might have fixed all your DI problems. I added some tests that looked similar to your code, they also work.

Try version 1.2.11 and tell me if that worked for you :)