Skip to content

Commit c0a2f5d

Browse files
committed
Merge pull request #153 from Koc/organize-migrations
Added support of the organize migrations configuration.
2 parents 6fcc8c5 + 4dd7fbe commit c0a2f5d

File tree

5 files changed

+130
-6
lines changed

5 files changed

+130
-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
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Doctrine\Bundle\MigrationsBundle\Tests\DependencyInjection;
4+
5+
use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
6+
use Doctrine\DBAL\Migrations\Configuration\Configuration;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
9+
10+
class DoctrineMigrationsExtensionTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testOrganizeMigrations()
13+
{
14+
$container = $this->getContainer();
15+
$extension = new DoctrineMigrationsExtension();
16+
17+
$config = array(
18+
'organize_migrations' => 'BY_YEAR',
19+
);
20+
21+
$extension->load(array('doctrine_migrations' => $config), $container);
22+
23+
$this->assertEquals(Configuration::VERSIONS_ORGANIZATION_BY_YEAR, $container->getParameter('doctrine_migrations.organize_migrations'));
24+
}
25+
26+
private function getContainer()
27+
{
28+
return new ContainerBuilder(new ParameterBag(array(
29+
'kernel.debug' => false,
30+
'kernel.bundles' => array(),
31+
'kernel.cache_dir' => sys_get_temp_dir(),
32+
'kernel.environment' => 'test',
33+
'kernel.root_dir' => __DIR__.'/../../', // src dir
34+
)));
35+
}
36+
}

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
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"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "~4.8"
2730
},
2831
"autoload": {
2932
"psr-4": { "Doctrine\\Bundle\\MigrationsBundle\\": "" }

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit colors="true" bootstrap="vendor/autoload.php">
4+
<testsuites>
5+
<testsuite name="DoctrineMigrationsBundle for the Symfony Framework">
6+
<directory>./Tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
10+
<filter>
11+
<whitelist>
12+
<directory>.</directory>
13+
<exclude>
14+
<directory>./Resources</directory>
15+
<directory>./Tests</directory>
16+
<directory>./vendor</directory>
17+
</exclude>
18+
</whitelist>
19+
</filter>
20+
</phpunit>

0 commit comments

Comments
 (0)