Skip to content

Instantly share code, notes, and snippets.

@hugohenrique
Last active July 12, 2016 15:29
Show Gist options
  • Save hugohenrique/3f99f630737d702a4f93244900ff97df to your computer and use it in GitHub Desktop.
Save hugohenrique/3f99f630737d702a4f93244900ff97df to your computer and use it in GitHub Desktop.
<?php
namespace App\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Component\Console\Application;
class ConsoleServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app['console'] = function () {
return new Application('MyApp', 'n/a');
};
}
}
<?php
namespace App\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
class DoctrineMigrationsServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
if (!isset($app['db'])) {
throw new \LogicException('You must register the DoctrineServiceProvider to use the DoctrineMigrationsServiceProvider.');
}
if (!isset($app['console'])) {
throw new \LogicException('You must register the ConsoleServiceProvider to use the DoctrineMigrationsServiceProvider.');
}
$app['migrations.configuration'] = function ($app) {
$options = $app['migrations.options'];
$config = new Configuration($app['db']);
$config->setName($options['name']);
$config->setMigrationsTableName($options['table_name']);
$config->setMigrationsDirectory($options['directory']);
$config->setMigrationsNamespace($options['namespace']);
$config->registerMigrationsFromDirectory($options['directory']);
return $config;
};
$app['console'] = $app->extend('console', function ($console) use ($app) {
/** @var Application $console */
$console->getHelperSet()->set(new QuestionHelper());
$commands = array(
new ExecuteCommand(),
new GenerateCommand(),
new MigrateCommand(),
new StatusCommand(),
new VersionCommand(),
);
if ($console->getHelperSet()->has('em')) {
$commands[] = new DiffCommand();
}
foreach ($commands as $command) {
/** @var \Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand $command */
$command->setMigrationConfiguration($app['migrations.configuration']);
$console->add($command);
}
return $console;
});
$app['migrations.options'] = [
'name' => null,
'namespace' => 'DoctrineMigrations',
'table_name' => 'doctrine_migration_versions',
'directory' => null,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment