fix(pre-update-event): use it to change repo only on update or install

This commit is contained in:
Jérémy Dufraisse 2023-03-19 09:47:36 +01:00
parent 9e6d84e085
commit 817ba4501d
2 changed files with 29 additions and 8 deletions

View File

@ -8,13 +8,14 @@
namespace Seacms\ComposerInstaller;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginInterface;
use TopFloor\ComposerCleanupVcsDirs\Plugin as TopFloorPlugin;
if (class_exists(TopFloorPlugin::class,true)){
abstract class ParentPlugin extends TopFloorPlugin implements Capable
abstract class ParentPlugin extends TopFloorPlugin implements Capable, EventSubscriberInterface
{
/**
* const to test if TopFloorPlugin
@ -23,7 +24,7 @@ if (class_exists(TopFloorPlugin::class,true)){
protected const IS_TOP_FLOOR_PLUGIN = true;
}
} else {
abstract class ParentPlugin implements PluginInterface, Capable
abstract class ParentPlugin implements PluginInterface, Capable, EventSubscriberInterface
{
/**

View File

@ -15,6 +15,8 @@ use Composer\Repository\PathRepository;
use Composer\Repository\RepositoryManager;
use Composer\Repository\VcsRepository;
use Exception;
use Composer\Script\Event as ScriptEvent;
use Composer\Script\ScriptEvents;
use Seacms\ComposerInstaller\Handler;
use Seacms\ComposerInstaller\NotFoundFileException;
use Seacms\ComposerInstaller\ParentPlugin;
@ -48,15 +50,26 @@ class Plugin extends ParentPlugin
$this->io = $io;
if (!self::IS_TOP_FLOOR_PLUGIN){
// do nothing
$this->handler = null;
return ;
}
$this->handler = new Handler($composer, $io);
try {
list('config' => $config,'path' => $path) = $this->getConfigJsonFile();
$this->updateRepositoryManager($config);
// $composer->getInstallationManager()->addInstaller(new LocalInstaller($io,$composer));
} catch (NotFoundFileException $ex){
// do nothing
}
/**
* manage preupdate event
* @param ScriptEvent $event
*/
public function onPreUpdate(ScriptEvent $event)
{
if (!is_null($this->handler)){
try {
list('config' => $config,'path' => $path) = $this->getConfigJsonFile();
$this->updateRepositoryManager($config);
// $composer->getInstallationManager()->addInstaller(new LocalInstaller($io,$composer));
} catch (NotFoundFileException $ex){
// do nothing
}
}
}
@ -223,4 +236,11 @@ class Plugin extends ParentPlugin
'Composer\Plugin\Capability\CommandProvider' => 'Seacms\\Command\\Command',
];
}
/* === implements EventsSubscriberInterface === */
public static function getSubscribedEvents()
{
return [
ScriptEvents::PRE_UPDATE_CMD => 'onPreUpdate'
];
}
}