slidesworker/servicelocator

A basic ServiceLocator for PHP inspired by ZendFramework2

v1.0.0-rc1 2014-03-03 22:16 UTC

This package is not auto-updated.

Last update: 2024-12-21 17:06:23 UTC


README

Latest Stable Version

Build Status Code Coverage Scrutinizer Quality Score Dependency Status

How to use

simple use

You can simple register your service to this ServiceLocator Fore more example see this file

namespace Example;

use SlidesWorker\ServiceLocator\ServiceLocator;

class Service1
{
}
class Service2
{
}

// setup ServiceLocator
$serviceLocator = new ServiceLocator();

$serviceLocator->set('service1', new Service1());
$serviceLocator->set('service2', new Service2());

$service1 = $serviceLocator->get('service1');
$service2 = $serviceLocator->get('service2');

with factory

The ServiceLocator can work with a some few factories. Classes that implements SlidesWorker\ServiceLocator\FactoryInterface and function or closure. Fore more example see this file

namespace Example;

use SlidesWorker\ServiceLocator\ServiceLocator;
use SlidesWorker\ServiceLocator\ServiceLocatorInterface;

class Service {}

// setup ServiceLocator
$serviceLocator = new ServiceLocator();


// factory as closure
$serviceLocator->setFactory('service', function (ServiceLocatorInterface $locator) {
    return Service();
});

// get a service
$service = $serviceLocator->get('service');

Initialzer and ServiceLocator

If you have the need that one objects must hold some other object Initializer is the right feature. In the case that ServiceLocator create the Object for you it run a Stack of few Initializer. You can add your own Initializer to this system, too.

A working example you find here

give service the ability to handle the ServiceLocator

ServiceLocator has the ability to add him self to a object. For this feature the object must be a instance of SlidesWorker\ServiceLocator\ServiceLocatorAwareTrait or SlidesWorker\ServiceLocator\ServiceLocatorAwareInterface

For more example see this file

namespace Example;

use SlidesWorker\ServiceLocator\ServiceLocator;
use SlidesWorker\ServiceLocator\ServiceLocatorInterface;
use SlidesWorker\ServiceLocator\ServiceLocatorAwareInterface;
use SlidesWorker\ServiceLocator\ServiceLocatorAwareTrait;

// only php 5.4 and higher
class ServiceCanHoldServiceLocator1
{
    use ServiceLocatorAwareTrait;
}


// setup ServiceLocator
$serviceLocator = new ServiceLocator();

// factory as closure
$serviceLocator->setFactory('service', function (ServiceLocatorInterface $locator) {
    return new ServiceCanHoldServiceLocator1();
});

$serviceLocator->get('service');