Last active
December 16, 2015 18:19
-
-
Save potfur/5476498 to your computer and use it in GitHub Desktop.
SIC - Simple [Dependency] Injection Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace sic; | |
/** | |
* Simple [Dependency] Injection Container | |
* | |
* @author Michal Wachowski <[email protected]> | |
* @license New BSD License | |
*/ | |
class SIC { | |
protected $definitions = array(); | |
protected $instances = array(); | |
/** | |
* Constructor | |
* | |
* @param array $definitions array containing definitions | |
*/ | |
public function __construct($definitions = array()) { | |
if($definitions === array()) { | |
return; | |
} | |
foreach($definitions as $name => $definition) { | |
$this->set($name, $definition); | |
} | |
} | |
/** | |
* Returns defined object instance or value from container | |
* If instance is shared - returns it by reference | |
* | |
* @param string $name name of retrieved definition | |
* | |
* @return mixed | |
* @throws \InvalidArgumentException | |
*/ | |
public function & get($name) { | |
if(!$this->exists($name)) { | |
throw new \InvalidArgumentException(sprintf('Definition for %s not found', $name)); | |
} | |
$result = null; | |
if($this->shared($name) && $this->instances[$name] !== null) { | |
return $this->instances[$name]; | |
} | |
$result = is_callable($this->definitions[$name]) ? $this->definitions[$name]($this) : $this->definitions[$name]; | |
if($this->shared($name)) { | |
$this->instances[$name] = $result; | |
} | |
return $result; | |
} | |
/** | |
* Adds definition to container | |
* If definition under name exists - will be overwritten | |
* | |
* @param string $name name for further retrieval | |
* @param callable $definition definition, callable or value | |
* @param bool $shared if true - first retrieval is stored, and all following will return stored value | |
* | |
* @return $this | |
*/ | |
public function set($name, $definition, $shared = false) { | |
$this->drop($name); | |
if(is_callable($definition) && $shared === true) { | |
$this->instances[$name] = null; | |
} | |
$this->definitions[$name] = $definition; | |
return $this; | |
} | |
/** | |
* Remove definition from container | |
* | |
* @param string $name dropped definition name | |
* | |
* @return $this | |
*/ | |
public function drop($name) { | |
if($this->exists($name)) { | |
unset($this->definitions[$name]); | |
} | |
if($this->shared($name)) { | |
unset($this->instances[$name]); | |
} | |
return $this; | |
} | |
/** | |
* Returns true if definition exists | |
* | |
* @param string $name checked definition name | |
* | |
* @return bool | |
*/ | |
public function exists($name) { | |
return array_key_exists($name, $this->definitions); | |
} | |
/** | |
* Returns true if definition is shared | |
* | |
* @param string $name | |
* | |
* @return bool | |
*/ | |
public function shared($name) { | |
return array_key_exists($name, $this->instances); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment