Skip to content

Commit 5d1e8c0

Browse files
committed
Initial commit of the ScriptHandler
0 parents  commit 5d1e8c0

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2012 Christophe Coevoet
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Managing your ignored parameters with Composer
2+
3+
This tool allows you to manage your ignored parameters when running a composer
4+
install or update. It wokrs when storing the parameters in a Yaml file under
5+
a ``parameters`` key.
6+
7+
## Usage
8+
9+
In your root composer.json, adds the following:
10+
11+
```json
12+
{
13+
"require": {
14+
"logsafe/composer-parameter-handler": "1.0.*"
15+
},
16+
"scripts": {
17+
"post-install-cmd": [
18+
"LogSafe\\ParameterHandler\\ScriptHandler::buildParameters"
19+
],
20+
"post-update-cmd": [
21+
"LogSafe\\ParameterHandler\\ScriptHandler::buildParameters"
22+
]
23+
},
24+
"extra": {
25+
"logsafe-parameters": {
26+
"file": "app/config/parameters.yml"
27+
}
28+
}
29+
}
30+
```
31+
32+
The ``app/config/parameters.yml`` will then be created or updated by the
33+
composer script, to match the structure of the dist file ``app/config/parameters.yml.dist``
34+
by asking you the missing parameters.
35+
36+
By default, the dist file is assumed to be in the same place than the parameters
37+
file, suffixed by ``.dist``. This can be changed in the configuration:
38+
39+
```json
40+
{
41+
"extra": {
42+
"logsafe-parameters": {
43+
"file": "app/config/parameters.yml",
44+
"dist-file": "app/config/parameters.dist.yml"
45+
}
46+
}
47+
}
48+
```
49+
50+
The script handler will ask you interactively for parameters which are missing
51+
in the parameters file, using the value of the dist file as default value.
52+
All prompted values are parsed as inline Yaml, to allow you to define ``true``,
53+
``false``, ``null`` or numbers easily.
54+
If composer is run in a non-interactive mode, the values of the dist file
55+
will be used for missing parameters.
56+
57+
## Using environment variables to set the parameters
58+
59+
For your prod environment, using an interactive prompt may not be possible
60+
when deploying. In this case, you can rely on environment variables to provide
61+
the parameters. This is achieved by providing a map between environment variables
62+
and the parameters they should fill:
63+
64+
```json
65+
{
66+
"extra": {
67+
"logsafe-parameters": {
68+
"env-map": {
69+
"MY_FIRST_PARAM": "my_first_param",
70+
"MY_SECOND_PARAM": "my_second_param"
71+
}
72+
}
73+
}
74+
}
75+
```
76+
77+
If an environment variable is set, its value will always replace the value
78+
set in the existing parameters file.
79+
80+
As environment variables can only be strings, they are also parsed as inline
81+
YAML values to allows specifying ``null``, ``false``, ``true`` or numbers
82+
easily.

ScriptHandler.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace LogSafe\ParameterHandler;
4+
5+
use Composer\IO\IOInterface;
6+
use Composer\Script\Event;
7+
use Symfony\Component\Yaml\Inline;
8+
use Symfony\Component\Yaml\Parser;
9+
use Symfony\Component\Yaml\Yaml;
10+
11+
class ScriptHandler
12+
{
13+
public static function buildParameters(Event $event)
14+
{
15+
$extras = $event->getComposer()->getPackage()->getExtra();
16+
17+
if (empty($extras['logsafe-parameters']['file'])) {
18+
throw new \InvalidArgumentException('The extra.logsafe-parameters.file setting is required to use this script handler.');
19+
}
20+
21+
$realFile = $extras['logsafe-parameters']['file'];
22+
23+
if (empty($extras['logsafe-parameters']['dist-file'])) {
24+
$distFile = $realFile.'.dist';
25+
} else {
26+
$distFile = $extras['logsafe-parameters']['dist-file'];
27+
}
28+
29+
if (!is_file($distFile)) {
30+
throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.'));
31+
}
32+
33+
$exists = is_file($realFile);
34+
35+
$yamlParser = new Parser();
36+
$io = $event->getIO();
37+
38+
if ($exists) {
39+
$io->write('<info>Updating the parameters.yml file.</info>');
40+
} else {
41+
$io->write('<info>Creating the parameters.yml file.</info>');
42+
}
43+
44+
// Find the expected params
45+
$expectedValues = $yamlParser->parse(file_get_contents($distFile));
46+
if (!isset($expectedValues['parameters'])) {
47+
throw new \InvalidArgumentException('The parameters.yml.dist file seems invalid.');
48+
}
49+
$expectedParams = (array) $expectedValues['parameters'];
50+
51+
// find the actual params
52+
$actualValues = array('parameters' => array());
53+
if ($exists) {
54+
$actualValues = array_merge($actualValues, $yamlParser->parse(file_get_contents($realFile)));
55+
}
56+
$actualParams = (array) $actualValues['parameters'];
57+
58+
// Remove the outdated params
59+
foreach ($actualParams as $key => $value) {
60+
if (!array_key_exists($key, $expectedParams)) {
61+
unset($actualParams[$key]);
62+
}
63+
}
64+
65+
$envMap = empty($extras['logsafe-parameters']['env-map']) ? array() : (array) $extras['logsafe-parameters']['env-map'];
66+
67+
// Add the params coming from the environment values
68+
$actualParams = array_replace($actualParams, self::getEnvValues($envMap));
69+
70+
$actualParams = self::getParams($io, $expectedParams, $actualParams);
71+
72+
file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump(array('parameters' => $actualParams)));
73+
}
74+
75+
private static function getEnvValues(array $envMap)
76+
{
77+
$params = array();
78+
foreach ($envMap as $env => $param) {
79+
$value = getenv($env);
80+
if ($value) {
81+
$params[$param] = Inline::parse($value);
82+
}
83+
}
84+
85+
return $params;
86+
}
87+
88+
private static function getParams(IOInterface $io, array $expectedParams, array $actualParams)
89+
{
90+
// Simply use the expectedParams value as default for the missing params.
91+
if (!$io->isInteractive()) {
92+
return array_replace($expectedParams, $actualParams);
93+
}
94+
95+
$isStarted = false;
96+
97+
foreach ($expectedParams as $key => $message) {
98+
if (array_key_exists($key, $actualParams)) {
99+
continue;
100+
}
101+
102+
if (!$isStarted) {
103+
$isStarted = true;
104+
$io->write('<comment>Some parameters are missing. Please provide them.</comment>');
105+
}
106+
107+
$default = Inline::dump($message);
108+
$value = $io->ask(sprintf('<question>%s</question> (<comment>%s</comment>):', $key, $default), $default);
109+
110+
$actualParams[$key] = Inline::parse($value);
111+
}
112+
113+
return $actualParams;
114+
}
115+
}

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "logsafe/composer-parameter-handler",
3+
"description": "Composer script handling your ignored parameter file",
4+
"keywords": ["parameters management"],
5+
"homepage": "https://github.com/LogSafe/ParameterHandler",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Christophe Coevoet",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"minimum-stability": "dev",
14+
"require": {
15+
"php": ">=5.3.3",
16+
"symfony/yaml": ">=2.0,<2.3-dev"
17+
},
18+
"require-dev": {
19+
"composer/composer": "*"
20+
},
21+
"autoload": {
22+
"psr-0": { "LogSafe\\ParameterHandler": "" }
23+
},
24+
"target-dir": "LogSafe/ParameterHandler",
25+
"extra": {
26+
"branch-alias": {
27+
"dev-master": "1.0.x-dev"
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)