Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\Capability\CommandProvider;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginInterface;
use craft\composer\commands\Provider;

/**
* Plugin is the Composer plugin that registers the Craft CMS composer installer.
*
* @author Pixel & Tonic, Inc. <support@craftcms.com>
*/
class Plugin implements PluginInterface
class Plugin implements PluginInterface, Capable
{
/**
* @var Installer
*/
private $installer;

/**
* @inheritdoc
*/
private Installer $installer;

public function activate(Composer $composer, IOInterface $io)
{
// Register the plugin installer
Expand All @@ -37,18 +34,23 @@ public function activate(Composer $composer, IOInterface $io)
}
}

/**
* @inheritdoc
*/
public function deactivate(Composer $composer, IOInterface $io)
{
$composer->getInstallationManager()->removeInstaller($this->installer);
}

/**
* @inheritdoc
*/
public function uninstall(Composer $composer, IOInterface $io)
{}

public function getCapabilities()
{
return [
CommandProvider::class => Provider::class,
];
}

public function getInstaller(): Installer
{
return $this->installer;
}
}
24 changes: 24 additions & 0 deletions src/commands/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace craft\composer\commands;

use Composer\IO\IOInterface;
use Composer\Plugin\Capability\CommandProvider;
use craft\composer\Plugin;

class Provider implements CommandProvider
{
protected IOInterface $io;
protected Plugin $plugin;

public function __construct(array $arguments)
{
$this->io = $arguments['io'];
$this->plugin = $arguments['plugin'];
}

public function getCommands()
{
return [new Regenerate($this->plugin)];
}
}
57 changes: 57 additions & 0 deletions src/commands/Regenerate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace craft\composer\commands;

use Composer\Command\BaseCommand;
use Composer\InstalledVersions;
use Composer\Package\PackageInterface;
use Composer\Semver\Constraint\MatchAllConstraint;
use craft\composer\Installer;
use craft\composer\Plugin;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Regenerate extends BaseCommand
{
public function __construct(
protected readonly Plugin $plugin,
) {
parent::__construct();
}

protected function configure(): void
{
$this->setName('craft-plugin-regenerate');
$this->setDescription(sprintf('Rebuild the %s file from the installed packages.', Installer::PLUGINS_FILE));
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln(sprintf('Regenerating %s...', Installer::PLUGINS_FILE));

/** @var PackageInterface[] $pluginPackages */
$pluginPackages = array_map(function ($name) {
return $this->getComposer()
->getRepositoryManager()
->getLocalRepository()
->findPackage($name, new MatchAllConstraint);
}, InstalledVersions::getInstalledPackagesByType('craft-plugin'));

if (empty($pluginPackages)) {
$output->writeln('No plugins are installed.');

return 0;
}

$output->writeln(sprintf('Found %d plugin package(s):', count($pluginPackages)));

foreach ($pluginPackages as $package) {
$output->writeln(sprintf(' - Re-adding %s', $package->getName()));
$this->plugin->getInstaller()->addPlugin($package);
}

$output->writeln('Done!');

return 0;
}
}