Skip to content

Commit db2dce4

Browse files
author
Alexandre Guidet
committed
Merge branch 'release-3.7.0'
2 parents 4fef64c + dbe5c5f commit db2dce4

12 files changed

Lines changed: 203 additions & 10 deletions

Migrate/Command/AbstractEnvCommand.php

Lines changed: 5 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,16 @@ 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+
$conf = $parser->parse();
7473

7574
$this->config = $conf;
7675

Migrate/Command/AddEnvCommand.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Migrate\Command;
99

10+
use Migrate\Config\ConfigLocator;
11+
use Symfony\Component\Console\Input\InputArgument;
1012
use Symfony\Component\Console\Input\InputInterface;
1113
use Symfony\Component\Console\Output\OutputInterface;
1214
use Symfony\Component\Console\Question\ChoiceQuestion;
@@ -19,11 +21,26 @@ protected function configure()
1921
$this
2022
->setName('migrate:addenv')
2123
->setDescription('Initialise an environment to work with php db migrate')
24+
->addArgument(
25+
'format',
26+
InputArgument::OPTIONAL,
27+
'Environment file format: (yml, json or php), default: yml'
28+
)
2229
;
2330
}
2431

2532
protected function execute(InputInterface $input, OutputInterface $output)
2633
{
34+
$format = $input->getArgument('format');
35+
$supportedFormats = array_keys(ConfigLocator::$SUPPORTED_PARSERS);
36+
37+
if (is_null($format)) {
38+
$format = 'yml';
39+
}
40+
41+
if (!in_array($format, $supportedFormats)) {
42+
throw new \RuntimeException(sprintf('Invalid file format: %s', $format));
43+
}
2744

2845
// init directories
2946
if(! file_exists($this->getMainDir())) {
@@ -46,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4663
$envQuestion = new Question("Please enter the name of the new environment <info>(default dev)</info>: ", "dev");
4764
$envName = $questions->ask($input, $output, $envQuestion );
4865

49-
$envConfigFile = $this->getEnvironmentDir() . '/' . $envName . '.yml';
66+
$envConfigFile = $this->getEnvironmentDir() . '/' . $envName . '.' . $format;
5067
if (file_exists($envConfigFile)) {
5168
throw new \InvalidArgumentException("environment [$envName] is already defined!");
5269
}
@@ -78,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7895
$defaultEditorQuestion = new Question("Please enter the text editor to use by default <info>(default vim)</info>: ", "vim");
7996
$defaultEditor = $questions->ask($input, $output, $defaultEditorQuestion);
8097

81-
$confTemplate = file_get_contents(__DIR__ . '/../../templates/env.yml.tpl');
98+
$confTemplate = file_get_contents(__DIR__ . '/../../templates/env.' . $format . '.tpl');
8299
$confTemplate = str_replace('{DRIVER}', $driver, $confTemplate);
83100
$confTemplate = str_replace('{HOST}', $dbHost, $confTemplate);
84101
$confTemplate = str_replace('{PORT}', $dbPort, $confTemplate);
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
public static $SUPPORTED_PARSERS = array(
14+
'yml' => '\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+
throw new \RuntimeException(
36+
sprintf('Environment file %s does not exist or file not supported', $nameWithoutExt)
37+
);
38+
}
39+
}

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+
}

templates/env.json.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"connection": {
3+
"host": "{HOST}",
4+
"driver": "{DRIVER}",
5+
"port": "{PORT}",
6+
"username": "{USERNAME}",
7+
"password": "{PASSWORD}",
8+
"database": "{DATABASE}"
9+
},
10+
"changelog": "{CHANGELOG}",
11+
"default_editor": "{EDITOR}"
12+
}

templates/env.php.tpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
"connection" => [
5+
"host" => "{HOST}",
6+
"driver" => "{DRIVER}",
7+
"port" => "{PORT}",
8+
"username" => "{USERNAME}",
9+
"password" => "{PASSWORD}",
10+
"database" => "{DATABASE}"
11+
],
12+
"changelog" => "{CHANGELOG}",
13+
"default_editor" => "{EDITOR}"
14+
];

0 commit comments

Comments
 (0)