davedevelopment / phpmig

Simple migrations system for php
Other
569 stars 92 forks source link

GenerateCommand decoupling #103

Closed spideyfusion closed 8 years ago

spideyfusion commented 8 years ago

Currently it's impossible to run the generate command outside of the console context (main application). Consider the following scenario which happened when I was making new commands with Robo (one of its features is that it can run Symfony Console commands):

/**
 * Generates a new migration.
 */
public function phpmigNew()
{
    $migrationGeneration = $this->taskSymfonyCommand(new \Phpmig\Console\Command\GenerateCommand());

    $migrationName = $this->ask('Name of the new migration');

    if (empty($migrationName)) {
        throw new InvalidArgumentException('You must provide a name for the migration.');
    }

    $migrationGeneration->arg('name', $migrationName);

    if ($this->confirm('Is this migration unsafe?')) {
        $migrationGeneration->opt('set', 'unsafe');
    }

    $migrationGeneration->run();
}

If you run this on an empty migration folder it runs without issues, but otherwise you get the following exception:

The "command" argument does not exist.

This happens because in the AbstractCommand class the getArgument method is used to check which command is currently running. The command argument only gets set if the command is ran within the Symfony Console application. See getDefaultInputDefinition method inside the Application class of the Symfony Console package:

https://github.com/symfony/console/blob/master/Application.php#L870

davedevelopment commented 8 years ago

Thanks!