get() public method

You may provide constructor parameters ($params) and object configurations ($config) that will be used during the creation of the instance. If the class implements [[\yii\base\Configurable]], the $config parameter will be passed as the last parameter to the class constructor; Otherwise, the configuration will be applied *after* the object is instantiated. Note that if the class is declared to be singleton by calling Container::setSingleton, the same instance of the class will be returned each time this method is called. In this case, the constructor parameters and object configurations will be used only if the class is instantiated the first time.
public get ( string $class, array $params = [], array $config = [] ) : object
$class string the class name or an alias name (e.g. `foo`) that was previously registered via [[set()]] or [[setSingleton()]].
$params array a list of constructor parameter values. The parameters should be provided in the order they appear in the constructor declaration. If you want to skip some parameters, you should index the remaining ones with the integers that represent their positions in the constructor parameter list.
$config array a list of name-value pairs that will be used to initialize the object properties.
return object an instance of the requested class.
示例#1
0
 /**
  * @param $from
  * @param $to
  * @param $data
  * @return mixed
  */
 public function convert($from, $to, $data)
 {
     $executor = new Executor();
     $decoder = $this->container->get($from);
     $encoder = $this->container->get($to);
     $executor->setDecodeService($decoder);
     $executor->setEncodeService($encoder);
     return $executor->transform($data);
 }
示例#2
0
 /**
  * @return Container
  */
 private static function getIoc()
 {
     $diConfig = (include 'di.php');
     $container = new Container();
     $container->setSingleton('workerMapper', $diConfig['workerMapper']);
     $container->setSingleton('entityfx\\utils\\workers\\contracts\\repositories\\WorkerRepositoryInterface', function ($container, $params, $config) use($diConfig) {
         return $container->get($diConfig['workerRepository'], [$container->get('workerMapper')]);
     });
     $container->set('entityfx\\utils\\workers\\contracts\\WorkerInterceptorInterface', function ($container, $params, $config) use($diConfig) {
         return $container->get($diConfig['workerInterceptor'], [$params['worker'], $container->get('entityfx\\utils\\workers\\contracts\\repositories\\WorkerRepositoryInterface')]);
     });
     return $container;
 }
示例#3
0
 /**
  * Generate fake data and
  */
 public function actionGenerate()
 {
     $input = $this->parseArguments(func_get_args());
     $container = new Container();
     $container->set(GeneratorInterface::class, array_merge(['class' => $this->generator_fqn], $input['generator']));
     $container->set(DbProviderInterface::class, array_merge(['class' => $this->dbprovider_fqn], $input['dbprovider']));
     $this->generator_obj = $container->get(GeneratorInterface::class);
     if (!$this->force && !$this->confirmGeneration()) {
         return;
     }
     $this->dbprovider_obj = $container->get(DbProviderInterface::class);
     Console::startProgress(0, $this->count);
     foreach ($this->dbprovider_obj->export($this->count) as $count) {
         Console::updateProgress($this->count - $count, $this->count);
     }
     Console::endProgress(true);
 }
示例#4
0
 protected function initEventHandlers(Container $container)
 {
     $this->on(self::EVENT_OBJECT_CHANGED, function (ObjectHistoryEvent $event) use($container) {
         /** @var contracts\ObjectHistoryManagerInterface $objectHistoryManager */
         $objectHistoryManager = $container->get('entityfx\\utils\\objectHistory\\contracts\\ObjectHistoryManagerInterface');
         $objectHistoryManager->objectModified($event->historyType, $event->guid, $event->category);
     });
 }
 /**
  * @return Container
  */
 private static function getIoc()
 {
     //$diConfig = include('di.php');
     $diConfig = [];
     $container = new Container();
     $container->setSingleton('clientProxyMapper', 'app\\utils\\webService\\implementation\\clientProxies\\mapper\\ClientProxyMapper');
     $container->setSingleton('app\\utils\\webService\\contracts\\clientProxies\\repositories\\WebClientProxyRepositoryInterface', function ($container, $params, $config) use($diConfig) {
         return $container->get('app\\utils\\webService\\implementation\\clientProxies\\repositories\\WebClientProxyRepository', [$container->get('clientProxyMapper')]);
     });
     /*$container->set('app\utils\workers\contracts\WorkerInterceptorInterface',
       function($container, $params, $config) use ($diConfig) {
           return  $container->get(
               $diConfig['workerInterceptor'],
               [$params['worker'], $container->get('app\utils\workers\contracts\repositories\WorkerRepositoryInterface')]
           );
       });*/
     return $container;
 }
