|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CodebarAg\CodingGuidelines\Composer; |
| 6 | + |
| 7 | +use CodebarAg\CodingGuidelines\Support\RefactorCommandSynchronizer; |
| 8 | +use Composer\Composer; |
| 9 | +use Composer\EventDispatcher\EventSubscriberInterface; |
| 10 | +use Composer\IO\IOInterface; |
| 11 | +use Composer\Plugin\PluginInterface; |
| 12 | +use Composer\Script\Event; |
| 13 | +use Composer\Script\ScriptEvents; |
| 14 | + |
| 15 | +final class RefactorCommandPlugin implements EventSubscriberInterface, PluginInterface |
| 16 | +{ |
| 17 | + private ?Composer $composer = null; |
| 18 | + |
| 19 | + private ?IOInterface $io = null; |
| 20 | + |
| 21 | + public function activate(Composer $composer, IOInterface $io): void |
| 22 | + { |
| 23 | + $this->composer = $composer; |
| 24 | + $this->io = $io; |
| 25 | + } |
| 26 | + |
| 27 | + public function deactivate(Composer $composer, IOInterface $io): void |
| 28 | + { |
| 29 | + // Nothing to clean up. |
| 30 | + } |
| 31 | + |
| 32 | + public function uninstall(Composer $composer, IOInterface $io): void |
| 33 | + { |
| 34 | + // Keep user's .cursor/commands/refactor.md file untouched on uninstall. |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return array<string, string> |
| 39 | + */ |
| 40 | + public static function getSubscribedEvents(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + ScriptEvents::POST_INSTALL_CMD => 'syncRefactorCommand', |
| 44 | + ScriptEvents::POST_UPDATE_CMD => 'syncRefactorCommand', |
| 45 | + ScriptEvents::POST_AUTOLOAD_DUMP => 'syncRefactorCommand', |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + public function syncRefactorCommand(Event $event): void |
| 50 | + { |
| 51 | + $composer = $this->composer ?? $event->getComposer(); |
| 52 | + $io = $this->io ?? $event->getIO(); |
| 53 | + |
| 54 | + $vendorDir = (string) $composer->getConfig()->get('vendor-dir'); |
| 55 | + $projectRoot = dirname($vendorDir); |
| 56 | + $packageRoot = dirname(__DIR__, 2); |
| 57 | + |
| 58 | + RefactorCommandSynchronizer::sync($projectRoot, $packageRoot, $io); |
| 59 | + } |
| 60 | +} |
0 commit comments