Commit be8b03bc by Qiang Xue

minor refactoring. [skip ci]

parent 359ae98f
...@@ -33,27 +33,22 @@ use yii\helpers\VarDumper; ...@@ -33,27 +33,22 @@ use yii\helpers\VarDumper;
* To start using this command you need to be familiar (read guide) for the Faker library and * To start using this command you need to be familiar (read guide) for the Faker library and
* generate fixtures template files, according to the given format: * generate fixtures template files, according to the given format:
* *
* ~~~ * ```php
* #users.php file under $templatePath * // users.php file under template path (by default @tests/unit/templates/fixtures)
*
* return [ * return [
* [ * 'name' => $faker->firstName,
* 'table_column0' => 'faker_formatter', * 'phone' => $faker->phoneNumber,
* ... * 'city' => $faker->city,
* 'table_columnN' => 'other_faker_formatter * 'password' => Yii::$app->getSecurity()->generatePasswordHash('password_' . $index),
* 'table_columnN+1' => function ($fixture, $faker, $index) { * 'auth_key' => Yii::$app->getSecurity()->generateRandomString(),
* //set needed fixture fields based on different conditions * 'intro' => $faker->sentence(7, true), // generate a sentence with 7 words
* return $fixture;
* }
* ],
* ]; * ];
* ~~~ * ```
* *
* If you use callback as a attribute value, then it will be called as shown with three parameters: * If you use callback as a attribute value, then it will be called as shown with three parameters:
* *
* - `$fixture` - current fixture array. * - `$faker`: the Faker generator instance
* - `$faker` - faker generator instance * - `$index`: the current fixture index. For example if user need to generate 3 fixtures for user table, it will be 0..2.
* - `$index` - current fixture index. For example if user need to generate 3 fixtures for user table, it will be 0..2
* *
* After you set all needed fields in callback, you need to return $fixture array back from the callback. * After you set all needed fields in callback, you need to return $fixture array back from the callback.
* *
...@@ -132,8 +127,6 @@ use yii\helpers\VarDumper; ...@@ -132,8 +127,6 @@ use yii\helpers\VarDumper;
* ], * ],
* ~~~ * ~~~
* *
* @property \Faker\Generator $generator This property is read-only.
*
* @author Mark Jebri <mark.github@yandex.ru> * @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0.0 * @since 2.0.0
*/ */
...@@ -250,13 +243,10 @@ class FixtureController extends \yii\console\controllers\FixtureController ...@@ -250,13 +243,10 @@ class FixtureController extends \yii\console\controllers\FixtureController
*/ */
public function getGenerator() public function getGenerator()
{ {
if (is_null($this->_generator)) { if ($this->_generator === null) {
//replacing - on _ because Faker support only en_US format and not intl $language = $this->language === null ? Yii::$app->language : $this->language;
$this->_generator = \Faker\Factory::create(str_replace('-', '_', $language));
$language = is_null($this->language) ? str_replace('-', '_', Yii::$app->language) : $this->language;
$this->_generator = \Faker\Factory::create($language);
} }
return $this->_generator; return $this->_generator;
} }
...@@ -311,7 +301,7 @@ class FixtureController extends \yii\console\controllers\FixtureController ...@@ -311,7 +301,7 @@ class FixtureController extends \yii\console\controllers\FixtureController
public function generateFixture($_template_, $index) public function generateFixture($_template_, $index)
{ {
// $faker and $index are exposed to the template file // $faker and $index are exposed to the template file
$faker = $this->generator; $faker = $this->getGenerator();
return require($_template_); return require($_template_);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment