Example #1
0
 public function init()
 {
     if (!$this->root || !file_exists($this->root)) {
         throw new InvalidParamException("The param: 'root' is invalid");
     }
     $this->services = array_merge($this->defaultServices(), $this->services);
     Container::$app = $this;
     Container::$instance = new Container();
 }
Example #2
0
/**
 * Shortcut helper function to create object via Object Configuration.
 *
 * @param $type
 * @param array $params
 * @return mixed
 * @throws InvalidConfigException
 */
function make($type, $params = [])
{
    if (!Container::$instance) {
        Container::$instance = new Container();
    }
    if (is_string($type)) {
        return Container::$instance->get($type, $params);
    } elseif (is_array($type) && isset($type['class'])) {
        $class = $type['class'];
        unset($type['class']);
        return Container::$instance->get($class, $params, $type);
    } elseif (is_callable($type, true)) {
        return call_user_func($type, $params);
    } elseif (is_array($type)) {
        throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
    } else {
        throw new InvalidConfigException("Unsupported configuration type: " . gettype($type));
    }
}
Example #3
0
 /**
  * Returns the actual object referenced by this Instance object.
  * @param ServiceLocator|Container $container the container used to locate the referenced object.
  * If null, the method will first try `Yii::$app` then `Yii::$container`.
  * @return object the actual object referenced by this Instance object.
  */
 public function get($container = null)
 {
     if ($container) {
         return $container->get($this->id);
     }
     if (Container::$app && Container::$app->has($this->id)) {
         return Container::$app->get($this->id);
     } else {
         return Container::$instance->get($this->id);
     }
 }