-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProcess.php
137 lines (121 loc) · 3.44 KB
/
Process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace EcEuropa\Toolkit\Task\File;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use Robo\Common\BuilderAwareTrait;
use Robo\Contract\BuilderAwareInterface;
use Robo\Result;
use Robo\Task\BaseTask;
/**
* Process a source file to its destination replacing tokens.
*
* ``` php
* <?php
* $this->taskProcess('behat.yml')->run();
* $this->taskProcess('behat.yml.dist', 'behat.yml')->run();
* ?>
* ```
*/
class Process extends BaseTask implements BuilderAwareInterface
{
use BuilderAwareTrait;
/**
* Source file.
*
* @var string
*/
protected string $source;
/**
* Destination file.
*
* @var string
*/
protected string $destination;
/**
* The content from the source.
*
* @var string
*/
protected string $content;
/**
* Constructs a new Process task.
*
* @param string $source
* The source file.
* @param string $destination
* The destination file, if empty assumes the $source as destination.
*/
public function __construct(string $source, string $destination = '')
{
$this->source = $source;
$this->destination = $destination;
if (empty($this->destination)) {
$this->destination = $source;
}
}
/**
* Get the content from the source.
*/
protected function loadContent()
{
if (!file_exists($this->source)) {
return false;
}
$this->content = file_get_contents($this->source);
return true;
}
/**
* Return the tokens found in the content.
*
* @return array
* An array with the tokens found in the content.
*/
protected function extractTokens()
{
preg_match_all('/\${(([A-Za-z]([A-Za-z0-9_\-]+)?\.?)+)}/', $this->content, $matches);
if (!empty($matches[0]) && is_array($matches[1])) {
return array_combine($matches[0], $matches[1]);
}
return [];
}
/**
* Process the content by replacing the tokens with the values in config.
*
* @return array|string[]
* The tokens.
*/
protected function processTokens()
{
$config = $this->getConfig();
return array_map(function ($key) use ($config) {
$value = $config->get($key);
if (is_array($value)) {
$array = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($value)));
return implode(',', $array);
}
return $value;
}, $this->extractTokens());
}
/**
* Execute the task.
*
* @return \Robo\Result
* The result of the task.
*/
public function run()
{
if (!$this->loadContent()) {
return Result::error($this, sprintf('File %s does not exist.', $this->source));
}
if ($this->source !== $this->destination) {
$this->printTaskInfo('Creating {filename}', ['filename' => $this->destination]);
$this->collectionBuilder()->taskFilesystemStack()
->copy($this->source, $this->destination, true)->run();
}
$tokens = $this->processTokens();
$result = $this->collectionBuilder()->taskReplaceInFile($this->destination)
->from(array_keys($tokens))->to(array_values($tokens))->run();
return new Result($this, $result->getExitCode(), $result->getMessage());
}
}