Skip to content

Commit c0b36e3

Browse files
committed
Generate VIP autoload whne adding/removing packages
1 parent 351ab2e commit c0b36e3

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Plugin.php

+27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Composer\Plugin\Capability\CommandProvider;
1010
use Composer\Plugin\Capable;
1111
use Composer\Plugin\PluginInterface;
12+
use Composer\Script\ScriptEvents;
1213

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

4849
$this->disableWordPressDefaultInstaller($composer);
50+
$this->dumpProdAutoload($composer, $factory);
4951
}
5052

5153
/**
@@ -85,4 +87,29 @@ private function disableWordPressDefaultInstaller(Composer $composer): void
8587
$rootPackageExtra['installer-disable'] = $disabledInstallers;
8688
$rootPackage->setExtra($rootPackageExtra);
8789
}
90+
91+
/**
92+
* @param Composer $composer
93+
* @param Factory $factory
94+
* @return void
95+
*/
96+
private function dumpProdAutoload(Composer $composer, Factory $factory): void
97+
{
98+
$dispatcher = $composer->getEventDispatcher();
99+
100+
$doing = false;
101+
$callback = static function () use ($factory, $dispatcher, &$doing, &$callback): void {
102+
if ($doing) {
103+
$dispatcher->removeListener($callback);
104+
105+
return;
106+
}
107+
$doing = true;
108+
$config = new Task\TaskConfig([Task\TaskConfig::PROD_AUTOLOAD => true]);
109+
$taskFactory = new Task\Factory($factory, $config);
110+
$taskFactory->generateProductionAutoload()->autorun();
111+
};
112+
113+
$dispatcher->addListener(ScriptEvents::POST_AUTOLOAD_DUMP, $callback, PHP_INT_MAX);
114+
}
88115
}

src/Task/GenerateProductionAutoload.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66

77
use Composer\Autoload\AutoloadGenerator;
88
use Composer\Composer;
9+
use Composer\EventDispatcher\EventDispatcher;
910
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
11+
use Composer\IO\ConsoleIO;
12+
use Composer\IO\IOInterface;
13+
use Composer\IO\NullIO;
14+
use Composer\PartialComposer;
1015
use Inpsyde\VipComposer\Config;
1116
use Inpsyde\VipComposer\Utils\InstalledPackages;
1217
use Inpsyde\VipComposer\Io;
1318
use Inpsyde\VipComposer\VipDirectories;
19+
use Seld\JsonLint\ParsingException;
1420

1521
final class GenerateProductionAutoload implements Task
1622
{
@@ -47,14 +53,32 @@ public function enabled(TaskConfig $taskConfig): bool
4753
|| $taskConfig->generateProdAutoload();
4854
}
4955

56+
/**
57+
* @return void
58+
*/
59+
public function autorun(): void
60+
{
61+
$this->doRun(null);
62+
}
63+
5064
/**
5165
* @param Io $io
5266
* @param TaskConfig $taskConfig
5367
* @return void
5468
*/
5569
public function run(Io $io, TaskConfig $taskConfig): void
5670
{
57-
$io->commentLine('Building production autoload...');
71+
$this->doRun($io);
72+
}
73+
74+
/**
75+
* @param Io $io
76+
* @return void
77+
* @throws ParsingException
78+
*/
79+
private function doRun(?Io $io): void
80+
{
81+
$io?->commentLine('Building production autoload...');
5882

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

97-
$io->infoLine('Done!');
121+
$io?->infoLine('Done!');
98122
}
99123

100124
/**

0 commit comments

Comments
 (0)