Completed
Push — master ( 26321c...f27ef8 )
by Pavel
02:08
created

processEnvironmentalVariables()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 1
nop 1
crap 6
1
<?php
2
3
namespace Paro\EnvironmentParameters;
4
5
use Composer\IO\IOInterface;
6
use Composer\Script\Event;
7
use Incenteev\ParameterHandler\Processor;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Yaml\Parser;
10
use Symfony\Component\Yaml\Yaml;
11
12
class IncenteevParametersProcessor
13
{
14
    private static $PARAMETER_KEY = null;
15
16
    /**
17
     * @var FileHandler
18
     */
19
    private $fileHandler;
20
    /**
21
     * @var IOInterface
22
     */
23
    private $io;
24
    /**
25
     * @var Filesystem
26
     */
27
    private $fs;
28
29
    /**
30
     * IncenteevParametersProcessor constructor.
31
     * @param Filesystem $fs
32
     * @param FileHandler $fileHandler
33
     * @param IOInterface $io
34
     */
35
    public function __construct(Filesystem $fs, FileHandler $fileHandler, IOInterface $io)
36
    {
37
        $this->fileHandler = $fileHandler;
38
        $this->io = $io;
39
        $this->fs = $fs;
40
    }
41
42
    /**
43
     * @param $configs
44
     * @param Event $event
45
     * @return bool
46
     */
47
    public function process($configs, Event $event)
48
    {
49
        if (!isset($configs['incenteev-parameters'])) {
50
            return true;
51
        }
52
53
        $processor = new Processor($event->getIO());
54
        $parameters = $configs['incenteev-parameters'];
55
        if (array_keys($parameters) !== range(0, count($parameters) - 1)) {
56
            $parameters = array($parameters);
57
        }
58
59
        if (empty($parameters['parameter-key'])) {
60
            self::$PARAMETER_KEY = 'parameters';
61
        } else {
62
            self::$PARAMETER_KEY = $parameters['parameter-key'];
63
        }
64
65
        foreach ($parameters as $config) {
66
            if (!is_array($config)) {
67
                throw new \InvalidArgumentException('The extra.environment-parameters.incenteev-parameters setting must be an array of configuration objects.');
68
            }
69
70
            $file = $this->fileHandler->findFile($config['file']);
71
72
            $outputFileName = $this->fileHandler->preparePath($configs['build-folder'] . '/' . (isset($config['name']) ? $config['name'] : $file));
73
            $this->processFile($file, $outputFileName . '.dist');
74
75
            $config['dist-file'] = $outputFileName . '.dist';
76
            $config['file'] = $outputFileName;
77
            $processor->processFile($config);
78
            $this->fs->remove($outputFileName . '.dist');
79
80
            $this->updateCommentInFile($outputFileName);
81
        }
82
83
        return true;
84
    }
85
86
    /**
87
     * @param $inFile
88
     * @param null $outFile
89
     * @param array $stack
90
     * @return array|bool|mixed
91
     */
92
    public function processFile($inFile, $outFile = null, array $stack = array())
93
    {
94
        $yamlParser = new Parser();
95
        $values = $yamlParser->parse(file_get_contents($this->fileHandler->findFile($inFile)));
96
97
        $values[self::$PARAMETER_KEY] = $this->processEnvironmentalVariables($values[self::$PARAMETER_KEY]);
98
99
        if (isset($values['imports']) && is_array($values['imports'])) {
100
            foreach ($values['imports'] as $importFile) {
101
                $filePath = $this->fileHandler->resolvePath($inFile, $importFile['resource']);
102
                $filePathFull = realpath($filePath);
103
                if (in_array($filePathFull, $stack)) {
104
                    $this->io->write(sprintf('<error>Skipping cyclic import in "%s" of the "%s" file</error>', $inFile, $filePath));
105
                    continue;
106
                }
107
                $stack[] = $filePathFull;
108
                $parametersFromFile = $this->processFile($filePath, null, $stack);
109
                $values = array_replace_recursive($parametersFromFile, $values);
110
            }
111
            unset($values['imports']);
112
        }
113
114
        if (!is_null($outFile)) {
115
	        ksort($values[self::$PARAMETER_KEY]);
116
            $this->fs->dumpFile($outFile, Yaml::dump($values));
117
        } else {
118
            return $values;
119
        }
120
121
        return true;
122
    }
123
124
    protected function updateCommentInFile($file)
125
    {
126
        $yamlParser = new Parser();
127
        $values = $yamlParser->parse(file_get_contents($file));
128
        file_put_contents($file, sprintf("# This file is auto-generated during the build process of '%s' environment at %s\n", $this->fileHandler->getArgumentValue('env'), date(DATE_ATOM)) . Yaml::dump($values), 99);
129
    }
130
131
    protected function processEnvironmentalVariables(array $parameters)
132
    {
133
        $t = $this; //php 5.3.x
134
        return array_map(function($item) use ($t) {
135
            if (!is_string($item)) {
136
                return $item;
137
            } else {
138
                return $t->fileHandler->processEnvironmentalVariable($item);
139
            }
140
        }, $parameters);
141
    }
142
}
143