Merge pull request #4112 from dokuwiki/bot/autofix
[dokuwiki.git] / bin / plugin.php
blob99c496bf75b2671e50aa18aa88ca38732742c1af
1 #!/usr/bin/env php
2 <?php
4 use dokuwiki\Extension\PluginController;
5 use splitbrain\phpcli\CLI;
6 use splitbrain\phpcli\Colors;
7 use splitbrain\phpcli\Options;
8 use dokuwiki\Extension\CLIPlugin;
9 use splitbrain\phpcli\TableFormatter;
11 if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
12 define('NOSESSION', 1);
13 require_once(DOKU_INC . 'inc/init.php');
15 class PluginCLI extends CLI
17 /**
18 * Register options and arguments on the given $options object
20 * @param Options $options
21 * @return void
23 protected function setup(Options $options)
25 $options->setHelp('Excecutes Plugin command line tools');
26 $options->registerArgument('plugin', 'The plugin CLI you want to run. Leave off to see list', false);
29 /**
30 * Your main program
32 * Arguments and options have been parsed when this is run
34 * @param Options $options
35 * @return void
37 protected function main(Options $options)
39 global $argv;
40 $argv = $options->getArgs();
42 if ($argv) {
43 $plugin = $this->loadPlugin($argv[0]);
44 if ($plugin instanceof CLIPlugin) {
45 $plugin->run();
46 } else {
47 $this->fatal('Command {cmd} not found.', ['cmd' => $argv[0]]);
49 } else {
50 echo $options->help();
51 $this->listPlugins();
55 /**
56 * List available plugins
58 protected function listPlugins()
60 /** @var PluginController $plugin_controller */
61 global $plugin_controller;
63 echo "\n";
64 echo "\n";
65 echo $this->colors->wrap('AVAILABLE PLUGINS:', Colors::C_BROWN);
66 echo "\n";
68 $list = $plugin_controller->getList('cli');
69 sort($list);
70 if ($list === []) {
71 echo $this->colors->wrap(" No plugins providing CLI components available\n", Colors::C_RED);
72 } else {
73 $tf = new TableFormatter($this->colors);
75 foreach ($list as $name) {
76 $plugin = $this->loadPlugin($name);
77 if (!$plugin instanceof CLIPlugin) continue;
78 $info = $plugin->getInfo();
80 echo $tf->format(
81 [2, '30%', '*'],
82 ['', $name, $info['desc']],
83 ['', Colors::C_CYAN, '']
89 /**
90 * Instantiate a CLI plugin
92 * @param string $name
93 * @return CLIPlugin|null
95 protected function loadPlugin($name)
97 if (plugin_isdisabled($name)) return null;
99 // execute the plugin CLI
100 $class = "cli_plugin_$name";
101 if (class_exists($class)) {
102 return new $class();
104 return null;
108 // Main
109 $cli = new PluginCLI();
110 $cli->run();