hhpack/service-locator
Composer 安装命令:
composer require hhpack/service-locator
包简介
Service locator for Hack
README 文档
README
Basic usage
Implementing a factory of creating service.
use HHPack\ServiceLocator\{ Service, ServiceFactory, Locator }; interface Logger implements Service { } final class LoggerService implements Logger { } final class LoggerFactory implements ServiceFactory { const type T = Logger; public function createService(Locator $locator): this::T { return new LoggerService(); } }
Specify the ServiceFactory, to create a service locator.
use HHPack\ServiceLocator\ServiceLocator; $locator = ServiceLocator::fromItems([ new LoggerFactory() ]); $logger = $locator->lookup(Logger::class);
Module of service factory
By using the modules that provide ServiceFactory, you can generate a new service locator.
use HHPack\ServiceLocator\{ ServiceFactory, Module }; final class CustomModule implements Module { public function getIterator() : Iterator<ServiceFactory> { yield new LoggerFactory(); } }
Using the defined modules, and generates a new service locator.
use HHPack\ServiceLocator\ServiceLocator; $locator = ServiceLocator::fromModule(new CustomModule()); $logger = $locator->lookup(Logger::class); $logger->put('logger loaded');
Module for environment
By using the EnvironmentModule, you will be able to load a module based on the setting of HHVM_ENV.
When HHVM_ENV is the production looks for a module named Production from the specified directory.
use HHPack\ServiceLocator\ServiceLocator; use HHPack\ServiceLocator\Module\EnvironmentModule; $module = new EnvironmentModule([ Pair { 'HHPack\\Service\\Example\\', __DIR__ } // autoload for module ]); $locator = ServiceLocator::fromModule($module); $logger = $locator->lookup(Logger::class); $logger->put('logger loaded');
By executing the environment.hack of example, it can be confirmed.
-
development
$ HHVM_ENV=development hhvm example/environment.hack $ development - logger loaded -
production
$ HHVM_ENV=production hhvm example/environment.hack $ production - logger loaded
Run the test
You can run the test with the following command.
composer install
composer test
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-21