Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] fix migration creator to validated existing migration class #292

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Console/Migrations/MigrateMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public function handle()
// Now we are ready to write the migration out to disk. Once we've written
// the migration out, we will dump-autoload for the entire framework to
// make sure that the migrations are registered by the class loaders.
$this->writeMigration($name, $table, $create);
try {
$this->writeMigration($name, $table, $create);

}catch (\InvalidArgumentException $e)
{
$this->components->error($e->getMessage());
}
}

/**
Expand Down
39 changes: 32 additions & 7 deletions Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(Filesystem $files, $customStubPath)
*/
public function create($name, $path, $table = null, $create = false)
{
$this->ensureMigrationDoesntAlreadyExist($name, $path);
$this->ensureMigrationDoesntAlreadyExist($table, $path);

// First we will get the stub file for the migration, which serves as a type
// of template for the migration. Once we have those we will populate the
Expand All @@ -82,24 +82,49 @@ public function create($name, $path, $table = null, $create = false)
/**
* Ensure that a migration with the given name doesn't already exist.
*
* @param string $name
* @param string $migrationPath
* @param string $table
* @param string $migrationPath
* @return void
*
* @throws \InvalidArgumentException
* @throws \ReflectionException
*/
protected function ensureMigrationDoesntAlreadyExist($name, $migrationPath = null)
protected function ensureMigrationDoesntAlreadyExist($table, $migrationPath = null)
{
if (! empty($migrationPath)) {
$migrationFiles = $this->files->glob($migrationPath.'/*.php');

foreach ($migrationFiles as $migrationFile) {
$this->files->requireOnce($migrationFile);
$this->throwAnExceptionIfExists($table, $migrationFile);
}
}
}

if (class_exists($className = $this->getClassName($name))) {
throw new InvalidArgumentException("A {$className} class already exists.");
/**
* @param string $table
* @param string $migrationPath
* @return void
*
* @throws \ReflectionException
*/
protected function throwAnExceptionIfExists($table, $migrationPath)
{
$migrationClass = (new \ReflectionClass($this->files->requireOnce($migrationPath)));
$upMethod = $migrationClass->getMethod('up');

$fileLines = file($upMethod->getFileName());
$functionLines = array_slice($fileLines, $upMethod->getStartLine() - 1, $upMethod->getEndLine() - $upMethod->getStartLine() + 1);
$functionCode = implode('', $functionLines);

preg_match_all("/create\('([^']+)/", $functionCode, $matches);

if (!empty($matches[1])) {
foreach ($matches[1] as $match) {
if($match == $table) {
$baseName = basename($migrationPath, '.php');
throw new InvalidArgumentException("A {$baseName} class already exists.");
}
}
}
}

Expand Down
Loading