Framework Independent Doctrine CLI Tools

Most frameworks that integrate with Doctrine Migrations implement their own wrapper commands. If you want to use them independently you'll need a fair amount of boilerplate code as well as a custom configuration file.

Most frameworks that integrate with Doctrine Migrations implement their own wrapper commands. If you want to use them independently you'll need a fair amount of boilerplate code as well as a custom configuration file.

If like me you prefer to have a central configuration managed by a DI container then this can be a problem.

So to the rescue comes Doctrine Migrations Helper which is a straightforwward Symfony Console Command provider which alows you to inject your own configuration.

Installing

In your composer.json file just add in your require block...

"require": {
    "rossriley/doctrine-migrations-helper"  : "~1.0"
}

Usage

Ideally you'll already have a console.php file which provides a Symfony Console Runner. In that file you can just add something like the following.

use DoctrineMigrationsHelper\CommandSet;

$migrationSet = new CommandSet($container->get("db-connection"));
$migrationSet->configuration = $container->get("migrations");

$console->addCommands($migrationSet->getCommands());

This usage assumes you have a container that is able to provide the two configurations, and that you have a console app initialised to $console.

For reference, if you want to manually configure the setup all you need to provide is a standard DBAL driver connection. It may look like the following:

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration;
use DoctrineMigrationsHelper\CommandSet;

$connection = DriverManager::getConnection([
            'dbname' =>   "test",
            'user' =>     "test",
            'password'=>  "secret",
            'host' =>     "localhost",
            'driver' =>   "pdo_mysql"
        ]);

$migrationSet = new CommandSet($connection);
$console->addCommands($migrationSet->getCommands());

Advanced Configuration

Once constructed, you have access to the Doctrine Migrations Configuration object so you can still manually override the default settings. Here's an example of how to set the namespace.

...

$migrationSet = new CommandSet($connection);
$migrationSet->configuration->setMigrationsNamespace("Example\Project\Migrations");
$console->addCommands($migrationSet->getCommands());

The configiration will be applied globally to all the commands before returning them as a simple array which you can add to your console app.

Contributions and feedback welcome, the project is on Github here, and for Composer install on Packagist here.