Skip to content

Instantly share code, notes, and snippets.

@markstory
Created October 30, 2013 03:26
Show Gist options
  • Save markstory/7226799 to your computer and use it in GitHub Desktop.
Save markstory/7226799 to your computer and use it in GitHub Desktop.
diff --git a/Cake/ORM/BehaviorRegistry.php b/Cake/ORM/BehaviorRegistry.php
index db1aa77..06d2d63 100644
--- a/Cake/ORM/BehaviorRegistry.php
+++ b/Cake/ORM/BehaviorRegistry.php
@@ -14,6 +14,7 @@
*/
namespace Cake\ORM;
+use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Error;
use Cake\ORM\Behavior;
@@ -113,7 +114,11 @@ class BehaviorRegistry extends ObjectRegistry {
if ($enable) {
$this->_eventManager->attach($instance);
}
- $this->_mapMethods($instance, $class, $alias);
+ $methods = Cache::remember($class . '_method_map', function () use ($instance, $class, $alias) {
+ $this->_mapMethods($instance, $class, $alias);
+ }, 'fast');
+ $this->_methodMap = $methods['methods'];
+ $this->_finderMap = $methods['finders'];
return $instance;
}
@@ -132,7 +137,7 @@ class BehaviorRegistry extends ObjectRegistry {
$events = $instance->implementedEvents();
$reflection = new \ReflectionClass($instance);
- $eventMethods = [];
+ $eventMethods = $methodMap = $finderMap = [];
foreach ($events as $e => $binding) {
if (is_array($binding) && isset($binding['callable']) && isset($binding['callable'])) {
$binding = $binding['callable'];
@@ -151,24 +156,25 @@ class BehaviorRegistry extends ObjectRegistry {
}
$methodName = strtolower($methodName);
- if (isset($this->_finderMap[$methodName]) || isset($this->_methodMap[$methodName])) {
+ if (isset($finderMap[$methodName]) || isset($methodMap[$methodName])) {
$error = __d(
'cake_dev',
'%s contains duplicate method "%s" which is already provided by %s',
$class,
$method->getName(),
- $this->_methodMap[$methodName]
+ $methodMap[$methodName]
);
throw new Error\Exception($error);
}
$isFinder = substr($methodName, 0, 4) === 'find';
if ($isFinder) {
- $this->_finderMap[$methodName] = $alias;
+ $finderMap[$methodName] = $alias;
} else {
- $this->_methodMap[$methodName] = $alias;
+ $methodMap[$methodName] = $alias;
}
}
+ return ['methods' => $methodMap, 'finders' => $finderMap];
}
/**
diff --git a/Cake/ORM/BehaviorRegistry.php b/Cake/ORM/BehaviorRegistry.php
index db1aa77..fa001c8 100644
--- a/Cake/ORM/BehaviorRegistry.php
+++ b/Cake/ORM/BehaviorRegistry.php
@@ -14,6 +14,7 @@
*/
namespace Cake\ORM;
+use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Error;
use Cake\ORM\Behavior;
@@ -58,6 +59,7 @@ class BehaviorRegistry extends ObjectRegistry {
*/
protected $_finderMap = [];
+ protected static $_cache = [];
/**
* Constructor
*
@@ -113,7 +115,9 @@ class BehaviorRegistry extends ObjectRegistry {
if ($enable) {
$this->_eventManager->attach($instance);
}
- $this->_mapMethods($instance, $class, $alias);
+ $methods = $this->_mapMethods($instance, $class, $alias);
+ $this->_methodMap = $methods['methods'];
+ $this->_finderMap = $methods['finders'];
return $instance;
}
@@ -129,10 +133,16 @@ class BehaviorRegistry extends ObjectRegistry {
* @throws Cake\Error\Exception when duplicate methods are connected.
*/
protected function _mapMethods(Behavior $instance, $class, $alias) {
+ if (empty(static::$_cache)) {
+ static::$_cache = Cache::read('behavior_method_map', 'fast');
+ }
+ if (isset(static::$_cache[$class])) {
+ return static::$_cache[$class];
+ }
$events = $instance->implementedEvents();
-
$reflection = new \ReflectionClass($instance);
- $eventMethods = [];
+
+ $eventMethods = $methodMap = $finderMap = [];
foreach ($events as $e => $binding) {
if (is_array($binding) && isset($binding['callable']) && isset($binding['callable'])) {
$binding = $binding['callable'];
@@ -151,24 +161,28 @@ class BehaviorRegistry extends ObjectRegistry {
}
$methodName = strtolower($methodName);
- if (isset($this->_finderMap[$methodName]) || isset($this->_methodMap[$methodName])) {
+ if (isset($finderMap[$methodName]) || isset($methodMap[$methodName])) {
$error = __d(
'cake_dev',
'%s contains duplicate method "%s" which is already provided by %s',
$class,
$method->getName(),
- $this->_methodMap[$methodName]
+ $methodMap[$methodName]
);
throw new Error\Exception($error);
}
$isFinder = substr($methodName, 0, 4) === 'find';
if ($isFinder) {
- $this->_finderMap[$methodName] = $alias;
+ $finderMap[$methodName] = $alias;
} else {
- $this->_methodMap[$methodName] = $alias;
+ $methodMap[$methodName] = $alias;
}
}
+ $map = ['methods' => $methodMap, 'finders' => $finderMap];
+ static::$_cache[$class] = $map;
+ Cache::write('behavior_method_map', static::$_cache, 'fast');
+ return $map;
}
/**
diff --git a/Cake/ORM/BehaviorRegistry.php b/Cake/ORM/BehaviorRegistry.php
index db1aa77..752386b 100644
--- a/Cake/ORM/BehaviorRegistry.php
+++ b/Cake/ORM/BehaviorRegistry.php
@@ -58,6 +58,7 @@ class BehaviorRegistry extends ObjectRegistry {
*/
protected $_finderMap = [];
+ protected static $_cache = [];
/**
* Constructor
*
@@ -113,7 +114,9 @@ class BehaviorRegistry extends ObjectRegistry {
if ($enable) {
$this->_eventManager->attach($instance);
}
- $this->_mapMethods($instance, $class, $alias);
+ $methods = $this->_mapMethods($instance, $class, $alias);
+ $this->_methodMap = $methods['methods'];
+ $this->_finderMap = $methods['finders'];
return $instance;
}
@@ -129,10 +132,13 @@ class BehaviorRegistry extends ObjectRegistry {
* @throws Cake\Error\Exception when duplicate methods are connected.
*/
protected function _mapMethods(Behavior $instance, $class, $alias) {
+ if (isset(static::$_cache[$class])) {
+ return static::$_cache[$class];
+ }
$events = $instance->implementedEvents();
-
$reflection = new \ReflectionClass($instance);
- $eventMethods = [];
+
+ $eventMethods = $methodMap = $finderMap = [];
foreach ($events as $e => $binding) {
if (is_array($binding) && isset($binding['callable']) && isset($binding['callable'])) {
$binding = $binding['callable'];
@@ -151,24 +157,27 @@ class BehaviorRegistry extends ObjectRegistry {
}
$methodName = strtolower($methodName);
- if (isset($this->_finderMap[$methodName]) || isset($this->_methodMap[$methodName])) {
+ if (isset($finderMap[$methodName]) || isset($methodMap[$methodName])) {
$error = __d(
'cake_dev',
'%s contains duplicate method "%s" which is already provided by %s',
$class,
$method->getName(),
- $this->_methodMap[$methodName]
+ $methodMap[$methodName]
);
throw new Error\Exception($error);
}
$isFinder = substr($methodName, 0, 4) === 'find';
if ($isFinder) {
- $this->_finderMap[$methodName] = $alias;
+ $finderMap[$methodName] = $alias;
} else {
- $this->_methodMap[$methodName] = $alias;
+ $methodMap[$methodName] = $alias;
}
}
+ $map = ['methods' => $methodMap, 'finders' => $finderMap];
+ static::$_cache[$class] = $map;
+ return $map;
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment