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

Feature/service locator #7

Merged
Merged
21 changes: 21 additions & 0 deletions src/CompiledContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,25 @@ protected function resolveFactory($callable, $entryName, array $extraParameters
throw new InvalidDefinition("Entry \"$entryName\" cannot be resolved: " . $e->getMessage());
}
}

/**
* Resolve ServiceLocator for given subscriber class (based on \DI\Definition\ServiceLocatorDefinition::resolve).
*
* @param string $requestingName class name of a subscriber, implementing ServiceSubscriberInterface
* @param string $repositoryClass ServiceLocatorRepository
* @return ServiceLocator
* @throws ServiceSubscriberException
*/
protected function resolveServiceLocator($requestingName, $repositoryClass)
{
if (!method_exists($requestingName, 'getSubscribedServices')) {
throw new ServiceSubscriberException(sprintf('The class %s does not implement ServiceSubscriberInterface.', $requestingName));
}

/** @var ServiceLocatorRepository $repository */
$repository = $this->delegateContainer->get($repositoryClass);
$services = $requestingName::getSubscribedServices();

return $repository->create($requestingName, $services);
}
}
7 changes: 7 additions & 0 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ private function compileDefinition(string $entryName, Definition $definition) :
$code = 'return ' . $this->compileValue($value) . ';';
break;
case $definition instanceof Reference:
if ($definition->isServiceLocatorEntry()) {
$requestingEntry = $definition->getRequestingName();
$serviceLocatorDefinition = $definition->getServiceLocatorDefinition();
$code = 'return $this->resolveServiceLocator(' . $this->compileValue($requestingEntry) . ', ' . $this->compileValue($serviceLocatorDefinition::$serviceLocatorRepositoryClass) . ');';
break;
}

$targetEntryName = $definition->getTargetEntryName();
$code = 'return $this->delegateContainer->get(' . $this->compileValue($targetEntryName) . ');';
// If this method is not yet compiled we store it for compilation
Expand Down
62 changes: 61 additions & 1 deletion src/Definition/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DI\Definition;

use DI\Definition\Exception\InvalidDefinition;
use DI\ServiceLocator;
use Psr\Container\ContainerInterface;

/**
Expand All @@ -13,6 +15,8 @@
*/
class Reference implements Definition, SelfResolvingDefinition
{
public static $serviceLocatorClass = ServiceLocator::class;

/**
* Entry name.
* @var string
Expand All @@ -25,12 +29,30 @@ class Reference implements Definition, SelfResolvingDefinition
*/
private $targetEntryName;

/**
* @var string
*/
private $requestingName;

/**
* @var bool
*/
private $isServiceLocatorEntry;

/**
* @var ServiceLocatorDefinition
*/
private $serviceLocatorDefinition;

/**
* @param string $targetEntryName Name of the target entry
* @param string $requestingName name of an entry - holder of a definition requesting this entry
*/
public function __construct(string $targetEntryName)
public function __construct(string $targetEntryName, $requestingName = null)
{
$this->targetEntryName = $targetEntryName;
$this->requestingName = $requestingName;
$this->isServiceLocatorEntry = $targetEntryName === self::$serviceLocatorClass;
}

public function getName() : string
Expand All @@ -48,13 +70,51 @@ public function getTargetEntryName() : string
return $this->targetEntryName;
}

/**
* Returns the name of the entity requesting this entry.
* @return string
*/
public function getRequestingName() : string
{
return $this->requestingName;
}

public function isServiceLocatorEntry() : bool
{
return $this->isServiceLocatorEntry;
}

public function getServiceLocatorDefinition() : ServiceLocatorDefinition
{
if (!$this->isServiceLocatorEntry || $this->requestingName === null) {
throw new InvalidDefinition(sprintf(
"Invalid service locator definition ('%s' for '%s')",
$this->targetEntryName,
$this->requestingName
));
}
if (!$this->serviceLocatorDefinition) {
$this->serviceLocatorDefinition = new ServiceLocatorDefinition($this->getTargetEntryName(), $this->requestingName);
}

return $this->serviceLocatorDefinition;
}

public function resolve(ContainerInterface $container)
{
if ($this->isServiceLocatorEntry) {
return $this->getServiceLocatorDefinition()->resolve($container);
}

return $container->get($this->getTargetEntryName());
}

