aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Plugins/PluginInfo.php
blob: 9809b138aa97f002105d16b409b9cd66de46bd58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

namespace Studip\Cli\Commands\Plugins;

use Studip\Cli\Commands\AbstractPluginCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PluginInfo extends AbstractPluginCommand
{
    protected static $defaultName = 'plugin:info';

    protected function configure(): void
    {
        $this->setDescription('Shows information about matching plugins.');
        $this->setHelp('This command shows information about plugins whose name contains the optional pattern.');
        $this->addArgument('pattern', InputArgument::OPTIONAL, 'pattern to search for');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $pattern = $input->getArgument('pattern');

        $pluginManager = \PluginManager::getInstance();
        $plugins = $pluginManager->getPluginInfos();

        if ($pattern) {
            $plugins = array_filter($plugins, function ($plugin) use ($pattern) {
                return false !== mb_stripos($plugin['name'], $pattern);
            });
        }

        $basepath = \Config::get()->PLUGINS_PATH;
        foreach ($plugins as $plugin) {
            $plugindir = $basepath . '/' . $plugin['path'];

            $plugin['class_exists'] = $this->pluginClassExists($plugindir, $plugin);
            $plugin['type'] = join(',', $plugin['type']);

            if (is_dir($plugindir . '/migrations')) {
                $schemaVersion = new \DBSchemaVersion($plugin['name']);
                $migrator = new \Migrator($plugindir . '/migrations', $schemaVersion);
                $plugin['pending_migrations'] = $migrator->pendingMigrations();
                $plugin['schema_version'] = $schemaVersion->get();
            }

            $pairs = [];
            foreach ($plugin as $key => $value) {
                $pairs[] = [$key, $value];
            }

            $table = new Table($output);
            $table->setHeaders(['Field', 'Value'])->setRows($pairs);
            $table->setStyle('box');
            $table->render();

            $output->writeln('');
        }

        return Command::SUCCESS;
    }

    private function pluginClassExists(string $plugindir, array $plugin)
    {
        $pluginfile = $plugindir . $plugin['class'] . '.class.php';
        if (file_exists($pluginfile)) {
            return 1;
        } else {
            $pluginfile = $plugindir . $plugin['class'] . '.php';
            if (file_exists($pluginfile)) {
                return 1;
            }
        }

        return 0;
    }
}