Skip to content

Commit

Permalink
TARC-885: allow using ContainerInterface instance as sifo container i…
Browse files Browse the repository at this point in the history
…n DependencyInjector class
  • Loading branch information
kpicaza committed Jul 16, 2020
1 parent 11fd9f0 commit a41dd29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Sifo/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function execute( $instance_name, $controller_name = null, $contai
self::$root = ROOT_PATH;
self::$application = dirname( __FILE__ );
self::$instance = $instance_name;
self::$container = $container ?? DependencyInjector::getInstance();
self::$container = DependencyInjector::getInstance(null, $container);

Benchmark::getInstance()->timingStart();

Expand Down
17 changes: 12 additions & 5 deletions src/Sifo/DependencyInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ class DependencyInjector implements ContainerInterface
* @var array
*/
protected $service_definitions;
/** @var null|ContainerInterface */
private $container;

/**
* Private constructor, use getInstance() instead to get an instance.
*/
private function __construct()
private function __construct($container = null)
{
$this->container = $container;
}

/**
Expand All @@ -70,17 +73,17 @@ private function __clone()
*
* @static
* @param null $instance_name
* @param null|ContainerInterface $container
* @return ContainerInterface|DependencyInjector Dependency injector instance.
*/
public static function getInstance($instance_name = null)
public static function getInstance($instance_name = null, $container = null)
{
if (null == $instance_name)
{
if (null == $instance_name) {
$instance_name = Bootstrap::$instance;
}

if (!isset(self::$instance[$instance_name])) {
self::$instance[$instance_name] = new self;
self::$instance[$instance_name] = new self($container);
}

return self::$instance[$instance_name];
Expand All @@ -96,6 +99,10 @@ public static function getInstance($instance_name = null)
*/
public function get($service_key, $get_private_service = false)
{
if (null !== $this->container) {
return $this->container->get($service_key);
}

if (!$this->service_definitions) {
$this->loadServiceDefinitions();
}
Expand Down

0 comments on commit a41dd29

Please sign in to comment.