/** * Creates context object if context config passed to `$context` param. * @param mixed $context the context object or config for creating it. * @param string $name the name of context. * @return Context instantiated instance of Context. * @throws InvalidConfigException if context has invalid config. */ protected function instantiateContext($context, $name) { if (!$context instanceof Context) { if (is_array($context)) { $defaultContext = is_array($this->defaultContext) ? $this->defaultContext : ['class' => $this->defaultContext]; $context = ArrayHelper::merge($defaultContext, $context); } $context = Yii::createObject($context); if (!$context instanceof Context) { throw new InvalidConfigException("Context '{$name}' has invalid config. It must be an instance of " . Context::className() . ' or a config for creating it.'); } if ($context->name === null) { $context->name = $name; } } return $context; }
/** * Changes structure of base class. * @see [[_changeStructure()]] * * Checks required property `$context`. * @throws InvalidConfigException if required property `$context` is missed or is incorrect. */ public function init() { $this->_changeStructure(); parent::init(); if (!$this->context instanceof Context) { throw new InvalidConfigException('An instance of ' . Context::className() . ' is required for ' . get_class($this) . '.'); } }