-
-
Save asgrim/72f3ee4c25b901ffed58501657ea98da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface AnInterface {} | |
final class AnImplementation implements AnInterface {} | |
final class ADecorator implements AnInterface | |
{ | |
private $real; | |
public function __construct(AnInterface $realImplementation) { // Note, depends on AnInterface here - but if we look this up in container, we get ADecorator again - cyclic dependency) | |
$this->real = $realImplementation; | |
} | |
} | |
// Container set up as such - note it's not going to work! | |
$container = new ServiceManager([ | |
'abstract_factories' => [ | |
ReflectionBasedAbstractFactory::class, | |
], | |
'aliases' => [ | |
AnInterface::class => ADecorator::class, // Define the concrete implementation to use for AnInterface::class | |
], | |
'factories' => [ | |
ADecorator::class => ReflectionBasedAbstractFactory::class, // Concrete implementation set up to use RBAF | |
], | |
]); | |
// This is how it SHOULD be done - because it's a decorator, we can only define with a factory (or maybe ConfigBasedAbstractFactory) | |
final class AnInterfaceFactory | |
{ | |
public function __invoke(ContainerInterface $container) { | |
return new ADecorator(new AnImplementation()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment