hhpack / service-locator
Service locator for Hack
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Language:Shell
Requires
- hhvm: >=3.21.0
- hhpack/package: ^1.4
- hhvm/hhvm-autoload: ^2.0
Requires (Dev)
- facebook/fbexpect: ^2.5
- hhvm/hacktest: ^1.5
This package is auto-updated.
Last update: 2024-12-20 14:37:51 UTC
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