mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-27 18:20:22 +02:00
introduced Service interface to ServiceLocator
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\More\ServiceLocator;
|
namespace DesignPatterns\More\ServiceLocator;
|
||||||
|
|
||||||
class LogService
|
class LogService implements Service
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
8
More/ServiceLocator/Service.php
Normal file
8
More/ServiceLocator/Service.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\More\ServiceLocator;
|
||||||
|
|
||||||
|
interface Service
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@@ -5,45 +5,24 @@ namespace DesignPatterns\More\ServiceLocator;
|
|||||||
class ServiceLocator
|
class ServiceLocator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array|Service[]
|
||||||
*/
|
*/
|
||||||
private $services = [];
|
private $services = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array|Service[]
|
||||||
*/
|
*/
|
||||||
private $instantiated = [];
|
private $instantiated = [];
|
||||||
|
|
||||||
/**
|
public function addInstance(string $class, Service $service)
|
||||||
* @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->services[$class] = $service;
|
||||||
$this->instantiated[$class] = $service;
|
$this->instantiated[$class] = $service;
|
||||||
$this->shared[$class] = $share;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function addClass(string $class, array $params)
|
||||||
* 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->services[$class] = $params;
|
||||||
$this->shared[$class] = $share;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function has(string $interface): bool
|
public function has(string $interface): bool
|
||||||
@@ -51,14 +30,9 @@ class ServiceLocator
|
|||||||
return isset($this->services[$interface]) || isset($this->instantiated[$interface]);
|
return isset($this->services[$interface]) || isset($this->instantiated[$interface]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function get(string $class): Service
|
||||||
* @param string $class
|
|
||||||
*
|
|
||||||
* @return object
|
|
||||||
*/
|
|
||||||
public function get(string $class)
|
|
||||||
{
|
{
|
||||||
if (isset($this->instantiated[$class]) && $this->shared[$class]) {
|
if (isset($this->instantiated[$class])) {
|
||||||
return $this->instantiated[$class];
|
return $this->instantiated[$class];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,9 +55,7 @@ class ServiceLocator
|
|||||||
throw new \OutOfRangeException('Too many arguments given');
|
throw new \OutOfRangeException('Too many arguments given');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->shared[$class]) {
|
|
||||||
$this->instantiated[$class] = $object;
|
$this->instantiated[$class] = $object;
|
||||||
}
|
|
||||||
|
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 30 KiB |
Reference in New Issue
Block a user