Skip to content

Commit c2cc16f

Browse files
committed
Added support of the organize migrations configuration.
1 parent 6fcc8c5 commit c2cc16f

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

Command/DoctrineCommand.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
namespace Doctrine\Bundle\MigrationsBundle\Command;
1616

17-
use Symfony\Component\DependencyInjection\ContainerInterface;
18-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19-
use Symfony\Bundle\FrameworkBundle\Console\Application;
2017
use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand as BaseCommand;
21-
use Doctrine\DBAL\Migrations\Configuration\Configuration;
2218
use Doctrine\DBAL\Migrations\Configuration\AbstractFileConfiguration;
19+
use Doctrine\DBAL\Migrations\Configuration\Configuration;
20+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
21+
use Symfony\Component\DependencyInjection\ContainerInterface;
22+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2323

2424
/**
2525
* Base class for Doctrine console commands to extend from.
@@ -65,6 +65,23 @@ public static function configureMigrations(ContainerInterface $container, Config
6565
$configuration->registerMigrationsFromDirectory($configuration->getMigrationsDirectory());
6666
}
6767

68+
$organizeMigrations = $container->getParameter('doctrine_migrations.organize_migrations');
69+
switch ($organizeMigrations) {
70+
case Configuration::VERSIONS_ORGANIZATION_BY_YEAR:
71+
$configuration->setMigrationsAreOrganizedByYear(true);
72+
break;
73+
74+
case Configuration::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH:
75+
$configuration->setMigrationsAreOrganizedByYearAndMonth(true);
76+
break;
77+
78+
case false:
79+
break;
80+
81+
default:
82+
throw new InvalidArgumentException('Invalid value for "doctrine_migrations.organize_migrations" parameter.');
83+
}
84+
6885
self::injectContainerToMigrations($container, $configuration->getMigrations());
6986
}
7087

DependencyInjection/Configuration.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,70 @@ class Configuration implements ConfigurationInterface
2727
/**
2828
* Generates the configuration tree.
2929
*
30-
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The config tree builder
30+
* @return TreeBuilder The config tree builder
3131
*/
3232
public function getConfigTreeBuilder()
3333
{
3434
$treeBuilder = new TreeBuilder();
3535
$rootNode = $treeBuilder->root('doctrine_migrations', 'array');
3636

37+
$organizeMigrationModes = $this->getOrganizeMigrationsModes();
38+
3739
$rootNode
3840
->children()
3941
->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end()
4042
->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end()
4143
->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end()
4244
->scalarNode('name')->defaultValue('Application Migrations')->end()
45+
->scalarNode('organize_migrations')->defaultValue(false)
46+
->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
47+
->validate()
48+
->ifTrue(function ($v) use ($organizeMigrationModes) {
49+
if (false === $v) {
50+
return false;
51+
}
52+
53+
if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
54+
return false;
55+
}
56+
57+
return true;
58+
})
59+
->thenInvalid('Invalid organize migrations mode value %s')
60+
->end()
61+
->validate()
62+
->ifString()
63+
->then(function ($v) {
64+
return constant('Doctrine\DBAL\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_'.strtoupper($v));
65+
})
66+
->end()
67+
->end()
4368
->end()
4469
;
4570

4671
return $treeBuilder;
4772
}
73+
74+
75+
/**
76+
* Find organize migrations modes for their names
77+
*
78+
* @return array
79+
*/
80+
private function getOrganizeMigrationsModes()
81+
{
82+
$constPrefix = 'VERSIONS_ORGANIZATION_';
83+
$prefixLen = strlen($constPrefix);
84+
$refClass = new \ReflectionClass('Doctrine\DBAL\Migrations\Configuration\Configuration');
85+
$constsArray = $refClass->getConstants();
86+
$namesArray = array();
87+
88+
foreach ($constsArray as $key => $value) {
89+
if (strpos($key, $constPrefix) === 0) {
90+
$namesArray[] = substr($key, $prefixLen);
91+
}
92+
}
93+
94+
return $namesArray;
95+
}
4896
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"php": ">=5.4.0",
2424
"symfony/framework-bundle": "~2.3|~3.0",
2525
"doctrine/doctrine-bundle": "~1.0",
26-
"doctrine/migrations": "~1.0"
26+
"doctrine/migrations": "^1.1"
2727
},
2828
"autoload": {
2929
"psr-4": { "Doctrine\\Bundle\\MigrationsBundle\\": "" }

0 commit comments

Comments
 (0)