Skip to content

Commit ac3a233

Browse files
committed
Added genenvironment command.
1 parent 39bda65 commit ac3a233

4 files changed

Lines changed: 174 additions & 32 deletions

File tree

bin/genenv

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Configurator\CliRunner;
5+
6+
$autoLocations = array(
7+
__DIR__ . '/../../autoload.php',
8+
__DIR__ . '/../../vendor/autoload.php',
9+
__DIR__ . '/../../../autoload.php',
10+
__DIR__ . '/../vendor/autoload.php',
11+
__DIR__ . '/vendor/autoload.php'
12+
);
13+
14+
foreach ($autoLocations as $file) {
15+
if (file_exists($file)) {
16+
define('COMPOSER_AUTOLOAD_LOCATION', $file);
17+
break;
18+
}
19+
}
20+
21+
if (!defined('COMPOSER_AUTOLOAD_LOCATION')) {
22+
fwrite(STDERR, 'Failed to find composer autoloader file.'.PHP_EOL);
23+
exit(1);
24+
}
25+
26+
require_once(COMPOSER_AUTOLOAD_LOCATION);
27+
28+
$runner = new CliRunner(CliRunner::GENENV);
29+
$runner->execute();

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"bin": [
1212
"bin/configurate",
13+
"bin/genenv",
1314
"bin/fpmconv"
1415
],
1516
"require-dev": {

src/Configurator/CliRunner.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
use Danack\Console\Input\InputArgument;
1010
use Auryn\Injector;
1111

12-
13-
14-
class CliRunner {
15-
12+
class CliRunner
13+
{
14+
const GENENV = 'genenv';
1615
const FPMCONV = 'fpmconv';
1716
const CONFIGURATE = 'configurate';
1817

@@ -30,6 +29,9 @@ function __construct($defaultCommand) {
3029
else if ($defaultCommand == CliRunner::CONFIGURATE) {
3130
$command = $this->makeConfigurateCommand();
3231
}
32+
else if ($defaultCommand == CliRunner::GENENV) {
33+
$command = $this->makeGenerateEnvCommand();
34+
}
3335
else {
3436
throw new \Exception("Unknown command type");
3537
}
@@ -66,7 +68,7 @@ function makeFpmConvCommand() {
6668
function makeConfigurateCommand() {
6769
$configurateCommand = new Command(
6870
self::CONFIGURATE,
69-
['Configurator\Configurator', 'run']
71+
['Configurator\Configurator', 'writeConfigFile']
7072
);
7173
$configurateCommand->setDescription("Transform a config file into an output file");
7274

@@ -79,9 +81,54 @@ function makeConfigurateCommand() {
7981
$configurateCommand->addArgument(
8082
'output',
8183
InputArgument::REQUIRED,
84+
'The output filename'
85+
);
86+
87+
$configurateCommand->addArgument(
88+
'environment',
89+
InputArgument::REQUIRED,
90+
'What environment to generated the config for.'
91+
);
92+
93+
$configurateCommand->addOption(
94+
'phpsettings',
95+
'p',
96+
InputArgument::OPTIONAL,
97+
'A comma separated list of PHP setting files.'
98+
);
99+
100+
$configurateCommand->addOption(
101+
'jssettings',
102+
'j',
103+
InputArgument::OPTIONAL,
104+
'A comma separated list of JSON setting files.'
105+
);
106+
107+
return $configurateCommand;
108+
}
109+
110+
111+
112+
function makeGenerateEnvCommand() {
113+
$configurateCommand = new Command(
114+
self::GENENV,
115+
['Configurator\Configurator', 'writeEnvironmentFile']
116+
);
117+
118+
$configurateCommand->setDescription("Generate a list of env settings into a PHP file");
119+
120+
$configurateCommand->addArgument(
121+
'input',
122+
InputArgument::REQUIRED,
82123
'The input filename'
83124
);
84125

126+
$configurateCommand->addArgument(
127+
'output',
128+
InputArgument::REQUIRED,
129+
'The output filename'
130+
);
131+
85132
$configurateCommand->addArgument(
86133
'environment',
87134
InputArgument::REQUIRED,
@@ -131,11 +178,15 @@ function execute() {
131178

132179
$questionHelper = new QuestionHelper();
133180
$questionHelper->setHelperSet($this->console->getHelperSet());
134-
$input = $parsedCommand->getInput();
181+
//$input = $parsedCommand->getInput();
135182

136183
$injector = createInjector();
137-
$params = formatKeyNames($parsedCommand->getParams());
138-
$injector->execute($parsedCommand->getCallable(), $params);
184+
185+
foreach ($parsedCommand->getParams() as $key => $value) {
186+
$injector->defineParam($key, $value);
187+
}
188+
189+
$injector->execute($parsedCommand->getCallable());
139190
}
140191
catch (ConfiguratorException $sce) {
141192
echo "Error running task: \n";

src/Configurator/Configurator.php

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Configurator;
44

55

6-
class Configurator {
7-
6+
class Configurator
7+
{
88
private $config = array();
99
private $configDefault = array();
1010
private $configEnvironment = array();
@@ -23,49 +23,70 @@ class Configurator {
2323
/**
2424
* @var
2525
*/
26-
private $environment;
27-
28-
public function run($input,
29-
$output,
30-
$environment,
31-
$jssettings = [],
32-
$phpsettings = []) {
33-
26+
private $environmentList;
27+
28+
private $jssettings;
29+
30+
private $phpsettings;
31+
32+
public function __construct(
33+
$input,
34+
$output,
35+
$environment,
36+
$jssettings = [],
37+
$phpsettings = []
38+
) {
3439
if (count($jssettings) == 0 && count($phpsettings) == 0) {
3540
throw new ConfiguratorException("One of PHP setting or JS settings must be set.");
3641
}
3742

38-
$this->environment = $environment;
3943
$this->inputFilename = $input;
4044
$this->outputFilename = $output;
45+
$this->environmentList = explode(',', $environment);
46+
$this->jssettings = $jssettings;
47+
$this->phpsettings = $phpsettings;
4148

42-
if ($phpsettings !== null) {
43-
$phpsettings = explode(',', $phpsettings);
49+
if ($this->phpsettings !== null) {
50+
$phpsettings = explode(',', $this->phpsettings);
4451
foreach ($phpsettings as $phpSetting) {
45-
$this->addPHPConfig($environment, $phpSetting);
52+
$this->addPHPConfig($phpSetting);
4653
}
4754
}
4855

49-
if ($jssettings !== null) {
50-
$jssettings = explode(',', $jssettings);
56+
if ($this->jssettings !== null) {
57+
$jssettings = explode(',', $this->jssettings);
5158
foreach ($jssettings as $jsSetting) {
52-
$this->addJSConfig($environment, $jsSetting);
59+
$this->addJSConfig($jsSetting);
5360
}
5461
}
62+
}
5563

64+
public function writeConfigFile()
65+
{
5666
$config = $this->configurate($this->inputFilename);
5767
$written = @file_put_contents($this->outputFilename, $config);
5868
if (!$written) {
5969
throw new \Exception("Failed to write config to file `$this->outputFilename`");
6070
}
6171
}
6272

73+
74+
public function writeEnvironmentFile()
75+
{
76+
$output = $this->genEnvironmentFile();
77+
$written = @file_put_contents($this->outputFilename, $output);
78+
if (!$written) {
79+
throw new \Exception("Failed to write config to file `$this->outputFilename`");
80+
}
81+
}
82+
6383

6484
/**
6585
* @param $environment
6686
* @param $filename
6787
*/
68-
function addJSConfig($environment, $filename) {
88+
function addJSConfig($filename)
89+
{
6990
$contents = @file_get_contents($filename);
7091
if ($contents == false) {
7192
throw new ConfiguratorException("Could not read file $filename.");
@@ -81,8 +102,10 @@ function addJSConfig($environment, $filename) {
81102
$this->addConfigDefault($data['default']);
82103
}
83104

84-
if (array_key_exists($environment, $data) == true) {
85-
$this->addConfigEnvironment($data[$environment]);
105+
foreach ($this->environmentList as $environment) {
106+
if (array_key_exists($environment, $data) == true) {
107+
$this->addConfigEnvironment($data[$environment]);
108+
}
86109
}
87110

88111
if (array_key_exists('override', $data) == true) {
@@ -108,14 +131,14 @@ private function addConfigOverride($data) {
108131
* @param $environment
109132
* @param $filename
110133
*/
111-
public function addPHPConfig($environment, $filename) {
134+
public function addPHPConfig($filename)
135+
{
112136
ob_start();
113137
require($filename);
114138
$contents = ob_get_contents();
115139
ob_end_clean();
116140

117141
if (strlen($contents) != 0) {
118-
119142
$message = sprintf(
120143
"Filename `%s` output some characters. Please check it is a valid PHP file.\n",
121144
$filename
@@ -128,8 +151,10 @@ public function addPHPConfig($environment, $filename) {
128151
$this->addConfigDefault($default);
129152
}
130153

131-
if (isset($$environment) == true) {
132-
$this->addConfigEnvironment($$environment);
154+
foreach ($this->environmentList as $environment) {
155+
if (isset($$environment) == true) {
156+
$this->addConfigEnvironment($$environment);
157+
}
133158
}
134159

135160
if (isset($override) == true) {
@@ -187,6 +212,42 @@ public function configurate($inputFilename) {
187212
return $configuration;
188213
}
189214

215+
216+
public function genEnvironmentFile()
217+
{
218+
$config = $this->getConfig();
219+
220+
$envRequired = require $this->inputFilename;
221+
222+
if (is_array($envRequired) == false) {
223+
throw new ConfiguratorException("Failed to get array from ".$this->inputFilename);
224+
}
225+
226+
$envOutput = "<?php\n";
227+
$envOutput .= "\n";
228+
229+
$envOutput .= "function getAppEnv() {\n";
230+
$envOutput .= " static \$env = [\n";
231+
232+
foreach ($envRequired as $env) {
233+
if (array_key_exists($env, $config) == false) {
234+
throw new ConfiguratorException("App needs $env but not found in config");
235+
}
236+
237+
$envOutput .= sprintf(
238+
" '%s' => '%s',\n",
239+
$env,
240+
$config[$env]
241+
);
242+
}
243+
$envOutput .= " ];\n";
244+
$envOutput .= "\n";
245+
$envOutput .= " return \$env;\n";
246+
$envOutput .= "}\n";
247+
248+
return $envOutput;
249+
}
250+
190251
public function getConfig()
191252
{
192253
$config = $this->config;

0 commit comments

Comments
 (0)