123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace Cube\Di;
- class Container implements ContainerInterface
- {
-
- protected $_services = array();
-
- public function set($name, $service)
- {
- if (!is_object($service) && !is_string($service)) {
- throw new \InvalidArgumentException("Only objects or strings can be registered with the container.");
- }
- if (!in_array($service, $this->_services, true)) {
- $this->_services[$name] = $service;
- }
- return $this;
- }
-
- public function get($name, array $params = array())
- {
- if (!isset($this->_services[$name])) {
- throw new \RuntimeException(sprintf("The service '%s' has not been registered with the container.", $name));
- }
- $service = $this->_services[$name];
- return !$service instanceof \Closure ? $service : call_user_func_array($service, $params);
- }
-
- public function has($name)
- {
- return isset($this->_services[$name]);
- }
-
- public function remove($name)
- {
- if (isset($this->_services[$name])) {
- unset($this->_services[$name]);
- }
- return $this;
- }
-
- public function clear()
- {
- $this->_services = array();
- }
- }
|