Skip to content

Commit cdf6b91

Browse files
committed
Extracted the logic out of the ScriptHandler
Closes #30
1 parent d89c064 commit cdf6b91

File tree

4 files changed

+352
-292
lines changed

4 files changed

+352
-292
lines changed

Processor.php

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
namespace Incenteev\ParameterHandler;
4+
5+
use Composer\IO\IOInterface;
6+
use Symfony\Component\Yaml\Inline;
7+
use Symfony\Component\Yaml\Parser;
8+
use Symfony\Component\Yaml\Yaml;
9+
10+
class Processor
11+
{
12+
private $io;
13+
14+
public function __construct(IOInterface $io)
15+
{
16+
$this->io = $io;
17+
}
18+
19+
public function processFile(array $config)
20+
{
21+
$config = $this->processConfig($config);
22+
23+
$realFile = $config['file'];
24+
$parameterKey = $config['parameter-key'];
25+
26+
$exists = is_file($realFile);
27+
28+
$yamlParser = new Parser();
29+
30+
$action = $exists ? 'Updating' : 'Creating';
31+
$this->io->write(sprintf('<info>%s the "%s" file</info>', $action, $realFile));
32+
33+
// Find the expected params
34+
$expectedValues = $yamlParser->parse(file_get_contents($config['dist-file']));
35+
if (!isset($expectedValues[$parameterKey])) {
36+
throw new \InvalidArgumentException('The dist file seems invalid.');
37+
}
38+
$expectedParams = (array) $expectedValues[$parameterKey];
39+
40+
// find the actual params
41+
$actualValues = array($parameterKey => array());
42+
if ($exists) {
43+
$existingValues = $yamlParser->parse(file_get_contents($realFile));
44+
if ($existingValues === null) {
45+
$existingValues = array();
46+
}
47+
if (!is_array($existingValues)) {
48+
throw new \InvalidArgumentException(sprintf('The existing "%s" file does not contain an array', $realFile));
49+
}
50+
$actualValues = array_merge($actualValues, $existingValues);
51+
}
52+
53+
$actualValues[$parameterKey] = $this->processParams($config, $expectedParams, (array) $actualValues[$parameterKey]);
54+
55+
// Preserve other top-level keys than `$parameterKey` in the file
56+
foreach ($expectedValues as $key => $setting) {
57+
if (!array_key_exists($key, $actualValues)) {
58+
$actualValues[$key] = $setting;
59+
}
60+
}
61+
62+
if (!is_dir($dir = dirname($realFile))) {
63+
mkdir($dir, 0755, true);
64+
}
65+
66+
file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump($actualValues, 99));
67+
}
68+
69+
private function processConfig(array $config)
70+
{
71+
if (empty($config['file'])) {
72+
throw new \InvalidArgumentException('The extra.incenteev-parameters.file setting is required to use this script handler.');
73+
}
74+
75+
if (empty($config['dist-file'])) {
76+
$config['dist-file'] = $config['file'].'.dist';
77+
}
78+
79+
if (!is_file($config['dist-file'])) {
80+
throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.', $config['dist-file']));
81+
}
82+
83+
if (empty($config['parameter-key'])) {
84+
$config['parameter-key'] = 'parameters';
85+
}
86+
87+
return $config;
88+
}
89+
90+
private function processParams(array $config, $expectedParams, $actualParams)
91+
{
92+
// Grab values for parameters that were renamed
93+
$renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map'];
94+
$actualParams = array_replace($actualParams, $this->processRenamedValues($renameMap, $actualParams));
95+
96+
$keepOutdatedParams = false;
97+
if (isset($config['keep-outdated'])) {
98+
$keepOutdatedParams = (boolean) $config['keep-outdated'];
99+
}
100+
101+
if (!$keepOutdatedParams) {
102+
// Remove the outdated params
103+
foreach ($actualParams as $key => $value) {
104+
if (!array_key_exists($key, $expectedParams)) {
105+
unset($actualParams[$key]);
106+
}
107+
}
108+
}
109+
110+
$envMap = empty($config['env-map']) ? array() : (array) $config['env-map'];
111+
112+
// Add the params coming from the environment values
113+
$actualParams = array_replace($actualParams, $this->getEnvValues($envMap));
114+
115+
return $this->getParams($expectedParams, $actualParams);
116+
}
117+
118+
private function getEnvValues(array $envMap)
119+
{
120+
$params = array();
121+
foreach ($envMap as $param => $env) {
122+
$value = getenv($env);
123+
if ($value) {
124+
$params[$param] = Inline::parse($value);
125+
}
126+
}
127+
128+
return $params;
129+
}
130+
131+
private function processRenamedValues(array $renameMap, array $actualParams)
132+
{
133+
foreach ($renameMap as $param => $oldParam) {
134+
if (array_key_exists($param, $actualParams)) {
135+
continue;
136+
}
137+
138+
if (!array_key_exists($oldParam, $actualParams)) {
139+
continue;
140+
}
141+
142+
$actualParams[$param] = $actualParams[$oldParam];
143+
}
144+
145+
return $actualParams;
146+
}
147+
148+
private function getParams(array $expectedParams, array $actualParams)
149+
{
150+
// Simply use the expectedParams value as default for the missing params.
151+
if (!$this->io->isInteractive()) {
152+
return array_replace($expectedParams, $actualParams);
153+
}
154+
155+
$isStarted = false;
156+
157+
foreach ($expectedParams as $key => $message) {
158+
if (array_key_exists($key, $actualParams)) {
159+
continue;
160+
}
161+
162+
if (!$isStarted) {
163+
$isStarted = true;
164+
$this->io->write('<comment>Some parameters are missing. Please provide them.</comment>');
165+
}
166+
167+
$default = Inline::dump($message);
168+
$value = $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $key, $default), $default);
169+
170+
$actualParams[$key] = Inline::parse($value);
171+
}
172+
173+
return $actualParams;
174+
}
175+
}

