mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-10-31 18:47:33 +08:00 
			
		
		
		
	Fix #19853: Added support for default value for \yii\helpers\Console::select()
				
					
				
			This commit is contained in:
		| @ -6,6 +6,7 @@ Yii Framework 2 Change Log | |||||||
|  |  | ||||||
| - Bug #18859: Fix `yii\web\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley) | - Bug #18859: Fix `yii\web\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley) | ||||||
| - Enh #19841: Allow jQuery 3.7 to be installed (wouter90) | - Enh #19841: Allow jQuery 3.7 to be installed (wouter90) | ||||||
|  | - Enh #19853: Added support for default value for `\yii\helpers\Console::select()` (rhertogh) | ||||||
|  |  | ||||||
|  |  | ||||||
| 2.0.48.1 May 24, 2023 | 2.0.48.1 May 24, 2023 | ||||||
|  | |||||||
| @ -52,6 +52,17 @@ version B between A and C, you need to follow the instructions | |||||||
| for both A and B. | for both A and B. | ||||||
|  |  | ||||||
|  |  | ||||||
|  | Upgrade from Yii 2.0.48 | ||||||
|  | ----------------------- | ||||||
|  |  | ||||||
|  | * Since Yii 2.0.49 the `yii\console\Controller::select()` function supports a default value and respects | ||||||
|  |   the `yii\console\Controller::$interactive` setting. Before the user was always prompted to select an option | ||||||
|  |   regardless of the `$interactive` setting. Now the `$default` value is automatically returned when `$interactive` is  | ||||||
|  |   `false`. | ||||||
|  | * The function signature for `yii\console\Controller::select()` and `yii\helpers\BaseConsole::select()` have changed. | ||||||
|  |   They now have an additional `$default = null` parameter. In case those methods are overwritten you will need to | ||||||
|  |   update your child classes accordingly. | ||||||
|  |  | ||||||
| Upgrade from Yii 2.0.46 | Upgrade from Yii 2.0.46 | ||||||
| ----------------------- | ----------------------- | ||||||
|  |  | ||||||
|  | |||||||
| @ -400,12 +400,19 @@ class Controller extends \yii\base\Controller | |||||||
|      * |      * | ||||||
|      * @param string $prompt the prompt message |      * @param string $prompt the prompt message | ||||||
|      * @param array $options Key-value array of options to choose from |      * @param array $options Key-value array of options to choose from | ||||||
|  |      * @param string|null $default value to use when the user doesn't provide an option. | ||||||
|  |      * If the default is `null`, the user is required to select an option. | ||||||
|      * |      * | ||||||
|      * @return string An option character the user chose |      * @return string An option character the user chose | ||||||
|  |      * @since 2.0.49 Added the $default argument | ||||||
|      */ |      */ | ||||||
|     public function select($prompt, $options = []) |     public function select($prompt, $options = [], $default = null) | ||||||
|     { |     { | ||||||
|         return Console::select($prompt, $options); |         if ($this->interactive) { | ||||||
|  |             return Console::select($prompt, $options, $default); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return $default; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -948,13 +948,17 @@ class BaseConsole | |||||||
|      * @param string $prompt the prompt message |      * @param string $prompt the prompt message | ||||||
|      * @param array $options Key-value array of options to choose from. Key is what is inputed and used, value is |      * @param array $options Key-value array of options to choose from. Key is what is inputed and used, value is | ||||||
|      * what's displayed to end user by help command. |      * what's displayed to end user by help command. | ||||||
|  |      * @param string|null $default value to use when the user doesn't provide an option. | ||||||
|  |      * If the default is `null`, the user is required to select an option. | ||||||
|      * |      * | ||||||
|      * @return string An option character the user chose |      * @return string An option character the user chose | ||||||
|  |      * @since 2.0.49 Added the $default argument | ||||||
|      */ |      */ | ||||||
|     public static function select($prompt, $options = []) |     public static function select($prompt, $options = [], $default = null) | ||||||
|     { |     { | ||||||
|         top: |         top: | ||||||
|         static::stdout("$prompt [" . implode(',', array_keys($options)) . ',?]: '); |         static::stdout("$prompt (" . implode(',', array_keys($options)) . ',?)' | ||||||
|  |             . ($default !== null ? '[' . $default . ']' : '') . ': '); | ||||||
|         $input = static::stdin(); |         $input = static::stdin(); | ||||||
|         if ($input === '?') { |         if ($input === '?') { | ||||||
|             foreach ($options as $key => $value) { |             foreach ($options as $key => $value) { | ||||||
| @ -962,6 +966,8 @@ class BaseConsole | |||||||
|             } |             } | ||||||
|             static::output(' ? - Show help'); |             static::output(' ? - Show help'); | ||||||
|             goto top; |             goto top; | ||||||
|  |         } elseif ($default !== null && $input === '') { | ||||||
|  |             return $default; | ||||||
|         } elseif (!array_key_exists($input, $options)) { |         } elseif (!array_key_exists($input, $options)) { | ||||||
|             goto top; |             goto top; | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -216,27 +216,6 @@ class ConsoleTest extends TestCase | |||||||
|         $expectedHtml =  "Error message. Here are some chars: < >\nError message. Here are even more chars: \"\""; |         $expectedHtml =  "Error message. Here are some chars: < >\nError message. Here are even more chars: \"\""; | ||||||
|         $this->assertEqualsWithoutLE($expectedHtml, Console::errorSummary($model, $options)); |         $this->assertEqualsWithoutLE($expectedHtml, Console::errorSummary($model, $options)); | ||||||
|     } |     } | ||||||
| } |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * @property string name |  | ||||||
|  * @property array types |  | ||||||
|  * @property string description |  | ||||||
|  */ |  | ||||||
| class TestConsoleModel extends DynamicModel |  | ||||||
| { |  | ||||||
|     public function rules() |  | ||||||
|     { |  | ||||||
|         return [ |  | ||||||
|             ['name', 'required'], |  | ||||||
|             ['name', 'string', 'max' => 100] |  | ||||||
|         ]; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public function init() |  | ||||||
|     { |  | ||||||
|         $this->defineAttribute('name'); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @covers \yii\helpers\BaseConsole::input() |      * @covers \yii\helpers\BaseConsole::input() | ||||||
| @ -420,20 +399,20 @@ class TestConsoleModel extends DynamicModel | |||||||
|  |  | ||||||
|         $this->sendInput('c'); |         $this->sendInput('c'); | ||||||
|         $result = ConsoleStub::select('Usual behavior', $options); |         $result = ConsoleStub::select('Usual behavior', $options); | ||||||
|         $this->assertEquals('Usual behavior [c,d,m,?]: ', $this->readOutput()); |         $this->assertEquals('Usual behavior (c,d,m,?): ', $this->readOutput()); | ||||||
|         $this->assertEquals('c', $result); |         $this->assertEquals('c', $result); | ||||||
|         $this->truncateStreams(); |         $this->truncateStreams(); | ||||||
|  |  | ||||||
|         $this->sendInput('x', 'd'); |         $this->sendInput('x', 'd'); | ||||||
|         $result = ConsoleStub::select('Wrong character', $options); |         $result = ConsoleStub::select('Wrong character', $options); | ||||||
|         $this->assertEquals('Wrong character [c,d,m,?]: Wrong character [c,d,m,?]: ', $this->readOutput()); |         $this->assertEquals('Wrong character (c,d,m,?): Wrong character (c,d,m,?): ', $this->readOutput()); | ||||||
|         $this->assertEquals('d', $result); |         $this->assertEquals('d', $result); | ||||||
|         $this->truncateStreams(); |         $this->truncateStreams(); | ||||||
|  |  | ||||||
|         $this->sendInput('?', 'm'); |         $this->sendInput('?', 'm'); | ||||||
|         $result = ConsoleStub::select('Using help', $options); |         $result = ConsoleStub::select('Using help', $options); | ||||||
|         $this->assertEquals( |         $this->assertEquals( | ||||||
|             'Using help [c,d,m,?]: ' |             'Using help (c,d,m,?): ' | ||||||
|             . ' c - cat' |             . ' c - cat' | ||||||
|             . PHP_EOL |             . PHP_EOL | ||||||
|             . ' d - dog' |             . ' d - dog' | ||||||
| @ -442,9 +421,41 @@ class TestConsoleModel extends DynamicModel | |||||||
|             . PHP_EOL |             . PHP_EOL | ||||||
|             . ' ? - Show help' |             . ' ? - Show help' | ||||||
|             . PHP_EOL |             . PHP_EOL | ||||||
|                 . 'Using help [c,d,m,?]: ', |             . 'Using help (c,d,m,?): ', | ||||||
|             $this->readOutput() |             $this->readOutput() | ||||||
|         ); |         ); | ||||||
|         $this->truncateStreams(); |         $this->truncateStreams(); | ||||||
|  |  | ||||||
|  |         $this->sendInput(''); | ||||||
|  |         $result = ConsoleStub::select('Use Default', $options, 'm'); | ||||||
|  |         $this->assertEquals('m', $result); | ||||||
|  |         $this->truncateStreams(); | ||||||
|  |  | ||||||
|  |         $this->sendInput('', 'd'); | ||||||
|  |         $result = ConsoleStub::select('Empty without Default', $options); | ||||||
|  |         $this->assertEquals('Empty without Default (c,d,m,?): Empty without Default (c,d,m,?): ', $this->readOutput()); | ||||||
|  |         $this->assertEquals('d', $result); | ||||||
|  |         $this->truncateStreams(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @property string name | ||||||
|  |  * @property array types | ||||||
|  |  * @property string description | ||||||
|  |  */ | ||||||
|  | class TestConsoleModel extends DynamicModel | ||||||
|  | { | ||||||
|  |     public function rules() | ||||||
|  |     { | ||||||
|  |         return [ | ||||||
|  |             ['name', 'required'], | ||||||
|  |             ['name', 'string', 'max' => 100] | ||||||
|  |         ]; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public function init() | ||||||
|  |     { | ||||||
|  |         $this->defineAttribute('name'); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 rhertogh
					rhertogh