Support policy factories
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.
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.
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.
Ok. Then a factory makes sense.
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?
Doing the change for 2.x and backporting is probably easier as we won't miss typehints.
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