From 6ec6f8d76cc637ede75a1c601d4d37b7d36a0347 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Fri, 2 Jun 2017 13:32:02 +0200 Subject: [PATCH] moved include file logic to separate method --- docs/guide/db-migrations.md | 5 +++-- .../controllers/BaseMigrateController.php | 17 +++++++++++++++-- .../console/controllers/MigrateController.php | 18 +----------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index 2fa582e572..e7ae53a688 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -835,9 +835,10 @@ The migration command comes with a few command-line options that can be used to When this is `true`, the user will be prompted before the command performs certain actions. You may want to set this to `false` if the command is being used in a background process. -* `migrationPath`: string (defaults to `@app/migrations`), specifies the directory storing all migration +* `migrationPath`: string|array (defaults to `@app/migrations`), specifies the directory storing all migration class files. This can be specified as either a directory path or a path [alias](concept-aliases.md). - Note that the directory must exist, or the command may trigger an error. + Note that the directory must exist, or the command may trigger an error. Since version 2.0.12 an array can be + specified for loading migrations from multiple sources. * `migrationTable`: string (defaults to `migration`), specifies the name of the database table for storing migration history information. The table will be automatically created by the command if it does not exist. diff --git a/framework/console/controllers/BaseMigrateController.php b/framework/console/controllers/BaseMigrateController.php index 2b97df2a5d..a4d05c8bec 100644 --- a/framework/console/controllers/BaseMigrateController.php +++ b/framework/console/controllers/BaseMigrateController.php @@ -716,6 +716,21 @@ abstract class BaseMigrateController extends Controller * @return \yii\db\MigrationInterface the migration instance */ protected function createMigration($class) + { + $this->includeMigrationFile($class); + return new $class(); + } + + /** + * Includes the migration file for a given migration class name. + * + * This function will do nothing on namespaced migrations, which are loaded by + * autoloading automatically. It will include the migration file, by searching + * [[migrationPath]] for classes without namespace. + * @param string $class the migration class name. + * @since 2.0.12 + */ + protected function includeMigrationFile($class) { $class = trim($class, '\\'); if (strpos($class, '\\') === false) { @@ -732,8 +747,6 @@ abstract class BaseMigrateController extends Controller require_once($file); } } - - return new $class(); } /** diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 3df4136c73..c6c9315295 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -182,23 +182,7 @@ class MigrateController extends BaseMigrateController */ protected function createMigration($class) { - $class = trim($class, '\\'); - if (strpos($class, '\\') === false) { - if (is_array($this->migrationPath)) { - foreach($this->migrationPath as $path) { - $file = $path . DIRECTORY_SEPARATOR . $class . '.php'; - if (is_file($file)) { - require_once($file); - break; - } - } - } else { - $file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php'; - require_once($file); - } - } - - + $this->includeMigrationFile($class); return new $class(['db' => $this->db]); }