summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Cli/Command.php
blob: d1a19862a785f0e79b7e3e5678491ec9352a1f53 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Cli;

use Icinga\Application\ApplicationBootstrap as App;
use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotReadableError;
use ipl\I18n\Translation;

abstract class Command
{
    use Translation;

    protected $app;
    protected $docs;

    /**
     * @var Params
     */
    protected $params;
    protected $screen;

    /**
     * Whether the --verbose switch is given and thus the set log level INFO is
     *
     * @var bool
     */
    protected $isVerbose;

    /**
     * Whether the --debug switch is given and thus the set log level DEBUG is
     *
     * @var bool
     */
    protected $isDebugging;

    protected $moduleName;
    protected $commandName;
    protected $actionName;

    protected $config;

    protected $configs;

    protected $defaultActionName = 'default';

    /** @var bool Whether to automatically load enabled modules */
    protected $loadEnabledModules = true;

    public function __construct(App $app, $moduleName, $commandName, $actionName, $initialize = true)
    {
        $this->app = $app;
        $this->moduleName   = $moduleName;
        $this->commandName  = $commandName;
        $this->actionName   = $actionName;
        $this->params       = $app->getParams();
        $this->screen       = Screen::instance();
        $this->trace        = $this->params->shift('trace', false);
        $this->isVerbose    = $this->params->shift('verbose', false);
        $this->isDebugging  = $this->params->shift('debug', false);
        $this->configs      = [];

        $this->translationDomain = $moduleName ?: 'icinga';

        if ($this->loadEnabledModules) {
            try {
                $app->getModuleManager()->loadEnabledModules();
            } catch (NotReadableError $e) {
                Logger::error(new IcingaException('Cannot load enabled modules. An exception was thrown:', $e));
            }
        }

        if ($initialize) {
            $this->init();
        }
    }

    public function Config($file = null)
    {
        if ($this->isModule()) {
            return $this->getModuleConfig($file);
        } else {
            return $this->getMainConfig($file);
        }
    }

    private function getModuleConfig($file = null)
    {
        if ($file === null) {
            if ($this->config === null) {
                $this->config = Config::module($this->moduleName);
            }
            return $this->config;
        } else {
            if (! array_key_exists($file, $this->configs)) {
                $this->configs[$file] = Config::module($this->moduleName, $file);
            }
            return $this->configs[$file];
        }
    }

    private function getMainConfig($file = null)
    {
        if ($file === null) {
            if ($this->config === null) {
                $this->config = Config::app();
            }
            return $this->config;
        } else {
            if (! array_key_exists($file, $this->configs)) {
                $this->configs[$file] = Config::app($file);
            }
            return $this->configs[$file];
        }
        return $this->config;
    }

    public function isModule()
    {
        return substr(get_class($this), 0, 14) === 'Icinga\\Module\\';
    }

    public function setParams(Params $params)
    {
        $this->params = $params;
    }

    public function hasRemainingParams()
    {
        return $this->params->count() > 0;
    }

    public function showTrace()
    {
        return $this->trace;
    }

    public function fail($msg)
    {
        throw new IcingaException('%s', $msg);
    }

    public function getDefaultActionName()
    {
        return $this->defaultActionName;
    }

    /**
     * Get {@link moduleName}
     *
     * @return string
     */
    public function getModuleName()
    {
        return $this->moduleName;
    }

    public function hasDefaultActionName()
    {
        return $this->hasActionName($this->defaultActionName);
    }

    public function hasActionName($name)
    {
        $actions = $this->listActions();
        return in_array($name, $actions);
    }

    public function listActions()
    {
        $actions = array();
        foreach (get_class_methods($this) as $method) {
            if (preg_match('~^([A-Za-z0-9]+)Action$~', $method, $m)) {
                $actions[] = $m[1];
            }
        }
        sort($actions);
        return $actions;
    }

    public function docs()
    {
        if ($this->docs === null) {
            $this->docs = new Documentation($this->app);
        }
        return $this->docs;
    }

    public function showUsage($action = null)
    {
        if ($action === null) {
            $action = $this->actionName;
        }
        echo $this->docs()->usage(
            $this->moduleName,
            $this->commandName,
            $action
        );
        return false;
    }

    public function init()
    {
    }
}