Skip to content

Instantly share code, notes, and snippets.

@asgrim

asgrim/foo.php Secret

Last active February 28, 2018 10:48
Show Gist options
  • Save asgrim/72f3ee4c25b901ffed58501657ea98da to your computer and use it in GitHub Desktop.
Save asgrim/72f3ee4c25b901ffed58501657ea98da to your computer and use it in GitHub Desktop.
<?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