diff --git a/README.md b/README.md index 43bc404..4cbd735 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,22 @@ Lazy service definitions for Pimple DI container. Travis build status: [![Build Status](https://travis-ci.org/spiechu/lazy-pimple.svg?branch=master)](https://travis-ci.org/spiechu/lazy-pimple) -## Installation +## Intro When using Pimple DIC, there is sometimes need to lazy load service and instantiate it only when needed. What's more, there is also possibility to lazy load event subscribers. (Now you'll see why `\Symfony\Component\EventDispatcher\EventSubscriberInterface` has static interface). -With use of this library, you can easy lazy load Pimple service definitions unless they're needed. +Under the hood this library uses [Proxy Manager](https://github.com/Ocramius/ProxyManager). Object's proxy is firstly generated. Until instance method call is needed, the proxy is being used. This means even static calls don't need object's instance and are called by proxy. + +## Usage + +### Lazy services + +With use of this library, you can easy lazy load Pimple service definitions until they're needed. ```php getLazyServiceDefinition(AwesomeService::class, function() { @@ -21,3 +28,113 @@ $pimpleContainer['awesome_service'] = function(Container $container) { }); }; ``` + +### Lazy event subscribers + +We'll start with typical event subscriber definition. + +```php +register(new LazyEventSubscriberServiceProvider( + $pimpleContainer['lazy_service_factory'], + // we're defining which service resolves to EventDispatcher + 'event_dispatcher', + [ + // we're defining subscribers + 'first_subscriber' => FirstSubscriber::class, + ] +)); +``` + +This way only when event actually took place, subscriber is instantiated and event handle method called. + +### Example Pimple service definition (taken from my "tests"). + +```php +getFactory($container['proxy_manager_cache_target_dir']); + }; + + $pimpleContainer['lazy_service_factory'] = function(Container $container) { + return new LazyServiceFactory($container['lazy_loading_value_holder_factory_factory']); + }; + + $pimpleContainer['event_dispatcher'] = function(Container $container) { + return new EventDispatcher(); + }; + + // imgine awesome service is expensive and should be lazy loaded + $pimpleContainer['awesome_service'] = function(Container $container) { + return $container['lazy_service_factory']->getLazyServiceDefinition(AwesomeService::class, function() { + return new AwesomeService(); + }); + }; + + $pimpleContainer['another_service'] = function(Container $container) { + // this one will receive proxy object + return new AnotherService($container['awesome_service']); + }; + + $pimpleContainer['event_emitting_service'] = function(Container $container) { + return new EventEmittingService($container['event_dispatcher']); + }; + + $pimpleContainer['first_subscriber'] = function(Container $container) { + // subscriber has no idea it will be lazy loaded + return new FirstSubscriber($container['awesome_service']); + }; + + $pimpleContainer->register(new LazyEventSubscriberServiceProvider( + $pimpleContainer['lazy_service_factory'], + // we're defining which service resolves to EventDispatcher + 'event_dispatcher', + [ + // we're defining subscribers + 'first_subscriber' => FirstSubscriber::class, + ] + )); + + return $pimpleContainer; +}); +```