4.1. Service Locator
THIS IS CONSIDERED TO BE AN ANTI-PATTERN!
Service Locator is considered for some people an anti-pattern. It violates the Dependency Inversion principle.Service Locator hides class’ dependencies instead of exposing them as you would do using the Dependency Injection. In case of changes of those dependencies you risk to break the functionality of classes which are using them, making your system difficult to maintain.
4.1.1. Purpose
To implement a loosely coupled architecture in order to get bettertestable, maintainable and extendable code. DI pattern and ServiceLocator pattern are an implementation of the Inverse of Control pattern.
4.1.2. Usage
With ServiceLocator
you can register a service for a giveninterface. By using the interface you can retrieve the service and useit in the classes of the application without knowing its implementation.You can configure and inject the Service Locator object on bootstrap.
4.1.3. Examples
- Zend Framework 2 uses Service Locator to create and share servicesused in the framework(i.e. EventManager, ModuleManager, all customuser services provided by modules, etc…)
4.1.4. UML Diagram
4.1.5. Code
You can also find this code on GitHub
ServiceLocator.php
- <?php
- namespace DesignPatterns\More\ServiceLocator;
- class ServiceLocator
- {
- /**
- * @var array
- */
- private $services = [];
- /**
- * @var array
- */
- private $instantiated = [];
- /**
- * @var array
- */
- private $shared = [];
- /**
- * 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[$class] = $service;
- $this->instantiated[$class] = $service;
- $this->shared[$class] = $share;
- }
- /**
- * instead of supplying a class here, you could also store a service for an interface
- *
- * @param string $class
- * @param array $params
- * @param bool $share
- */
- public function addClass(string $class, array $params, bool $share = true)
- {
- $this->services[$class] = $params;
- $this->shared[$class] = $share;
- }
- public function has(string $interface): bool
- {
- return isset($this->services[$interface]) || isset($this->instantiated[$interface]);
- }
- /**
- * @param string $class
- *
- * @return object
- */
- public function get(string $class)
- {
- if (isset($this->instantiated[$class]) && $this->shared[$class]) {
- return $this->instantiated[$class];
- }
- $args = $this->services[$class];
- 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');
- }
- if ($this->shared[$class]) {
- $this->instantiated[$class] = $object;
- }
- return $object;
- }
- }
LogService.php
- <?php
- namespace DesignPatterns\More\ServiceLocator;
- class LogService
- {
- }
4.1.6. Test
Tests/ServiceLocatorTest.php
- <?php
- namespace DesignPatterns\More\ServiceLocator\Tests;
- use DesignPatterns\More\ServiceLocator\LogService;
- use DesignPatterns\More\ServiceLocator\ServiceLocator;
- use PHPUnit\Framework\TestCase;
- class ServiceLocatorTest extends TestCase
- {
- /**
- * @var ServiceLocator
- */
- private $serviceLocator;
- public function setUp()
- {
- $this->serviceLocator = new ServiceLocator();
- }
- public function testHasServices()
- {
- $this->serviceLocator->addInstance(LogService::class, new LogService());
- $this->assertTrue($this->serviceLocator->has(LogService::class));
- $this->assertFalse($this->serviceLocator->has(self::class));
- }
- public function testGetWillInstantiateLogServiceIfNoInstanceHasBeenCreatedYet()
- {
- $this->serviceLocator->addClass(LogService::class, []);
- $logger = $this->serviceLocator->get(LogService::class);
- $this->assertInstanceOf(LogService::class, $logger);
- }
- }