crodas / service-provider
Little configuration manager and dependency injection
Installs: 4 133
Dependents: 4
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- crodas/file-util: ~0.1
- crodas/notoj: ^1.0
- crodas/simple-view-engine: ~0.1
- crodas/watch-files: ~0.1
- seld/jsonlint: *
- symfony/yaml: ~2.6
This package is auto-updated.
Last update: 2024-11-12 19:16:21 UTC
README
Little configuration manager and dependency injection.
It is an attempt to build a simple library to define services, and how to consume them. A service is a configurable resource that is consumed by our software, think of it as a mysql_connect
resource, where you can have different configuration for you local and production environment.
A service can be defined using annotations.
/** * @Service(mysql, { * host: {default:"localhost", type: string}, * user: {default:"root", type: string}, * pass: {default:"", type: string}, * port: {default: 3306, type: integer}, * db: {type: string}, * }) */ function get_mysql_service(Array $config) { return new mysqli( $config['host'], $config['user'], $config['pass'], $config['db'], $config['port'] ); }
That's it, we just defined a service which in a reallity it is a function or method which returns something. In its annotation we defined its name and their configuration validation.
When you want to get access to the mysql service
$service = new \ServiceProvider\Provider( 'production.config.yml', // the configuration file 'where/services/are/defined/', // where the files are defined. It can use * comodin 'production.generated.php' // to improve things we generate code, here is where to save it ); $db = $service->get('mysql'); $db->query("SELECT * FROM users");
Events
As a bonus this library support events triggering/handling the right way. Basically at generation time it would look for @EventSubscriber(<name>, [<preference>=0])
over a method or function. To trigger an event is very simple:
$service = new \ServiceProvider\Provider( 'production.config.yml', // the configuration file 'where/services/are/defined/', // where the files are defined. It can use * comodin 'production.generated.php' // to improve things we generate code, here is where to save it ); $events = $service->get('event_manager'); $result = $events->trigger('foo.bar', array('arg1' => 'foo')); var_dump('this event had ' . $result->getCalls() . ' handlers');
And somewhere in the code
/** * @EventSubscriber(foo.bar) */ function some_handler($event) { $args = $event->getArguments(); $event->stopPropagation(); /* I'm the last one */ }