From db0c470f946c8a44c236a0adf392066b67078c28 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Sun, 20 Feb 2011 18:38:02 +0100 Subject: [PATCH] added a DI extension for DoctrineMigrations, removed --bundle option in favor of application level settings --- Command/DoctrineCommand.php | 16 ++--- Command/MigrationsDiffDoctrineCommand.php | 3 +- Command/MigrationsExecuteDoctrineCommand.php | 3 +- Command/MigrationsGenerateDoctrineCommand.php | 3 +- Command/MigrationsMigrateDoctrineCommand.php | 3 +- Command/MigrationsStatusDoctrineCommand.php | 3 +- Command/MigrationsVersionDoctrineCommand.php | 6 +- DependencyInjection/Configuration.php | 33 ++++++++++ .../DoctrineMigrationsExtension.php | 60 +++++++++++++++++++ 9 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/DoctrineMigrationsExtension.php diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php index bed7b9e..10a1c96 100644 --- a/Command/DoctrineCommand.php +++ b/Command/DoctrineCommand.php @@ -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 @@ */ 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')); } } diff --git a/Command/MigrationsDiffDoctrineCommand.php b/Command/MigrationsDiffDoctrineCommand.php index aab81ed..b96d48b 100644 --- a/Command/MigrationsDiffDoctrineCommand.php +++ b/Command/MigrationsDiffDoctrineCommand.php @@ -31,7 +31,6 @@ protected function configure() $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 @@ public function execute(InputInterface $input, OutputInterface $output) 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); } diff --git a/Command/MigrationsExecuteDoctrineCommand.php b/Command/MigrationsExecuteDoctrineCommand.php index 8dbd1f1..6b1b29c 100644 --- a/Command/MigrationsExecuteDoctrineCommand.php +++ b/Command/MigrationsExecuteDoctrineCommand.php @@ -30,7 +30,6 @@ protected function configure() $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 @@ public function execute(InputInterface $input, OutputInterface $output) 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); } diff --git a/Command/MigrationsGenerateDoctrineCommand.php b/Command/MigrationsGenerateDoctrineCommand.php index 4e668ff..e03541a 100644 --- a/Command/MigrationsGenerateDoctrineCommand.php +++ b/Command/MigrationsGenerateDoctrineCommand.php @@ -30,7 +30,6 @@ protected function configure() $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 @@ public function execute(InputInterface $input, OutputInterface $output) 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); } diff --git a/Command/MigrationsMigrateDoctrineCommand.php b/Command/MigrationsMigrateDoctrineCommand.php index d2c7695..da28fbc 100644 --- a/Command/MigrationsMigrateDoctrineCommand.php +++ b/Command/MigrationsMigrateDoctrineCommand.php @@ -30,7 +30,6 @@ protected function configure() $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 @@ public function execute(InputInterface $input, OutputInterface $output) 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); } diff --git a/Command/MigrationsStatusDoctrineCommand.php b/Command/MigrationsStatusDoctrineCommand.php index 0766028..e4380a8 100644 --- a/Command/MigrationsStatusDoctrineCommand.php +++ b/Command/MigrationsStatusDoctrineCommand.php @@ -30,7 +30,6 @@ protected function configure() $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 @@ public function execute(InputInterface $input, OutputInterface $output) 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); } diff --git a/Command/MigrationsVersionDoctrineCommand.php b/Command/MigrationsVersionDoctrineCommand.php index 2ff4f27..c5fac35 100644 --- a/Command/MigrationsVersionDoctrineCommand.php +++ b/Command/MigrationsVersionDoctrineCommand.php @@ -30,7 +30,6 @@ protected function configure() $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 @@ public function execute(InputInterface $input, OutputInterface $output) { DoctrineCommand::setApplicationEntityManager($this->application, $input->getOption('em')); + $configuration = $this->getMigrationConfiguration($input, $output); + DoctrineCommand::configureMigrations($this->application->getKernel()->getContainer(), $configuration); + parent::execute($input, $output); } -} \ No newline at end of file +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..a91eaa2 --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,33 @@ + + */ +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(); + } +} diff --git a/DependencyInjection/DoctrineMigrationsExtension.php b/DependencyInjection/DoctrineMigrationsExtension.php new file mode 100644 index 0000000..ac444a5 --- /dev/null +++ b/DependencyInjection/DoctrineMigrationsExtension.php @@ -0,0 +1,60 @@ + + * + * 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 + */ +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'; + } +}