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; 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`_ You can also find these code on `GitHub`_
ServiceLocatorInterface.php
.. literalinclude:: ServiceLocatorInterface.php
:language: php
:linenos:
ServiceLocator.php ServiceLocator.php
.. literalinclude:: ServiceLocator.php .. literalinclude:: ServiceLocator.php
:language: php :language: php
:linenos: :linenos:
LogServiceInterface.php
.. literalinclude:: LogServiceInterface.php
:language: php
:linenos:
LogService.php LogService.php
.. literalinclude:: LogService.php .. literalinclude:: LogService.php
:language: php :language: php
:linenos: :linenos:
DatabaseServiceInterface.php
.. literalinclude:: DatabaseServiceInterface.php
:language: php
:linenos:
DatabaseService.php
.. literalinclude:: DatabaseService.php
:language: php
:linenos:
Test Test
---- ----

View File

@@ -2,104 +2,87 @@
namespace DesignPatterns\More\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
class ServiceLocator implements ServiceLocatorInterface class ServiceLocator
{ {
/** /**
* All services.
*
* @var array * @var array
*/ */
private $services; private $services = [];
/** /**
* The services which have an instance.
*
* @var array * @var array
*/ */
private $instantiated; private $instantiated = [];
/** /**
* True if a service can be shared.
*
* @var array * @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->services[$class] = $service;
$this->instantiated = array(); $this->instantiated[$class] = $service;
$this->shared = array(); $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 $class
* @param string|object $service * @param array $params
* @param bool $share * @param bool $share
*/ */
public function add($interface, $service, $share = true) public function addClass(string $class, array $params, bool $share = true)
{ {
/* $this->services[$class] = $params;
* When you add a service, you should register it $this->shared[$class] = $share;
* 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;
} }
/** public function has(string $interface): bool
* Checks if a service is registered.
*
* @param string $interface
*
* @return bool
*/
public function has($interface)
{ {
return isset($this->services[$interface]) || isset($this->instantiated[$interface]); return isset($this->services[$interface]) || isset($this->instantiated[$interface]);
} }
/** /**
* Gets the service registered for the interface. * @param string $class
* *
* @param string $interface * @return object
*
* @return mixed
*/ */
public function get($interface) public function get(string $class)
{ {
// Retrieves the instance if it exists and it is shared if (isset($this->instantiated[$class]) && $this->shared[$class]) {
if (isset($this->instantiated[$interface]) && $this->shared[$interface]) { return $this->instantiated[$class];
return $this->instantiated[$interface];
} }
// otherwise gets the service registered. $args = $this->services[$class];
$service = $this->services[$interface];
// You should check if the service class exists and switch (count($args)) {
// the class is instantiable. 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 if ($this->shared[$class]) {
// when you create a service, you can decide $this->instantiated[$class] = $object;
// 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;
} }
return $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; namespace DesignPatterns\More\ServiceLocator\Tests;
use DesignPatterns\More\ServiceLocator\DatabaseService;
use DesignPatterns\More\ServiceLocator\LogService; use DesignPatterns\More\ServiceLocator\LogService;
use DesignPatterns\More\ServiceLocator\ServiceLocator; use DesignPatterns\More\ServiceLocator\ServiceLocator;
use PHPUnit_Framework_TestCase as TestCase; use PHPUnit_Framework_TestCase as TestCase;
class ServiceLocatorTest extends TestCase class ServiceLocatorTest extends TestCase
{ {
/**
* @var LogService
*/
private $logService;
/**
* @var DatabaseService
*/
private $databaseService;
/** /**
* @var ServiceLocator * @var ServiceLocator
*/ */
@@ -27,116 +16,21 @@ class ServiceLocatorTest extends TestCase
public function setUp() public function setUp()
{ {
$this->serviceLocator = new ServiceLocator(); $this->serviceLocator = new ServiceLocator();
$this->logService = new LogService();
$this->databaseService = new DatabaseService();
} }
public function testHasServices() public function testHasServices()
{ {
$this->serviceLocator->add( $this->serviceLocator->addInstance(LogService::class, new LogService());
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->logService
);
$this->serviceLocator->add( $this->assertTrue($this->serviceLocator->has(LogService::class));
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->assertFalse($this->serviceLocator->has(self::class));
$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'));
} }
public function testServicesWithObject() public function testGetWillInstantiateLogServiceIfNoInstanceHasBeenCreatedYet()
{ {
$this->serviceLocator->add( $this->serviceLocator->addClass(LogService::class, []);
'DesignPatterns\More\ServiceLocator\LogServiceInterface', $logger = $this->serviceLocator->get(LogService::class);
$this->logService
);
$this->serviceLocator->add( $this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogService', $logger);
'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')
);
} }
} }