From 4cf356df8d9ef1faa7f8c4e4e030d84f12df31a7 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 10 Jan 2014 20:28:17 -0500 Subject: [PATCH] adjust directories for PSR-4. --- composer.json | 4 +- extensions/faker/CHANGELOG.md | 7 + extensions/faker/FixtureController.php | 368 +++++++++++++++++++++++++++++ extensions/faker/LICENSE.md | 32 +++ extensions/faker/README.md | 162 +++++++++++++ extensions/faker/composer.json | 26 ++ extensions/yii/faker/CHANGELOG.md | 7 - extensions/yii/faker/FixtureController.php | 368 ----------------------------- extensions/yii/faker/LICENSE.md | 32 --- extensions/yii/faker/README.md | 162 ------------- extensions/yii/faker/composer.json | 27 --- 11 files changed, 598 insertions(+), 597 deletions(-) create mode 100644 extensions/faker/CHANGELOG.md create mode 100644 extensions/faker/FixtureController.php create mode 100644 extensions/faker/LICENSE.md create mode 100644 extensions/faker/README.md create mode 100644 extensions/faker/composer.json delete mode 100644 extensions/yii/faker/CHANGELOG.md delete mode 100644 extensions/yii/faker/FixtureController.php delete mode 100644 extensions/yii/faker/LICENSE.md delete mode 100644 extensions/yii/faker/README.md delete mode 100644 extensions/yii/faker/composer.json diff --git a/composer.json b/composer.json index 41529cc..be0b7de 100644 --- a/composer.json +++ b/composer.json @@ -96,6 +96,9 @@ "twig/twig": "required by yii2-twig extension" }, "autoload": { + "psr-4": { + "yii\\faker\\": "extensions/faker/" + }, "psr-0": { "yii\\apidoc\\": "extensions/", "yii\\authclient\\": "extensions/", @@ -103,7 +106,6 @@ "yii\\codeception\\": "extensions/", "yii\\debug\\": "extensions/", "yii\\elasticsearch\\": "extensions/", - "yii\\faker\\": "extensions/", "yii\\gii\\": "extensions/", "yii\\imagine\\" : "extensions/", "yii\\jui\\": "extensions/", diff --git a/extensions/faker/CHANGELOG.md b/extensions/faker/CHANGELOG.md new file mode 100644 index 0000000..269f928 --- /dev/null +++ b/extensions/faker/CHANGELOG.md @@ -0,0 +1,7 @@ +Yii Framework 2 faker extension Change Log +============================================== + +2.0.0 beta under development +---------------------------- + +- Initial release. \ No newline at end of file diff --git a/extensions/faker/FixtureController.php b/extensions/faker/FixtureController.php new file mode 100644 index 0000000..f3b24d2 --- /dev/null +++ b/extensions/faker/FixtureController.php @@ -0,0 +1,368 @@ + [ + * 'fixture' => [ + * 'class' => 'yii\faker\FixtureController', + * ], + * ], + * ~~~ + * + * 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: + * + * ~~~ + * #users.php file under $templatePath + * + * return [ + * [ + * 'table_column0' => 'faker_formatter', + * ... + * 'table_columnN' => 'other_faker_formatter + * 'table_columnN+1' => function ($fixture, $faker, $index) { + * //set needed fixture fields based on different conditions + * return $fixture; + * } + * ], + * ]; + * ~~~ + * + * If you use callback as a attribute value, then it will be called as shown with three parameters: + * + * - `$fixture` - current fixture array. + * - `$faker` - faker generator instance + * - `$index` - current fixture index. For example if user need to generate 3 fixtures for tbl_user, 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 prepared needed templates for tables you can simply generate your fixtures via command + * + * ~~~ + * yii fixture/generate users + * + * //also a short version of this command (generate action is default) + * yii fixture users + * + * //to generate fixtures for several tables, use "," as a separator, for example: + * yii fixture users,profile + * ~~~ + * + * In the code above "users" is template name, after this command run, new file named same as template + * will be created under the `$fixturePath` folder. + * You can generate fixtures for all templates by specifying keyword "all" + * + * ~~~ + * yii fixture/generate all + * ~~~ + * + * This command will generate fixtures for all template files that are stored under $templatePath and + * store fixtures under $fixturePath with file names same as templates names. + * + * You can specify how many fixtures per file you need by the second parameter. In the code below we generate + * all fixtures and in each file there will be 3 rows (fixtures). + * + * ~~~ + * yii fixture/generate all 3 + * ~~~ + * + * You can specify different options of this command: + * + * ~~~ + * //generate fixtures in russian language + * yii fixture/generate users 5 --language=ru_RU + * + * //read templates from the other path + * 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. + * yii fixture/generate all --fixturePath=@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); + * After you created custom provider, for example: + * + * ~~~ + * class Book extends \Faker\Provider\Base + * { + * public function title($nbWords = 5) + * { + * $sentence = $this->generator->sentence($nbWords); + * return mb_substr($sentence, 0, mb_strlen($sentence) - 1); + * } + * + * public function ISBN() + * { + * return $this->generator->randomNumber(13); + * } + * } + * ~~~ + * + * you can use it by adding it to the $providers property of the current command. In your console.php config: + * + * ~~~ + * 'controllerMap' => [ + * 'fixture' => [ + * 'class' => 'yii\faker\FixtureController', + * 'providers' => [ + * 'app\tests\unit\faker\providers\Book', + * ], + * ], + * ], + * ~~~ + * + * @property \Faker\Generator $generator + * + * @author Mark Jebri + * @since 2.0.0 + */ +class FixtureController extends \yii\console\controllers\FixtureController +{ + /** + * type of fixture generating + */ + const GENERATE_ALL = 'all'; + + /** + * @var string controller default action ID. + */ + public $defaultAction = 'generate'; + /** + * Alias to the template path, where all tables templates are stored. + * @var string + */ + public $templatePath = '@tests/unit/templates/fixtures'; + /** + * Language to use when generating fixtures data. + * @var string + */ + public $language; + /** + * Additional data providers that can be created by user and will be added to the Faker generator. + * More info in [Faker](https://github.com/fzaninotto/Faker.) library docs. + * @var array + */ + public $providers = []; + /** + * Faker generator instance + * @var \Faker\Generator + */ + private $_generator; + + + /** + * Returns the names of the global options for this command. + * @return array the names of the global options for this command. + */ + public function globalOptions() + { + return array_merge(parent::globalOptions(), [ + 'templatePath', 'language' + ]); + } + + public function beforeAction($action) + { + if (parent::beforeAction($action)) { + $this->checkPaths(); + $this->addProviders(); + return true; + } else { + return false; + } + } + + /** + * Generates fixtures and fill them with Faker data. + * @param string $file filename for the table template. You can generate all fixtures for all tables + * by specifying keyword "all" as filename. + * @param integer $times how much fixtures do you want per table + */ + public function actionGenerate(array $file, $times = 2) + { + $templatePath = Yii::getAlias($this->templatePath); + $fixturePath = Yii::getAlias($this->fixturePath); + + if ($this->needToGenerateAll($file[0])) { + $files = FileHelper::findFiles($templatePath, ['only' => ['.php']]); + } else { + $filesToSearch = []; + foreach ($file as $fileName) { + $filesToSearch[] = $fileName . '.php'; + } + $files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]); + } + + if (empty($files)) { + throw new Exception("No files were found by name: \"" . implode(', ', $file) . "\".\n" + . "Check that template with these name exists, under template path: \n\"{$templatePath}\"." + ); + } + + if (!$this->confirmGeneration($files)) { + return; + } + + foreach ($files as $templateFile) { + $fixtureFileName = basename($templateFile); + $template = $this->getTemplate($templateFile); + $fixtures = []; + + for ($i = 0; $i < $times; $i++) { + $fixtures[$i] = $this->generateFixture($template, $i); + } + + $content = $this->exportFixtures($fixtures); + file_put_contents($fixturePath . '/' . $fixtureFileName, $content); + $this->stdout("Fixture file was generated under: " . realpath($fixturePath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN); + } + } + + /** + * Returns Faker generator instance. Getter for private property. + * @return \Faker\Generator + */ + public function getGenerator() + { + if (is_null($this->_generator)) { + //replacing - on _ because Faker support only en_US format and not intl + + $language = is_null($this->language) ? str_replace('-', '_', Yii::$app->language) : $this->language; + $this->_generator = \Faker\Factory::create($language); + } + + return $this->_generator; + } + + /** + * Check if the template path and migrations path exists and writable. + */ + public function checkPaths() + { + $path = Yii::getAlias($this->templatePath); + + if (!is_dir($path)) { + throw new Exception("The template path \"{$this->templatePath}\" not exist"); + } + } + + /** + * Adds users providers to the faker generator. + */ + public function addProviders() + { + foreach ($this->providers as $provider) { + $this->generator->addProvider(new $provider($this->generator)); + } + } + + /** + * Checks if needed to generate all fixtures. + * @param string $file + * @return bool + */ + public function needToGenerateAll($file) + { + return $file == self::GENERATE_ALL; + } + + /** + * Returns generator template for the given fixture name + * @param string $file template file + * @return array generator template + * @throws \yii\console\Exception if wrong file format + */ + public function getTemplate($file) + { + $template = require($file); + + if (!is_array($template)) { + throw new Exception("The template file \"$file\" has wrong format. It should return valid template array"); + } + + return $template; + } + + /** + * Returns exported to the string representation of given fixtures array. + * @param array $fixtures + * @return string exported fixtures format + */ + public function exportFixtures($fixtures) + { + $content = " $value) { + $content .= "\n\t\t'{$name}' => '{$value}',"; + } + + $content .= "\n\t],"; + + } + $content .= "\n];\n"; + return $content; + } + + /** + * Generates fixture from given template + * @param array $template fixture template + * @param integer $index current fixture index + * @return array fixture + */ + public function generateFixture($template, $index) + { + $fixture = []; + + foreach ($template as $attribute => $fakerProperty) { + if (!is_string($fakerProperty)) { + $fixture = call_user_func_array($fakerProperty, [$fixture, $this->generator, $index]); + } else { + $fixture[$attribute] = $this->generator->$fakerProperty; + } + } + + return $fixture; + } + + /** + * Prompts user with message if he confirm generation with given fixture templates files. + * @param array $files + * @return boolean + */ + public function confirmGeneration($files) + { + $this->stdout("Fixtures will be generated under the path: \n", Console::FG_YELLOW); + $this->stdout(realpath(Yii::getAlias($this->fixturePath, false)) . "\n\n", Console::FG_GREEN); + $this->stdout("Templates will be taken from path: \n", Console::FG_YELLOW); + $this->stdout(realpath(Yii::getAlias($this->templatePath, false)) . "\n\n", Console::FG_GREEN); + + foreach ($files as $index => $fileName) { + $this->stdout(" " . ($index + 1) . ". " . basename($fileName) . "\n", Console::FG_GREEN); + } + return $this->confirm('Generate above fixtures?'); + } +} diff --git a/extensions/faker/LICENSE.md b/extensions/faker/LICENSE.md new file mode 100644 index 0000000..6edcc4f --- /dev/null +++ b/extensions/faker/LICENSE.md @@ -0,0 +1,32 @@ +The Yii framework is free software. It is released under the terms of +the following BSD License. + +Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii Software LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/extensions/faker/README.md b/extensions/faker/README.md new file mode 100644 index 0000000..9b40c54 --- /dev/null +++ b/extensions/faker/README.md @@ -0,0 +1,162 @@ +Faker Extension for Yii 2 +========================= + +This extension provides a [`Faker`](https://github.com/fzaninotto/Faker) fixture command for Yii 2. + + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist yiisoft/yii2-faker "*" +``` + +or add + +```json +"yiisoft/yii2-faker": "*" +``` + +to the require section of your composer.json. + + +Usage +----- + +To use this extension, simply add the following code in your application configuration (console.php): + +```php +'controllerMap' => [ + 'fixture' => [ + 'class' => 'yii\faker\FixtureController', + ], +], +``` +Set valid ```test``` alias in your console config, for example for ```basic``` application template, this should be added +to ```console.php``` config: ```Yii::setAlias('tests', __DIR__ . '/../tests');``` +To start using this command you need to be familiar (read guide) for the [Faker](https://github.com/fzaninotto/Faker) library and +generate fixtures template files, according to the given format: + +```php +//users.php file under template path (by default @tests/unit/templates/fixtures) +return [ + [ + 'table_column0' => 'faker_formatter', + ... + 'table_columnN' => 'other_faker_formatter' + 'body' => function ($fixture, $faker, $index) { + //set needed fixture fields based on different conditions + + $fixture['body'] = $faker->sentence(7,true); //generate sentence exact with 7 words. + return $fixture; + } + ], +]; +``` + +If you use callback as a attribute value, then it will be called as shown with three parameters: + +* ```$fixture``` - current fixture array. +* ```$faker``` - faker generator instance +* ```$index``` - current fixture index. For example if user need to generate 3 fixtures for tbl_user, it will be 0..2. + +After you set all needed fields in callback, you need to return $fixture array back from the callback. + +Another example of valid template: + +```php +use yii\helpers\Security; + +return [ + 'name' => 'firstName', + 'phone' => 'phoneNumber', + 'city' => 'city', + 'password' => function ($fixture, $faker, $index) { + $fixture['password'] = Security::generatePasswordHash('password_' . $index); + return $fixture; + }, + 'auth_key' => function ($fixture, $faker, $index) { + $fixture['auth_key'] = Security::generateRandomKey(); + return $fixture; + }, +]; +``` + +After you prepared needed templates for tables you can simply generate your fixtures via command + +```php +//generate fixtures for the users table based on users fixture template +php yii fixture/generate users + +//also a short version of this command ("generate" action is default) +php yii fixture users + +//to generate fixtures for several tables, use "," as a separator, for example: +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 +will be created under the fixtures path (by default ```@tests/unit/fixtures```) folder. +You can generate fixtures for all templates by specifying keyword ```all```. + +```php +php yii fixture/generate all +``` + +This command will generate fixtures for all template files that are stored under template path and +store fixtures under fixtures path with file names same as templates names. +You can specify how many fixtures per file you need by the second parameter. In the code below we generate +all fixtures and in each file there will be 3 rows (fixtures). + +```php +php yii fixture/generate all 3 +``` +You can specify different options of this command: + +```php +//generate fixtures in russian language +php yii fixture/generate users 5 --language='ru_RU' + +//read templates from the other path +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. +php yii fixture/generate all --fixturePath='@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; +After you created custom provider, for example: + +```php +class Book extends \Faker\Provider\Base +{ + public function title($nbWords = 5) + { + $sentence = $this->generator->sentence($nbWords); + return mb_substr($sentence, 0, mb_strlen($sentence) - 1); + } + + public function ISBN() + { + return $this->generator->randomNumber(13); + } + + } +``` + +You can use it by adding it to the ```$providers``` property of the current command. In your console.php config: + +```php +'controllerMap' => [ + 'fixture' => [ + 'class' => 'yii\faker\FixtureController', + 'providers' => [ + 'app\tests\unit\faker\providers\Book', + ], + ], +] +``` diff --git a/extensions/faker/composer.json b/extensions/faker/composer.json new file mode 100644 index 0000000..5af5319 --- /dev/null +++ b/extensions/faker/composer.json @@ -0,0 +1,26 @@ +{ + "name": "yiisoft/yii2-faker", + "description": "Fixture generator. The Faker integration for the Yii framework.", + "keywords": ["yii", "faker", "fixture"], + "type": "yii2-extension", + "license": "BSD-3-Clause", + "support": { + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Mark Jebri", + "email": "mark.github@yandex.ru" + } + ], + "require": { + "yiisoft/yii2": "*", + "fzaninotto/faker": "*" + }, + "autoload": { + "psr-4": { "yii\\faker\\": "" } + } +} diff --git a/extensions/yii/faker/CHANGELOG.md b/extensions/yii/faker/CHANGELOG.md deleted file mode 100644 index 269f928..0000000 --- a/extensions/yii/faker/CHANGELOG.md +++ /dev/null @@ -1,7 +0,0 @@ -Yii Framework 2 faker extension Change Log -============================================== - -2.0.0 beta under development ----------------------------- - -- Initial release. \ No newline at end of file diff --git a/extensions/yii/faker/FixtureController.php b/extensions/yii/faker/FixtureController.php deleted file mode 100644 index f3b24d2..0000000 --- a/extensions/yii/faker/FixtureController.php +++ /dev/null @@ -1,368 +0,0 @@ - [ - * 'fixture' => [ - * 'class' => 'yii\faker\FixtureController', - * ], - * ], - * ~~~ - * - * 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: - * - * ~~~ - * #users.php file under $templatePath - * - * return [ - * [ - * 'table_column0' => 'faker_formatter', - * ... - * 'table_columnN' => 'other_faker_formatter - * 'table_columnN+1' => function ($fixture, $faker, $index) { - * //set needed fixture fields based on different conditions - * return $fixture; - * } - * ], - * ]; - * ~~~ - * - * If you use callback as a attribute value, then it will be called as shown with three parameters: - * - * - `$fixture` - current fixture array. - * - `$faker` - faker generator instance - * - `$index` - current fixture index. For example if user need to generate 3 fixtures for tbl_user, 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 prepared needed templates for tables you can simply generate your fixtures via command - * - * ~~~ - * yii fixture/generate users - * - * //also a short version of this command (generate action is default) - * yii fixture users - * - * //to generate fixtures for several tables, use "," as a separator, for example: - * yii fixture users,profile - * ~~~ - * - * In the code above "users" is template name, after this command run, new file named same as template - * will be created under the `$fixturePath` folder. - * You can generate fixtures for all templates by specifying keyword "all" - * - * ~~~ - * yii fixture/generate all - * ~~~ - * - * This command will generate fixtures for all template files that are stored under $templatePath and - * store fixtures under $fixturePath with file names same as templates names. - * - * You can specify how many fixtures per file you need by the second parameter. In the code below we generate - * all fixtures and in each file there will be 3 rows (fixtures). - * - * ~~~ - * yii fixture/generate all 3 - * ~~~ - * - * You can specify different options of this command: - * - * ~~~ - * //generate fixtures in russian language - * yii fixture/generate users 5 --language=ru_RU - * - * //read templates from the other path - * 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. - * yii fixture/generate all --fixturePath=@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); - * After you created custom provider, for example: - * - * ~~~ - * class Book extends \Faker\Provider\Base - * { - * public function title($nbWords = 5) - * { - * $sentence = $this->generator->sentence($nbWords); - * return mb_substr($sentence, 0, mb_strlen($sentence) - 1); - * } - * - * public function ISBN() - * { - * return $this->generator->randomNumber(13); - * } - * } - * ~~~ - * - * you can use it by adding it to the $providers property of the current command. In your console.php config: - * - * ~~~ - * 'controllerMap' => [ - * 'fixture' => [ - * 'class' => 'yii\faker\FixtureController', - * 'providers' => [ - * 'app\tests\unit\faker\providers\Book', - * ], - * ], - * ], - * ~~~ - * - * @property \Faker\Generator $generator - * - * @author Mark Jebri - * @since 2.0.0 - */ -class FixtureController extends \yii\console\controllers\FixtureController -{ - /** - * type of fixture generating - */ - const GENERATE_ALL = 'all'; - - /** - * @var string controller default action ID. - */ - public $defaultAction = 'generate'; - /** - * Alias to the template path, where all tables templates are stored. - * @var string - */ - public $templatePath = '@tests/unit/templates/fixtures'; - /** - * Language to use when generating fixtures data. - * @var string - */ - public $language; - /** - * Additional data providers that can be created by user and will be added to the Faker generator. - * More info in [Faker](https://github.com/fzaninotto/Faker.) library docs. - * @var array - */ - public $providers = []; - /** - * Faker generator instance - * @var \Faker\Generator - */ - private $_generator; - - - /** - * Returns the names of the global options for this command. - * @return array the names of the global options for this command. - */ - public function globalOptions() - { - return array_merge(parent::globalOptions(), [ - 'templatePath', 'language' - ]); - } - - public function beforeAction($action) - { - if (parent::beforeAction($action)) { - $this->checkPaths(); - $this->addProviders(); - return true; - } else { - return false; - } - } - - /** - * Generates fixtures and fill them with Faker data. - * @param string $file filename for the table template. You can generate all fixtures for all tables - * by specifying keyword "all" as filename. - * @param integer $times how much fixtures do you want per table - */ - public function actionGenerate(array $file, $times = 2) - { - $templatePath = Yii::getAlias($this->templatePath); - $fixturePath = Yii::getAlias($this->fixturePath); - - if ($this->needToGenerateAll($file[0])) { - $files = FileHelper::findFiles($templatePath, ['only' => ['.php']]); - } else { - $filesToSearch = []; - foreach ($file as $fileName) { - $filesToSearch[] = $fileName . '.php'; - } - $files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]); - } - - if (empty($files)) { - throw new Exception("No files were found by name: \"" . implode(', ', $file) . "\".\n" - . "Check that template with these name exists, under template path: \n\"{$templatePath}\"." - ); - } - - if (!$this->confirmGeneration($files)) { - return; - } - - foreach ($files as $templateFile) { - $fixtureFileName = basename($templateFile); - $template = $this->getTemplate($templateFile); - $fixtures = []; - - for ($i = 0; $i < $times; $i++) { - $fixtures[$i] = $this->generateFixture($template, $i); - } - - $content = $this->exportFixtures($fixtures); - file_put_contents($fixturePath . '/' . $fixtureFileName, $content); - $this->stdout("Fixture file was generated under: " . realpath($fixturePath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN); - } - } - - /** - * Returns Faker generator instance. Getter for private property. - * @return \Faker\Generator - */ - public function getGenerator() - { - if (is_null($this->_generator)) { - //replacing - on _ because Faker support only en_US format and not intl - - $language = is_null($this->language) ? str_replace('-', '_', Yii::$app->language) : $this->language; - $this->_generator = \Faker\Factory::create($language); - } - - return $this->_generator; - } - - /** - * Check if the template path and migrations path exists and writable. - */ - public function checkPaths() - { - $path = Yii::getAlias($this->templatePath); - - if (!is_dir($path)) { - throw new Exception("The template path \"{$this->templatePath}\" not exist"); - } - } - - /** - * Adds users providers to the faker generator. - */ - public function addProviders() - { - foreach ($this->providers as $provider) { - $this->generator->addProvider(new $provider($this->generator)); - } - } - - /** - * Checks if needed to generate all fixtures. - * @param string $file - * @return bool - */ - public function needToGenerateAll($file) - { - return $file == self::GENERATE_ALL; - } - - /** - * Returns generator template for the given fixture name - * @param string $file template file - * @return array generator template - * @throws \yii\console\Exception if wrong file format - */ - public function getTemplate($file) - { - $template = require($file); - - if (!is_array($template)) { - throw new Exception("The template file \"$file\" has wrong format. It should return valid template array"); - } - - return $template; - } - - /** - * Returns exported to the string representation of given fixtures array. - * @param array $fixtures - * @return string exported fixtures format - */ - public function exportFixtures($fixtures) - { - $content = " $value) { - $content .= "\n\t\t'{$name}' => '{$value}',"; - } - - $content .= "\n\t],"; - - } - $content .= "\n];\n"; - return $content; - } - - /** - * Generates fixture from given template - * @param array $template fixture template - * @param integer $index current fixture index - * @return array fixture - */ - public function generateFixture($template, $index) - { - $fixture = []; - - foreach ($template as $attribute => $fakerProperty) { - if (!is_string($fakerProperty)) { - $fixture = call_user_func_array($fakerProperty, [$fixture, $this->generator, $index]); - } else { - $fixture[$attribute] = $this->generator->$fakerProperty; - } - } - - return $fixture; - } - - /** - * Prompts user with message if he confirm generation with given fixture templates files. - * @param array $files - * @return boolean - */ - public function confirmGeneration($files) - { - $this->stdout("Fixtures will be generated under the path: \n", Console::FG_YELLOW); - $this->stdout(realpath(Yii::getAlias($this->fixturePath, false)) . "\n\n", Console::FG_GREEN); - $this->stdout("Templates will be taken from path: \n", Console::FG_YELLOW); - $this->stdout(realpath(Yii::getAlias($this->templatePath, false)) . "\n\n", Console::FG_GREEN); - - foreach ($files as $index => $fileName) { - $this->stdout(" " . ($index + 1) . ". " . basename($fileName) . "\n", Console::FG_GREEN); - } - return $this->confirm('Generate above fixtures?'); - } -} diff --git a/extensions/yii/faker/LICENSE.md b/extensions/yii/faker/LICENSE.md deleted file mode 100644 index 6edcc4f..0000000 --- a/extensions/yii/faker/LICENSE.md +++ /dev/null @@ -1,32 +0,0 @@ -The Yii framework is free software. It is released under the terms of -the following BSD License. - -Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - * Neither the name of Yii Software LLC nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/extensions/yii/faker/README.md b/extensions/yii/faker/README.md deleted file mode 100644 index 9b40c54..0000000 --- a/extensions/yii/faker/README.md +++ /dev/null @@ -1,162 +0,0 @@ -Faker Extension for Yii 2 -========================= - -This extension provides a [`Faker`](https://github.com/fzaninotto/Faker) fixture command for Yii 2. - - -Installation ------------- - -The preferred way to install this extension is through [composer](http://getcomposer.org/download/). - -Either run - -``` -php composer.phar require --prefer-dist yiisoft/yii2-faker "*" -``` - -or add - -```json -"yiisoft/yii2-faker": "*" -``` - -to the require section of your composer.json. - - -Usage ------ - -To use this extension, simply add the following code in your application configuration (console.php): - -```php -'controllerMap' => [ - 'fixture' => [ - 'class' => 'yii\faker\FixtureController', - ], -], -``` -Set valid ```test``` alias in your console config, for example for ```basic``` application template, this should be added -to ```console.php``` config: ```Yii::setAlias('tests', __DIR__ . '/../tests');``` -To start using this command you need to be familiar (read guide) for the [Faker](https://github.com/fzaninotto/Faker) library and -generate fixtures template files, according to the given format: - -```php -//users.php file under template path (by default @tests/unit/templates/fixtures) -return [ - [ - 'table_column0' => 'faker_formatter', - ... - 'table_columnN' => 'other_faker_formatter' - 'body' => function ($fixture, $faker, $index) { - //set needed fixture fields based on different conditions - - $fixture['body'] = $faker->sentence(7,true); //generate sentence exact with 7 words. - return $fixture; - } - ], -]; -``` - -If you use callback as a attribute value, then it will be called as shown with three parameters: - -* ```$fixture``` - current fixture array. -* ```$faker``` - faker generator instance -* ```$index``` - current fixture index. For example if user need to generate 3 fixtures for tbl_user, it will be 0..2. - -After you set all needed fields in callback, you need to return $fixture array back from the callback. - -Another example of valid template: - -```php -use yii\helpers\Security; - -return [ - 'name' => 'firstName', - 'phone' => 'phoneNumber', - 'city' => 'city', - 'password' => function ($fixture, $faker, $index) { - $fixture['password'] = Security::generatePasswordHash('password_' . $index); - return $fixture; - }, - 'auth_key' => function ($fixture, $faker, $index) { - $fixture['auth_key'] = Security::generateRandomKey(); - return $fixture; - }, -]; -``` - -After you prepared needed templates for tables you can simply generate your fixtures via command - -```php -//generate fixtures for the users table based on users fixture template -php yii fixture/generate users - -//also a short version of this command ("generate" action is default) -php yii fixture users - -//to generate fixtures for several tables, use "," as a separator, for example: -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 -will be created under the fixtures path (by default ```@tests/unit/fixtures```) folder. -You can generate fixtures for all templates by specifying keyword ```all```. - -```php -php yii fixture/generate all -``` - -This command will generate fixtures for all template files that are stored under template path and -store fixtures under fixtures path with file names same as templates names. -You can specify how many fixtures per file you need by the second parameter. In the code below we generate -all fixtures and in each file there will be 3 rows (fixtures). - -```php -php yii fixture/generate all 3 -``` -You can specify different options of this command: - -```php -//generate fixtures in russian language -php yii fixture/generate users 5 --language='ru_RU' - -//read templates from the other path -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. -php yii fixture/generate all --fixturePath='@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; -After you created custom provider, for example: - -```php -class Book extends \Faker\Provider\Base -{ - public function title($nbWords = 5) - { - $sentence = $this->generator->sentence($nbWords); - return mb_substr($sentence, 0, mb_strlen($sentence) - 1); - } - - public function ISBN() - { - return $this->generator->randomNumber(13); - } - - } -``` - -You can use it by adding it to the ```$providers``` property of the current command. In your console.php config: - -```php -'controllerMap' => [ - 'fixture' => [ - 'class' => 'yii\faker\FixtureController', - 'providers' => [ - 'app\tests\unit\faker\providers\Book', - ], - ], -] -``` diff --git a/extensions/yii/faker/composer.json b/extensions/yii/faker/composer.json deleted file mode 100644 index 526aa1d..0000000 --- a/extensions/yii/faker/composer.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "yiisoft/yii2-faker", - "description": "Fixture generator. The Faker integration for the Yii framework.", - "keywords": ["yii", "faker", "fixture"], - "type": "yii2-extension", - "license": "BSD-3-Clause", - "support": { - "forum": "http://www.yiiframework.com/forum/", - "wiki": "http://www.yiiframework.com/wiki/", - "irc": "irc://irc.freenode.net/yii", - "source": "https://github.com/yiisoft/yii2" - }, - "authors": [ - { - "name": "Mark Jebri", - "email": "mark.github@yandex.ru" - } - ], - "require": { - "yiisoft/yii2": "*", - "fzaninotto/faker": "*" - }, - "autoload": { - "psr-0": { "yii\\faker\\": "" } - }, - "target-dir": "yii/faker" -} \ No newline at end of file