|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyCorp\Bundle\EasyAdminBundle; |
| 4 | + |
| 5 | +use Composer\Composer; |
| 6 | +use Composer\DependencyResolver\Operation\InstallOperation; |
| 7 | +use Composer\DependencyResolver\Operation\UpdateOperation; |
| 8 | +use Composer\EventDispatcher\EventSubscriberInterface; |
| 9 | +use Composer\Factory; |
| 10 | +use Composer\Installer\PackageEvent; |
| 11 | +use Composer\Installer\PackageEvents; |
| 12 | +use Composer\IO\IOInterface; |
| 13 | +use Composer\Package\PackageInterface; |
| 14 | +use Composer\Plugin\PluginInterface; |
| 15 | + |
| 16 | +final class NoFinalClassPlugin implements PluginInterface, EventSubscriberInterface |
| 17 | +{ |
| 18 | + private IOInterface $io; |
| 19 | + |
| 20 | + public function activate(Composer $composer, IOInterface $io) |
| 21 | + { |
| 22 | + $this->io = $io; |
| 23 | + } |
| 24 | + |
| 25 | + public static function getSubscribedEvents() |
| 26 | + { |
| 27 | + return [ |
| 28 | + PackageEvents::POST_PACKAGE_INSTALL => 'onPackageInstall', |
| 29 | + PackageEvents::POST_PACKAGE_UPDATE => 'onPackageUpdate', |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + public function deactivate(Composer $composer, IOInterface $io) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public function uninstall(Composer $composer, IOInterface $io) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + public function onPackageInstall(PackageEvent $event) |
| 42 | + { |
| 43 | + if (!$this->isComposerWorkingOn('easycorp/easyadmin-bundle', $event) && !$this->isComposerWorkingOn('easycorp/easyadmin-no-final-plugin', $event)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $this->removeFinalFromAllEasyAdminClasses(); |
| 48 | + } |
| 49 | + |
| 50 | + public function onPackageUpdate(PackageEvent $event) |
| 51 | + { |
| 52 | + if (!$this->isComposerWorkingOn('easycorp/easyadmin-bundle', $event)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $this->removeFinalFromAllEasyAdminClasses(); |
| 57 | + } |
| 58 | + |
| 59 | + public function removeFinalFromAllEasyAdminClasses() |
| 60 | + { |
| 61 | + $vendorDirPath = $this->getVendorDirPath(); |
| 62 | + $easyAdminDirPath = $vendorDirPath.'/easycorp/easyadmin-bundle'; |
| 63 | + foreach ($this->getFilePathsOfAllEasyAdminClasses($easyAdminDirPath) as $filePath) { |
| 64 | + file_put_contents( |
| 65 | + $filePath, |
| 66 | + str_replace('final class ', 'class ', file_get_contents($filePath)), |
| 67 | + flags: \LOCK_EX |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + $this->io->write('Updated all EasyAdmin PHP files to make classes non-final'); |
| 72 | + } |
| 73 | + |
| 74 | + private function isComposerWorkingOn(string $packageName, PackageEvent $event): bool |
| 75 | + { |
| 76 | + /** @var PackageInterface|null $package */ |
| 77 | + $package = null; |
| 78 | + |
| 79 | + foreach ($event->getOperations() as $operation) { |
| 80 | + if ('install' === $operation->getOperationType()) { |
| 81 | + /** @var InstallOperation $operation */ |
| 82 | + $package = $operation->getPackage(); |
| 83 | + } elseif ('update' === $operation->getOperationType()) { |
| 84 | + /** @var UpdateOperation $operation */ |
| 85 | + $package = $operation->getInitialPackage(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $packageName === $package?->getName(); |
| 90 | + } |
| 91 | + |
| 92 | + private function getVendorDirPath(): string |
| 93 | + { |
| 94 | + $composerJsonFilePath = Factory::getComposerFile(); |
| 95 | + $composerJsonContents = json_decode(file_get_contents($composerJsonFilePath), associative: true, flags: JSON_THROW_ON_ERROR); |
| 96 | + $projectDir = dirname(realpath($composerJsonFilePath)); |
| 97 | + |
| 98 | + return $composerJsonContents['config']['vendor-dir'] ?? $projectDir.'/vendor'; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return iterable Returns the file paths of all PHP files that contain EasyAdmin classes |
| 103 | + */ |
| 104 | + private function getFilePathsOfAllEasyAdminClasses(string $easyAdminDirPath): iterable |
| 105 | + { |
| 106 | + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($easyAdminDirPath, \FilesystemIterator::SKIP_DOTS)) as $filePath) { |
| 107 | + if (is_dir($filePath) || !str_ends_with($filePath, '.php')) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + yield $filePath; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments