Skip to content

Commit

Permalink
Generate VIP autoload whne adding/removing packages
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Apr 12, 2024
1 parent 351ab2e commit c0b36e3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Composer\Plugin\Capability\CommandProvider;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginInterface;
use Composer\Script\ScriptEvents;

/*
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
Expand Down Expand Up @@ -46,6 +47,7 @@ public function activate(Composer $composer, IOInterface $io)
$manager->addInstaller($factory->installer());

$this->disableWordPressDefaultInstaller($composer);
$this->dumpProdAutoload($composer, $factory);
}

/**
Expand Down Expand Up @@ -85,4 +87,29 @@ private function disableWordPressDefaultInstaller(Composer $composer): void
$rootPackageExtra['installer-disable'] = $disabledInstallers;
$rootPackage->setExtra($rootPackageExtra);
}

/**
* @param Composer $composer
* @param Factory $factory
* @return void
*/
private function dumpProdAutoload(Composer $composer, Factory $factory): void
{
$dispatcher = $composer->getEventDispatcher();

$doing = false;
$callback = static function () use ($factory, $dispatcher, &$doing, &$callback): void {
if ($doing) {
$dispatcher->removeListener($callback);

return;
}
$doing = true;
$config = new Task\TaskConfig([Task\TaskConfig::PROD_AUTOLOAD => true]);
$taskFactory = new Task\Factory($factory, $config);
$taskFactory->generateProductionAutoload()->autorun();
};

$dispatcher->addListener(ScriptEvents::POST_AUTOLOAD_DUMP, $callback, PHP_INT_MAX);
}
}
28 changes: 26 additions & 2 deletions src/Task/GenerateProductionAutoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

use Composer\Autoload\AutoloadGenerator;
use Composer\Composer;
use Composer\EventDispatcher\EventDispatcher;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\IO\ConsoleIO;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use Composer\PartialComposer;
use Inpsyde\VipComposer\Config;
use Inpsyde\VipComposer\Utils\InstalledPackages;
use Inpsyde\VipComposer\Io;
use Inpsyde\VipComposer\VipDirectories;
use Seld\JsonLint\ParsingException;

final class GenerateProductionAutoload implements Task
{
Expand Down Expand Up @@ -47,14 +53,32 @@ public function enabled(TaskConfig $taskConfig): bool
|| $taskConfig->generateProdAutoload();
}

/**
* @return void
*/
public function autorun(): void
{
$this->doRun(null);
}

/**
* @param Io $io
* @param TaskConfig $taskConfig
* @return void
*/
public function run(Io $io, TaskConfig $taskConfig): void
{
$io->commentLine('Building production autoload...');
$this->doRun($io);
}

/**
* @param Io $io
* @return void
* @throws ParsingException
*/
private function doRun(?Io $io): void
{
$io?->commentLine('Building production autoload...');

$vendorDir = $this->config->composerConfigValue('vendor-dir');
$prodAutoloadDirname = $this->config->prodAutoloadDir();
Expand Down Expand Up @@ -94,7 +118,7 @@ public function run(Io $io, TaskConfig $taskConfig): void
file_put_contents("{$vendorDir}/autoload.php", $composerAutoloadContents);
}

$io->infoLine('Done!');
$io?->infoLine('Done!');
}

/**
Expand Down

0 comments on commit c0b36e3

Please sign in to comment.