|
| 1 | +.. _index-service-manager-label: |
| 2 | + |
| 3 | +Service manager |
| 4 | +=============== |
| 5 | + |
| 6 | +The Service Manager is a **service/object locator**, tasked with retrieving other objects. |
| 7 | +It fully compatible with the PSR's containers - https://www.php-fig.org/psr/psr-11/ |
| 8 | + |
| 9 | +Installation |
| 10 | +------------ |
| 11 | + |
| 12 | +Run the following to install this library: |
| 13 | + |
| 14 | + |
| 15 | +.. code-block:: bash |
| 16 | +
|
| 17 | + $ composer require esase/tiny-service-manager |
| 18 | +
|
| 19 | +Shared services |
| 20 | +--------------- |
| 21 | + |
| 22 | +The most frequently used type of services it's shared services or singletons. |
| 23 | +The service manager always keeps and retrieves only the one instance of an object. |
| 24 | +It means you may reduce consuming of the memory and keep the state in a one object. |
| 25 | + |
| 26 | +-------------- |
| 27 | +Shared example |
| 28 | +-------------- |
| 29 | + |
| 30 | +.. code-block:: php |
| 31 | +
|
| 32 | + <?php |
| 33 | +
|
| 34 | + use Tiny\ServiceManager\ServiceManager; |
| 35 | + use stdClass; |
| 36 | +
|
| 37 | + $serviceManager = new ServiceManager([ |
| 38 | + // pass a service name and it's factory |
| 39 | + 'TestService' => function() { |
| 40 | + return new stdClass(); |
| 41 | + } |
| 42 | + ]); |
| 43 | +
|
| 44 | + var_dump($serviceManager->has('TestService')); // prints `true` |
| 45 | +
|
| 46 | + $service1 = $serviceManager->get('TestService'); |
| 47 | + $service2 = $serviceManager->get('TestService'); |
| 48 | +
|
| 49 | + var_dump($service1 === $service2); // prints `true` |
| 50 | +
|
| 51 | +Discrete services |
| 52 | +----------------- |
| 53 | + |
| 54 | +Some times we don't need singletons, we need to retrieve a new object instance whenever we get it from the service manager. |
| 55 | + |
| 56 | +.. code-block:: php |
| 57 | +
|
| 58 | + <?php |
| 59 | +
|
| 60 | + use Tiny\ServiceManager\ServiceManager; |
| 61 | + use stdClass; |
| 62 | +
|
| 63 | + // the constructor accepts discrete services as a second parameter |
| 64 | + $serviceManager = new ServiceManager([], [ |
| 65 | + // pass a service name and it's factory |
| 66 | + 'TestService' => function() { // now this is not a shared service |
| 67 | + return new stdClass(); |
| 68 | + } |
| 69 | + ]); |
| 70 | +
|
| 71 | + var_dump($serviceManager->has('TestService')); // prints `true` |
| 72 | +
|
| 73 | + $service1 = $serviceManager->get('TestService'); |
| 74 | + $service2 = $serviceManager->get('TestService'); |
| 75 | +
|
| 76 | + var_dump($service1 === $service2); // prints `false` they are different |
| 77 | +
|
| 78 | +Factories |
| 79 | +--------- |
| 80 | + |
| 81 | +There are two types of factories which you can use for building you objects: :code:`Closure` and :code:`Class factories` |
| 82 | + |
| 83 | +--------------- |
| 84 | +Closure example |
| 85 | +--------------- |
| 86 | + |
| 87 | +The closure it's just an `anonymous` (or `lambda`) function which is called for building an object: |
| 88 | + |
| 89 | +.. code-block:: php |
| 90 | +
|
| 91 | + <?php |
| 92 | +
|
| 93 | + use Tiny\ServiceManager\ServiceManager; |
| 94 | + use stdClass; |
| 95 | +
|
| 96 | + $serviceManager = new ServiceManager([ |
| 97 | + TestService::class => function( |
| 98 | + ServiceManager $serviceManager, |
| 99 | + string $targetClass |
| 100 | + ) { |
| 101 | + return new stdClass(); |
| 102 | + } |
| 103 | + ]); |
| 104 | +
|
| 105 | +It's a good practice to use a class name as key for services, :code:`TestService::class` in our case. |
| 106 | +Also as you can see the service manager always passes two parameters inside factories: |
| 107 | + |
| 108 | +- :code:`$serviceManager` - it's just a reference to it self which may be used for retrieving other dependencies. |
| 109 | +- :code:`$targetClass` - a class name which we are trying to build. |
| 110 | + |
| 111 | +--------------------- |
| 112 | +Class factory example |
| 113 | +--------------------- |
| 114 | + |
| 115 | +Remember each factory class must include :code:`__invoke` method. |
| 116 | + |
| 117 | +.. code-block:: php |
| 118 | +
|
| 119 | + <?php |
| 120 | +
|
| 121 | + use Tiny\ServiceManager\ServiceManager; |
| 122 | +
|
| 123 | + $serviceManager = new ServiceManager([], [ |
| 124 | + TestService::class => TestServiceFactory::class |
| 125 | + ]); |
| 126 | +
|
| 127 | + class TestServiceFactory |
| 128 | + { |
| 129 | + public function __invoke( |
| 130 | + ServiceManager $serviceManager, |
| 131 | + string $targetClass |
| 132 | + ): TestService { |
| 133 | + // we even may inject different services |
| 134 | + return new TestService( |
| 135 | + $serviceManager->get(OtherService::class) |
| 136 | + ... |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | +
|
| 141 | +If you don't need to provide extra dependencies in you service you may use a default factory class, |
| 142 | +which just creates you service: |
| 143 | + |
| 144 | +.. code-block:: php |
| 145 | +
|
| 146 | + <?php |
| 147 | +
|
| 148 | + use Tiny\ServiceManager\ServiceManager; |
| 149 | + use Tiny\ServiceManager\Factory\InvokableFactory; |
| 150 | +
|
| 151 | + $serviceManager = new ServiceManager([], [ |
| 152 | + TestService::class => InvokableFactory::class |
| 153 | + ]); |
| 154 | +
|
| 155 | + ... |
0 commit comments