To use ServiceLocator, you first need to register component IDs with the corresponding component definitions with the locator by calling ServiceLocator::set or ServiceLocator::setComponents. You can then call ServiceLocator::get to retrieve a component with the specified ID. The locator will automatically instantiate and configure the component according to the definition. For example, php $locator = new \yii\di\ServiceLocator; $locator->setComponents([ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'sqlite:path/to/file.db', ], 'cache' => [ 'class' => 'yii\caching\DbCache', 'db' => 'db', ], ]); $db = $locator->get('db'); // or $locator->db $cache = $locator->get('cache'); // or $locator->cache Because Module extends from ServiceLocator, modules and the application are all service locators. For more details and usage information on ServiceLocator, see the guide article on service locators.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Component
コード例 #1
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');
 }
コード例 #2
0
ファイル: Module.php プロジェクト: anmoroz/yii2-analytics
 private function initConfigurator()
 {
     if (!class_exists($this->configClass)) {
         throw new ErrorException('Analytics module is unable to find Configurator class. Please, set correct {configClass} variable');
     }
     $configurationComponent = new $this->configClass();
     if (!$configurationComponent instanceof \anmoroz\analytics\components\AbstractConfigurator) {
         throw new ErrorException('DB adapter name is invalid. Please, set correct {dbAdapterName} variable');
     }
     $this->locator->set('configurator', $configurationComponent);
 }
コード例 #3
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;
 }
コード例 #4
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);
 }
コード例 #5
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;
 }
コード例 #6
0
ファイル: Module.php プロジェクト: Abbas-Hashemian/yii2
 /**
  * Constructor.
  * @param string $id the ID of this module.
  * @param Module $parent the parent module (if any).
  * @param array $config name-value pairs that will be used to initialize the object properties.
  */
 public function __construct($id, $parent = null, $config = [])
 {
     $this->id = $id;
     $this->module = $parent;
     parent::__construct($config);
 }
コード例 #7
0
ファイル: Rhoone.php プロジェクト: rhoone/yii2-rhoone
 public function __construct($config = array())
 {
     parent::__construct($config);
     $count = $this->registerComponents();
     Yii::info("{$count} rhoone component(s) registered.", __METHOD__);
 }
コード例 #8
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);
     }
 }
コード例 #9
0
 protected function setMigrationComponent()
 {
     $locator = new ServiceLocator();
     $controllerMap = ArrayHelper::merge(\Yii::$app->controllerMap, ['migrate' => ['class' => 'webvimark\\modules\\migrations\\components\\MigrateController']]);
     $locator->set('controllerMap', $controllerMap);
 }
コード例 #10
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $count = $this->load();
     Yii::info("{$count} rhoone extension(s) loaded.", __METHOD__);
 }
コード例 #11
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']]));
 }