authorization icon indicating copy to clipboard operation
authorization copied to clipboard

Support policy factories

Open robertpustulka opened this issue 5 years ago • 5 comments

I have a case where my ORM policies need some dependencies. I'd like to pull those from a DIC. I was thinking that it may be a good idea to add support for policy factories. The feature might be useful with an upcoming core DIC support.

How this may work?

interface PolicyFactoryInterface
{
    public function create(string $className): object;
}

class DefaultPolicyFactory implements PolicyFactoryInterface
{
    public function create(string $className): object
    {
        return new $className;
    }
}

class OrmResolver implements ResolverInterface
{
    public function setFactory(PolicyFactoryInterface $factory): void
    {
        $this->policyFactory = $factory;
    }

    protected function getFactory(): PolicyFactoryInterface
    {
        if (!$this->policyFactory) {
            $this->policyFactory = new DefaultPolicyFactory;
        }
        return $this->policyFactory;
    }

    protected function findPolicy(string $class, string $name, string $namespace)
    {
        ...

        return new $this->getFactory()->create($policyClass);
    }
}

We would need to update all resolvers to use a factory internally, but with an optional setter injection this should be BC.

robertpustulka avatar Jul 09 '20 13:07 robertpustulka

Would getting the resolvers from the DIC allow you to inject the required dependencies/container into the resolver as needed? Or are you trying to solve for built-in resolvers needing to have additional dependencies when creating policies? I ask because resolvers are already a factory for policies in many ways.

markstory avatar Jul 11 '20 03:07 markstory

Resolvers now do two things: they find the policy class and then create the policy object. I'd like to seperate those responsibilities in a way that extending built in resolvers would be easier when DIC comes in.

In my case the policy class need dependiencies. I'd like to avoid making the resolver dependent on a DIC, making the instantiation process transparent to the resolver and reusable across resolvers.

robertpustulka avatar Jul 24 '20 06:07 robertpustulka

Ok. Then a factory makes sense.

markstory avatar Jul 29 '20 15:07 markstory

Should this be implemented for 2.x first and then backported to 1.x or implemented for 1.x and then merged into 2.x?

robertpustulka avatar Aug 31 '20 06:08 robertpustulka

Doing the change for 2.x and backporting is probably easier as we won't miss typehints.

markstory avatar Sep 03 '20 01:09 markstory

This issue is stale because it has been open for 120 days with no activity. Remove the stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Dec 24 '22 00:12 github-actions[bot]