diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index b1d8fb9..e71a4d3 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -274,7 +274,7 @@ class m150811_220037_create_post extends Migration ### Drop Table ```php -yii migrate/create drop_post +yii migrate/create drop_post --fields=title:string(12):notNull:unique,body:text ``` generates @@ -290,6 +290,9 @@ class m150811_220037_drop_post extends Migration public function down() { $this->createTable('post', [ + 'id' => $this->primaryKey(), + 'title' => $this->string(12)->notNull()->unique(), + 'body' => $this->text() ]); } } @@ -614,10 +617,10 @@ The migration command comes with a few command-line options that can be used to named `$className` to get the migration class name. * `generatorTemplateFiles`: array (defaults to `[ - 'create' => '@yii/views/createMigration.php', - 'drop' => '@yii/views/dropMigration.php', - 'add' => '@yii/views/addMigration.php', - 'remove' => '@yii/views/removeMigration.php', + 'create_table' => '@yii/views/createTableMigration.php', + 'drop_table' => '@yii/views/dropTableMigration.php', + 'add_column' => '@yii/views/addColumnMigration.php', + 'drop_column' => '@yii/views/dropColumnMigration.php', 'create_junction' => '@yii/views/createJunctionMigration.php' ]`), specifies template files for generating migration code. See "[Generating Migrations](#generating-migrations)" for more details. diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index d1a160e..e567c4d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -44,6 +44,7 @@ Yii Framework 2 Change Log - Enh #4104: Added `yii\filters\auth\AuthMetod::optional` for optional authentification in all child classes (SilverFire) - Enh #7566: Improved `\yii\validators\CompareValidator` default messages (slinstj) - Enh #7581: Added ability to specify range using anonymous function in `RangeValidator` (RomeroMsk) +- Enh #8284: Added `\yii\captcha\CaptchaAction::$imageLibrary` property allowing to set image rendering library (AnatolyRugalev) - Enh #8613: `yii\widgets\FragmentCache` will not store empty content anymore which fixes some problems related to `yii\filters\PageCache` (kidol) - Enh #8649: Added total applied migrations to final report (vernik91) - Enh #9282: Improved JSON error handling to support PHP 5.5 error codes (freezy-sk) diff --git a/framework/captcha/CaptchaAction.php b/framework/captcha/CaptchaAction.php index ed36d73..9bbbaf3 100644 --- a/framework/captcha/CaptchaAction.php +++ b/framework/captcha/CaptchaAction.php @@ -98,6 +98,12 @@ class CaptchaAction extends Action * If not set, it means the verification code will be randomly generated. */ public $fixedVerifyCode; + /** + * @var string the rendering library to use. Currently supported only 'gd' and 'imagick'. + * If not set, library will be determined automatically. + * @since 2.0.7 + */ + public $imageLibrary; /** @@ -236,13 +242,21 @@ class CaptchaAction extends Action * Renders the CAPTCHA image. * @param string $code the verification code * @return string image contents + * @throws InvalidConfigException if imageLibrary is not supported */ protected function renderImage($code) { - if (Captcha::checkRequirements() === 'gd') { - return $this->renderImageByGD($code); + if (isset($this->imageLibrary)) { + $imageLibrary = $this->imageLibrary; } else { + $imageLibrary = Captcha::checkRequirements(); + } + if ($imageLibrary === 'gd') { + return $this->renderImageByGD($code); + } elseif ($imageLibrary === 'imagick') { return $this->renderImageByImagick($code); + } else { + throw new InvalidConfigException("Defined library '{$imageLibrary}' is not supported"); } } diff --git a/framework/console/controllers/BaseMigrateController.php b/framework/console/controllers/BaseMigrateController.php index 739056c..b7695e2 100644 --- a/framework/console/controllers/BaseMigrateController.php +++ b/framework/console/controllers/BaseMigrateController.php @@ -511,26 +511,27 @@ abstract class BaseMigrateController extends Controller 'field_second' => $secondTable, ]); } elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) { - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['add']), [ + $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['add_column']), [ 'className' => $className, 'table' => mb_strtolower($matches[2], Yii::$app->charset), 'fields' => $this->fields ]); } elseif (preg_match('/^drop_(.+)_from_(.+)$/', $name, $matches)) { - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['remove']), [ + $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop_column']), [ 'className' => $className, 'table' => mb_strtolower($matches[2], Yii::$app->charset), 'fields' => $this->fields ]); } elseif (preg_match('/^create_(.+)$/', $name, $matches)) { $this->addDefaultPrimaryKey(); - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create']), [ + $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create_table']), [ 'className' => $className, 'table' => mb_strtolower($matches[1], Yii::$app->charset), 'fields' => $this->fields ]); } elseif (preg_match('/^drop_(.+)$/', $name, $matches)) { - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop']), [ + $this->addDefaultPrimaryKey(); + $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop_table']), [ 'className' => $className, 'table' => mb_strtolower($matches[1], Yii::$app->charset), 'fields' => $this->fields diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 8359979..999d89a 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -67,10 +67,10 @@ class MigrateController extends BaseMigrateController * @inheritdoc */ public $generatorTemplateFiles = [ - 'create' => '@yii/views/createMigration.php', - 'drop' => '@yii/views/dropMigration.php', - 'add' => '@yii/views/addColumnMigration.php', - 'remove' => '@yii/views/dropColumnMigration.php', + 'create_table' => '@yii/views/createTableMigration.php', + 'drop_table' => '@yii/views/dropTableMigration.php', + 'add_column' => '@yii/views/addColumnMigration.php', + 'drop_column' => '@yii/views/dropColumnMigration.php', 'create_junction' => '@yii/views/createJunctionMigration.php' ]; /** diff --git a/framework/views/createMigration.php b/framework/views/createTableMigration.php similarity index 100% rename from framework/views/createMigration.php rename to framework/views/createTableMigration.php diff --git a/framework/views/dropMigration.php b/framework/views/dropTableMigration.php similarity index 100% rename from framework/views/dropMigration.php rename to framework/views/dropTableMigration.php diff --git a/tests/framework/console/controllers/MigrateControllerTestTrait.php b/tests/framework/console/controllers/MigrateControllerTestTrait.php index e2d4325..d3498e8 100644 --- a/tests/framework/console/controllers/MigrateControllerTestTrait.php +++ b/tests/framework/console/controllers/MigrateControllerTestTrait.php @@ -109,6 +109,18 @@ CODE; } /** + * Change class name migration to $class + * @param string $class name class + * @return string content generated class migration + * @see https://github.com/yiisoft/yii2/pull/10213 + */ + protected function parseNameClassMigration($class) + { + $files = FileHelper::findFiles($this->migrationPath); + return preg_replace('/class (m\d+_\d+) extents Migration {/', "class $class extends Migration {", file_get_contents($files[0])); + } + + /** * Checks if applied migration history matches expected one. * @param array $expectedMigrations migration names in expected order * @param string $message failure message @@ -154,7 +166,7 @@ CODE; $migrationName = 'DefaultTest'; $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $this->runMigrateControllerAction('create', [$migrationName]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $newLine = '\n'; $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); } public function testGenerateCreateMigration() @@ -203,7 +215,7 @@ CODE; 'body:text:notNull' ] ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $this->runMigrateControllerAction('create', [ @@ -239,7 +251,7 @@ CODE; ], ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $this->runMigrateControllerAction('create', [ @@ -270,7 +282,7 @@ CODE; 'fields' => [ ], ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); } public function testGenerateDropMigration() @@ -302,8 +314,40 @@ CODE; $this->runMigrateControllerAction('create', [ $migrationName ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); + + $code = <<dropTable('test'); + } + + public function down() + { + \$this->createTable('test', [ + 'id' => \$this->primaryKey() + ]); + } +} + +CODE; + $this->assertEqualsWithoutLE($code, $file); + + $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; + $this->runMigrateControllerAction('create', [ + $migrationName, + 'fields' => [ + 'body:text:notNull' + ], + ]); + $file = $this->parseNameClassMigration($class); $code = <<createTable('test', [ + 'id' => \$this->primaryKey(), + 'body' => \$this->text()->notNull() ]); } } CODE; - $this->assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $this->runMigrateControllerAction('create', [ @@ -335,7 +381,7 @@ CODE; ], ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); } public function testGenerateAddColumnMigration() @@ -373,7 +419,7 @@ CODE; 'created_at:dateTime' ] ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); } public function testGenerateDropColumnMigration() @@ -413,7 +459,7 @@ CODE; 'created_at:dateTime' ] ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $this->runMigrateControllerAction('create', [ @@ -449,7 +495,7 @@ CODE; 'created_at:dateTime' ] ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); } public function testGenerateCreateJunctionMigration() @@ -484,7 +530,7 @@ CODE; $this->runMigrateControllerAction('create', [ $migrationName, ]); - $files = FileHelper::findFiles($this->migrationPath); + $file = $this->parseNameClassMigration($class); $code = <<assertEqualsWithoutLE($code, file_get_contents($files[0])); + $this->assertEqualsWithoutLE($code, $file); } public function testUp()