crodas/service-provider
Composer 安装命令:
composer require crodas/service-provider
包简介
Little configuration manager and dependency injection
README 文档
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 */ }
统计信息
- 总下载量: 4.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2013-05-06