Browse Source

Fixes #1748: code cleanup.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
f331547584
  1. 52
      extensions/yii/faker/FixtureController.php
  2. 31
      framework/yii/console/controllers/FixtureController.php

52
extensions/yii/faker/FixtureController.php

@ -19,7 +19,7 @@ use yii\helpers\Console;
* work these fixtures can be generated automatically, based on prepared template.
* This command is a simple wrapper for the fixtures library [Faker](https://github.com/fzaninotto/Faker).
*
* You should configure this command as follows (you can use any alias, not only "faker:fixture"):
* You should configure this command as follows (you can use any alias, not only "fixture"):
*
* ~~~
* 'controllerMap' => [
@ -49,59 +49,60 @@ use yii\helpers\Console;
* ~~~
*
* 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
*
* - `$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
*
* ~~~
* php yii fixture/generate users
* yii fixture/generate users
*
* //also a short version of this command (generate action is default)
* php yii fixture users
* yii fixture users
*
* //to generate fixtures for several tables, use "," as a separator, for example:
* php yii fixture users,profile
* 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 $fixturesPath 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 yii fixture/generate all
* yii fixture/generate all
* ~~~
*
* This command will generate fixtures for all template files that are stored under $templatePath and
* store fixtures under $fixturesPath with file names same as templates names.
* 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).
*
* ~~~
* php yii fixture/generate all 3
* yii fixture/generate all 3
* ~~~
*
* You can specify different options of this command:
*
* ~~~
* //generate fixtures in russian languge
* php yii fixture/generate users 5 --language='ru_RU'
* //generate fixtures in russian language
* 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'
* 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 --fixturesPath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3'
* 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)
@ -132,6 +133,7 @@ use yii\helpers\Console;
*
* @property \Faker\Generator $generator
*
* @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0.0
*/
class FixtureController extends \yii\console\controllers\FixtureController
@ -197,18 +199,18 @@ class FixtureController extends \yii\console\controllers\FixtureController
/**
* 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 specifiyng keyword "all" as filename.
* 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);
$fixturesPath = Yii::getAlias($this->fixturesPath);
$fixturePath = Yii::getAlias($this->fixturePath);
if ($this->needToGenerateAll($file[0])) {
$files = FileHelper::findFiles($templatePath, ['only' => ['.php']]);
}
else {
} else {
$filesToSearch = [];
foreach ($file as $fileName) {
$filesToSearch[] = $fileName . '.php';
}
@ -236,8 +238,8 @@ class FixtureController extends \yii\console\controllers\FixtureController
}
$content = $this->exportFixtures($fixtures);
file_put_contents($fixturesPath.'/'.$fixtureFileName, $content);
$this->stdout("Fixture file was generated under: " . realpath($fixturesPath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN);
file_put_contents($fixturePath . '/' . $fixtureFileName, $content);
$this->stdout("Fixture file was generated under: " . realpath($fixturePath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN);
}
}
@ -308,7 +310,7 @@ class FixtureController extends \yii\console\controllers\FixtureController
/**
* Returns exported to the string representation of given fixtures array.
* @param type $fixtures
* @param array $fixtures
* @return string exported fixtures format
*/
public function exportFixtures($fixtures)
@ -359,12 +361,12 @@ class FixtureController extends \yii\console\controllers\FixtureController
public function confirmGeneration($files)
{
$this->stdout("Fixtures will be generated under the path: \n", Console::FG_YELLOW);
$this->stdout(realpath(Yii::getAlias($this->fixturesPath, false)) ."\n\n", Console::FG_GREEN);
$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);
$this->stdout(" " . ($index + 1) . ". " . basename($fileName) . "\n", Console::FG_GREEN);
}
return $this->confirm('Generate above fixtures?');
}

31
framework/yii/console/controllers/FixtureController.php

@ -17,6 +17,7 @@ use yii\console\Exception;
* to the specific tables of the different database connections.
*
* To use this command simply configure your console.php config like this:
*
* ~~~
* 'db' => [
* 'class' => 'yii\db\Connection',
@ -31,17 +32,17 @@ use yii\console\Exception;
* ~~~
*
* ~~~
* #load fixtures under $fixturesPath to the "users" table
* php yii fixture/apply users
* #load fixtures under $fixturePath to the "users" table
* yii fixture/apply users
*
* #also a short version of this command (generate action is default)
* php yii fixture users
* yii fixture users
*
* #load fixtures under $fixturesPath to the "users" table to the different connection
* php yii fixture/apply users --db='someOtherDbConneciton'
* #load fixtures under $fixturePath to the "users" table to the different connection
* yii fixture/apply users --db='someOtherDbConneciton'
*
* #load fixtures under different $fixturesPath to the "users" table.
* php yii fixture/apply users --fixturesPath='@app/some/other/path/to/fixtures'
* #load fixtures under different $fixturePath to the "users" table.
* yii fixture/apply users --fixturePath='@app/some/other/path/to/fixtures'
* ~~~
*
* @author Mark Jebri <mark.github@yandex.ru>
@ -61,7 +62,7 @@ class FixtureController extends Controller
* Alias to the path, where all fixtures are stored.
* @var string
*/
public $fixturesPath = '@tests/unit/fixtures';
public $fixturePath = '@tests/unit/fixtures';
/**
* Id of the database connection component of the application.
@ -76,14 +77,14 @@ class FixtureController extends Controller
public function globalOptions()
{
return array_merge(parent::globalOptions(), [
'db','fixturesPath'
'db', 'fixturePath'
]);
}
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
* It checks that fixtures path and database connection are available.
* @param type $action
* @param \yii\base\Action $action
* @return boolean
*/
public function beforeAction($action)
@ -103,7 +104,7 @@ class FixtureController extends Controller
*/
public function actionApply($fixture)
{
$this->fixtureManager->basePath = $this->fixturesPath;
$this->fixtureManager->basePath = $this->fixturePath;
$this->fixtureManager->db = $this->db;
$this->loadFixtures([$fixture]);
}
@ -114,7 +115,7 @@ class FixtureController extends Controller
*/
public function actionClear($table)
{
$this->dbConnection->createCommand()->truncateTable($table)->execute();
$this->getDbConnection()->createCommand()->truncateTable($table)->execute();
echo "Table \"{$table}\" was successfully cleared. \n";
}
@ -124,17 +125,17 @@ class FixtureController extends Controller
*/
public function checkRequirements()
{
$path = Yii::getAlias($this->fixturesPath, false);
$path = Yii::getAlias($this->fixturePath, false);
if (!is_dir($path) || !is_writable($path)) {
throw new Exception("The fixtures path \"{$this->fixturesPath}\" not exist or is not writable");
throw new Exception("The fixtures path \"{$this->fixturePath}\" not exist or is not writable");
}
}
/**
* Returns database connection component
* @return yii\db\Connection|null
* @return \yii\db\Connection|null
*/
public function getDbConnection()
{

Loading…
Cancel
Save