GithubHelp home page GithubHelp logo

service-locator's Introduction

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

service-locator's People

Contributors

holyshared avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

service-locator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.