set() public method

For example, php a class name $locator->set('cache', 'yii\caching\FileCache'); a configuration array $locator->set('db', [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ]); an anonymous function $locator->set('cache', function ($params) { return new \yii\caching\FileCache; }); an instance $locator->set('cache', new \yii\caching\FileCache); If a component definition with the same ID already exists, it will be overwritten.
public set ( string $id, mixed $definition )
$id string component ID (e.g. `db`).
$definition mixed the component definition to be registered with this locator. It can be one of the following: - a class name - a configuration array: the array contains name-value pairs that will be used to initialize the property values of the newly created object when [[get()]] is called. The `class` element is required and stands for the the class of the object to be created. - a PHP callable: either an anonymous function or an array representing a class method (e.g. `['Foo', 'bar']`). The callable will be called by [[get()]] to return an object associated with the specified component ID. - an object: When [[get()]] is called, this object will be returned.
コード例 #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
 public function registerTasks($tasksConfig)
 {
     foreach ($tasksConfig as $id => $task) {
         $this->_container->set("tasks.{$id}", ['class' => Task::className(), 'id' => $id, 'closure' => $task]);
         $this->_tasks[] = $id;
     }
 }
コード例 #3
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);
 }
コード例 #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
 protected function setMigrationComponent()
 {
     $locator = new ServiceLocator();
     $controllerMap = ArrayHelper::merge(\Yii::$app->controllerMap, ['migrate' => ['class' => 'webvimark\\modules\\migrations\\components\\MigrateController']]);
     $locator->set('controllerMap', $controllerMap);
 }
コード例 #6
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']]));
 }