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

3.next: add DIC support to OrmResolver #275

Merged
merged 1 commit into from
Jan 20, 2024
Merged

Conversation

LordSimal
Copy link
Contributor

This is a proposal initiated by @jamisonbryant to make at least the OrmResolver more DI aware.

Instead of having to pass on services to ->can() etc. one can easily inject the needed services into the policies which actually need them to do whatever logic is required to authorize the current user on the given action.

To get this working from a user perspective, all they would have to adjust in their Application::getAuthorizationService method is

$ormResolver = new OrmResolver();

to

$ormResolver = new OrmResolver('App', [], $this->getContainer());

After that all policies retrieved by the OrmResolver can leverage constructor injection.

If we want to go this way then we should also make the MapResolver DI aware as well.

@@ -146,7 +159,13 @@ protected function findPolicy(string $class, string $name, string $namespace): m
throw new MissingPolicyException([$class]);
}

return new $policyClass();
if ($this->container && $this->container->has($policyClass)) {
$policy = $this->container->get($policyClass);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the interface, get() can throw a variety of exceptions. Should this be handled by this method (i.e. fallback style) or leave it entirely to userland code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well get($policyClass) won't throw any more since we checked the container with has($policyClass).

This is the same procedure as we do when parsing controller action arguments
just with the difference that the container could not be set in our case to be as BC as possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, $this->container->get() can still throw ContainerExceptionInterface however you're correct in that the has() check will prevent the NotFoundException from being thrown.

At least, that's my understanding from reading ContainerInterface. I checked out the ControllerFactory code and see what you mean...but I must ask: should we be catching/handling the container exception there as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the league/container as an implementation for the PSR-11 Container Interface. The only reason this exception could be thrown is here.

So technically, one could add a service provider which lies about providing a specific service.
But then I'd say its fine if we just let that exception bubble up to the top and not catch it because it's a configuration problem of the user (or a plugin), not something we should actively catch and handle for the user.

@markstory
Copy link
Member

Seems like a good change to me.

@LordSimal LordSimal marked this pull request as ready for review January 17, 2024 07:02
@LordSimal
Copy link
Contributor Author

@jamisonbryant anything to add from your side? If not I'd merge this and add support for DI to the MapResolver as well

@jamisonbryant
Copy link
Contributor

No, this looks good to me. Well done!

@LordSimal LordSimal merged commit d2302dd into 3.next Jan 20, 2024
7 checks passed
@LordSimal LordSimal deleted the 3.next-orm-resolver-dic branch January 20, 2024 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants