Skip to content
Browse files

Add caching of get_extensions(). This is made kinda fun by the static…

…ness of parts of it.
  • Loading branch information...
1 parent 52e9100 commit 29390c466834b515874da6d70a23b31f8e2464cd @gsnedders committed Mar 14, 2010
Showing with 37 additions and 7 deletions.
  1. +36 −7 src/extension.php
  2. +1 −0 src/xml/data.php
View
43 src/extension.php
@@ -6,6 +6,17 @@
protected static $static_ext = array();
protected $object_ext = array();
+ private static $changed_statically = array();
+ private $static_change_start = 0;
+ private $modified = false;
+ private $cache;
+
+ public function __construct()
+ {
+ $this->static_change_start = count(self::$changed_statically);
+ $this->cache = new CacheArray;
+ }
+
public static function add_static_extension($extpoint, $ext, $priority)
{
if (!static::static_ext_point_exists($extpoint))
@@ -14,6 +25,7 @@ public static function add_static_extension($extpoint, $ext, $priority)
}
else
{
+ self::$changed_statically[] = get_called_class();
static::$static_ext[$extpoint][(int) $priority][] = $ext;
}
}
@@ -60,6 +72,7 @@ public function add_extension_point($name)
{
if (!isset(static::$static_ext[$name]) && !isset($this->object_ext[$name]))
{
+ $this->modified = true;
$this->object_ext[$name] = array();
}
else
@@ -69,7 +82,26 @@ public function add_extension_point($name)
}
public function get_extensions($extpoint)
- {
+ {
+ if (isset($this->cache[$extpoint]) && !$this->modified)
+ {
+ $valid = true;
+ for (; $this->static_change_start < count(self::$changed_statically); $this->static_change_start++)
+ {
+ if ($this instanceof self::$changed_statically[$this->static_change_start])
+ {
+ $valid = false;
+ $this->static_change_start = count(self::$changed_statically);
+ break;
+ }
+ }
+
+ if ($valid)
+ {
+ return $this->cache[$extpoint];
+ }
+ }
+
$extensions = array();
$extpoint_exists = false;
@@ -97,26 +129,23 @@ public function get_extensions($extpoint)
if ($extpoint_exists)
{
+ $return = array();
if ($extensions)
{
// Sort by priority (where lower is higher priority).
ksort($extensions, SORT_NUMERIC);
// Flatten to a 1D array and remove duplicates.
- $return = array();
foreach (call_user_func_array('array_merge', $extensions) as $extension)
{
if (!in_array($extension, $return))
{
$return[] = $extension;
}
}
- return $return;
- }
- else
- {
- return array();
}
+ $this->cache[$extpoint] = $return;
+ return $return;
}
else
{
View
1 src/xml/data.php
@@ -5,6 +5,7 @@ class Data extends \ComplexPie\Data
{
public function __construct()
{
+ parent::__construct();
$this->add_extension('get', get_class($this) . '::get', ~PHP_INT_MAX);
}

0 comments on commit 29390c4

Please sign in to comment.
Something went wrong with that request. Please try again.