From 5d2e293af0a48a30fcdadeab490fd613eb5b54fe Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Mon, 22 Jun 2020 17:19:41 -0600 Subject: [PATCH] large performance improvements --- src/FlatArrayInterface.php | 3 -- src/FlatArrayTrait.php | 39 ++---------------------- src/SelfReferencingFlatArray.php | 52 +++++++++++++++----------------- 3 files changed, 27 insertions(+), 67 deletions(-) diff --git a/src/FlatArrayInterface.php b/src/FlatArrayInterface.php index 3977df0..f4da1ef 100644 --- a/src/FlatArrayInterface.php +++ b/src/FlatArrayInterface.php @@ -13,7 +13,4 @@ interface FlatArrayInterface extends \ArrayAccess, \Iterator public function pop(?string $name); public function unshift(?string $name, $value); public function shift(?string $name); - - public function lock(); - public function unlock(); } diff --git a/src/FlatArrayTrait.php b/src/FlatArrayTrait.php index 878f106..318db13 100644 --- a/src/FlatArrayTrait.php +++ b/src/FlatArrayTrait.php @@ -5,23 +5,9 @@ namespace Flatrr; trait FlatArrayTrait { private $_arrayData = array(); - private $_locked = false; - - public function lock() - { - $this->_locked = true; - } - - public function unlock() - { - $this->_locked = false; - } public function push(?string $name, $value) { - if ($this->_locked) { - return null; - } $arr = $this->get($name); if ($arr !== null && !is_array($arr)) { return; @@ -35,9 +21,6 @@ trait FlatArrayTrait public function pop(?string $name) { - if ($this->_locked) { - return null; - } $arr = $this->get($name); if ($arr !== null && !is_array($arr)) { return; @@ -49,9 +32,6 @@ trait FlatArrayTrait public function unshift(?string $name, $value) { - if ($this->_locked) { - return null; - } $arr = $this->get($name); if ($arr !== null && !is_array($arr)) { return; @@ -65,9 +45,6 @@ trait FlatArrayTrait public function shift(?string $name) { - if ($this->_locked) { - return null; - } $arr = $this->get($name); if ($arr !== null && !is_array($arr)) { return; @@ -79,9 +56,6 @@ trait FlatArrayTrait public function set(?string $name, $value) { - if ($this->_locked) { - return $this->get($name); - } return $this->flattenSearch($name, $value); } @@ -90,11 +64,7 @@ trait FlatArrayTrait return $this->flattenSearch($name); } - public function unset(?string $name) - { - if ($this->_locked) { - return null; - } + function unset(?string $name) { $this->flattenSearch($name, null, true); } @@ -149,9 +119,6 @@ trait FlatArrayTrait */ public function merge($value, string $name = null, bool $overwrite = false) { - if ($this->_locked) { - return null; - } if (!isset($this[$name])) { //easiest possible outcome, old value doesn't exist, so we can just write the value $this->set($name, $value); @@ -160,7 +127,7 @@ trait FlatArrayTrait //both new and old values are arrays foreach ($value as $k => $v) { if ($name) { - $k = $name.'.'.$k; + $k = $name . '.' . $k; } $this->merge($v, $k, $overwrite); } @@ -198,7 +165,7 @@ trait FlatArrayTrait return null; } //build a reference to where this name should be - $parent = &$this->_arrayData; + $parent = &$this->_arrayData; $name = explode('.', $name); $key = array_pop($name); foreach ($name as $part) { diff --git a/src/SelfReferencingFlatArray.php b/src/SelfReferencingFlatArray.php index 13f83b9..76cc415 100644 --- a/src/SelfReferencingFlatArray.php +++ b/src/SelfReferencingFlatArray.php @@ -4,7 +4,7 @@ namespace Flatrr; class SelfReferencingFlatArray extends FlatArray { - public function get(string $name=null, bool $raw = false, $unescape = true) + public function get(string $name = null, bool $raw = false, $unescape = true) { $out = parent::get($name); if ($raw) { @@ -51,40 +51,36 @@ class SelfReferencingFlatArray extends FlatArray */ protected function filter($value) { - if ($value === null) { - return null; - } - //search/replace on string values - if (is_string($value)) { - //search for valid replacements - $value = preg_replace_callback( - //search for things like ${var/name}, escape with \ before last brace - '/\$\{([^\}]*[^\.\\\])\}/', - //replace match with value from $this if it exists - function ($matches) { - if (null !== $value = $this->get($matches[1], false, false)) { - if (!is_array($value)) { - return $value; - } - } - return $matches[0]; - }, - //applied to $value - $value - ); - //return - return $value; - } //map this function onto array values if (is_array($value)) { return array_map( - function ($i) { - return $this->filter($i); - }, + [$this, 'filter'], + $value + ); + } + //search/replace on string values + if (is_string($value) && strpos($value, '${') !== false) { + //search for valid replacements + return preg_replace_callback( + //search for things like ${var/name}, escape with \ before last brace + '/\$\{([^\}]*[^\.\\\])\}/S', + //replace match with value from $this if it exists + [$this, 'filter_regex'], + //applied to $value $value ); } //fall back to just returning value, it's some other datatype return $value; } + + protected function filter_regex($matches) + { + if (null !== $value = $this->get($matches[1], false, false)) { + if (!is_array($value)) { + return $value; + } + } + return $matches[0]; + } }