| 1 |
[![Latest Stable Version]](https://packagist.org/packages/wikimedia/services) [![License]](https://packagist.org/packages/wikimedia/services) |
| 2 |
|
| 3 |
Services |
| 4 |
===================== |
| 5 |
|
| 6 |
A [PSR-11][]-compliant services framework. |
| 7 |
Services are created by instantiators (callables), |
| 8 |
which are usually defined in separate wiring files. |
| 9 |
|
| 10 |
Usage |
| 11 |
----- |
| 12 |
|
| 13 |
```php |
| 14 |
$services = new ServiceContainer(); |
| 15 |
|
| 16 |
$services->defineService( |
| 17 |
'MyService', |
| 18 |
static function ( ServiceContainer $services ): MyService { |
| 19 |
return new MyService(); |
| 20 |
} |
| 21 |
); |
| 22 |
|
| 23 |
$services->loadWiringFiles( [ |
| 24 |
'path/to/ServiceWiring.php', |
| 25 |
] ); |
| 26 |
``` |
| 27 |
|
| 28 |
Where `ServiceWiring.php` looks like this: |
| 29 |
|
| 30 |
```php |
| 31 |
return [ |
| 32 |
|
| 33 |
'MyOtherService' => static function ( ServiceContainer $services ): MyOtherService { |
| 34 |
return new MyOtherService( $services->get( 'MyService' ) ); |
| 35 |
}, |
| 36 |
|
| 37 |
// ... |
| 38 |
|
| 39 |
]; |
| 40 |
``` |
| 41 |
|
| 42 |
Each instantiator receives the service container as the first argument, |
| 43 |
from which it may retrieve further services as needed. |
| 44 |
Additional arguments for each instantiator may be specified |
| 45 |
when constructing the `ServiceContainer`. |
| 46 |
|
| 47 |
Custom subclasses of `ServiceContainer` |
| 48 |
may offer easier access to certain services: |
| 49 |
|
| 50 |
```php |
| 51 |
class MyServiceContainer extends ServiceContainer { |
| 52 |
|
| 53 |
public function getMyService(): MyService { |
| 54 |
return $this->get( 'MyService' ); |
| 55 |
} |
| 56 |
|
| 57 |
public function getMyOtherService(): MyOtherService { |
| 58 |
return $this->get( 'MyOtherService' ); |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
// ServiceWiring.php |
| 64 |
return [ |
| 65 |
|
| 66 |
'MyOtherService' => static function ( MyServiceContainer $services ): MyOtherService { |
| 67 |
return new MyOtherService( $services->getMyService() ); |
| 68 |
}, |
| 69 |
|
| 70 |
]; |
| 71 |
``` |
| 72 |
|
| 73 |
Running tests |
| 74 |
------------- |
| 75 |
|
| 76 |
composer install --prefer-dist |
| 77 |
composer test |
| 78 |
|
| 79 |
|
| 80 |
History |
| 81 |
------- |
| 82 |
|
| 83 |
This library was first introduced in [MediaWiki 1.27][] ([I3c25c0ac17][]). It |
| 84 |
was split out of the MediaWiki codebase and published as an independent library |
| 85 |
during the [MediaWiki 1.33][] and [MediaWiki 1.34][] development cycles. |
| 86 |
|
| 87 |
|
| 88 |
--- |
| 89 |
[PSR-11]: https://www.php-fig.org/psr/psr-11/ |
| 90 |
[MediaWiki 1.27]: https://www.mediawiki.org/wiki/MediaWiki_1.27 |
| 91 |
[I3c25c0ac17]: https://gerrit.wikimedia.org/r/264403 |
| 92 |
[MediaWiki 1.33]: https://www.mediawiki.org/wiki/MediaWiki_1.33 |
| 93 |
[MediaWiki 1.34]: https://www.mediawiki.org/wiki/MediaWiki_1.34 |
| 94 |
[Latest Stable Version]: https://poser.pugx.org/wikimedia/services/v/stable.svg |
| 95 |
[License]: https://poser.pugx.org/wikimedia/services/license.svg |
| 96 |
|