diff --git a/More/ServiceLocator/LogService.php b/More/ServiceLocator/LogService.php index 84c999c..1653a0b 100644 --- a/More/ServiceLocator/LogService.php +++ b/More/ServiceLocator/LogService.php @@ -2,6 +2,7 @@ namespace DesignPatterns\More\ServiceLocator; -class LogService +class LogService implements Service { + } diff --git a/More/ServiceLocator/Service.php b/More/ServiceLocator/Service.php new file mode 100644 index 0000000..73bec99 --- /dev/null +++ b/More/ServiceLocator/Service.php @@ -0,0 +1,8 @@ +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) + public function addClass(string $class, array $params) { $this->services[$class] = $params; - $this->shared[$class] = $share; } public function has(string $interface): bool @@ -51,14 +30,9 @@ class ServiceLocator return isset($this->services[$interface]) || isset($this->instantiated[$interface]); } - /** - * @param string $class - * - * @return object - */ - public function get(string $class) + public function get(string $class): Service { - if (isset($this->instantiated[$class]) && $this->shared[$class]) { + if (isset($this->instantiated[$class])) { return $this->instantiated[$class]; } @@ -81,9 +55,7 @@ class ServiceLocator throw new \OutOfRangeException('Too many arguments given'); } - if ($this->shared[$class]) { - $this->instantiated[$class] = $object; - } + $this->instantiated[$class] = $object; return $object; } diff --git a/More/ServiceLocator/uml/uml.png b/More/ServiceLocator/uml/uml.png index 1136eaa..7d878c0 100644 Binary files a/More/ServiceLocator/uml/uml.png and b/More/ServiceLocator/uml/uml.png differ