77
88namespace Migrate \Command ;
99
10+ use Migrate \Config \ConfigLocator ;
11+ use Symfony \Component \Console \Input \InputArgument ;
1012use Symfony \Component \Console \Input \InputInterface ;
1113use Symfony \Component \Console \Output \OutputInterface ;
1214use 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 );
0 commit comments