PHP7 ServiceLocator

This commit is contained in:
Dominik Liebler
2016-09-22 18:55:46 +02:00
parent 6ac29916c3
commit f55008ddc7
8 changed files with 55 additions and 247 deletions

View File

@@ -2,104 +2,87 @@
namespace DesignPatterns\More\ServiceLocator;
class ServiceLocator implements ServiceLocatorInterface
class ServiceLocator
{
/**
* All services.
*
* @var array
*/
private $services;
private $services = [];
/**
* The services which have an instance.
*
* @var array
*/
private $instantiated;
private $instantiated = [];
/**
* True if a service can be shared.
*
* @var array
*/
private $shared;
private $shared = [];
public function __construct()
/**
* instead of supplying a class here, you could also store a service for an interface
*
* @param string $class
* @param object $service
* @param bool $share
*/
public function addInstance(string $class, $service, bool $share = true)
{
$this->services = array();
$this->instantiated = array();
$this->shared = array();
$this->services[$class] = $service;
$this->instantiated[$class] = $service;
$this->shared[$class] = $share;
}
/**
* Registers a service with specific interface.
* instead of supplying a class here, you could also store a service for an interface
*
* @param string $interface
* @param string|object $service
* @param bool $share
* @param string $class
* @param array $params
* @param bool $share
*/
public function add($interface, $service, $share = true)
public function addClass(string $class, array $params, bool $share = true)
{
/*
* When you add a service, you should register it
* with its interface or with a string that you can use
* in the future even if you will change the service implementation.
*/
if (is_object($service) && $share) {
$this->instantiated[$interface] = $service;
}
$this->services[$interface] = (is_object($service) ? get_class($service) : $service);
$this->shared[$interface] = $share;
$this->services[$class] = $params;
$this->shared[$class] = $share;
}
/**
* Checks if a service is registered.
*
* @param string $interface
*
* @return bool
*/
public function has($interface)
public function has(string $interface): bool
{
return isset($this->services[$interface]) || isset($this->instantiated[$interface]);
}
/**
* Gets the service registered for the interface.
* @param string $class
*
* @param string $interface
*
* @return mixed
* @return object
*/
public function get($interface)
public function get(string $class)
{
// Retrieves the instance if it exists and it is shared
if (isset($this->instantiated[$interface]) && $this->shared[$interface]) {
return $this->instantiated[$interface];
if (isset($this->instantiated[$class]) && $this->shared[$class]) {
return $this->instantiated[$class];
}
// otherwise gets the service registered.
$service = $this->services[$interface];
$args = $this->services[$class];
// You should check if the service class exists and
// the class is instantiable.
switch (count($args)) {
case 0:
$object = new $class();
break;
case 1:
$object = new $class($args[0]);
break;
case 2:
$object = new $class($args[0], $args[1]);
break;
case 3:
$object = new $class($args[0], $args[1], $args[2]);
break;
default:
throw new \OutOfRangeException('Too many arguments given');
}
// This example is a simple implementation, but
// when you create a service, you can decide
// if $service is a factory or a class.
// By registering a factory you can create your services
// using the DependencyInjection pattern.
// ...
// Creates the service object
$object = new $service();
// and saves it if the service must be shared.
if ($this->shared[$interface]) {
$this->instantiated[$interface] = $object;
if ($this->shared[$class]) {
$this->instantiated[$class] = $object;
}
return $object;