forked from PHP-DI/PHP-DI
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
975 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?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; | ||
|
||
/** | ||
* @param string $name Entry name | ||
* @param string $requestingName name of an entry - holder of a definition requesting service locator | ||
*/ | ||
public function __construct( | ||
private string $name, | ||
private string $requestingName | ||
) { | ||
} | ||
|
||
/** | ||
* Returns the name of the entry in the container. | ||
*/ | ||
public function getName() : string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name) : void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* Returns the name of the holder of the definition requesting service locator. | ||
*/ | ||
public function getRequestingName() : string | ||
{ | ||
return $this->requestingName; | ||
} | ||
|
||
/** | ||
* Resolve the definition and return the resulting value. | ||
* | ||
* @throws ServiceSubscriberException | ||
*/ | ||
public function resolve(ContainerInterface $container) : ServiceLocator | ||
{ | ||
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. | ||
*/ | ||
public function isResolvable(ContainerInterface $container) : bool | ||
{ | ||
return method_exists($this->requestingName, 'getSubscribedServices'); | ||
} | ||
|
||
public function replaceNestedDefinitions(callable $replacer) : void | ||
{ | ||
// no nested definitions | ||
} | ||
|
||
/** | ||
* Definitions can be cast to string for debugging information. | ||
*/ | ||
public function __toString() : string | ||
{ | ||
return sprintf( | ||
'get(%s) for \'%s\'', | ||
$this->name, | ||
$this->requestingName | ||
); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DI; | ||
|
||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
/** | ||
* Class ServiceLocator. | ||
* | ||
* Serving "lazy" dependencies for classes using ServiceSubscriberInterface. | ||
* Suggested as a lightweight alternative for heavyweight proxies from ocramius/proxy-manager | ||
*/ | ||
class ServiceLocator implements ContainerInterface | ||
{ | ||
/** | ||
* Constructor. | ||
* @param string|null $subscriber className of a ServiceSubscriber to which this service locator instance belongs to | ||
*/ | ||
public function __construct( | ||
private ContainerInterface $container, | ||
array $services, | ||
private ?string $subscriber = null | ||
) { | ||
$this->setServices($services); | ||
} | ||
|
||
protected function setServices(array $services) | ||
{ | ||
foreach ($services as $key => $value) { | ||
if (is_numeric($key)) { | ||
$key = $value; | ||
} | ||
$this->services[$key] = $value; | ||
} | ||
} | ||
|
||
/** | ||
* Get defined services. | ||
*/ | ||
public function getServices() : array | ||
{ | ||
return $this->services; | ||
} | ||
|
||
/** | ||
* Get name of a class to which this service locator instance belongs to. | ||
*/ | ||
public function getSubscriber() : string | ||
{ | ||
return $this->subscriber; | ||
} | ||
|
||
/** | ||
* Finds a service by its identifier. | ||
* | ||
* @param string $id Identifier of the entry to look for. | ||
* | ||
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. | ||
* @throws ContainerExceptionInterface Error while retrieving the entry. | ||
*/ | ||
public function get(string $id) : mixed | ||
{ | ||
if (!isset($this->services[$id])) { | ||
throw new NotFoundException("Service '$id' is not defined."); | ||
} | ||
|
||
return $this->container->get($this->services[$id]); | ||
} | ||
|
||
/** | ||
* Returns true if the container can return an entry for the given identifier. | ||
* Returns false otherwise. | ||
* | ||
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. | ||
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | ||
* | ||
* @param string $id Identifier of the entry to look for. | ||
*/ | ||
public function has(string $id) : bool | ||
{ | ||
if (!isset($this->services[$id])) { | ||
return false; | ||
} | ||
|
||
return $this->container->has($this->services[$id]); | ||
} | ||
} |
Oops, something went wrong.