Skip to content

Commit 11a9668

Browse files
committed
Add multiple configuration formats support
1 parent 7dc6fd3 commit 11a9668

7 files changed

Lines changed: 122 additions & 6 deletions

File tree

Migrate/Command/AbstractEnvCommand.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
namespace Migrate\Command;
1010

1111

12+
use Migrate\Config\ConfigLocator;
1213
use Migrate\Migration;
1314
use Migrate\Utils\ArrayUtil;
14-
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
17-
use Symfony\Component\Yaml\Yaml;
1817

1918
class AbstractEnvCommand extends AbstractComand
2019
{
@@ -61,16 +60,20 @@ protected function checkEnv()
6160

6261
protected function init(InputInterface $input, OutputInterface $output, $env = null)
6362
{
64-
$configDirectory = array(getcwd() . '/.php-database-migration/environments');
65-
$locator = new FileLocator($configDirectory);
63+
$configDirectory = getcwd() . '/.php-database-migration/environments';
64+
$configLocator = new ConfigLocator($configDirectory);
6665

6766
if ($env === null) {
6867
$env = $input->getArgument('env');
6968
}
7069

71-
$envFile = $locator->locate($env . '.yml');
70+
$parser = $configLocator->locate($env);
7271

73-
$conf = Yaml::parse(file_get_contents($envFile));
72+
if (is_null($parser)) {
73+
throw new \RuntimeException("Could not locate supported environment file");
74+
}
75+
76+
$conf = $parser->parse();
7477

7578
$this->config = $conf;
7679

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: roman
5+
* Date: 7/24/17
6+
* Time: 09:47
7+
*/
8+
9+
namespace Migrate\Config;
10+
11+
abstract class BaseConfigParser implements ConfigParser
12+
{
13+
protected $configFile;
14+
15+
public function __construct($configFile)
16+
{
17+
$this->configFile = $configFile;
18+
}
19+
20+
abstract public function parse();
21+
}

Migrate/Config/ConfigLocator.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: roman
5+
* Date: 7/24/17
6+
* Time: 09:49
7+
*/
8+
9+
namespace Migrate\Config;
10+
11+
class ConfigLocator
12+
{
13+
static $SUPPORTED_PARSERS = [
14+
'yaml' => '\Migrate\Config\YamlConfigParser',
15+
'json' => '\Migrate\Config\JsonConfigParser',
16+
'php' => '\Migrate\Config\PhpConfigParser'
17+
];
18+
19+
private $configPath;
20+
21+
public function __construct($configPath)
22+
{
23+
$this->configPath = $configPath;
24+
}
25+
26+
public function locate($nameWithoutExt)
27+
{
28+
foreach (array_keys(self::$SUPPORTED_PARSERS) as $format) {
29+
$path = $this->configPath . '/' . $nameWithoutExt . '.' . $format;
30+
if (file_exists($path)) {
31+
$parserClass = self::$SUPPORTED_PARSERS[$format];
32+
return new $parserClass($path);
33+
}
34+
}
35+
return null;
36+
}
37+
}

Migrate/Config/ConfigParser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Migrate\Config;
4+
5+
interface ConfigParser
6+
{
7+
public function parse();
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Migrate\Config;
4+
5+
class JsonConfigParser extends BaseConfigParser
6+
{
7+
public function parse()
8+
{
9+
return json_decode(file_get_contents($this->configFile), true);
10+
}
11+
}

Migrate/Config/PhpConfigParser.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: roman
5+
* Date: 7/24/17
6+
* Time: 09:45
7+
*/
8+
9+
namespace Migrate\Config;
10+
11+
class PhpConfigParser extends BaseConfigParser
12+
{
13+
public function parse()
14+
{
15+
return require $this->configFile;
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: roman
5+
* Date: 7/24/17
6+
* Time: 09:41
7+
*/
8+
9+
namespace Migrate\Config;
10+
11+
use Symfony\Component\Yaml\Yaml;
12+
13+
class YamlConfigParser extends BaseConfigParser
14+
{
15+
public function parse()
16+
{
17+
return Yaml::parse(file_get_contents($this->configFile));
18+
}
19+
}

0 commit comments

Comments
 (0)