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

[php-di v7] Service Locator #25

Open
wants to merge 5 commits into
base: feature/php-di-7-annotations-autowiring-options
Choose a base branch
from

Conversation

partikus
Copy link
Collaborator

@partikus partikus commented Jan 10, 2024

Follow up - Service Locator introduced before by @falkenhawk in #7

copied from #7

ServiceLocator

  • Serving "lazy" dependencies for classes using ServiceSubscriberInterface.
  • A ServiceSubscriber exposes its dependencies via static getSubscribedServices() method.
  • A ServiceLocator instance could then be injected into a class via constructor or a property - the instance would be already configured with dependences read from getSubscribedServices(), but the dependences won't be instantiated until first get - that's how "laziness" is introduced
  • DI\Definition\Reference checks if it's a ServiceLocator entry by comparing its name with DI\Definition\Reference::$serviceLocatorClass
  • Reference definitions are passed with additional parameter - $requestingName which generally points to name of the class which implements ServiceSubscriberInterface - to resolve ServiceLocator for that class
  • Suggested as a lightweight alternative for heavyweight proxies from ocramius/proxy-manager

note:

Usage:

class MyClass implements ServiceSubscriberInterface
{
    /**
     * @var ServiceLocator
     */
    protected $serviceLocator;

    /**
     * @param ServiceLocator $serviceLocator
     */
    public function __construct(ServiceLocator $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * Lazy instantiate heavy dependencies on-demand
     */
    public static function getSubscribedServices(): array
    {
        return [
            SomeClass::class,
            'heavyDependency' => HeavyService::class,
        ];
    }

    public function doOperation()
    {
        $someClass = $this->serviceLocator->get(SomeClass::class);
        return $someClass->doSomething();
    }

    public function getSomethingFromThatHeavyDependency()
    {
        // this method may be rarely used, and it might be good idea to skip resolving the dependency every time during instantiation for performance reasons
        return $this->serviceLocator->get('heavyDependency')->getSomething();
    }
}

@partikus partikus force-pushed the feature/php-di-7-service-locator branch from 2208f44 to 13a4b40 Compare January 10, 2024 12:35
@partikus partikus self-assigned this Jan 10, 2024
@partikus partikus added the enhancement New feature or request label Jan 10, 2024
@partikus partikus force-pushed the feature/php-di-7-service-locator branch 3 times, most recently from aab1878 to 8031d21 Compare January 10, 2024 12:49
@partikus partikus force-pushed the feature/php-di-7-annotations-autowiring-options branch from 1a859b4 to 40b4b87 Compare January 10, 2024 12:51
@partikus partikus force-pushed the feature/php-di-7-service-locator branch from 8031d21 to 87ad7d4 Compare January 10, 2024 12:51
@partikus partikus force-pushed the feature/php-di-7-annotations-autowiring-options branch from 40b4b87 to 55a5e10 Compare January 11, 2024 11:08
@partikus partikus force-pushed the feature/php-di-7-service-locator branch from 6527923 to fe94f5d Compare January 30, 2024 15:38
@partikus partikus force-pushed the feature/php-di-7-service-locator branch from fe94f5d to 4395f1c Compare January 30, 2024 15:49
* @var string|null name of an entry - holder of a definition requesting this entry
*/
private ?string $requestingName = null,
private ?ServiceLocatorDefinition $serviceLocatorDefinition = null
Copy link
Member

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

@falkenhawk falkenhawk changed the title php8-mod: service locator feat [php-di v7] Service Locator Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants