Failed Conditions
Push — master ( 5c0b2e...874fc8 )
by Andreas
32:37 queued 29:04
created

PluginCLI::main()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/php
2
<?php
3
4
use splitbrain\phpcli\CLI;
5
use splitbrain\phpcli\Colors;
6
use splitbrain\phpcli\Options;
7
8
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
9
define('NOSESSION', 1);
10
require_once(DOKU_INC . 'inc/init.php');
11
12
class PluginCLI extends CLI {
13
14
    /**
15
     * Register options and arguments on the given $options object
16
     *
17
     * @param Options $options
18
     * @return void
19
     */
20
    protected function setup(Options $options) {
21
        $options->setHelp('Excecutes Plugin command line tools');
22
        $options->registerArgument('plugin', 'The plugin CLI you want to run. Leave off to see list', false);
23
    }
24
25
    /**
26
     * Your main program
27
     *
28
     * Arguments and options have been parsed when this is run
29
     *
30
     * @param Options $options
31
     * @return void
32
     */
33
    protected function main(Options $options) {
34
        global $argv;
35
        $argv = $options->getArgs();
36
37
        if($argv) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $argv of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            $plugin = $this->loadPlugin($argv[0]);
39
            if($plugin !== null) {
40
                $plugin->run();
41
            } else {
42
                $this->fatal('Command {cmd} not found.', ['cmd' => $argv[0]]);
43
            }
44
        } else {
45
            echo $options->help();
46
            $this->listPlugins();
47
        }
48
    }
49
50
    /**
51
     * List available plugins
52
     */
53
    protected function listPlugins() {
54
        /** @var Doku_Plugin_Controller $plugin_controller */
55
        global $plugin_controller;
56
57
        echo "\n";
58
        echo "\n";
59
        echo $this->colors->wrap('AVAILABLE PLUGINS:', Colors::C_BROWN);
60
        echo "\n";
61
62
        $list = $plugin_controller->getList('cli');
63
        sort($list);
64
        if(!count($list)) {
65
            echo $this->colors->wrap("  No plugins providing CLI components available\n", Colors::C_RED);
66
        } else {
67
            $tf = new \splitbrain\phpcli\TableFormatter($this->colors);
68
69
            foreach($list as $name) {
70
                $plugin = $this->loadPlugin($name);
71
                if($plugin === null) continue;
72
                $info = $plugin->getInfo();
73
74
                echo $tf->format(
75
                    [2, '30%', '*'],
76
                    ['', $name, $info['desc']],
77
                    ['', Colors::C_CYAN, '']
78
79
                );
80
            }
81
        }
82
    }
83
84
    /**
85
     * Instantiate a CLI plugin
86
     *
87
     * @param string $name
88
     * @return DokuWiki_CLI_Plugin|null
89
     */
90
    protected
91
    function loadPlugin($name) {
92
        // execute the plugin CLI
93
        $class = "cli_plugin_$name";
94
        if(class_exists($class)) {
95
            return new $class();
96
        }
97
        return null;
98
    }
99
}
100
101
// Main
102
$cli = new PluginCLI();
103
$cli->run();
104