Skip to content

Commit

Permalink
Automatically Disable Other Layout(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Jun 24, 2022
1 parent cbccf8c commit 5c0db33
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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'
];
Expand Down

0 comments on commit 5c0db33

Please sign in to comment.