From 5c0db3361a424bb2c75f2d8bf89f66d21c7a0e5e Mon Sep 17 00:00:00 2001 From: Taufik Nurrohman Date: Fri, 24 Jun 2022 23:14:37 +0700 Subject: [PATCH] Automatically Disable Other Layout(s) --- src/Plugin.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Plugin.php b/src/Plugin.php index ba1e9d0..80d92f9 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -3,28 +3,38 @@ namespace Mecha\Composer; use const DIRECTORY_SEPARATOR; +use const GLOB_NOSORT; use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_UNICODE; use function basename; use function dirname; +use function glob; use function json_decode; use function json_encode; +use function rename; use function rmdir; use function strpos; +use function substr; use function unlink; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use Composer\Composer; +use Composer\DependencyResolver\Operation\InstallOperation; +use Composer\DependencyResolver\Operation\UninstallOperation; +use Composer\DependencyResolver\Operation\UpdateOperation; use Composer\EventDispatcher\EventSubscriberInterface; use Composer\IO\IOInterface; +use Composer\Installer\PackageEvent; +use Composer\Installer\PackageEvents; use Composer\Plugin\PluginInterface; use Composer\Script\Event; use Composer\Script\ScriptEvents; class Plugin implements PluginInterface, EventSubscriberInterface { + private $installer; public function activate(Composer $composer, IOInterface $io) { $composer->getInstallationManager()->addInstaller($this->installer = new Installer($io, $composer)); } @@ -61,12 +71,29 @@ public function onPostCreateProject(Event $event) { } } } + public function onPostPackageInstall(PackageEvent $event) { + $name = basename(($package = $event->getOperation()->getPackage())->getName()); + $r = dirname($event->getComposer()->getConfig()->get('vendor-dir'), 2); + // Automatically disable other layout after installing this layout + if ('y.' === substr($name, 0, 2)) { + $name = substr($name, 2); + $folder = strtr(dirname($r . '/' . $this->installer->getInstallPath($package)), ['/' => DIRECTORY_SEPARATOR]); + foreach (glob($folder . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . 'index.php', GLOB_NOSORT) as $v) { + if ($name === basename(dirname($v))) { + continue; + } + // $event->getIO()->write('Disabling ' . basename(dirname($v)) . ' layout...'); + rename($v, dirname($v) . DIRECTORY_SEPARATOR . '.index.php'); + } + } + } public function onPostUpdate(Event $event) { return $this->onPostCreateProject($event); } public function uninstall(Composer $composer, IOInterface $io) {} public static function getSubscribedEvents() { return [ + PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageInstall', ScriptEvents::POST_CREATE_PROJECT_CMD => 'onPostCreateProject', ScriptEvents::POST_UPDATE_CMD => 'onPostUpdate' ];