[icinga-checkins] icinga.org: icingaweb2-module-director/feature-ddo-playground: Sync: move helper methods to SyncUtils

git at icinga.org git at icinga.org
Tue Jul 19 10:31:51 CEST 2016


Module: icingaweb2-module-director
Branch: feature-ddo-playground
Commit: 465740c37ca18971475186a0415814738eaffd6d
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=465740c37ca18971475186a0415814738eaffd6d

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Jul 13 13:52:15 2016 +0200

Sync: move helper methods to SyncUtils

---

 library/Director/Import/Sync.php      |  115 ++-------------------------------
 library/Director/Import/SyncUtils.php |  111 +++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 109 deletions(-)

diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php
index 30434d3..c3d3978 100644
--- a/library/Director/Import/Sync.php
+++ b/library/Director/Import/Sync.php
@@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Import;
 
 use Exception;
 use Icinga\Data\Filter\Filter;
+use Icinga\Module\Director\Import\SyncUtils;
 use Icinga\Module\Director\Objects\IcingaObject;
 use Icinga\Module\Director\Objects\ImportSource;
 use Icinga\Module\Director\Objects\IcingaService;
@@ -115,22 +116,6 @@ class Sync
     }
 
     /**
-     * Extract variable names in the form ${var_name} from a given string
-     *
-     * @param  string $string
-     *
-     * @return array  List of variable names (without ${})
-     */
-    protected function extractVariableNames($string)
-    {
-        if (preg_match_all('/\${([A-Za-z0-9\._-]+)}/', $string, $m, PREG_PATTERN_ORDER)) {
-            return $m[1];
-        } else {
-            return array();
-        }
-    }
-
-    /**
      * Transform the given value to an array
      *
      * @param  array|string|null $value
@@ -149,95 +134,6 @@ class Sync
     }
 
     /**
-     * Recursively extract a value from a nested structure
-     *
-     * For a $val looking like
-     *
-     * { 'vars' => { 'disk' => { 'sda' => { 'size' => '256G' } } } }
-     *
-     * and a key vars.disk.sda given as [ 'vars', 'disk', 'sda' ] this would
-     * return { size => '255GB' }
-     *
-     * @param  string $val  The value to extract data from
-     * @param  object $keys A list of nested keys pointing to desired data
-     *
-     * @return mixed
-     */
-    protected function getDeepValue($val, $keys)
-    {
-        $key = array_shift($keys);
-        if (! property_exists($val, $key)) {
-            return null;
-        }
-
-        if (empty($keys)) {
-            return $val->$key;
-        }
-
-        return $this->getDeepValue($val->$key, $keys);
-    }
-
-    /**
-     * Return a specific value from a given row object
-     *
-     * Supports also keys pointing to nested structures like vars.disk.sda
-     *
-     * @param  object $row    stdClass object providing property values
-     * @param  string $string Variable/property name
-     *
-     * @return mixed
-     */
-    public function getSpecificValue($row, $var)
-    {
-        if (strpos($var, '.') === false) {
-            if ($row instanceof IcingaObject) {
-                return $row->$var;
-            }
-            if (! property_exists($row, $var)) {
-                return null;
-            }
-
-            return $row->$var;
-        } else {
-            $parts = explode('.', $var);
-            $main = array_shift($parts);
-            if (! is_object($row->$main)) {
-                throw new IcingaException('Data is not nested, cannot access %s: %s', $var, var_export($row, 1));
-            }
-
-            return $this->getDeepValue($row->$main, $parts);
-        }
-    }
-
-    /**
-     * Fill variables in the given string pattern
-     *
-     * This replaces all occurances of ${var_name} with the corresponding
-     * property $row->var_name of the given row object. Missing variables are
-     * replaced by an empty string. This works also fine in case there are
-     * multiple variables to be found in your string.
-     *
-     * @param  string $string String with opional variables/placeholders
-     * @param  object $row    stdClass object providing property values
-     *
-     * @return string
-     */
-    protected function fillVariables($string, $row)
-    {
-        if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) {
-            return $this->getSpecificValue($row, $m[1]);
-        }
-
-        // PHP 5.3 :(
-        $self = $this;
-        $func = function ($match) use ($self, $row) {
-            return $self->getSpecificValue($row, $match[1]);
-        };
-
-        return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
-    }
-
-    /**
      * Raise PHP resource limits
      *
      * TODO: do this in a failsafe way, and only if necessary
@@ -328,7 +224,7 @@ class Sync
                 $this->sourceColumns[$sourceId] = array();
             }
 
-            foreach ($this->extractVariableNames($p->source_expression) as $varname) {
+            foreach (SyncUtils::extractVariableNames($p->source_expression) as $varname) {
                 $this->sourceColumns[$sourceId][$varname] = $varname;
                 // -> ? $fieldMap[
             }
@@ -398,7 +294,8 @@ class Sync
             $this->imported[$sourceId] = array();
             foreach ($rows as $row) {
                 if ($this->hasCombinedKey()) {
-                    $key = $this->fillVariables($this->sourceKeyPattern, $row);
+                    $key = SyncUtils::fillVariables($this->sourceKeyPattern, $row);
+
                     if (array_key_exists($key, $this->imported[$sourceId])) {
                         throw new IcingaException(
                             'Trying to import row "%s" (%s) twice: %s VS %s',
@@ -485,7 +382,7 @@ class Sync
                     }
                 }
 
-                $key = $this->fillVariables(
+                $key = SyncUtils::fillVariables(
                     $this->destinationKeyPattern,
                     $object
                 );
@@ -539,7 +436,7 @@ class Sync
 
                     $prop = $p->destination_field;
 
-                    $val = $this->fillVariables($p->source_expression, $row);
+                    $val = SyncUtils::fillVariables($p->source_expression, $row);
 
                     if (substr($prop, 0, 5) === 'vars.') {
                         $varName = substr($prop, 5);
diff --git a/library/Director/Import/SyncUtils.php b/library/Director/Import/SyncUtils.php
new file mode 100644
index 0000000..0c89615
--- /dev/null
+++ b/library/Director/Import/SyncUtils.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Icinga\Module\Director\Import;
+
+use Icinga\Module\Director\Objects\IcingaObject;
+
+class SyncUtils
+{
+    /**
+     * Extract variable names in the form ${var_name} from a given string
+     *
+     * @param  string $string
+     *
+     * @return array  List of variable names (without ${})
+     */
+    public static function extractVariableNames($string)
+    {
+        if (preg_match_all('/\${([A-Za-z0-9\._-]+)}/', $string, $m, PREG_PATTERN_ORDER)) {
+            return $m[1];
+        } else {
+            return array();
+        }
+    }
+
+    /**
+     * Recursively extract a value from a nested structure
+     *
+     * For a $val looking like
+     *
+     * { 'vars' => { 'disk' => { 'sda' => { 'size' => '256G' } } } }
+     *
+     * and a key vars.disk.sda given as [ 'vars', 'disk', 'sda' ] this would
+     * return { size => '255GB' }
+     *
+     * @param  string $val  The value to extract data from
+     * @param  object $keys A list of nested keys pointing to desired data
+     *
+     * @return mixed
+     */
+    public static function getDeepValue($val, $keys)
+    {
+        $key = array_shift($keys);
+        if (! property_exists($val, $key)) {
+            return null;
+        }
+
+        if (empty($keys)) {
+            return $val->$key;
+        }
+
+        return static::getDeepValue($val->$key, $keys);
+    }
+
+    /**
+     * Return a specific value from a given row object
+     *
+     * Supports also keys pointing to nested structures like vars.disk.sda
+     *
+     * @param  object $row    stdClass object providing property values
+     * @param  string $string Variable/property name
+     *
+     * @return mixed
+     */
+    public static function getSpecificValue($row, $var)
+    {
+        if (strpos($var, '.') === false) {
+            if ($row instanceof IcingaObject) {
+                return $row->$var;
+            }
+            if (! property_exists($row, $var)) {
+                return null;
+            }
+
+            return $row->$var;
+        } else {
+            $parts = explode('.', $var);
+            $main = array_shift($parts);
+            if (! is_object($row->$main)) {
+                throw new IcingaException('Data is not nested, cannot access %s: %s', $var, var_export($row, 1));
+            }
+
+            return static::getDeepValue($row->$main, $parts);
+        }
+    }
+
+    /**
+     * Fill variables in the given string pattern
+     *
+     * This replaces all occurances of ${var_name} with the corresponding
+     * property $row->var_name of the given row object. Missing variables are
+     * replaced by an empty string. This works also fine in case there are
+     * multiple variables to be found in your string.
+     *
+     * @param  string $string String with opional variables/placeholders
+     * @param  object $row    stdClass object providing property values
+     *
+     * @return string
+     */
+    public static function fillVariables($string, $row)
+    {
+        if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) {
+            return static::getSpecificValue($row, $m[1]);
+        }
+
+        $func = function ($match) use ($row) {
+            return static::getSpecificValue($row, $match[1]);
+        };
+
+        return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
+    }
+}



More information about the icinga-checkins mailing list