Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Container implements ArrayAccess {
- /**
- * Container bindings
- * @var array
- */
- protected $bindings = array();
- /**
- * Container instances
- * @var array
- */
- protected $instances = array();
- /**
- * Container Closures
- * @var array
- */
- protected $closures = array();
- /**
- * Maximum depth of function makeClassWithParameters
- * @var int
- */
- protected $maxdepth = 10;
- // ================================================== //
- /**
- * Public function to create new binding.
- *
- * @param string $key Binding name
- * @param mixed $value Object to store
- * @param bool $singleton Store as singleton?
- * @return void
- */
- public function bind($key, $value, $singleton = false)
- {
- $this->clearBind($key);
- $this->makeBind($key, $value, (bool)$singleton);
- }
- /**
- * Function to get stored object or create new to store.
- * If there's no stored object it will create new instance of
- * class which name is $key
- *
- * @param string $key Binding name or class name
- * @param array $parameters Parameters for new class
- * @param bool $singleton Store new class as singleton?
- * @return mixed
- */
- public function make($key, $parameters = array(), $singleton = false)
- {
- if($bind = $this->getBind($key))
- {
- if(isset($bind['type']) and $bind['type'] == 'closure')
- {
- $instance = $this->closures[$key];
- $instance = $instance();
- if(isset($bind['singleton']) and $bind['singleton'] == true)
- {
- $this->clearBind($key);
- $this->makeBind($key, $instance, true);
- }
- return $instance;
- }
- elseif(isset($bind['type']) and $bind['type'] == 'instance')
- {
- return $this->instances[$key];
- }
- $this->clearBind($key);
- return false;
- }
- else
- {
- $instance = $this->makeClassWithParameters($key, (array)$parameters);
- if($instance != false)
- {
- $this->bind($key, $instance);
- }
- return $instance;
- }
- }
- // ================================================== //
- /**
- * Creating new class with parameters.
- * If new class constructor require another class as parameter
- * it will be created, but max depth is limited!
- *
- * @throws OutOfBoundsException
- * @uses ReflectionClass
- *
- * @param string $name Class name
- * @param array $params Class parameters
- * @param int $depth Depth counter
- * @return mixed
- */
- protected function makeClassWithParameters($name, array $params, $depth = 1)
- {
- if($depth >= $this->maxdepth)
- {
- throw new OutOfBoundsException('Maximum depth of makeClassWithParameters is '.$this->maxdepth);
- return false;
- }
- if(class_exists($name))
- {
- $class = new ReflectionClass($name);
- $constructor = $class->getConstructor();
- if(!is_null($constructor) and empty($params))
- {
- foreach($constructor->getParameters() as $paramdata)
- {
- $classparam = $paramdata->getClass();
- if(is_null($classparam))
- {
- if($paramdata->isOptional())
- {
- $params[] = $paramdata->getDefaultValue();
- }
- else
- {
- $params[] = null;
- }
- }
- else
- {
- $params[] = $this->makeClassWithParameters($classparam->name, array(), $depth+1);
- }
- }
- }
- return $class->newInstanceArgs($params);
- }
- else
- {
- return false;
- }
- }
- /**
- * Getting bind if exist
- *
- * @return mixed
- */
- protected function getBind($key)
- {
- return isset($this->bindings[$key]) ? $this->bindings[$key] : false;
- }
- /**
- * Protected function to create new binding.
- *
- * @param string $key Binding name
- * @param mixed $value Object to store
- * @param bool $singleton Store as singleton?
- * @return void
- */
- protected function makeBind($key, $value, $singleton)
- {
- $bind = array();
- if($value instanceof Closure)
- {
- $bind['type'] = 'closure';
- $bind['singleton'] = (bool)$singleton;
- $this->closures[$key] = $value;
- }
- else
- {
- $bind['type'] = 'instance';
- $this->instances[$key] = $value;
- }
- $this->bindings[$key] = $bind;
- }
- /**
- * Clear existing binding
- *
- * @param string $key Binding name
- * @return void
- */
- protected function clearBind($key)
- {
- unset($this->bindings[$key], $this->instances[$key], $this->closures[$key]);
- }
- // ================================================== //
- /**
- * Getting container content as array
- *
- * @param string $key Binding name
- * @return mixed
- */
- public function offsetGet($key)
- {
- return $this->make($key);
- }
- /**
- * Set container content as array
- *
- * @param string $key Binding name
- * @param mixed $value Object to store
- * @return void
- */
- public function offsetSet($key, $value)
- {
- if(!($value instanceof Closure))
- {
- $value = function() use ($value) {
- return $value;
- };
- }
- $this->bind($key, $value, false);
- }
- /**
- * Checking binding as container array
- *
- * @param string $key Binding name
- * @return bool
- */
- public function offsetExists($key)
- {
- return isset($this->bindings[$key]);
- }
- /**
- * Using function unset for container as array
- *
- * @param string $key Binding name
- * @return void
- */
- public function offsetUnset($key)
- {
- $this->clearBind($key);
- }
- /**
- * Magic method __get
- *
- * @param string $key
- * @return mixed
- */
- public function __get($key)
- {
- return $this[$key];
- }
- /**
- * Magic method __set
- *
- * @param string $key
- * @param mixed $value
- * @return void
- */
- public function __set($key, $value)
- {
- $this[$key] = $value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement