80 lines
3.4 KiB
PHP
80 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Spark\Commands;
|
|
|
|
use Spark\SparkApplication;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Helper\Table;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
#[AsCommand(name:'plugins', description:'Manage plugins from global repository')]
|
|
class PluginsCommand extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this->addOption("enable", null, InputOption::VALUE_REQUIRED, "Enable (symlink) a plugin");
|
|
$this->addOption("disable", null, InputOption::VALUE_REQUIRED, "Disable (remove) a plugin");
|
|
$this->addOption("force", "f", InputOption::VALUE_NONE, "Remove a plugin even if not a symlink");
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$env = $this->getEnvironment();
|
|
$app = $this->getApplication();
|
|
|
|
$globalPlugins = getenv("SPARK_PLUGINS");
|
|
$localPlugins = $env->getConfigDirectory()."/plugins";
|
|
|
|
$globalPluginList = glob($globalPlugins."/*");
|
|
$globalBasePluginList = array_map("basename", $globalPluginList);
|
|
$localPluginList = array_map("basename", glob($localPlugins."/*"));
|
|
|
|
if ($plugin = $input->getOption("enable")) {
|
|
if (in_array($plugin, $localPluginList)) {
|
|
$output->writeln("Plugin <info>{$plugin}</> already enabled!");
|
|
return Command::SUCCESS;
|
|
}
|
|
if (!in_array($plugin, $globalBasePluginList)) {
|
|
$output->writeln("<error>No such plugin {$plugin}</>");
|
|
return Command::FAILURE;
|
|
}
|
|
symlink($globalPlugins."/".$plugin, $localPlugins."/".$plugin);
|
|
$output->writeln("Enabled plugin <info>{$plugin}</>!");
|
|
return Command::SUCCESS;
|
|
} elseif ($plugin = $input->getOption("disable")) {
|
|
$force = $input->getOption("force");
|
|
if (!in_array($plugin, $localPluginList)) {
|
|
$output->writeln("<error>Plugin {$plugin} not enabled!</>");
|
|
return Command::FAILURE;
|
|
}
|
|
if (!is_link($localPlugins."/".$plugin) && !$force) {
|
|
$output->writeln("<error>Plugin is not a symlink and --force not specified</>");
|
|
return Command::FAILURE;
|
|
} elseif (is_link($localPlugins."/".$plugin)) {
|
|
unlink($localPlugins."/".$plugin);
|
|
$output->writeln("Disabled plugin <info>{$plugin}</>!");
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|
|
|
|
foreach ($globalPluginList as $plugin) {
|
|
if (!is_dir($plugin)) continue;
|
|
if (!file_exists($plugin."/sparkplug.php")) continue;
|
|
$head = (file($plugin."/sparkplug.php", FILE_IGNORE_NEW_LINES)[0])??null;
|
|
if (str_contains($head, "//")) {
|
|
$json = "{" . substr($head, strpos($head, "//") + 3) . "}";
|
|
$info = json_decode($json)??object();
|
|
} else {
|
|
$info = object();
|
|
}
|
|
$installed = in_array(basename($plugin), $localPluginList);
|
|
$badge = ($installed)?"<fg=green>\u{2714}</>":"<fg=gray>\u{25cc}</>";
|
|
$output->writeln(sprintf(" %s <fg=%s>%-20s</> %s", $badge, ($installed?"#0ff":"#088"), basename($plugin), $info->name??null));
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|