diff --git a/More/ServiceLocator/DatabaseService.php b/More/ServiceLocator/DatabaseService.php deleted file mode 100644 index 776ba2d..0000000 --- a/More/ServiceLocator/DatabaseService.php +++ /dev/null @@ -1,7 +0,0 @@ -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; diff --git a/More/ServiceLocator/ServiceLocatorInterface.php b/More/ServiceLocator/ServiceLocatorInterface.php deleted file mode 100644 index 3738624..0000000 --- a/More/ServiceLocator/ServiceLocatorInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -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); } }