ScriptHandler.php

+3-162
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace Incenteev\ParameterHandler;
44

5-
use Composer\IO\IOInterface;
65
use Composer\Script\Event;
7-
use Symfony\Component\Yaml\Inline;
8-
use Symfony\Component\Yaml\Parser;
9-
use Symfony\Component\Yaml\Yaml;
106

117
class ScriptHandler
128
{
@@ -28,169 +24,14 @@ public static function buildParameters(Event $event)
2824
$configs = array($configs);
2925
}
3026

27+
$processor = new Processor($event->getIO());
28+
3129
foreach ($configs as $config) {
3230
if (!is_array($config)) {
3331
throw new \InvalidArgumentException('The extra.incenteev-parameters setting must be an array of configuration objects.');
3432
}
3533

36-
self::processFile($config, $event->getIO());
37-
}
38-
}
39-
40-
private static function processFile(array $config, IOInterface $io)
41-
{
42-
$config = self::processConfig($config);
43-
44-
$realFile = $config['file'];
45-
$parameterKey = $config['parameter-key'];
46-
47-
$exists = is_file($realFile);
48-
49-
$yamlParser = new Parser();
50-
51-
$action = $exists ? 'Updating' : 'Creating';
52-
$io->write(sprintf('<info>%s the "%s" file</info>', $action, $realFile));
53-
54-
// Find the expected params
55-
$expectedValues = $yamlParser->parse(file_get_contents($config['dist-file']));
56-
if (!isset($expectedValues[$parameterKey])) {
57-
throw new \InvalidArgumentException('The dist file seems invalid.');
58-
}
59-
$expectedParams = (array) $expectedValues[$parameterKey];
60-
61-
// find the actual params
62-
$actualValues = array($parameterKey => array());
63-
if ($exists) {
64-
$existingValues = $yamlParser->parse(file_get_contents($realFile));
65-
if ($existingValues === null) {
66-
$existingValues = array();
67-
}
68-
if (!is_array($existingValues)) {
69-
throw new \InvalidArgumentException(sprintf('The existing "%s" file does not contain an array', $realFile));
70-
}
71-
$actualValues = array_merge($actualValues, $existingValues);
72-
}
73-
74-
$actualValues[$parameterKey] = self::processParams($config, $io, $expectedParams, (array) $actualValues[$parameterKey]);
75-
76-
// Preserve other top-level keys than `$parameterKey` in the file
77-
foreach ($expectedValues as $key => $setting) {
78-
if (!array_key_exists($key, $actualValues)) {
79-
$actualValues[$key] = $setting;
80-
}
81-
}
82-
83-
if (!is_dir($dir = dirname($realFile))) {
84-
mkdir($dir, 0755, true);
85-
}
86-
87-
file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump($actualValues, 99));
88-
}
89-
90-
private static function processConfig(array $config)
91-
{
92-
if (empty($config['file'])) {
93-
throw new \InvalidArgumentException('The extra.incenteev-parameters.file setting is required to use this script handler.');
94-
}
95-
96-
if (empty($config['dist-file'])) {
97-
$config['dist-file'] = $config['file'].'.dist';
34+
$processor->processFile($config);
9835
}
99-
100-
if (!is_file($config['dist-file'])) {
101-
throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.', $config['dist-file']));
102-
}
103-
104-
if (empty($config['parameter-key'])) {
105-
$config['parameter-key'] = 'parameters';
106-
}
107-
108-
return $config;
109-
}
110-
111-
private static function processParams(array $config, IOInterface $io, $expectedParams, $actualParams)
112-
{
113-
// Grab values for parameters that were renamed
114-
$renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map'];
115-
$actualParams = array_replace($actualParams, self::processRenamedValues($renameMap, $actualParams));
116-
117-
$keepOutdatedParams = false;
118-
if (isset($config['keep-outdated'])) {
119-
$keepOutdatedParams = (boolean) $config['keep-outdated'];
120-
}
121-
122-
if (!$keepOutdatedParams) {
123-
// Remove the outdated params
124-
foreach ($actualParams as $key => $value) {
125-
if (!array_key_exists($key, $expectedParams)) {
126-
unset($actualParams[$key]);
127-
}
128-
}
129-
}
130-
131-
$envMap = empty($config['env-map']) ? array() : (array) $config['env-map'];
132-
133-
// Add the params coming from the environment values
134-
$actualParams = array_replace($actualParams, self::getEnvValues($envMap));
135-
136-
return self::getParams($io, $expectedParams, $actualParams);
137-
}
138-
139-
private static function getEnvValues(array $envMap)
140-
{
141-
$params = array();
142-
foreach ($envMap as $param => $env) {
143-
$value = getenv($env);
144-
if ($value) {
145-
$params[$param] = Inline::parse($value);
146-
}
147-
}
148-
149-
return $params;
150-
}
151-
152-
private static function processRenamedValues(array $renameMap, array $actualParams)
153-
{
154-
foreach ($renameMap as $param => $oldParam) {
155-
if (array_key_exists($param, $actualParams)) {
156-
continue;
157-
}
158-
159-
if (!array_key_exists($oldParam, $actualParams)) {
160-
continue;
161-
}
162-
163-
$actualParams[$param] = $actualParams[$oldParam];
164-
}
165-
166-
return $actualParams;
167-
}
168-
169-
private static function getParams(IOInterface $io, array $expectedParams, array $actualParams)
170-
{
171-
// Simply use the expectedParams value as default for the missing params.
172-
if (!$io->isInteractive()) {
173-
return array_replace($expectedParams, $actualParams);
174-
}
175-
176-
$isStarted = false;
177-
178-
foreach ($expectedParams as $key => $message) {
179-
if (array_key_exists($key, $actualParams)) {
180-
continue;
181-
}
182-
183-
if (!$isStarted) {
184-
$isStarted = true;
185-
$io->write('<comment>Some parameters are missing. Please provide them.</comment>');
186-
}
187-
188-
$default = Inline::dump($message);
189-
$value = $io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $key, $default), $default);
190-
191-
$actualParams[$key] = Inline::parse($value);
192-
}
193-
194-
return $actualParams;
19536
}
19637
}

0 commit comments

Comments
 (0)