-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNoFinalClassPlugin.php
More file actions
128 lines (106 loc) · 4.09 KB
/
NoFinalClassPlugin.php
File metadata and controls
128 lines (106 loc) · 4.09 KB
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
<?php
namespace EasyCorp\Bundle\EasyAdminBundle;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Factory;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
final class NoFinalClassPlugin implements PluginInterface, EventSubscriberInterface
{
private IOInterface $io;
public function activate(Composer $composer, IOInterface $io)
{
$this->io = $io;
}
public static function getSubscribedEvents()
{
return [
PackageEvents::POST_PACKAGE_INSTALL => 'onPackageInstall',
PackageEvents::POST_PACKAGE_UPDATE => 'onPackageUpdate',
ScriptEvents::POST_INSTALL_CMD => 'onInstallCmd',
ScriptEvents::POST_UPDATE_CMD => 'onUpdateCmd',
];
}
public function deactivate(Composer $composer, IOInterface $io)
{
}
public function uninstall(Composer $composer, IOInterface $io)
{
}
public function onPackageInstall(PackageEvent $event)
{
if (!$this->isComposerWorkingOn('easycorp/easyadmin-bundle', $event) && !$this->isComposerWorkingOn('easycorp/easyadmin-no-final-plugin', $event)) {
return;
}
$this->removeFinalFromAllEasyAdminClasses();
}
public function onPackageUpdate(PackageEvent $event)
{
if (!$this->isComposerWorkingOn('easycorp/easyadmin-bundle', $event)) {
return;
}
$this->removeFinalFromAllEasyAdminClasses();
}
public function onInstallCmd(Event $event)
{
$this->removeFinalFromAllEasyAdminClasses();
}
public function onUpdateCmd(Event $event)
{
$this->removeFinalFromAllEasyAdminClasses();
}
public function removeFinalFromAllEasyAdminClasses()
{
$vendorDirPath = $this->getVendorDirPath();
$easyAdminDirPath = $vendorDirPath.'/easycorp/easyadmin-bundle';
foreach ($this->getFilePathsOfAllEasyAdminClasses($easyAdminDirPath) as $filePath) {
file_put_contents(
$filePath,
str_replace('final class ', 'class ', file_get_contents($filePath)),
flags: \LOCK_EX
);
}
$this->io->write('Updated all EasyAdmin PHP files to make classes non-final');
}
private function isComposerWorkingOn(string $packageName, PackageEvent $event): bool
{
/** @var PackageInterface|null $package */
$package = null;
foreach ($event->getOperations() as $operation) {
if ('install' === $operation->getOperationType()) {
/** @var InstallOperation $operation */
$package = $operation->getPackage();
} elseif ('update' === $operation->getOperationType()) {
/** @var UpdateOperation $operation */
$package = $operation->getInitialPackage();
}
}
return $packageName === $package?->getName();
}
private function getVendorDirPath(): string
{
$composerJsonFilePath = Factory::getComposerFile();
$composerJsonContents = json_decode(file_get_contents($composerJsonFilePath), associative: true, flags: JSON_THROW_ON_ERROR);
$projectDir = dirname(realpath($composerJsonFilePath));
return $composerJsonContents['config']['vendor-dir'] ?? $projectDir.'/vendor';
}
/**
* @return iterable Returns the file paths of all PHP files that contain EasyAdmin classes
*/
private function getFilePathsOfAllEasyAdminClasses(string $easyAdminDirPath): iterable
{
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($easyAdminDirPath, \FilesystemIterator::SKIP_DOTS)) as $filePath) {
if (is_dir($filePath) || !str_ends_with($filePath, '.php')) {
continue;
}
yield $filePath;
}
}
}