public function isResolvable(ContainerInterface $container) : bool
{
if ($this->isServiceLocatorEntry) {
return $this->getServiceLocatorDefinition()->isResolvable($container);
}

return $container->has($this->getTargetEntryName());
}

Expand Down
104 changes: 104 additions & 0 deletions src/Definition/ServiceLocatorDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace DI\Definition;

use DI\ServiceLocator;
use DI\ServiceLocatorRepository;
use DI\ServiceSubscriberException;
use Psr\Container\ContainerInterface;

class ServiceLocatorDefinition implements Definition, SelfResolvingDefinition
{
public static $serviceLocatorRepositoryClass = ServiceLocatorRepository::class;

/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $requestingName;

/**
* @param string $name Entry name
* @param string $requestingName name of an entry - holder of a definition requesting service locator
*/
public function __construct($name, $requestingName)
{
$this->name = $name;
$this->requestingName = $requestingName;
}

/**
* Returns the name of the entry in the container.
*/
public function getName() : string
{
return $this->name;
}

public function setName(string $name)
{
$this->name = $name;
}

/**
* Returns the name of the holder of the definition requesting service locator.
* @return string
*/
public function getRequestingName() : string
{
return $this->requestingName;
}

/**
* Resolve the definition and return the resulting value.
*
* @param ContainerInterface $container
* @return ServiceLocator
* @throws ServiceSubscriberException
*/
public function resolve(ContainerInterface $container)
{
if (!method_exists($this->requestingName, 'getSubscribedServices')) {
throw new ServiceSubscriberException(sprintf('The class %s does not implement ServiceSubscriberInterface.', $this->requestingName));
}

/** @var ServiceLocatorRepository $repository */
$repository = $container->get(self::$serviceLocatorRepositoryClass);
$services = $this->requestingName::getSubscribedServices();

return $repository->create($this->requestingName, $services);
}

/**
* Check if a definition can be resolved.
* @param ContainerInterface $container
* @return bool
*/
public function isResolvable(ContainerInterface $container) : bool
{
return method_exists($this->requestingName, 'getSubscribedServices');
}

public function replaceNestedDefinitions(callable $replacer)
{
// no nested definitions
}

/**
* Definitions can be cast to string for debugging information.
*/
public function __toString()
{
return sprintf(
'get(%s) for \'%s\'',
$this->name,
$this->requestingName
);
}
}
4 changes: 2 additions & 2 deletions src/Definition/Source/AnnotationBasedAutowiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function readProperty(ReflectionProperty $property, ObjectDefinition $de
}

$definition->addPropertyInjection(
new PropertyInjection($property->getName(), new Reference($entryName), $classname)
new PropertyInjection($property->getName(), new Reference($entryName, $classname), $classname)
);
}

Expand Down Expand Up @@ -220,7 +220,7 @@ private function readConstructor(ReflectionClass $class, ObjectDefinition $defin
$entryName = $this->getMethodParameter($index, $parameter, []);

if ($entryName !== null) {
$parameters[$index] = new Reference($entryName);
$parameters[$index] = new Reference($entryName, $class->getName());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Definition/Source/ReflectionBasedAutowiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function autowire(string $name, ObjectDefinition $definition = null)
$class = new \ReflectionClass($className);
$constructor = $class->getConstructor();
if ($constructor && $constructor->isPublic()) {
$constructorInjection = MethodInjection::constructor($this->getParametersDefinition($constructor));
$constructorInjection = MethodInjection::constructor($this->getParametersDefinition($constructor, $class->getName()));
$definition->completeConstructorInjection($constructorInjection);
}

Expand All @@ -52,7 +52,7 @@ public function getDefinitions() : array
/**
* Read the type-hinting from the parameters of the function.
*/
private function getParametersDefinition(\ReflectionFunctionAbstract $constructor) : array
private function getParametersDefinition(\ReflectionFunctionAbstract $constructor, string $className) : array
{
$parameters = [];

Expand All @@ -65,7 +65,7 @@ private function getParametersDefinition(\ReflectionFunctionAbstract $constructo
$parameterClass = $parameter->getClass();

if ($parameterClass) {
$parameters[$index] = new Reference($parameterClass->getName());
$parameters[$index] = new Reference($parameterClass->getName(), $className);
}
}

Expand Down
Loading