summaryrefslogtreecommitdiff
path: root/platform/www/bin/plugin.php
blob: 84a800ecb7ae2f21f18add1ce4650c336290d206 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env php
<?php

use dokuwiki\Extension\PluginController;
use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Colors;
use splitbrain\phpcli\Options;

if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
define('NOSESSION', 1);
require_once(DOKU_INC . 'inc/init.php');

class PluginCLI extends CLI {

    /**
     * Register options and arguments on the given $options object
     *
     * @param Options $options
     * @return void
     */
    protected function setup(Options $options) {
        $options->setHelp('Excecutes Plugin command line tools');
        $options->registerArgument('plugin', 'The plugin CLI you want to run. Leave off to see list', false);
    }

    /**
     * Your main program
     *
     * Arguments and options have been parsed when this is run
     *
     * @param Options $options
     * @return void
     */
    protected function main(Options $options) {
        global $argv;
        $argv = $options->getArgs();

        if($argv) {
            $plugin = $this->loadPlugin($argv[0]);
            if($plugin !== null) {
                $plugin->run();
            } else {
                $this->fatal('Command {cmd} not found.', ['cmd' => $argv[0]]);
            }
        } else {
            echo $options->help();
            $this->listPlugins();
        }
    }

    /**
     * List available plugins
     */
    protected function listPlugins() {
        /** @var PluginController $plugin_controller */
        global $plugin_controller;

        echo "\n";
        echo "\n";
        echo $this->colors->wrap('AVAILABLE PLUGINS:', Colors::C_BROWN);
        echo "\n";

        $list = $plugin_controller->getList('cli');
        sort($list);
        if(!count($list)) {
            echo $this->colors->wrap("  No plugins providing CLI components available\n", Colors::C_RED);
        } else {
            $tf = new \splitbrain\phpcli\TableFormatter($this->colors);

            foreach($list as $name) {
                $plugin = $this->loadPlugin($name);
                if($plugin === null) continue;
                $info = $plugin->getInfo();

                echo $tf->format(
                    [2, '30%', '*'],
                    ['', $name, $info['desc']],
                    ['', Colors::C_CYAN, '']

                );
            }
        }
    }

    /**
     * Instantiate a CLI plugin
     *
     * @param string $name
     * @return \dokuwiki\Extension\CLIPlugin|null
     */
    protected function loadPlugin($name) {
        // execute the plugin CLI
        $class = "cli_plugin_$name";
        if(class_exists($class)) {
            return new $class();
        }
        return null;
    }
}

// Main
$cli = new PluginCLI();
$cli->run();