mirror of
https://github.com/Icinga/icingaweb2-module-businessprocess.git
synced 2026-02-10 22:43:04 -05:00
LegacyStorage: split logic into more methods
We want to optimize the process and be able to read from raw strings too.
This commit is contained in:
parent
7d31250816
commit
bdac469099
1 changed files with 132 additions and 107 deletions
|
|
@ -89,14 +89,6 @@ class LegacyStorage extends Storage
|
|||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BusinessProcess
|
||||
*/
|
||||
public function loadProcess($name)
|
||||
{
|
||||
return $this->parse($name);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function storeProcess(BusinessProcess $process)
|
||||
|
|
@ -114,11 +106,41 @@ class LegacyStorage extends Storage
|
|||
return $this->getConfigDir() . '/' . $name . '.conf';
|
||||
}
|
||||
|
||||
protected function parse($name)
|
||||
public function loadFromString($name, $string)
|
||||
{
|
||||
$file = $this->currentFilename = $this->getFilename($name);
|
||||
$bp = new BusinessProcess();
|
||||
$bp->setName($name);
|
||||
$this->parseString($string, $bp);
|
||||
$this->loadHeader($name, $bp);
|
||||
return $bp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BusinessProcess
|
||||
*/
|
||||
public function loadProcess($name)
|
||||
{
|
||||
$bp = new BusinessProcess();
|
||||
$bp->setName($name);
|
||||
$this->parseFile($name, $bp);
|
||||
$this->loadHeader($name, $bp);
|
||||
return $bp;
|
||||
}
|
||||
|
||||
protected function loadHeader($name, $bp)
|
||||
{
|
||||
// TODO: do not open twice, this is quick and dirty based on existing code
|
||||
$file = $this->currentFilename = $this->getFilename($name);
|
||||
$header = $this->readHeader($file, $name);
|
||||
$bp->setTitle($header['Title']);
|
||||
if ($header['Backend']) {
|
||||
$bp->loadBackendByName($header['Backend']);
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseFile($name, $bp)
|
||||
{
|
||||
$file = $this->currentFilename = $this->getFilename($name);
|
||||
$fh = @fopen($file, 'r');
|
||||
if (! $fh) {
|
||||
throw new SystemPermissionException('Could not open ' . $file);
|
||||
|
|
@ -126,111 +148,114 @@ class LegacyStorage extends Storage
|
|||
|
||||
$this->parsing_line_number = 0;
|
||||
while ($line = fgets($fh)) {
|
||||
$line = trim($line);
|
||||
|
||||
$this->parsing_line_number++;
|
||||
|
||||
if (empty($line)) {
|
||||
continue;
|
||||
}
|
||||
if ($line[0] === '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: substr?
|
||||
if (preg_match('~^display~', $line)) {
|
||||
list($display, $name, $desc) = preg_split('~\s*;\s*~', substr($line, 8), 3);
|
||||
$node = $bp->getNode($name)->setAlias($desc)->setDisplay($display);
|
||||
if ($display > 0) {
|
||||
$bp->addRootNode($name);
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('~^external_info~', $line)) {
|
||||
list($name, $script) = preg_split('~\s*;\s*~', substr($line, 14), 2);
|
||||
$node = $bp->getNode($name)->setInfoCommand($script);
|
||||
}
|
||||
|
||||
// New feature:
|
||||
// if (preg_match('~^extra_info~', $line)) {
|
||||
// list($name, $script) = preg_split('~\s*;\s*~', substr($line, 14), 2);
|
||||
// $node = $this->getNode($name)->setExtraInfo($script);
|
||||
// }
|
||||
|
||||
if (preg_match('~^info_url~', $line)) {
|
||||
list($name, $url) = preg_split('~\s*;\s*~', substr($line, 9), 2);
|
||||
$node = $bp->getNode($name)->setInfoUrl($url);
|
||||
}
|
||||
|
||||
if (strpos($line, '=') === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
list($name, $value) = preg_split('~\s*=\s*~', $line, 2);
|
||||
|
||||
if (strpos($name, ';') !== false) {
|
||||
$this->parseError('No semicolon allowed in varname');
|
||||
}
|
||||
|
||||
$op = '&';
|
||||
if (preg_match_all('~([\|\+&])~', $value, $m)) {
|
||||
$op = implode('', $m[1]);
|
||||
for ($i = 1; $i < strlen($op); $i++) {
|
||||
if ($op[$i] !== $op[$i - 1]) {
|
||||
$this->parseError('Mixing operators is not allowed');
|
||||
}
|
||||
}
|
||||
}
|
||||
$op = $op[0];
|
||||
$op_name = $op;
|
||||
|
||||
if ($op === '+') {
|
||||
if (! preg_match('~^(\d+)\s*of:\s*(.+?)$~', $value, $m)) {
|
||||
$this->parseError('syntax: <var> = <num> of: <var1> + <var2> [+ <varn>]*');
|
||||
}
|
||||
$op_name = $m[1];
|
||||
$value = $m[2];
|
||||
}
|
||||
$cmps = preg_split('~\s*\\' . $op . '\s*~', $value);
|
||||
|
||||
foreach ($cmps as & $val) {
|
||||
if (strpos($val, ';') !== false) {
|
||||
if ($bp->hasNode($val)) continue;
|
||||
|
||||
list($host, $service) = preg_split('~;~', $val, 2);
|
||||
if ($service === 'Hoststatus') {
|
||||
$bp->createHost($host);
|
||||
} else {
|
||||
$bp->createService($host, $service);
|
||||
}
|
||||
}
|
||||
if ($val[0] === '@' && strpos($val, ':') !== false) {
|
||||
list($config, $nodeName) = preg_split('~:\s*~', substr($val, 1), 2);
|
||||
$bp->createImportedNode($config, $nodeName);
|
||||
$val = $nodeName;
|
||||
}
|
||||
}
|
||||
|
||||
$node = new BpNode($bp, (object) array(
|
||||
'name' => $name,
|
||||
'operator' => $op_name,
|
||||
'child_names' => $cmps
|
||||
));
|
||||
$bp->addNode($name, $node);
|
||||
$this->parseLine($line, $bp);
|
||||
}
|
||||
|
||||
fclose($fh);
|
||||
unset($this->parsing_line_number);
|
||||
unset($this->currentFilename);
|
||||
}
|
||||
|
||||
// TODO: do not open twice, this is quick and dirty based on existing code
|
||||
$header = $this->readHeader($file, $name);
|
||||
$bp->setTitle($header['Title']);
|
||||
if ($header['Backend']) {
|
||||
$bp->loadBackendByName($header['Backend']);
|
||||
protected function parseString($string, $bp)
|
||||
{
|
||||
foreach (preg_split('/\n/', $string) as $line) {
|
||||
$this->parseLine($line, $bp);
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseLine(& $line, $bp)
|
||||
{
|
||||
$line = trim($line);
|
||||
|
||||
$this->parsing_line_number++;
|
||||
|
||||
if (empty($line)) {
|
||||
return;
|
||||
}
|
||||
if ($line[0] === '#') {
|
||||
return;
|
||||
}
|
||||
|
||||
return $bp;
|
||||
// TODO: substr?
|
||||
if (preg_match('~^display~', $line)) {
|
||||
list($display, $name, $desc) = preg_split('~\s*;\s*~', substr($line, 8), 3);
|
||||
$node = $bp->getNode($name)->setAlias($desc)->setDisplay($display);
|
||||
if ($display > 0) {
|
||||
$bp->addRootNode($name);
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('~^external_info~', $line)) {
|
||||
list($name, $script) = preg_split('~\s*;\s*~', substr($line, 14), 2);
|
||||
$node = $bp->getNode($name)->setInfoCommand($script);
|
||||
}
|
||||
|
||||
// New feature:
|
||||
// if (preg_match('~^extra_info~', $line)) {
|
||||
// list($name, $script) = preg_split('~\s*;\s*~', substr($line, 14), 2);
|
||||
// $node = $this->getNode($name)->setExtraInfo($script);
|
||||
// }
|
||||
|
||||
if (preg_match('~^info_url~', $line)) {
|
||||
list($name, $url) = preg_split('~\s*;\s*~', substr($line, 9), 2);
|
||||
$node = $bp->getNode($name)->setInfoUrl($url);
|
||||
}
|
||||
|
||||
if (strpos($line, '=') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
list($name, $value) = preg_split('~\s*=\s*~', $line, 2);
|
||||
|
||||
if (strpos($name, ';') !== false) {
|
||||
$this->parseError('No semicolon allowed in varname');
|
||||
}
|
||||
|
||||
$op = '&';
|
||||
if (preg_match_all('~([\|\+&])~', $value, $m)) {
|
||||
$op = implode('', $m[1]);
|
||||
for ($i = 1; $i < strlen($op); $i++) {
|
||||
if ($op[$i] !== $op[$i - 1]) {
|
||||
$this->parseError('Mixing operators is not allowed');
|
||||
}
|
||||
}
|
||||
}
|
||||
$op = $op[0];
|
||||
$op_name = $op;
|
||||
|
||||
if ($op === '+') {
|
||||
if (! preg_match('~^(\d+)\s*of:\s*(.+?)$~', $value, $m)) {
|
||||
$this->parseError('syntax: <var> = <num> of: <var1> + <var2> [+ <varn>]*');
|
||||
}
|
||||
$op_name = $m[1];
|
||||
$value = $m[2];
|
||||
}
|
||||
$cmps = preg_split('~\s*\\' . $op . '\s*~', $value);
|
||||
|
||||
foreach ($cmps as & $val) {
|
||||
if (strpos($val, ';') !== false) {
|
||||
if ($bp->hasNode($val)) continue;
|
||||
|
||||
list($host, $service) = preg_split('~;~', $val, 2);
|
||||
if ($service === 'Hoststatus') {
|
||||
$bp->createHost($host);
|
||||
} else {
|
||||
$bp->createService($host, $service);
|
||||
}
|
||||
}
|
||||
if ($val[0] === '@' && strpos($val, ':') !== false) {
|
||||
list($config, $nodeName) = preg_split('~:\s*~', substr($val, 1), 2);
|
||||
$bp->createImportedNode($config, $nodeName);
|
||||
$val = $nodeName;
|
||||
}
|
||||
}
|
||||
|
||||
$node = new BpNode($bp, (object) array(
|
||||
'name' => $name,
|
||||
'operator' => $op_name,
|
||||
'child_names' => $cmps
|
||||
));
|
||||
$bp->addNode($name, $node);
|
||||
}
|
||||
|
||||
protected function parseError($msg)
|
||||
|
|
|
|||
Loading…
Reference in a new issue