mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-29 16:54:04 +02:00
Application\Hook: move existing ones, keep compat
This commit is contained in:
parent
576747f792
commit
d903f850da
111
library/Icinga/Application/Hook/GrapherHook.php
Normal file
111
library/Icinga/Application/Hook/GrapherHook.php
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Icinga\Application\Hook;
|
||||||
|
|
||||||
|
use Icinga\Exception\ProgrammingError;
|
||||||
|
use Icinga\Module\Monitoring\Object\MonitoredObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Icinga Web Grapher Hook base class
|
||||||
|
*
|
||||||
|
* Extend this class if you want to integrate your graphing solution nicely into
|
||||||
|
* Icinga Web.
|
||||||
|
*/
|
||||||
|
abstract class GrapherHook extends WebBaseHook
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Whether this grapher provides previews
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $hasPreviews = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this grapher provides tiny previews
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $hasTinyPreviews = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor must live without arguments right now
|
||||||
|
*
|
||||||
|
* Therefore the constructor is final, we might change our opinion about
|
||||||
|
* this one far day
|
||||||
|
*/
|
||||||
|
final public function __construct()
|
||||||
|
{
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwrite this function if you want to do some initialization stuff
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function init()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this grapher provides previews
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasPreviews()
|
||||||
|
{
|
||||||
|
return $this->hasPreviews;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this grapher provides tiny previews
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasTinyPreviews()
|
||||||
|
{
|
||||||
|
return $this->hasTinyPreviews;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether a graph for the monitoring object exist
|
||||||
|
*
|
||||||
|
* @param MonitoredObject $object
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
abstract public function has(MonitoredObject $object);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a preview for the given object
|
||||||
|
*
|
||||||
|
* This function must return an empty string if no graph exists.
|
||||||
|
*
|
||||||
|
* @param MonitoredObject $object
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* @throws ProgrammingError
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function getPreviewHtml(MonitoredObject $object)
|
||||||
|
{
|
||||||
|
throw new ProgrammingError('This hook provide previews but it is not implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a tiny preview for the given object
|
||||||
|
*
|
||||||
|
* This function must return an empty string if no graph exists.
|
||||||
|
*
|
||||||
|
* @param MonitoredObject $object
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* @throws ProgrammingError
|
||||||
|
*/
|
||||||
|
public function getTinyPreviewHtml(MonitoredObject $object)
|
||||||
|
{
|
||||||
|
throw new ProgrammingError('This hook provide tiny previews but it is not implemented');
|
||||||
|
}
|
||||||
|
}
|
124
library/Icinga/Application/Hook/TicketHook.php
Normal file
124
library/Icinga/Application/Hook/TicketHook.php
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Icinga\Application\Hook;
|
||||||
|
|
||||||
|
use ErrorException;
|
||||||
|
use Exception;
|
||||||
|
use Icinga\Application\Logger;
|
||||||
|
use Icinga\Exception\IcingaException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for ticket hooks
|
||||||
|
*
|
||||||
|
* Extend this class if you want to integrate your ticketing solution Icinga Web 2
|
||||||
|
*/
|
||||||
|
abstract class TicketHook
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Last error, if any
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
protected $lastError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ticket hook
|
||||||
|
*
|
||||||
|
* @see init() For hook initialization.
|
||||||
|
*/
|
||||||
|
final public function __construct()
|
||||||
|
{
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwrite this function for hook initialization, e.g. loading the hook's config
|
||||||
|
*/
|
||||||
|
protected function init()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the hook as failed w/ the given message
|
||||||
|
*
|
||||||
|
* @param string $message Error message or error format string
|
||||||
|
* @param mixed ...$arg Format string argument
|
||||||
|
*/
|
||||||
|
private function fail($message)
|
||||||
|
{
|
||||||
|
$args = array_slice(func_get_args(), 1);
|
||||||
|
$lastError = vsprintf($message, $args);
|
||||||
|
Logger::debug($lastError);
|
||||||
|
$this->lastError = $lastError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the last error, if any
|
||||||
|
*
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getLastError()
|
||||||
|
{
|
||||||
|
return $this->lastError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the pattern
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract public function getPattern();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a link for each matched element in the subject text
|
||||||
|
*
|
||||||
|
* @param array $match Array of matched elements according to {@link getPattern()}
|
||||||
|
*
|
||||||
|
* @return string Replacement string
|
||||||
|
*/
|
||||||
|
abstract public function createLink($match);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create links w/ {@link createLink()} in the given text that matches to the subject from {@link getPattern()}
|
||||||
|
*
|
||||||
|
* In case of errors a debug message is recorded to the log and any subsequent call to {@link createLinks()} will
|
||||||
|
* be a no-op.
|
||||||
|
*
|
||||||
|
* @param string $text
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
final public function createLinks($text)
|
||||||
|
{
|
||||||
|
if ($this->lastError !== null) {
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pattern = $this->getPattern();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->fail('Can\'t create ticket links: Retrieving the pattern failed: %s', IcingaException::describe($e));
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
if (empty($pattern)) {
|
||||||
|
$this->fail('Can\'t create ticket links: Pattern is empty');
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$text = preg_replace_callback(
|
||||||
|
$pattern,
|
||||||
|
array($this, 'createLink'),
|
||||||
|
$text
|
||||||
|
);
|
||||||
|
} catch (ErrorException $e) {
|
||||||
|
$this->fail('Can\'t create ticket links: Pattern is invalid: %s', IcingaException::describe($e));
|
||||||
|
return $text;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->fail('Can\'t create ticket links: %s', IcingaException::describe($e));
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
namespace Icinga\Web\Hook;
|
namespace Icinga\Application\Hook;
|
||||||
|
|
||||||
use Zend_Controller_Action_HelperBroker;
|
use Zend_Controller_Action_HelperBroker;
|
||||||
use Zend_View;
|
use Zend_View;
|
14
library/Icinga/Web/Hook.php
Normal file
14
library/Icinga/Web/Hook.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Web;
|
||||||
|
|
||||||
|
use Icinga\Application\Hook as NewHookImplementation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Icinga Web Hook registry
|
||||||
|
*
|
||||||
|
* Deprecated, please use Icinga\Application\Hook instead
|
||||||
|
*/
|
||||||
|
class Hook extends NewHookImplementation
|
||||||
|
{
|
||||||
|
}
|
@ -3,109 +3,11 @@
|
|||||||
|
|
||||||
namespace Icinga\Web\Hook;
|
namespace Icinga\Web\Hook;
|
||||||
|
|
||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Application\Hook\GrapherHook as BaseHook;
|
||||||
use Icinga\Module\Monitoring\Object\MonitoredObject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Icinga Web Grapher Hook base class
|
* Deprecated, compat only.
|
||||||
*
|
*
|
||||||
* Extend this class if you want to integrate your graphing solution nicely into
|
* Please implement hooks in Icinga\Application\Hook
|
||||||
* Icinga Web.
|
|
||||||
*/
|
*/
|
||||||
abstract class GrapherHook extends WebBaseHook
|
abstract class GrapherHook extends BaseHook {}
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Whether this grapher provides previews
|
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $hasPreviews = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether this grapher provides tiny previews
|
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $hasTinyPreviews = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor must live without arguments right now
|
|
||||||
*
|
|
||||||
* Therefore the constructor is final, we might change our opinion about
|
|
||||||
* this one far day
|
|
||||||
*/
|
|
||||||
final public function __construct()
|
|
||||||
{
|
|
||||||
$this->init();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overwrite this function if you want to do some initialization stuff
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
protected function init()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether this grapher provides previews
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasPreviews()
|
|
||||||
{
|
|
||||||
return $this->hasPreviews;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether this grapher provides tiny previews
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasTinyPreviews()
|
|
||||||
{
|
|
||||||
return $this->hasTinyPreviews;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether a graph for the monitoring object exist
|
|
||||||
*
|
|
||||||
* @param MonitoredObject $object
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
abstract public function has(MonitoredObject $object);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a preview for the given object
|
|
||||||
*
|
|
||||||
* This function must return an empty string if no graph exists.
|
|
||||||
*
|
|
||||||
* @param MonitoredObject $object
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* @throws ProgrammingError
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function getPreviewHtml(MonitoredObject $object)
|
|
||||||
{
|
|
||||||
throw new ProgrammingError('This hook provide previews but it is not implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a tiny preview for the given object
|
|
||||||
*
|
|
||||||
* This function must return an empty string if no graph exists.
|
|
||||||
*
|
|
||||||
* @param MonitoredObject $object
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* @throws ProgrammingError
|
|
||||||
*/
|
|
||||||
public function getTinyPreviewHtml(MonitoredObject $object)
|
|
||||||
{
|
|
||||||
throw new ProgrammingError('This hook provide tiny previews but it is not implemented');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -3,122 +3,11 @@
|
|||||||
|
|
||||||
namespace Icinga\Web\Hook;
|
namespace Icinga\Web\Hook;
|
||||||
|
|
||||||
use ErrorException;
|
use Icinga\Application\Hook\TicketHook as BaseHook;
|
||||||
use Exception;
|
|
||||||
use Icinga\Application\Logger;
|
|
||||||
use Icinga\Exception\IcingaException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for ticket hooks
|
* Deprecated, compat only.
|
||||||
*
|
*
|
||||||
* Extend this class if you want to integrate your ticketing solution Icinga Web 2
|
* Please implement hooks in Icinga\Application\Hook
|
||||||
*/
|
*/
|
||||||
abstract class TicketHook
|
abstract class TicketHook extends BaseHook {}
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Last error, if any
|
|
||||||
*
|
|
||||||
* @var string|null
|
|
||||||
*/
|
|
||||||
protected $lastError;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new ticket hook
|
|
||||||
*
|
|
||||||
* @see init() For hook initialization.
|
|
||||||
*/
|
|
||||||
final public function __construct()
|
|
||||||
{
|
|
||||||
$this->init();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overwrite this function for hook initialization, e.g. loading the hook's config
|
|
||||||
*/
|
|
||||||
protected function init()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the hook as failed w/ the given message
|
|
||||||
*
|
|
||||||
* @param string $message Error message or error format string
|
|
||||||
* @param mixed ...$arg Format string argument
|
|
||||||
*/
|
|
||||||
private function fail($message)
|
|
||||||
{
|
|
||||||
$args = array_slice(func_get_args(), 1);
|
|
||||||
$lastError = vsprintf($message, $args);
|
|
||||||
Logger::debug($lastError);
|
|
||||||
$this->lastError = $lastError;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the last error, if any
|
|
||||||
*
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getLastError()
|
|
||||||
{
|
|
||||||
return $this->lastError;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the pattern
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
abstract public function getPattern();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a link for each matched element in the subject text
|
|
||||||
*
|
|
||||||
* @param array $match Array of matched elements according to {@link getPattern()}
|
|
||||||
*
|
|
||||||
* @return string Replacement string
|
|
||||||
*/
|
|
||||||
abstract public function createLink($match);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create links w/ {@link createLink()} in the given text that matches to the subject from {@link getPattern()}
|
|
||||||
*
|
|
||||||
* In case of errors a debug message is recorded to the log and any subsequent call to {@link createLinks()} will
|
|
||||||
* be a no-op.
|
|
||||||
*
|
|
||||||
* @param string $text
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
final public function createLinks($text)
|
|
||||||
{
|
|
||||||
if ($this->lastError !== null) {
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$pattern = $this->getPattern();
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->fail('Can\'t create ticket links: Retrieving the pattern failed: %s', IcingaException::describe($e));
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
if (empty($pattern)) {
|
|
||||||
$this->fail('Can\'t create ticket links: Pattern is empty');
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
$text = preg_replace_callback(
|
|
||||||
$pattern,
|
|
||||||
array($this, 'createLink'),
|
|
||||||
$text
|
|
||||||
);
|
|
||||||
} catch (ErrorException $e) {
|
|
||||||
$this->fail('Can\'t create ticket links: Pattern is invalid: %s', IcingaException::describe($e));
|
|
||||||
return $text;
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->fail('Can\'t create ticket links: %s', IcingaException::describe($e));
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user