mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 22:57:40 +08:00 
			
		
		
		
	Merge pull request #15347 from KoJIT2009/di-support-instance-by-property-and-setters
di-support-instance-for-property
This commit is contained in:
		@ -34,6 +34,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
- Enh #15221: Improved the `help/list-action-options` console command output for command options without a description (brandonkelly)
 | 
					- Enh #15221: Improved the `help/list-action-options` console command output for command options without a description (brandonkelly)
 | 
				
			||||||
- Enh #15332: Always check for availability of `openssl_pseudo_random_bytes`, even if LibreSSL is available (sammousa)
 | 
					- Enh #15332: Always check for availability of `openssl_pseudo_random_bytes`, even if LibreSSL is available (sammousa)
 | 
				
			||||||
- Enh #15335: Added `FileHelper::unlink()` that works well under all OSes (samdark)
 | 
					- Enh #15335: Added `FileHelper::unlink()` that works well under all OSes (samdark)
 | 
				
			||||||
 | 
					- Enh #15347: Add `Instance` support for object property in DI container (kojit2009)
 | 
				
			||||||
- Enh #15340: Test CHANGELOG.md for valid format (sammousa)
 | 
					- Enh #15340: Test CHANGELOG.md for valid format (sammousa)
 | 
				
			||||||
- Enh #15360: Refactored `BaseConsole::updateProgress()` (developeruz)
 | 
					- Enh #15360: Refactored `BaseConsole::updateProgress()` (developeruz)
 | 
				
			||||||
- Bug #15317: Regenerate CSRF token if an empty value is given (sammousa)
 | 
					- Bug #15317: Regenerate CSRF token if an empty value is given (sammousa)
 | 
				
			||||||
 | 
				
			|||||||
@ -375,6 +375,8 @@ class Container extends Component
 | 
				
			|||||||
            return $reflection->newInstanceArgs($dependencies);
 | 
					            return $reflection->newInstanceArgs($dependencies);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $config = $this->resolveDependencies($config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!empty($dependencies) && $reflection->implementsInterface('yii\base\Configurable')) {
 | 
					        if (!empty($dependencies) && $reflection->implementsInterface('yii\base\Configurable')) {
 | 
				
			||||||
            // set $config as the last parameter (existing one will be overwritten)
 | 
					            // set $config as the last parameter (existing one will be overwritten)
 | 
				
			||||||
            $dependencies[count($dependencies) - 1] = $config;
 | 
					            $dependencies[count($dependencies) - 1] = $config;
 | 
				
			||||||
 | 
				
			|||||||
@ -15,7 +15,9 @@ use yiiunit\data\ar\Cat;
 | 
				
			|||||||
use yiiunit\data\ar\Order;
 | 
					use yiiunit\data\ar\Order;
 | 
				
			||||||
use yiiunit\data\ar\Type;
 | 
					use yiiunit\data\ar\Type;
 | 
				
			||||||
use yiiunit\framework\di\stubs\Bar;
 | 
					use yiiunit\framework\di\stubs\Bar;
 | 
				
			||||||
 | 
					use yiiunit\framework\di\stubs\BarSetter;
 | 
				
			||||||
use yiiunit\framework\di\stubs\Foo;
 | 
					use yiiunit\framework\di\stubs\Foo;
 | 
				
			||||||
 | 
					use yiiunit\framework\di\stubs\FooProperty;
 | 
				
			||||||
use yiiunit\framework\di\stubs\Qux;
 | 
					use yiiunit\framework\di\stubs\Qux;
 | 
				
			||||||
use yiiunit\framework\di\stubs\QuxInterface;
 | 
					use yiiunit\framework\di\stubs\QuxInterface;
 | 
				
			||||||
use yiiunit\TestCase;
 | 
					use yiiunit\TestCase;
 | 
				
			||||||
@ -95,6 +97,19 @@ class ContainerTest extends TestCase
 | 
				
			|||||||
        $this->assertInstanceOf($Bar, $foo->bar);
 | 
					        $this->assertInstanceOf($Bar, $foo->bar);
 | 
				
			||||||
        $this->assertInstanceOf($Qux, $foo->bar->qux);
 | 
					        $this->assertInstanceOf($Qux, $foo->bar->qux);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // predefined property parameters
 | 
				
			||||||
 | 
					        $fooSetter = FooProperty::className();
 | 
				
			||||||
 | 
					        $barSetter = BarSetter::className();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $container = new Container();
 | 
				
			||||||
 | 
					        $container->set('foo', ['class' => $fooSetter, 'bar' => Instance::of('bar')]);
 | 
				
			||||||
 | 
					        $container->set('bar', ['class' => $barSetter, 'qux' => Instance::of('qux')]);
 | 
				
			||||||
 | 
					        $container->set('qux', $Qux);
 | 
				
			||||||
 | 
					        $foo = $container->get('foo');
 | 
				
			||||||
 | 
					        $this->assertInstanceOf($fooSetter, $foo);
 | 
				
			||||||
 | 
					        $this->assertInstanceOf($barSetter, $foo->bar);
 | 
				
			||||||
 | 
					        $this->assertInstanceOf($Qux, $foo->bar->qux);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // wiring by closure
 | 
					        // wiring by closure
 | 
				
			||||||
        $container = new Container();
 | 
					        $container = new Container();
 | 
				
			||||||
        $container->set('qux', new Qux());
 | 
					        $container->set('qux', new Qux());
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										40
									
								
								tests/framework/di/stubs/BarSetter.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								tests/framework/di/stubs/BarSetter.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @link      http://www.yiiframework.com/
 | 
				
			||||||
 | 
					 * @copyright Copyright (c) 2008 Yii Software LLC
 | 
				
			||||||
 | 
					 * @license   http://www.yiiframework.com/license/
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace yiiunit\framework\di\stubs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use yii\base\BaseObject;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author Qiang Xue <qiang.xue@gmail.com>
 | 
				
			||||||
 | 
					 * @since  2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @property QuxInterface $qux
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class BarSetter extends BaseObject
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var QuxInterface
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private $qux;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @return QuxInterface
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function getQux()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return $this->qux;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @param mixed $qux
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function setQux(QuxInterface $qux)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->qux = $qux;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										24
									
								
								tests/framework/di/stubs/FooProperty.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								tests/framework/di/stubs/FooProperty.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @link      http://www.yiiframework.com/
 | 
				
			||||||
 | 
					 * @copyright Copyright (c) 2008 Yii Software LLC
 | 
				
			||||||
 | 
					 * @license   http://www.yiiframework.com/license/
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace yiiunit\framework\di\stubs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use yii\base\BaseObject;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author Qiang Xue <qiang.xue@gmail.com>
 | 
				
			||||||
 | 
					 * @since  2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @property BarSetter $bar
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class FooProperty extends BaseObject
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var BarSetter
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public $bar;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user