summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Cli/Documentation.php
blob: 111745efa28442e8e38a7844468f272d31ed157b (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Cli;

use Icinga\Application\ApplicationBootstrap as App;
use Icinga\Cli\Documentation\CommentParser;
use ReflectionClass;
use ReflectionMethod;

class Documentation
{
    protected $icinga;

    public function __construct(App $app)
    {
        $this->app = $app;
        $this->loader = $app->cliLoader();
    }

    public function usage($module = null, $command = null, $action = null)
    {
        if ($module) {
            $module = $this->loader->resolveModuleName($module);
            return $this->moduleUsage($module, $command, $action);
        }
        if ($command) {
            $command = $this->loader->resolveCommandName($command);
            return $this->commandUsage($command, $action);
        }
        return $this->globalUsage();
    }

    public function globalUsage()
    {
        $d = "USAGE: icingacli [module] <command> [action] [options]\n\n"
           . "Available commands:\n\n";
        foreach ($this->loader->listCommands() as $command) {
            if ($command !== 'autocomplete') {
                $obj = $this->loader->getCommandInstance($command);
                $d .= sprintf(
                    "  %-14s  %s\n",
                    $command,
                    $this->getClassTitle($obj)
                );
            }
        }
        $d .= "\nAvailable modules:\n\n";
        foreach ($this->loader->listModules() as $module) {
            $d .= '  ' . $module . "\n";
        }
        $d .= "\nGlobal options:\n\n"
            . "  --log [t]       Log to <t>, either stderr, file or syslog (default: stderr)\n"
            . "  --log-path <f>  Which file to log into in case of --log file\n"
            . "  --verbose       Be verbose\n"
            . "  --debug         Show debug output\n"
            . "  --help          Show help\n"
            . "  --benchmark     Show benchmark summary\n"
            . "  --watch [s]     Refresh output every <s> seconds (default: 5)\n"
            . "  --version       Shows version of Icinga Web 2, loaded modules and PHP\n"
            ;
        $d .= "\nShow help on a specific command : icingacli help <command>"
            . "\nShow help on a specific module  : icingacli help <module>"
            . "\n";
        return $d;
    }

    public function moduleUsage($module, $command = null, $action = null)
    {
        $commands = $this->loader->listModuleCommands($module);

        if (empty($commands)) {
            return "The '$module' module does not provide any CLI commands\n";
        }
        $d = '';
        if ($command) {
            $obj = $this->loader->getModuleCommandInstance($module, $command);
        }
        if ($command === null) {
            $d = "USAGE: icingacli $module <command> [<action>] [options]\n\n"
               . "Available commands:\n\n";
            foreach ($commands as $command) {
                $d .= '  ' . $command . "\n";
            }
            $d .= "\nShow help on a specific command: icingacli help $module <command>\n";
        } elseif ($action === null) {
            $d .= $this->showCommandActions($obj, $command);
        } else {
            $action = $this->loader->resolveObjectActionName($obj, $action);
            $d .= $this->getMethodDocumentation($obj, $action);
        }
        return $d;
    }

    /**
     * @param   Command $command
     * @param   string  $name
     *
     * @return  string
     */
    protected function showCommandActions($command, $name)
    {
        $actions = $command->listActions();
        $d = $this->getClassDocumentation($command)
           . "Available actions:\n\n";
        foreach ($actions as $action) {
            $d .= sprintf(
                "  %-14s  %s\n",
                $action,
                $this->getMethodTitle($command, $action)
            );
        }
        $d .= "\nShow help on a specific action: icingacli help ";
        if ($command->isModule()) {
            $d .= $command->getModuleName() . ' ';
        }
        $d .= "$name <action>\n";
        return $d;
    }

    public function commandUsage($command, $action = null)
    {
        $obj = $this->loader->getCommandInstance($command);
        $action = $this->loader->resolveObjectActionName($obj, $action);

        $d = "\n";
        if ($action) {
            $d .= $this->getMethodDocumentation($obj, $action);
        } else {
            $d .= $this->showCommandActions($obj, $command);
        }
        return $d;
    }

    protected function getClassTitle($class)
    {
        $ref = new ReflectionClass($class);
        $comment = new CommentParser($ref->getDocComment());
        return $comment->getTitle();
    }

    protected function getClassDocumentation($class)
    {
        $ref = new ReflectionClass($class);
        $comment = new CommentParser($ref->getDocComment());
        return $comment->dump();
    }

    protected function getMethodTitle($class, $method)
    {
        $ref = new ReflectionMethod($class, $method . 'Action');
        $comment = new CommentParser($ref->getDocComment());
        return $comment->getTitle();
    }

    protected function getMethodDocumentation($class, $method)
    {
        $ref = new ReflectionMethod($class, $method . 'Action');
        $comment = new CommentParser($ref->getDocComment());
        return $comment->dump();
    }
}