From ad419057caa29656da79462d95e455fcb213c5e8 Mon Sep 17 00:00:00 2001 From: zafex Date: Sun, 24 Mar 2019 23:25:02 +0700 Subject: [PATCH] singleton is using factory --- src/Container.php | 29 ----------------------------- src/ContainerAwareTrait.php | 8 +------- src/ContainerFactory.php | 13 ++++++++++--- src/Contracts/Container.php | 7 ------- 4 files changed, 11 insertions(+), 46 deletions(-) diff --git a/src/Container.php b/src/Container.php index f575342..525bff9 100644 --- a/src/Container.php +++ b/src/Container.php @@ -24,19 +24,6 @@ class Container implements IContainer */ protected $definitions = []; - /** - * @var mixed - */ - protected static $instance = null; - - /** - * @param array $definitions - */ - public function __construct() - { - static::setInstance($this); - } - /** * @param $id * @return mixed @@ -71,14 +58,6 @@ public function get($id) return $this->components[$id]; } - public static function getInstance(): IContainer - { - if (!(static::$instance instanceof IContainer)) { - static::$instance = new static; - } - return static::$instance; - } - /** * @param $id */ @@ -211,14 +190,6 @@ public function set($id, $argument) } } - /** - * @param IContainer $container - */ - public static function setInstance(IContainer $container) - { - static::$instance = $container; - } - /** * @param ReflectionFunctionAbstract $function * @param array $params diff --git a/src/ContainerAwareTrait.php b/src/ContainerAwareTrait.php index 95fb7fa..89a63fd 100644 --- a/src/ContainerAwareTrait.php +++ b/src/ContainerAwareTrait.php @@ -2,7 +2,6 @@ namespace Viloveul\Container; -use Viloveul\Container\ContainerFactory; use Viloveul\Container\Contracts\Container as IContainer; trait ContainerAwareTrait @@ -17,12 +16,7 @@ trait ContainerAwareTrait */ public function getContainer(): IContainer { - if ($this->container instanceof IContainer) { - return $this->container; - } else { - $this->container = ContainerFactory::instance(); - return $this->container; - } + return $this->container; } /** diff --git a/src/ContainerFactory.php b/src/ContainerFactory.php index 34a710e..9544b0e 100644 --- a/src/ContainerFactory.php +++ b/src/ContainerFactory.php @@ -8,16 +8,23 @@ class ContainerFactory implements IContainerFactory { + /** + * @var mixed + */ + protected static $container; + /** * @param array $definitions * @return mixed */ public static function instance(array $definitions = []): IContainer { - $container = Container::getInstance(); + if (!(static::$container instanceof IContainer)) { + static::$container = new Container(); + } foreach ($definitions as $key => $value) { - $container->set($key, $value); + static::$container->set($key, $value); } - return $container; + return static::$container; } } diff --git a/src/Contracts/Container.php b/src/Contracts/Container.php index 95f34a0..cefa921 100644 --- a/src/Contracts/Container.php +++ b/src/Contracts/Container.php @@ -11,8 +11,6 @@ interface Container extends ContainerInterface */ public function __get($id); - public static function getInstance(): self; - /** * @param $callback * @param array $params @@ -43,9 +41,4 @@ public function raw($id); * @param array $params */ public function remap($id, $target, array $params = []); - - /** - * @param self $instance - */ - public static function setInstance(self $instance); }