large performance improvements

This commit is contained in:
Joby 2020-06-22 17:19:41 -06:00
parent bc642be178
commit 5d2e293af0
3 changed files with 27 additions and 67 deletions

View file

@ -13,7 +13,4 @@ interface FlatArrayInterface extends \ArrayAccess, \Iterator
public function pop(?string $name); public function pop(?string $name);
public function unshift(?string $name, $value); public function unshift(?string $name, $value);
public function shift(?string $name); public function shift(?string $name);
public function lock();
public function unlock();
} }

View file

@ -5,23 +5,9 @@ namespace Flatrr;
trait FlatArrayTrait trait FlatArrayTrait
{ {
private $_arrayData = array(); private $_arrayData = array();
private $_locked = false;
public function lock()
{
$this->_locked = true;
}
public function unlock()
{
$this->_locked = false;
}
public function push(?string $name, $value) public function push(?string $name, $value)
{ {
if ($this->_locked) {
return null;
}
$arr = $this->get($name); $arr = $this->get($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return;
@ -35,9 +21,6 @@ trait FlatArrayTrait
public function pop(?string $name) public function pop(?string $name)
{ {
if ($this->_locked) {
return null;
}
$arr = $this->get($name); $arr = $this->get($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return;
@ -49,9 +32,6 @@ trait FlatArrayTrait
public function unshift(?string $name, $value) public function unshift(?string $name, $value)
{ {
if ($this->_locked) {
return null;
}
$arr = $this->get($name); $arr = $this->get($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return;
@ -65,9 +45,6 @@ trait FlatArrayTrait
public function shift(?string $name) public function shift(?string $name)
{ {
if ($this->_locked) {
return null;
}
$arr = $this->get($name); $arr = $this->get($name);
if ($arr !== null && !is_array($arr)) { if ($arr !== null && !is_array($arr)) {
return; return;
@ -79,9 +56,6 @@ trait FlatArrayTrait
public function set(?string $name, $value) public function set(?string $name, $value)
{ {
if ($this->_locked) {
return $this->get($name);
}
return $this->flattenSearch($name, $value); return $this->flattenSearch($name, $value);
} }
@ -90,11 +64,7 @@ trait FlatArrayTrait
return $this->flattenSearch($name); return $this->flattenSearch($name);
} }
public function unset(?string $name) function unset(?string $name) {
{
if ($this->_locked) {
return null;
}
$this->flattenSearch($name, null, true); $this->flattenSearch($name, null, true);
} }
@ -149,9 +119,6 @@ trait FlatArrayTrait
*/ */
public function merge($value, string $name = null, bool $overwrite = false) public function merge($value, string $name = null, bool $overwrite = false)
{ {
if ($this->_locked) {
return null;
}
if (!isset($this[$name])) { if (!isset($this[$name])) {
//easiest possible outcome, old value doesn't exist, so we can just write the value //easiest possible outcome, old value doesn't exist, so we can just write the value
$this->set($name, $value); $this->set($name, $value);
@ -160,7 +127,7 @@ trait FlatArrayTrait
//both new and old values are arrays //both new and old values are arrays
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
if ($name) { if ($name) {
$k = $name.'.'.$k; $k = $name . '.' . $k;
} }
$this->merge($v, $k, $overwrite); $this->merge($v, $k, $overwrite);
} }
@ -198,7 +165,7 @@ trait FlatArrayTrait
return null; return null;
} }
//build a reference to where this name should be //build a reference to where this name should be
$parent = &$this->_arrayData; $parent = &$this->_arrayData;
$name = explode('.', $name); $name = explode('.', $name);
$key = array_pop($name); $key = array_pop($name);
foreach ($name as $part) { foreach ($name as $part) {

View file

@ -4,7 +4,7 @@ namespace Flatrr;
class SelfReferencingFlatArray extends FlatArray 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); $out = parent::get($name);
if ($raw) { if ($raw) {
@ -51,40 +51,36 @@ class SelfReferencingFlatArray extends FlatArray
*/ */
protected function filter($value) 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 //map this function onto array values
if (is_array($value)) { if (is_array($value)) {
return array_map( return array_map(
function ($i) { [$this, 'filter'],
return $this->filter($i); $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 $value
); );
} }
//fall back to just returning value, it's some other datatype //fall back to just returning value, it's some other datatype
return $value; 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];
}
} }