|
1 | 1 | Built-in Providers |
2 | 2 | ================== |
3 | 3 |
|
| 4 | + |
4 | 5 | FastForward Container provides ready-to-use provider classes to help you register services quickly: |
5 | 6 |
|
6 | 7 | - **ArrayServiceProvider**: Register factories and extensions using plain arrays. |
7 | 8 | - **AggregateServiceProvider**: Combine multiple providers into one. |
8 | 9 | - **ServiceProviderContainer**: Wraps a provider as a PSR-11 container. |
9 | 10 |
|
10 | | -Basic Example |
11 | | -------------- |
| 11 | +Examples |
| 12 | +-------- |
| 13 | + |
| 14 | +**ArrayServiceProvider** |
| 15 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | +Register factories and extensions using arrays: |
12 | 17 |
|
13 | 18 | .. code-block:: php |
14 | 19 |
|
15 | 20 | use FastForward\Container\ServiceProvider\ArrayServiceProvider; |
16 | 21 | use FastForward\Container\ServiceProviderContainer; |
17 | 22 |
|
18 | 23 | $provider = new ArrayServiceProvider([ |
19 | | - 'foo' => fn() => new FooService(), |
20 | | - 'bar' => fn() => new BarService(), |
| 24 | + 'foo' => fn() => new FooService(), |
| 25 | + 'bar' => fn() => new BarService(), |
| 26 | + ], [ |
| 27 | + 'foo' => function ($container, $previous) { |
| 28 | + $previous->setBar($container->get('bar')); |
| 29 | + return $previous; |
| 30 | + }, |
21 | 31 | ]); |
22 | 32 |
|
23 | 33 | $container = new ServiceProviderContainer($provider); |
24 | 34 | $foo = $container->get('foo'); |
25 | 35 |
|
26 | | -You can also use the ``container()`` helper function: |
| 36 | +Or using the ``container()`` helper: |
27 | 37 |
|
28 | 38 | .. code-block:: php |
29 | 39 |
|
30 | 40 | use FastForward\Container\container; |
31 | | -
|
32 | 41 | $container = container($provider); |
33 | 42 | $foo = $container->get('foo'); |
| 43 | +
|
| 44 | +**AggregateServiceProvider** |
| 45 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 46 | +Combine multiple providers into one: |
| 47 | + |
| 48 | +.. code-block:: php |
| 49 | +
|
| 50 | + use FastForward\Container\ServiceProvider\ArrayServiceProvider; |
| 51 | + use FastForward\Container\ServiceProvider\AggregateServiceProvider; |
| 52 | + use FastForward\Container\ServiceProviderContainer; |
| 53 | +
|
| 54 | + $providerA = new ArrayServiceProvider([ |
| 55 | + 'foo' => fn() => new FooService(), |
| 56 | + ]); |
| 57 | + $providerB = new ArrayServiceProvider([ |
| 58 | + 'bar' => fn() => new BarService(), |
| 59 | + ]); |
| 60 | +
|
| 61 | + $aggregate = new AggregateServiceProvider($providerA, $providerB); |
| 62 | + $container = new ServiceProviderContainer($aggregate); |
| 63 | +
|
| 64 | + $foo = $container->get('foo'); // from providerA |
| 65 | + $bar = $container->get('bar'); // from providerB |
| 66 | +
|
| 67 | +Or using the ``container()`` helper: |
| 68 | + |
| 69 | +.. code-block:: php |
| 70 | +
|
| 71 | + $container = container($providerA, $providerB); |
| 72 | + $foo = $container->get('foo'); |
| 73 | + $bar = $container->get('bar'); |
| 74 | +
|
| 75 | +**ServiceProviderContainer** |
| 76 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 77 | +Wraps any ServiceProviderInterface as a PSR-11 container: |
| 78 | + |
| 79 | +.. code-block:: php |
| 80 | +
|
| 81 | + use FastForward\Container\ServiceProvider\ArrayServiceProvider; |
| 82 | + use FastForward\Container\ServiceProviderContainer; |
| 83 | +
|
| 84 | + $provider = new ArrayServiceProvider([ |
| 85 | + 'foo' => fn() => new FooService(), |
| 86 | + ]); |
| 87 | + $container = new ServiceProviderContainer($provider); |
| 88 | + $foo = $container->get('foo'); |
0 commit comments