Skip to content

Commit

Permalink
refactoring the configuration loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeSimonson committed Aug 7, 2015
1 parent 873d399 commit e95b277
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,57 @@ abstract class AbstractFileConfiguration extends Configuration
*/
private $loaded = false;

/**
* @var array of possible configuration properties in migrations configuration.
*/
private $configurationProperties = [
'name' => 'setName',
'table_name' => 'setMigrationsTableName',
'migrations_namespace' => 'setMigrationsNamespace',
'organize_migrations' => 'setMigrationOrganisation',
'migrations_directory' => 'loadMigrationsFromDirectory',
'migrations' => 'loadMigrations',
];

protected function setConfiguration(Array $config)
{
foreach($config as $configurationKey => $configurationValue) {
if (!isset($this->configurationProperties[$configurationKey])) {
throw MigrationException::configurationKeyDoesNotExists($configurationKey);
}
$this->{$this->configurationProperties[$configurationKey]}($configurationValue);
}
}

private function loadMigrationsFromDirectory($migrationsDirectory)
{
$this->setMigrationsDirectory($migrationsDirectory);
$this->registerMigrationsFromDirectory($migrationsDirectory);
}

private function loadMigrations($migrations)
{
if (is_array($migrations)) {
foreach ($migrations as $migration) {
$this->registerMigration($migration['version'], $migration['class']);
}
}
}

private function setMigrationOrganisation($migrationOrganisation)
{
if (strcasecmp($migrationOrganisation, static::VERSIONS_ORGANIZATION_BY_YEAR) == 0) {
$this->setMigrationsAreOrganizedByYear();
} else if (strcasecmp($migrationOrganisation, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) == 0) {
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
trigger_error(
'Unknown ' . var_export($migrationOrganisation, true) . ' for configuration "organize_migrations".',
E_USER_NOTICE
);
}
}

/**
* Load the information from the passed configuration file
*
Expand Down
37 changes: 5 additions & 32 deletions lib/Doctrine/DBAL/Migrations/Configuration/ArrayConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,12 @@ protected function doLoad($file)
throw new \InvalidArgumentException('Given config file does not exist');
}

$array = require $file;
$config = require $file;

if (isset($array['name'])) {
$this->setName($array['name']);
}
if (isset($array['table_name'])) {
$this->setMigrationsTableName($array['table_name']);
}
if (isset($array['migrations_namespace'])) {
$this->setMigrationsNamespace($array['migrations_namespace']);
}
if (isset($array['organize_migrations'])) {
$versions_organization = $array['organize_migrations'];
if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR) == 0) {
$this->setMigrationsAreOrganizedByYear();
} else if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) == 0) {
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
trigger_error(
'Unknown ' . var_export($versions_organization, true) . ' for configuration "organize_migrations".',
E_USER_NOTICE
);
}
}
if (isset($array['migrations_directory'])) {
$migrationsDirectory = $this->getDirectoryRelativeToFile($file, $array['migrations_directory']);
$this->setMigrationsDirectory($migrationsDirectory);
$this->registerMigrationsFromDirectory($migrationsDirectory);
}
if (isset($array['migrations']) && is_array($array['migrations'])) {
foreach ($array['migrations'] as $migration) {
$this->registerMigration($migration['version'], $migration['class']);
}
if (isset($config['migrations_directory'])) {
$config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, $config['migrations_directory']);
}

$this->setConfiguration($config);
}
}
38 changes: 6 additions & 32 deletions lib/Doctrine/DBAL/Migrations/Configuration/JsonConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,13 @@ protected function doLoad($file)
throw new \InvalidArgumentException('Given config file does not exist');
}

$array = json_decode(file_get_contents($file), true);
$config = json_decode(file_get_contents($file), true);

if (isset($array['name'])) {
$this->setName($array['name']);
}
if (isset($array['table_name'])) {
$this->setMigrationsTableName($array['table_name']);
}
if (isset($array['migrations_namespace'])) {
$this->setMigrationsNamespace($array['migrations_namespace']);
}
if (isset($array['organize_migrations'])) {
$versions_organization = $array['organize_migrations'];
if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR) == 0) {
$this->setMigrationsAreOrganizedByYear();
} else if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) == 0) {
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
trigger_error(
'Unknown ' . var_export($versions_organization, true) . ' for configuration "organize_migrations".',
E_USER_NOTICE
);
}
}
if (isset($array['migrations_directory'])) {
$migrationsDirectory = $this->getDirectoryRelativeToFile($file, $array['migrations_directory']);
$this->setMigrationsDirectory($migrationsDirectory);
$this->registerMigrationsFromDirectory($migrationsDirectory);
}
if (isset($array['migrations']) && is_array($array['migrations'])) {
foreach ($array['migrations'] as $migration) {
$this->registerMigration($migration['version'], $migration['class']);
}
if (isset($config['migrations_directory'])) {
$config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, $config['migrations_directory']);
}

$this->setConfiguration($config);

}
}
33 changes: 13 additions & 20 deletions lib/Doctrine/DBAL/Migrations/Configuration/XmlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,31 @@ class XmlConfiguration extends AbstractFileConfiguration
*/
protected function doLoad($file)
{
if (!file_exists($file)) {
throw new \InvalidArgumentException('Given config file does not exist');
}

$config = [];
$xml = simplexml_load_file($file);
if (isset($xml->name)) {
$this->setName((string) $xml->name);
$config['name'] = (string) $xml->name;
}
if (isset($xml->table['name'])) {
$this->setMigrationsTableName((string) $xml->table['name']);
$config['table_name'] = (string) $xml->table['name'];
}
if (isset($xml->{'migrations-namespace'})) {
$this->setMigrationsNamespace((string) $xml->{'migrations-namespace'});
$config['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
}
if (isset($xml->{'organize-migrations'})) {
$versions_organization = $xml->{'organize-migrations'};
if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR) === 0) {
$this->setMigrationsAreOrganizedByYear();
} else if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) === 0) {
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
trigger_error(
'Unknown ' . var_export($versions_organization, true) . ' for configuration "organize-migrations".',
E_USER_NOTICE
);
}
$config['organize_migrations'] = $xml->{'organize-migrations'};
}
if (isset($xml->{'migrations-directory'})) {
$migrationsDirectory = $this->getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
$this->setMigrationsDirectory($migrationsDirectory);
$this->registerMigrationsFromDirectory($migrationsDirectory);
$config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
}
if (isset($xml->migrations->migration)) {
foreach ($xml->migrations->migration as $migration) {
$this->registerMigration((string) $migration['version'], (string) $migration['class']);
}
$config['migrations'] = $xml->migrations->migration;
}

