Browse Source

changed command signature, docs improved.

tags/2.0.0-beta
Mark 11 years ago
parent
commit
c5bd2702de
  1. 30
      extensions/yii/faker/FixtureController.php
  2. 22
      extensions/yii/faker/README.md

30
extensions/yii/faker/FixtureController.php

@ -23,7 +23,7 @@ use yii\helpers\Console;
* *
* ~~~ * ~~~
* 'controllerMap' => [ * 'controllerMap' => [
* 'faker' => [ * 'fixture' => [
* 'class' => 'yii\faker\FixtureController', * 'class' => 'yii\faker\FixtureController',
* ], * ],
* ], * ],
@ -57,13 +57,13 @@ use yii\helpers\Console;
* After you prepared needed templates for tables you can simply generate your fixtures via command * After you prepared needed templates for tables you can simply generate your fixtures via command
* *
* ~~~ * ~~~
* php yii faker/generate users * php yii fixture/generate users
* *
* //also a short version of this command (generate action is default) * //also a short version of this command (generate action is default)
* php yii faker users * php yii fixture users
* *
* //to generate fixtures for several tables, use "," as a separator, for example: * //to generate fixtures for several tables, use "," as a separator, for example:
* php yii faker users,profile * php yii fixture users,profile
* ~~~ * ~~~
* *
* In the code above "users" is template name, after this command run, new file named same as template * In the code above "users" is template name, after this command run, new file named same as template
@ -71,7 +71,7 @@ use yii\helpers\Console;
* You can generate fixtures for all templates by specifying keyword "all_fixtures" * You can generate fixtures for all templates by specifying keyword "all_fixtures"
* *
* ~~~ * ~~~
* php yii faker/generate all_fixtures * php yii fixture/generate all
* ~~~ * ~~~
* *
* This command will generate fixtures for all template files that are stored under $templatePath and * This command will generate fixtures for all template files that are stored under $templatePath and
@ -81,20 +81,20 @@ use yii\helpers\Console;
* all fixtures and in each file there will be 3 rows (fixtures). * all fixtures and in each file there will be 3 rows (fixtures).
* *
* ~~~ * ~~~
* php yii faker/generate all_fixtures 3 * php yii fixture/generate all 3
* ~~~ * ~~~
* *
* You can specify different options of this command: * You can specify different options of this command:
* *
* ~~~ * ~~~
* //generate fixtures in russian languge * //generate fixtures in russian languge
* php yii faker/generate users 5 --language='ru_RU' * php yii fixture/generate users 5 --language='ru_RU'
* *
* //read templates from the other path * //read templates from the other path
* php yii faker/generate all_fixtures --templatePath='@app/path/to/my/custom/templates' * php yii fixture/generate all --templatePath='@app/path/to/my/custom/templates'
* *
* //generate fixtures into other folders, but be sure that this folders exists or you will get notice about that. * //generate fixtures into other folders, but be sure that this folders exists or you will get notice about that.
* php yii faker/generate all_fixtures --fixturesPath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3' * php yii fixture/generate all --fixturesPath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3'
* ~~~ * ~~~
* *
* You also can create your own data providers for custom tables fields, see Faker library guide for more info (https://github.com/fzaninotto/Faker); * You also can create your own data providers for custom tables fields, see Faker library guide for more info (https://github.com/fzaninotto/Faker);
@ -121,7 +121,7 @@ use yii\helpers\Console;
* *
* ~~~ * ~~~
* 'controllerMap' => [ * 'controllerMap' => [
* 'faker' => [ * 'fixture' => [
* 'class' => 'yii\faker\FixtureController', * 'class' => 'yii\faker\FixtureController',
* 'providers' => [ * 'providers' => [
* 'app\tests\unit\faker\providers\Book', * 'app\tests\unit\faker\providers\Book',
@ -140,7 +140,7 @@ class FixtureController extends \yii\console\controllers\FixtureController
/** /**
* type of fixture generating * type of fixture generating
*/ */
const GENERATE_ALL = 'all_fixtures'; const GENERATE_ALL = 'all';
/** /**
* @var string controller default action ID. * @var string controller default action ID.
@ -197,19 +197,19 @@ class FixtureController extends \yii\console\controllers\FixtureController
/** /**
* Generates fixtures and fill them with Faker data. * Generates fixtures and fill them with Faker data.
* @param string $file filename for the table template. You can generate all fixtures for all tables * @param string $file filename for the table template. You can generate all fixtures for all tables
* by specifiyng keyword "all_fixtures" as filename. * by specifiyng keyword "all" as filename.
* @param integer $times how much fixtures do you want per table * @param integer $times how much fixtures do you want per table
*/ */
public function actionGenerate($file, $times = 2) public function actionGenerate(array $file, $times = 2)
{ {
$templatePath = Yii::getAlias($this->templatePath); $templatePath = Yii::getAlias($this->templatePath);
$fixturesPath = Yii::getAlias($this->fixturesPath); $fixturesPath = Yii::getAlias($this->fixturesPath);
if ($this->needToGenerateAll($file)) { if ($this->needToGenerateAll($file[0])) {
$files = FileHelper::findFiles($templatePath, ['only' => ['.php']]); $files = FileHelper::findFiles($templatePath, ['only' => ['.php']]);
} }
else { else {
foreach(explode(',', $file) as $fileName) { foreach($file as $fileName) {
$filesToSearch[] = $fileName . '.php'; $filesToSearch[] = $fileName . '.php';
} }
$files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]); $files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]);

22
extensions/yii/faker/README.md

@ -29,7 +29,7 @@ To use this extension, simply add the following code in your application config
```php ```php
'controllerMap' => [ 'controllerMap' => [
'faker' => [ 'fixture' => [
'class' => 'yii\faker\FixtureController', 'class' => 'yii\faker\FixtureController',
], ],
], ],
@ -88,21 +88,21 @@ After you prepared needed templates for tables you can simply generate your fixt
```php ```php
//generate fixtures for the users table based on users fixture template //generate fixtures for the users table based on users fixture template
php yii faker/generate users php yii fixture/generate users
//also a short version of this command ("generate" action is default) //also a short version of this command ("generate" action is default)
php yii faker users php yii fixture users
//to generate fixtures for several tables, use "," as a separator, for example: //to generate fixtures for several tables, use "," as a separator, for example:
php yii faker users,profile,some_other_table php yii fixture users,profile,some_other_table
``` ```
In the code above "users" is template name, after this command run, new file named same as template In the code above "users" is template name, after this command run, new file named same as template
will be created under the fixtures path (by default ```@tests/unit/fixtures```) folder. will be created under the fixtures path (by default ```@tests/unit/fixtures```) folder.
You can generate fixtures for all templates by specifying keyword ```all_fixtures```. You can generate fixtures for all templates by specifying keyword ```all```.
```php ```php
php yii faker/generate all_fixtures php yii fixture/generate all_fixtures
``` ```
This command will generate fixtures for all template files that are stored under template path and This command will generate fixtures for all template files that are stored under template path and
@ -111,19 +111,19 @@ You can specify how many fixtures per file you need by the second parameter. In
all fixtures and in each file there will be 3 rows (fixtures). all fixtures and in each file there will be 3 rows (fixtures).
```php ```php
php yii faker/generate all_fixtures 3 php yii fixture/generate all 3
``` ```
You can specify different options of this command: You can specify different options of this command:
```php ```php
//generate fixtures in russian language //generate fixtures in russian language
php yii faker/generate users 5 --language='ru_RU' php yii fixture/generate users 5 --language='ru_RU'
//read templates from the other path //read templates from the other path
php yii faker/generate all_fixtures --templatePath='@app/path/to/my/custom/templates' php yii fixture/generate all --templatePath='@app/path/to/my/custom/templates'
//generate fixtures into other folders, but be sure that this folders exists or you will get notice about that. //generate fixtures into other folders, but be sure that this folders exists or you will get notice about that.
php yii faker/generate all_fixtures --fixturesPath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3' php yii fixture/generate all --fixturesPath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3'
``` ```
You also can create your own data providers for custom tables fields, see [Faker]((https://github.com/fzaninotto/Faker)) library guide for more info; You also can create your own data providers for custom tables fields, see [Faker]((https://github.com/fzaninotto/Faker)) library guide for more info;
@ -150,7 +150,7 @@ You can use it by adding it to the ```$providers``` property of the current comm
```php ```php
'controllerMap' => [ 'controllerMap' => [
'faker' => [ 'fixture' => [
'class' => 'yii\faker\FixtureController', 'class' => 'yii\faker\FixtureController',
'providers' => [ 'providers' => [
'app\tests\unit\faker\providers\Book', 'app\tests\unit\faker\providers\Book',

Loading…
Cancel
Save