Skip to content

Commit

Permalink
added a DI extension for DoctrineMigrations, removed --bundle option …
Browse files Browse the repository at this point in the history
…in favor of application level settings
  • Loading branch information
lsmith77 committed Feb 20, 2011
1 parent 143756b commit db0c470
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 19 deletions.
16 changes: 9 additions & 7 deletions Command/DoctrineCommand.php
Expand Up @@ -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.
Expand All @@ -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'));
}
}
3 changes: 1 addition & 2 deletions Command/MigrationsDiffDoctrineCommand.php
Expand Up @@ -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.')
;
}
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Command/MigrationsExecuteDoctrineCommand.php
Expand Up @@ -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.')
;
}
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Command/MigrationsGenerateDoctrineCommand.php
Expand Up @@ -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.')
;
}
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Command/MigrationsMigrateDoctrineCommand.php
Expand Up @@ -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.')
;
}
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Command/MigrationsStatusDoctrineCommand.php
Expand Up @@ -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.')
;
}
Expand All @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions Command/MigrationsVersionDoctrineCommand.php
Expand Up @@ -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.')
;
}
Expand All @@ -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);
}
}
}
33 changes: 33 additions & 0 deletions DependencyInjection/Configuration.php
@@ -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();
}
}
60 changes: 60 additions & 0 deletions DependencyInjection/DoctrineMigrationsExtension.php
@@ -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';
}
}

0 comments on commit db0c470

Please sign in to comment.