Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Prevent circular dependency lookup in RpcControllerFactory #19

Conversation

weierophinney
Copy link
Member

As reported in #18, a circular dependency lookup can occur when specifying a static method of a class as a callable for an RPC service that matches the class name for the RPC service. As an example:

return [
    'zf-rpc' => [
        SomeController::class => [
            'callable' => SomeController::class . '::bar',
        ],
    ],
];

In the above case, the RpcControllerFactory would be invoked for SomeController::class, find the callable entry of {NS}\SomeController::bar, split this into the class and method, and then attempt to fetch SomeController::class again, ad infinitum.

This patch adds a private property, $lastRequestedControllerService, which is set to the service being pulled whenever we attempt to marshal a service from the controller manager. If a subsequent invocation finds that the service requested matches that property value, then we do not attempt to pull from the controller manager again, but move on to the parent container (if we have one), or directly instantiating the class. (The $lastRequestedControllerService is reset otherwise.)

Fixes #18

As reported in zfcampus#18, a circular dependency lookup can occur when
specifying a static method of a class as a `callable` for an RPC service
that matches the class name for the RPC service. As an example:

```php
return [
    'zf-rpc' => [
        SomeController::class => [
            'callable' => SomeController::class . '::bar',
        ],
    ],
];
```

In the above case, the `RpcControllerFactory` would be invoked for
`SomeController::class`, find the `callable` entry of
`{NS}\SomeController::bar`, split this into the class and method, and
then attempt to fetch `SomeController::class` again, ad infinitum.

This patch adds a private property, `$lastRequestedControllerService`,
which is set to the service being pulled whenever we attempt to marshal
a service from the controller manager. If a subsequent invocation finds
that the service requested matches that property value, then we do not
attempt to pull from the controller manager again, but move on to the
parent container (if we have one), or directly instantiating the class.
(The `$lastRequestedControllerService` is reset otherwise.)

Fixes zfcampus#18
@weierophinney weierophinney force-pushed the hotfix/18-controller-classes-as-rpc branch from 474fcb4 to 6143763 Compare January 7, 2019 21:07
@weierophinney weierophinney merged commit 6143763 into zfcampus:master Jan 7, 2019
weierophinney added a commit that referenced this pull request Jan 7, 2019
weierophinney added a commit that referenced this pull request Jan 7, 2019
weierophinney added a commit that referenced this pull request Jan 7, 2019
@weierophinney weierophinney deleted the hotfix/18-controller-classes-as-rpc branch January 7, 2019 21:26
@Wilt
Copy link

Wilt commented Jan 8, 2019

Great, thanks for providing a solution to this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants