Skip to content

hhpack/service-locator

Repository files navigation

service-locator

Latest Stable Version CircleCI Dependency Status License

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