示例#6
0
 public function testDefault()
 {
     $namespace = __NAMESPACE__ . '\\stubs';
     $QuxInterface = "{$namespace}\\QuxInterface";
     $Foo = Foo::className();
     $Bar = Bar::className();
     $Qux = Qux::className();
     // automatic wiring
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $foo = $container->get($Foo);
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // full wiring
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $container->set($Bar);
     $container->set($Qux);
     $container->set($Foo);
     $foo = $container->get($Foo);
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // wiring by closure
     $container = new Container();
     $container->set('foo', function () {
         $qux = new Qux();
         $bar = new Bar($qux);
         return new Foo($bar);
     });
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // wiring by closure which uses container
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $container->set('foo', function (Container $c, $params, $config) {
         return $c->get(Foo::className());
     });
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // predefined constructor parameters
     $container = new Container();
     $container->set('foo', $Foo, [Instance::of('bar')]);
     $container->set('bar', $Bar, [Instance::of('qux')]);
     $container->set('qux', $Qux);
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
 }
示例#7
0
 public function createUrl($params)
 {
     $params = is_string($params) ? [0 => $params] : $params;
     $languagePovider = $this->conteiner->get('languageProvider');
     $languages = $languagePovider->getLanguages();
     $receive = new ReceiveContainer();
     $receive->addReceiver(new ParamsLanguageReceive($params, $this->languageKey));
     if ($this->detectInSession) {
         $receive->addReceiver(new SessionLanguageReceive($this->sessionLanguageKey));
     }
     if ($this->detectInCookie) {
         $receive->addReceiver(new CookieLanguageReceive($this->cookieLanguageKey));
     }
     $language = $receive->getLanguage();
     unset($params[$this->languageKey]);
     if (!isset($language)) {
         $language = \Yii::$app->language;
     }
     $this->language = $language;
     //        $language = isset($language) ? $language : $this->language;
     $language = $this->lowerCase ? strtolower($language) : $language;
     $language = $this->useShortSyntax ? preg_replace('~(\\w{2})-\\w{2}~i', '$1', $language, 1) : $language;
     return $this->showDefault || strcasecmp($language, $this->defaultLanguage) != 0 ? substr_replace(parent::createUrl($params), !empty($language) ? "/{$language}" : '', strlen($this->baseUrl), 0) : parent::createUrl($params);
 }
示例#8
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 (Yii::$app && Yii::$app->has($this->id)) {
         return Yii::$app->get($this->id);
     } else {
         return Yii::$container->get($this->id);
     }
 }
 /**
  * @dataProvider dataProviderConfigurable
  *
  * @param ContainerConfiguratorTestStubYiiBaseObject|ContainerConfiguratorTestStubYiiBaseConfigurable|string $class
  *
  * @throws WrongConfigException
  */
 public function testConfigurable($class)
 {
     if (!class_exists($class, false)) {
         $this->markTestSkipped("Class '{$class}' not found, maybe yii2 old version, it's ok");
     }
     $config = ['service' => ['class' => $class, 'arguments' => [['type' => ContainerConfigurator::ARGUMENT_TYPE_VALUE, 'value' => 'argument1value'], ['type' => ContainerConfigurator::ARGUMENT_TYPE_VALUE, 'value' => 'argument2value']], 'properties' => ['property1' => ['type' => ContainerConfigurator::ARGUMENT_TYPE_VALUE, 'value' => 'property1value'], 'property2' => ['type' => ContainerConfigurator::ARGUMENT_TYPE_VALUE, 'value' => 'property2value']]]];
     $container = new Container();
     $containerConfigurator = new ContainerConfigurator($container);
     $containerConfigurator->configure($config);
     /** @var ContainerConfiguratorTestStubYiiBaseObject|ContainerConfiguratorTestStubYiiBaseConfigurable $stub */
     $stub = $container->get('service');
     $class::test($this, ['class' => $config['service']['class'], 'arguments' => ['argument1value', 'argument2value', ['property1' => 'property1value', 'property2' => 'property2value']]], $stub);
 }