get() public method

Returns the component instance with the specified ID.
See also: has()
See also: set()
public get ( string $id, boolean $throwException = true ) : object | null
$id string component ID (e.g. `db`).
$throwException boolean whether to throw an exception if `$id` is not registered with the locator before.
return object | null the component of the specified ID. If `$throwException` is false and `$id` is not registered before, null will be returned.
コード例 #1
0
 protected function runTasks()
 {
     foreach ($this->_tasks as $k => $id) {
         Console::output(sprintf('Running task "%s" (%d/%d)', $id, $k + 1, count($this->_tasks)));
         $result = $this->_container->get("tasks.{$id}")->run($this->_container, $this);
         if ($result === false) {
             Console::error(sprintf('Task "%s" failed.', $id));
             return false;
         }
     }
     return true;
 }
コード例 #2
0
ファイル: ServiceLocatorTest.php プロジェクト: howq/yii2
 public function testShared()
 {
     // with configuration: shared
     $container = new ServiceLocator();
     $className = TestClass::className();
     $container->set($className, ['class' => $className, 'prop1' => 10, 'prop2' => 20]);
     $object = $container->get($className);
     $this->assertEquals(10, $object->prop1);
     $this->assertEquals(20, $object->prop2);
     $this->assertTrue($object instanceof $className);
     // check shared
     $object2 = $container->get($className);
     $this->assertTrue($object2 instanceof $className);
     $this->assertTrue($object === $object2);
 }
コード例 #3
0
 /**
  * Get twocheckout return class instance
  *
  * @access public
  * @return \yii\twocheckout\TwoCheckoutReturn
  */
 public function getReturn()
 {
     if (!$this->locator->has('return')) {
         $this->locator->set('return', '\\yii\\twocheckout\\TwoCheckoutReturn');
     }
     return $this->locator->get('return');
 }
コード例 #4
0
 /**
  * This method is used to add conditions to query
  *
  * @param string $attribute
  * @param string|array $value
  * @return $this
  */
 public function setAttribute($attribute, $value)
 {
     $query = $this->getQueryInstance();
     if ($this->locator->has($attribute)) {
         /** @var QueryHandlerInterface $specialHandler */
         $specialHandler = $this->locator->get($attribute);
         list($query->query, $query->filter, $query->aggregations) = $specialHandler->handle(['query' => $query->query, 'filter' => $query->filter, 'aggregations' => $query->aggregations, 'value' => $value]);
     } elseif ($this->isValidAttribute($attribute)) {
         $filter = new Term([$attribute => $value]);
         $query->filter->addMust($filter);
     }
     return $this;
 }
コード例 #5
0
ファイル: Instance.php プロジェクト: phaniapsr/yiicomm
 /**
  * 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);
     }
 }
コード例 #6
0
ファイル: Module.php プロジェクト: anmoroz/yii2-analytics
 private function initElastica()
 {
     $configurator = $this->locator->get('configurator');
     $this->locator->set('elastica', new \anmoroz\analytics\components\ElasticaBase($this->elasticSearch, $configurator->getIndexName(), $configurator->getTypeName()));
 }
コード例 #7
0
 /**
  * Add ViewHelperComponent to service locator for view script
  *
  * @param array $aggregationList
  * @throws \yii\base\InvalidConfigException
  */
 private function viewHelperToLocator(array $aggregationList)
 {
     $this->locator->set('viewHelper', \Yii::createObject(['class' => ViewHelperComponent::className(), 'aggregationList' => $aggregationList, 'arrayIterator' => $this->arrayIterator, 'configuratorProperties' => $this->locator->get('configurator')->getProperties(), 'db' => $this->locator->get('db'), 'groupby' => $this->options['group']['by']]));
 }