1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\ComposerPackages\Exporter; |
6
|
|
|
|
7
|
|
|
use Composer\Config; |
8
|
|
|
use Composer\Package\AliasPackage; |
9
|
|
|
use Composer\Package\PackageInterface; |
10
|
|
|
use Composer\Package\RootPackageInterface; |
11
|
|
|
use Composer\Script\Event; |
12
|
|
|
use drupol\ComposerPackages\Twig\CamelCaseExtension; |
13
|
|
|
use drupol\ComposerPackages\Twig\VarExportExtension; |
14
|
|
|
use Twig\Environment; |
15
|
|
|
use Twig\Loader\FilesystemLoader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Exporter. |
19
|
|
|
*/ |
20
|
|
|
abstract class Exporter implements ExporterInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Composer\Script\Event |
24
|
|
|
*/ |
25
|
|
|
private $event; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Twig\Environment |
29
|
|
|
*/ |
30
|
|
|
private $twig; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Exporter constructor. |
34
|
|
|
* |
35
|
|
|
* @param \Composer\Script\Event $event |
36
|
|
|
*/ |
37
|
2 |
|
public function __construct(Event $event) |
38
|
|
|
{ |
39
|
2 |
|
$this->twig = new Environment( |
40
|
2 |
|
new FilesystemLoader(__DIR__ . '/../../templates') |
41
|
|
|
); |
42
|
|
|
|
43
|
2 |
|
$this->twig->addExtension(new CamelCaseExtension()); |
44
|
2 |
|
$this->twig->addExtension(new VarExportExtension()); |
45
|
|
|
|
46
|
2 |
|
$this->event = $event; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $filename |
51
|
|
|
* |
52
|
|
|
* @throws \ReflectionException |
53
|
|
|
* @throws \Twig\Error\LoaderError |
54
|
|
|
* @throws \Twig\Error\RuntimeError |
55
|
|
|
* @throws \Twig\Error\SyntaxError |
56
|
|
|
*/ |
57
|
2 |
|
public function exportToFile(string $filename): void |
58
|
|
|
{ |
59
|
2 |
|
$composer = $this->getEvent()->getComposer(); |
60
|
|
|
|
61
|
2 |
|
$data = $this->exportToArray() + [ |
62
|
2 |
|
'generatedAt' => time(), |
63
|
2 |
|
'rootPackageName' => $this->getEvent()->getComposer()->getPackage()->getName(), |
64
|
|
|
]; |
65
|
|
|
|
66
|
2 |
|
$installPath = $this->locateRootPackageInstallPath($composer->getConfig(), $composer->getPackage()) |
67
|
2 |
|
. '/build/' . (new \ReflectionClass($this))->getShortName() . '.php'; |
68
|
|
|
|
69
|
2 |
|
$installPathTmp = $installPath . '_' . uniqid('tmp', true); |
70
|
2 |
|
file_put_contents($installPathTmp, $this->twig->render($filename, $data)); |
71
|
2 |
|
chmod($installPathTmp, 0664); |
72
|
2 |
|
rename($installPathTmp, $installPath); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return \Composer\Script\Event |
77
|
|
|
*/ |
78
|
2 |
|
protected function getEvent(): Event |
79
|
|
|
{ |
80
|
2 |
|
return $this->event; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Composer\Package\RootPackageInterface $rootPackage |
85
|
|
|
* |
86
|
|
|
* @return \Composer\Package\PackageInterface |
87
|
|
|
*/ |
88
|
2 |
|
private function getRootPackageAlias(RootPackageInterface $rootPackage): PackageInterface |
89
|
|
|
{ |
90
|
2 |
|
$package = $rootPackage; |
91
|
|
|
|
92
|
2 |
|
while ($package instanceof AliasPackage) { |
93
|
|
|
$package = $package->getAliasOf(); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return $package; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \Composer\Config $composerConfig |
101
|
|
|
* @param \Composer\Package\RootPackageInterface $rootPackage |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
2 |
|
private function locateRootPackageInstallPath(Config $composerConfig, RootPackageInterface $rootPackage): string |
106
|
|
|
{ |
107
|
2 |
|
if ('drupol/composer-packages' === $this->getRootPackageAlias($rootPackage)->getName()) { |
108
|
2 |
|
return \dirname($composerConfig->get('vendor-dir')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $composerConfig->get('vendor-dir') . '/drupol/composer-packages'; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|