Skip to content

Commit

Permalink
singleton is using factory
Browse files Browse the repository at this point in the history
  • Loading branch information
zafex committed Mar 24, 2019
1 parent 2315e2a commit ad41905
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 46 deletions.
29 changes: 0 additions & 29 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions src/ContainerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Viloveul\Container;

use Viloveul\Container\ContainerFactory;
use Viloveul\Container\Contracts\Container as IContainer;

trait ContainerAwareTrait
Expand All @@ -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;
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 0 additions & 7 deletions src/Contracts/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface Container extends ContainerInterface
*/
public function __get($id);

public static function getInstance(): self;

/**
* @param $callback
* @param array $params
Expand Down Expand Up @@ -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);
}

0 comments on commit ad41905

Please sign in to comment.