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

@ -1,7 +0,0 @@
<?php
namespace DesignPatterns\More\ServiceLocator;
class DatabaseService implements DatabaseServiceInterface
{
}

View File

@ -1,7 +0,0 @@
<?php
namespace DesignPatterns\More\ServiceLocator;
interface DatabaseServiceInterface
{
}

View File

@ -2,6 +2,6 @@
namespace DesignPatterns\More\ServiceLocator;
class LogService implements LogServiceInterface
class LogService
{
}

View File

@ -1,7 +0,0 @@
<?php
namespace DesignPatterns\More\ServiceLocator;
interface LogServiceInterface
{
}

View File

@ -35,42 +35,18 @@ Code
You can also find these code on `GitHub`_
ServiceLocatorInterface.php
.. literalinclude:: ServiceLocatorInterface.php
:language: php
:linenos:
ServiceLocator.php
.. literalinclude:: ServiceLocator.php
:language: php
:linenos:
LogServiceInterface.php
.. literalinclude:: LogServiceInterface.php
:language: php
:linenos:
LogService.php
.. literalinclude:: LogService.php
:language: php
:linenos:
DatabaseServiceInterface.php
.. literalinclude:: DatabaseServiceInterface.php
:language: php
:linenos:
DatabaseService.php
.. literalinclude:: DatabaseService.php
:language: php
:linenos:
Test
----

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;

View File

@ -1,24 +0,0 @@
<?php
namespace DesignPatterns\More\ServiceLocator;
interface ServiceLocatorInterface
{
/**
* Checks if a service is registered.
*
* @param string $interface
*
* @return bool
*/
public function has($interface);
/**
* Gets the service registered for the interface.
*
* @param string $interface
*
* @return mixed
*/
public function get($interface);
}

View File

@ -2,23 +2,12 @@
namespace DesignPatterns\More\ServiceLocator\Tests;
use DesignPatterns\More\ServiceLocator\DatabaseService;
use DesignPatterns\More\ServiceLocator\LogService;
use DesignPatterns\More\ServiceLocator\ServiceLocator;
use PHPUnit_Framework_TestCase as TestCase;
class ServiceLocatorTest extends TestCase
{
/**
* @var LogService
*/
private $logService;
/**
* @var DatabaseService
*/
private $databaseService;
/**
* @var ServiceLocator
*/
@ -27,116 +16,21 @@ class ServiceLocatorTest extends TestCase
public function setUp()
{
$this->serviceLocator = new ServiceLocator();
$this->logService = new LogService();
$this->databaseService = new DatabaseService();
}
public function testHasServices()
{
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->logService
);
$this->serviceLocator->addInstance(LogService::class, new LogService());
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
$this->databaseService
);
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
$this->assertFalse($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\FakeServiceInterface'));
$this->assertTrue($this->serviceLocator->has(LogService::class));
$this->assertFalse($this->serviceLocator->has(self::class));
}
public function testServicesWithObject()
public function testGetWillInstantiateLogServiceIfNoInstanceHasBeenCreatedYet()
{
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->logService
);
$this->serviceLocator->addClass(LogService::class, []);
$logger = $this->serviceLocator->get(LogService::class);
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
$this->databaseService
);
$this->assertSame(
$this->logService,
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
);
$this->assertSame(
$this->databaseService,
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
);
}
public function testServicesWithClass()
{
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
get_class($this->logService)
);
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
get_class($this->databaseService)
);
$this->assertNotSame(
$this->logService,
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
);
$this->assertInstanceOf(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
);
$this->assertNotSame(
$this->databaseService,
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
);
$this->assertInstanceOf(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
);
}
public function testServicesNotShared()
{
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->logService,
false
);
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
$this->databaseService,
false
);
$this->assertNotSame(
$this->logService,
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
);
$this->assertInstanceOf(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
);
$this->assertNotSame(
$this->databaseService,
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
);
$this->assertInstanceOf(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
);
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogService', $logger);
}
}