$this->setConfiguration($config);
}
}
41 changes: 9 additions & 32 deletions lib/Doctrine/DBAL/Migrations/Configuration/YamlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,16 @@ class YamlConfiguration extends AbstractFileConfiguration
*/
protected function doLoad($file)
{
$array = Yaml::parse(file_get_contents($file));

if (isset($array['name'])) {
$this->setName($array['name']);
}
if (isset($array['table_name'])) {
$this->setMigrationsTableName($array['table_name']);
}
if (isset($array['migrations_namespace'])) {
$this->setMigrationsNamespace($array['migrations_namespace']);
}
if (isset($array['organize_migrations'])) {
$versions_organization = $array['organize_migrations'];
if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR) == 0) {
$this->setMigrationsAreOrganizedByYear();
} else if (strcasecmp($versions_organization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) == 0) {
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
trigger_error(
'Unknown ' . var_export($versions_organization, true) . ' for configuration "organize_migrations".',
E_USER_NOTICE
);
}
if (!file_exists($file)) {
throw new \InvalidArgumentException('Given config file does not exist');
}
if (isset($array['migrations_directory'])) {
$migrationsDirectory = $this->getDirectoryRelativeToFile($file, $array['migrations_directory']);
$this->setMigrationsDirectory($migrationsDirectory);
$this->registerMigrationsFromDirectory($migrationsDirectory);
}
if (isset($array['migrations']) && is_array($array['migrations'])) {
foreach ($array['migrations'] as $migration) {
$this->registerMigration($migration['version'], $migration['class']);
}

$config = Yaml::parse(file_get_contents($file));

if (isset($config['migrations_directory'])) {
$config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, $config['migrations_directory']);
}

$this->setConfiguration($config);
}
}
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Migrations/MigrationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public static function configurationIncompatibleWithFinder(
9
);
}

public static function configurationKeyDoesNotExists($key)
{
return new self(sprintf('Migrations configuration key "%s" does not exists.', $key), 10);
}
}

0 comments on commit e95b277

Please sign in to comment.