1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the skeleton package. |
7
|
|
|
* |
8
|
|
|
* (c) Gennady Knyazkin <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Gennadyx\Skeleton\Action; |
15
|
|
|
|
16
|
|
|
use Gennadyx\Skeleton\Action\Traits\VarAwareTrait; |
17
|
|
|
use Gennadyx\Skeleton\Exception\RuntimeException; |
18
|
|
|
use Gennadyx\Skeleton\VarAwareInterface; |
19
|
|
|
use Symfony\Component\Finder\Finder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Replace variables in all skeleton files |
23
|
|
|
* |
24
|
|
|
* @author Gennady Knyazkin <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class VarReplace implements ActionInterface, VarAwareInterface |
27
|
|
|
{ |
28
|
|
|
use VarAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
2 |
|
public function execute() |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
/** @var Finder $finder */ |
37
|
2 |
|
$finder = Finder::create() |
38
|
2 |
|
->in($this->vars['skeleton']) |
39
|
2 |
|
->ignoreDotFiles(false); |
40
|
2 |
|
$keys = $this->vars->keys(); |
41
|
|
|
|
42
|
2 |
|
$keys->each(function ($variable) use ($finder) { |
43
|
2 |
|
$finder->contains(sprintf(':%s:', $variable)); |
44
|
2 |
|
}); |
45
|
|
|
|
46
|
2 |
|
$variables = $keys->map(function ($item) { |
47
|
2 |
|
return sprintf(':%s:', $item); |
48
|
2 |
|
})->toArray(); |
49
|
|
|
|
50
|
2 |
|
foreach ($finder as $file) { |
51
|
2 |
|
$newContent = str_replace( |
52
|
2 |
|
$variables, |
53
|
2 |
|
$this->vars->values()->toArray(), |
54
|
2 |
|
$file->getContents() |
55
|
|
|
); |
56
|
2 |
|
file_put_contents($file->getPathname(), $newContent); |
57
|
|
|
} |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
60
|
|
|
} |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function getPriority(): int |
67
|
|
|
{ |
68
|
4 |
|
return 0; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|