forked from PHP-DI/PHP-DI
-
Notifications
You must be signed in to change notification settings - Fork 1
[php-di v7] Service Locator #25
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
Open
partikus
wants to merge
5
commits into
feature/php-di-7-annotations-autowiring-options
Choose a base branch
from
feature/php-di-7-service-locator
base: feature/php-di-7-annotations-autowiring-options
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
987e5bd
porting service locator feat for php-di v7
partikus f885a5b
ServiceLocatorTest: fixes "creation of dynamic property is deprecated"
marcing 4395f1c
missing patches from #7
partikus cd87c88
sort Reference out
partikus e8efad6
service-locator - pass requesting name when reading constructor
partikus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,90 @@ | ||
| <?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 | ||
| { | ||
| private array $services = []; | ||
|
|
||
| public function __construct( | ||
| private ContainerInterface $container, | ||
| array $services, | ||
| /** @var string|null className of a ServiceSubscriber to which this service locator instance belongs to */ | ||
| private ?string $subscriber = null | ||
| ) { | ||
| $this->setServices($services); | ||
| } | ||
|
|
||
| protected function setServices(array $services) : void | ||
| { | ||
| 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this from constructor parameters, this should not be altered from outside. Ref: https://github.com/ovos/PHP-DI/pull/7/files#diff-addce6eaa59f011f6a80f000d9b021f007d8410be8044e3a747a6aa93ac54053R55