1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Paysera\PhpStormHelper\Service; |
6
|
|
|
|
7
|
|
|
use Paysera\PhpStormHelper\TemplateControl; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
11
|
|
|
|
12
|
|
|
class StructureConfigurator |
13
|
|
|
{ |
14
|
|
|
private $filesystem; |
15
|
|
|
|
16
|
8 |
|
public function __construct(Filesystem $filesystem) |
17
|
|
|
{ |
18
|
8 |
|
$this->filesystem = $filesystem; |
19
|
8 |
|
} |
20
|
|
|
|
21
|
8 |
|
public function configure(string $pathToTemplate, string $target, array $options) |
22
|
|
|
{ |
23
|
8 |
|
$options['projectName'] = basename($target); |
24
|
|
|
|
25
|
8 |
|
$finder = new Finder(); |
26
|
|
|
|
27
|
|
|
/** @var SplFileInfo[] $files */ |
28
|
8 |
|
$files = $finder->in($pathToTemplate)->files(); |
29
|
|
|
|
30
|
8 |
|
foreach ($files as $file) { |
31
|
8 |
|
$this->copyFile($file, $target . '/.idea', $options); |
32
|
|
|
} |
33
|
8 |
|
} |
34
|
|
|
|
35
|
8 |
|
private function copyFile(SplFileInfo $file, string $target, array $options) |
36
|
|
|
{ |
37
|
8 |
|
$filename = $file->getRelativePathname(); |
38
|
|
|
|
39
|
8 |
|
$filename = preg_replace_callback('/\$([^$]+)\$/', function (array $match) use ($options) { |
40
|
6 |
|
return $options[$match[1]]; |
41
|
8 |
|
}, $filename); |
42
|
|
|
|
43
|
8 |
|
if (mb_substr($filename, -13) === '.template.php') { |
44
|
6 |
|
$contents = $this->parseTemplate($file->getPathname(), $options); |
45
|
6 |
|
$filename = mb_substr($filename, 0, -13); |
46
|
|
|
} else { |
47
|
8 |
|
$contents = $file->getContents(); |
48
|
|
|
} |
49
|
|
|
|
50
|
8 |
|
if ($contents === null) { |
51
|
6 |
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
$this->filesystem->mkdir(dirname($target . '/' . $filename)); |
55
|
8 |
|
$this->filesystem->dumpFile($target . '/' . $filename, $contents); |
56
|
8 |
|
} |
57
|
|
|
|
58
|
6 |
|
private function parseTemplate(string $filePath, array $options) |
59
|
|
|
{ |
60
|
6 |
|
extract($options, EXTR_SKIP); |
61
|
6 |
|
ob_start(); |
62
|
6 |
|
$returnCode = require $filePath; |
63
|
6 |
|
$contents = ob_get_clean(); |
64
|
|
|
|
65
|
6 |
|
if ($returnCode === TemplateControl::SKIP) { |
66
|
6 |
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
return $contents; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|