Improve array parsing

This commit is contained in:
Dennis Eichhorn 2018-03-31 21:58:13 +02:00
parent e4531d3726
commit 051c123b77
3 changed files with 15 additions and 12 deletions

View File

@ -54,16 +54,16 @@ class EventManager
/** /**
* Attach new event * Attach new event
* *
* @param string $group Name of the event (unique) * @param string $group Name of the event (unique)
* @param \Closure $callback Callback for the event * @param mixed $callback Callback or route for the event
* @param bool $remove Remove event after triggering it? * @param bool $remove Remove event after triggering it?
* @param bool $reset Reset event after triggering it? Remove must be false! * @param bool $reset Reset event after triggering it? Remove must be false!
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function attach(string $group, \Closure $callback, bool $remove = false, bool $reset = false) : bool public function attach(string $group, $callback, bool $remove = false, bool $reset = false) : bool
{ {
if (isset($this->callbacks[$group])) { if (isset($this->callbacks[$group])) {
return false; return false;
@ -96,6 +96,7 @@ class EventManager
} }
if (!$this->hasOutstanding($group)) { if (!$this->hasOutstanding($group)) {
// todo if it is route then call dispatcher?
$this->callbacks[$group]['func']($data); $this->callbacks[$group]['func']($data);
if ($this->callbacks[$group]['remove']) { if ($this->callbacks[$group]['remove']) {

View File

@ -188,7 +188,7 @@ class InstallerAbstract
$appRoutes = array_merge_recursive($appRoutes, $moduleRoutes); $appRoutes = array_merge_recursive($appRoutes, $moduleRoutes);
if (is_writable($destRoutePath)) { if (is_writable($destRoutePath)) {
file_put_contents($destRoutePath, '<?php return' . ArrayParser::serializeArray($appRoutes) . ';', LOCK_EX); file_put_contents($destRoutePath, '<?php return ' . ArrayParser::serializeArray($appRoutes) . ';', LOCK_EX);
} else { } else {
throw new PermissionException($destRoutePath); throw new PermissionException($destRoutePath);
} }

View File

@ -29,13 +29,14 @@ class ArrayParser
/** /**
* Serializing array (recursively). * Serializing array (recursively).
* *
* @param array $arr Array to serialize * @param array $arr Array to serialize
* @param int $depth Array depth
* *
* @return string * @return string
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function serializeArray(array $arr) : string public static function serializeArray(array $arr, int $depth = 1) : string
{ {
$stringify = '[' . PHP_EOL; $stringify = '[' . PHP_EOL;
@ -44,26 +45,27 @@ class ArrayParser
$key = '"' . $key . '"'; $key = '"' . $key . '"';
} }
$stringify .= ' ' . $key . ' => ' . self::parseVariable($val) . ',' . PHP_EOL; $stringify .= str_repeat(' ', $depth * 4) . $key . ' => ' . self::parseVariable($val, $depth + 1) . ',' . PHP_EOL;
} }
return $stringify . ']'; return $stringify . str_repeat(' ', ($depth - 1) * 4) . ']';
} }
/** /**
* Serialize value. * Serialize value.
* *
* @param mixed $value Value to serialzie * @param mixed $value Value to serialzie
* @param int $depth Array depth
* *
* @return string * @return string
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function parseVariable($value) : string public static function parseVariable($value, int $depth = 1) : string
{ {
if (is_array($value)) { if (is_array($value)) {
return ArrayParser::serializeArray($value) . PHP_EOL; return ArrayParser::serializeArray($value, $depth);
} elseif (is_string($value)) { } elseif (is_string($value)) {
return '"' . $value . '"'; return '"' . $value . '"';
} elseif (is_scalar($value)) { } elseif (is_scalar($value)) {