added a DI extension for DoctrineMigrations, removed --bundle option in favor of application level settings

This commit is contained in:
Lukas Kahwe Smith 2011-02-20 18:38:02 +01:00
parent 092403909e
commit c518074306
9 changed files with 111 additions and 19 deletions

View File

@ -11,10 +11,10 @@
namespace Symfony\Bundle\DoctrineMigrationsBundle\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand as BaseCommand;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\Common\Util\Inflector;
/**
* Base class for Doctrine console commands to extend from.
@ -23,15 +23,17 @@ use Doctrine\Common\Util\Inflector;
*/
abstract class DoctrineCommand extends BaseCommand
{
public static function configureMigrationsForBundle(Application $application, $bundle, Configuration $configuration)
public static function configureMigrations(ContainerInterface $container, Configuration $configuration)
{
$bundle = $application->getKernel()->getBundle($bundle);
$dir = $bundle->getPath().'/DoctrineMigrations';
$dir = $container->getParameter('doctrine_migrations.dir_name');
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
$configuration->setMigrationsNamespace($bundle->getNamespace().'\DoctrineMigrations');
$configuration->setMigrationsNamespace($container->getParameter('doctrine_migrations.namespace'));
$configuration->setMigrationsDirectory($dir);
$configuration->registerMigrationsFromDirectory($dir);
$configuration->setName($bundle->getName().' Migrations');
$configuration->setMigrationsTableName(Inflector::tableize($bundle->getName()).'_migration_versions');
$configuration->setName($container->getParameter('doctrine_migrations.name'));
$configuration->setMigrationsTableName($container->getParameter('doctrine_migrations.table_name'));
}
}

View File

@ -31,7 +31,6 @@ class MigrationsDiffDoctrineCommand extends DiffCommand
$this
->setName('doctrine:migrations:diff')
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The bundle to load migrations configuration from.')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
;
}
@ -41,7 +40,7 @@ class MigrationsDiffDoctrineCommand extends DiffCommand
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
DoctrineCommand::configureMigrationsForBundle($this->application, $input->getOption('bundle'), $configuration);
DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration);
parent::execute($input, $output);
}

View File

@ -30,7 +30,6 @@ class MigrationsExecuteDoctrineCommand extends ExecuteCommand
$this
->setName('doctrine:migrations:execute')
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The bundle to load migrations configuration from.')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
;
}
@ -40,7 +39,7 @@ class MigrationsExecuteDoctrineCommand extends ExecuteCommand
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
DoctrineCommand::configureMigrationsForBundle($this->application, $input->getOption('bundle'), $configuration);
DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration);
parent::execute($input, $output);
}

View File

@ -30,7 +30,6 @@ class MigrationsGenerateDoctrineCommand extends GenerateCommand
$this
->setName('doctrine:migrations:generate')
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The bundle to load migrations configuration from.')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
;
}
@ -40,7 +39,7 @@ class MigrationsGenerateDoctrineCommand extends GenerateCommand
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
DoctrineCommand::configureMigrationsForBundle($this->application, $input->getOption('bundle'), $configuration);
DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration);
parent::execute($input, $output);
}

View File

@ -30,7 +30,6 @@ class MigrationsMigrateDoctrineCommand extends MigrateCommand
$this
->setName('doctrine:migrations:migrate')
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The bundle to load migrations configuration from.')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
;
}
@ -40,7 +39,7 @@ class MigrationsMigrateDoctrineCommand extends MigrateCommand
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
DoctrineCommand::configureMigrationsForBundle($this->application, $input->getOption('bundle'), $configuration);
DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration);
parent::execute($input, $output);
}

View File

@ -30,7 +30,6 @@ class MigrationsStatusDoctrineCommand extends StatusCommand
$this
->setName('doctrine:migrations:status')
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The bundle to load migrations configuration from.')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
;
}
@ -40,7 +39,7 @@ class MigrationsStatusDoctrineCommand extends StatusCommand
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
DoctrineCommand::configureMigrationsForBundle($this->application, $input->getOption('bundle'), $configuration);
DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration);
parent::execute($input, $output);
}

View File

@ -30,7 +30,6 @@ class MigrationsVersionDoctrineCommand extends VersionCommand
$this
->setName('doctrine:migrations:version')
->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The bundle to load migrations configuration from.')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
;
}
@ -39,6 +38,9 @@ class MigrationsVersionDoctrineCommand extends VersionCommand
{
DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration);
parent::execute($input, $output);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Symfony\Bundle\DoctrineMigrationsBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
/**
* DoctrineMigrationsExtension configuration structure.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class Configuration
{
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\Config\Definition\NodeInterface
*/
public function getConfigTree()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('doctrine_migrations', 'array');
$rootNode
->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end()
->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end()
->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end()
->scalarNode('name')->defaultValue('Application Migrations')->end();
return $treeBuilder->buildTree();
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMigrationsBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* DoctrineMigrationsExtension.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class DoctrineMigrationsExtension extends Extension
{
/**
* Responds to the twig configuration parameter.
*
* @param array $configs
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->process($configuration->getConfigTree(), $configs);
foreach ($config as $key => $value) {
$container->setParameter($this->getAlias().'.'.$key, $value);
}
}
/**
* Returns the base path for the XSD files.
*
* @return string The XSD base path
*/
public function getXsdValidationBasePath()
{
return __DIR__.'/../Resources/config/schema';
}
public function getNamespace()
{
return 'http://www.symfony-project.org/schema/dic/doctrine/migrations';
}
}