wikimedia / services
Generic service to manage named services using lazy instantiation based on instantiator callback functions
Requires
- php: >=7.2.9
- psr/container: ^1.1 || ^2.0
- wikimedia/scoped-callback: ^3.0 || ^4.0
Requires (Dev)
- mediawiki/mediawiki-codesniffer: 39.0.0
- mediawiki/mediawiki-phan-config: 0.11.1
- mediawiki/minus-x: 1.1.1
- ockcyp/covers-validator: 1.4.0
- php-parallel-lint/php-console-highlighter: 1.0.0
- php-parallel-lint/php-parallel-lint: 1.3.2
- phpunit/phpunit: ^8.5
Provides
- psr/container-implementation: 1.0.0
This package is auto-updated.
Last update: 2023-09-17 21:57:19 UTC
README
Services
A PSR-11-compliant services framework. Services are created by instantiators (callables), which are usually defined in separate wiring files.
Usage
$services = new ServiceContainer(); $services->defineService( 'MyService', function ( ServiceContainer $services ) { return new MyService(); } ); $services->loadWiringFiles( [ 'path/to/ServiceWiring.php', ] );
Where ServiceWiring.php
looks like this:
return [ 'MyOtherService' => function ( ServiceContainer $services ) { return new MyOtherService( $services->get( 'MyService' ) ); }, // ... ];
Each instantiator receives the service container as the first argument,
from which it may retrieve further services as needed.
Additional arguments for each instantiator may be specified
when constructing the ServiceContainer
.
Custom subclasses of ServiceContainer
may offer easier access to certain services:
class MyServiceContainer extends ServiceContainer { public function getMyService(): MyService { return $this->get( 'MyService' ); } public function getMyOtherService(): MyOtherService { return $this->get( 'MyOtherService' ); } } // ServiceWiring.php return [ 'MyOtherService' => function ( MyServiceContainer $services ) { return new MyOtherService( $services->getMyService() ); }, ];
Running tests
composer install --prefer-dist
composer test
History
This library was first introduced in MediaWiki 1.27 (I3c25c0ac17). It was split out of the MediaWiki codebase and published as an independent library during the MediaWiki 1.33 and MediaWiki 1.34 development cycles.