icingaweb2/library/Icinga/Application/Cli.php

167 lines
4.0 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Application;
2013-10-22 14:26:45 +02:00
use Icinga\Application\Platform;
use Icinga\Application\ApplicationBootstrap;
use Icinga\Cli\Params;
use Icinga\Cli\Loader;
use Icinga\Cli\Screen;
use Icinga\Application\Logger;
2013-10-22 14:26:45 +02:00
use Icinga\Application\Benchmark;
use Icinga\Data\ConfigObject;
2013-10-22 14:26:45 +02:00
use Icinga\Exception\ProgrammingError;
2014-03-31 18:35:28 +02:00
require_once __DIR__ . '/ApplicationBootstrap.php';
class Cli extends ApplicationBootstrap
{
protected $isCli = true;
2013-10-22 14:26:45 +02:00
protected $params;
protected $showBenchmark = false;
protected $watchTimeout;
protected $cliLoader;
protected $verbose;
protected $debug;
protected function bootstrap()
{
$this->assertRunningOnCli();
$this->setupLogging()
->setupErrorHandling()
->loadConfig()
->setupTimezone()
->setupInternationalization()
->parseBasicParams()
->setupLogger()
->setupResourceFactory()
->setupModuleManager()
->loadSetupModuleIfNecessary();
}
protected function setupLogging()
{
Logger::create(
new ConfigObject(
array(
2014-10-16 15:59:35 +02:00
'level' => Logger::INFO,
'log' => 'stdout',
)
)
);
return $this;
2013-10-22 14:26:45 +02:00
}
public function cliLoader()
{
if ($this->cliLoader === null) {
$this->cliLoader = new Loader($this);
}
return $this->cliLoader;
}
protected function parseBasicParams()
2013-10-22 14:26:45 +02:00
{
$this->params = Params::parse();
if ($this->params->shift('help')) {
$this->params->unshift('help');
}
if ($this->params->shift('autocomplete')) {
$this->params->unshift('autocomplete');
}
2013-10-22 14:26:45 +02:00
$watch = $this->params->shift('watch');
if ($watch === true) {
$watch = 5;
}
if (preg_match('~^\d+$~', $watch)) {
$this->watchTimeout = (int) $watch;
}
$this->debug = (int) $this->params->get('debug');
$this->verbose = (int) $this->params->get('verbose');
2013-10-22 14:26:45 +02:00
$this->showBenchmark = (bool) $this->params->shift('benchmark');
return $this;
}
public function getParams()
{
return $this->params;
}
public function dispatchModule($name, $basedir = null)
{
$this->getModuleManager()->loadModule($name, $basedir);
$this->cliLoader()->setModuleName($name);
$this->dispatch();
}
2013-10-22 14:26:45 +02:00
public function dispatch()
{
Benchmark::measure('Dispatching CLI command');
if ($this->watchTimeout === null) {
$this->dispatchOnce();
} else {
$this->dispatchEndless();
}
}
protected function dispatchOnce()
{
$loader = $this->cliLoader();
2013-10-22 14:26:45 +02:00
$loader->parseParams();
$loader->dispatch();
Benchmark::measure('All done');
if ($this->showBenchmark) {
Benchmark::dump();
}
}
protected function dispatchEndless()
{
$loader = $this->cliLoader();
2013-10-22 14:26:45 +02:00
$loader->parseParams();
$screen = Screen::instance();
2013-10-22 14:26:45 +02:00
while (true) {
Benchmark::measure('Watch mode - loop begins');
ob_start();
2013-10-22 14:26:45 +02:00
$params = clone($this->params);
$loader->dispatch($params);
2013-10-22 14:26:45 +02:00
Benchmark::measure('Dispatch done');
if ($this->showBenchmark) {
Benchmark::dump();
}
Benchmark::reset();
$out = ob_get_contents();
ob_end_clean();
echo $screen->clear() . $out;
2013-10-22 14:26:45 +02:00
sleep($this->watchTimeout);
}
}
/**
* Fail if Icinga has not been called on CLI
*
2013-10-22 14:26:45 +02:00
* @throws ProgrammingError
* @return void
*/
private function assertRunningOnCli()
{
if (Platform::isCli()) {
return;
}
throw new ProgrammingError('Icinga is not running on CLI');
}
}