Skip to content

Commit

Permalink
use array_key_change_case and restructure cache
Browse files Browse the repository at this point in the history
by consistently changing key case, when asking a behavior for which
methods it implements, a few strtolower calls can be avoided.

Using static::$_cacheProperty gets confused as it is shared by all
subclasses... so index the cache by class name.
  • Loading branch information
AD7six committed Nov 10, 2013
1 parent 02ab55d commit 9fdc819
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
12 changes: 8 additions & 4 deletions Cake/ORM/Behavior.php
Expand Up @@ -92,7 +92,10 @@
class Behavior implements EventListener {

/**
* A cache of the methods provided by this class
* Method cache for behaviors.
*
* Stores the reflected method + finder methods per class.
* This prevents reflecting the same class multiple times in a single process.
*
* @var array
*/
Expand Down Expand Up @@ -228,8 +231,9 @@ public function implementedMethods() {
* @return array
*/
protected function _reflectionMethods() {
if (isset(static::$_reflectionMethods)) {
return static::$_reflectionMethods;
$class = get_class($this);
if (isset(self::$_reflectionMethods[$class])) {
return self::$_reflectionMethods[$class];
}

$events = $this->implementedEvents();
Expand Down Expand Up @@ -265,7 +269,7 @@ protected function _reflectionMethods() {
}
}

return static::$_reflectionMethods = $return;
return self::$_reflectionMethods[$class] = $return;
}

}
21 changes: 4 additions & 17 deletions Cake/ORM/BehaviorRegistry.php
Expand Up @@ -58,16 +58,6 @@ class BehaviorRegistry extends ObjectRegistry {
*/
protected $_finderMap = [];

/**
* Method cache for behaviors.
*
* Stores the reflected method + finder methods per class.
* This prevents reflecting the same class multiple times in a single process.
*
* @var array
*/
protected static $_methodCache = [];

/**
* Constructor
*
Expand Down Expand Up @@ -124,8 +114,8 @@ protected function _create($class, $alias, $settings) {
$this->_eventManager->attach($instance);
}
$methods = $this->_getMethods($instance, $class, $alias);
$this->_methodMap = array_merge($this->_methodMap, $methods['methods']);
$this->_finderMap = array_merge($this->_finderMap, $methods['finders']);
$this->_methodMap += $methods['methods'];
$this->_finderMap += $methods['finders'];
return $instance;
}

Expand All @@ -141,11 +131,10 @@ protected function _create($class, $alias, $settings) {
* @throws Cake\Error\Exception when duplicate methods are connected.
*/
protected function _getMethods(Behavior $instance, $class, $alias) {
$finders = $instance->implementedFinders();
$methods = $instance->implementedMethods();
$finders = array_change_key_case($instance->implementedFinders());
$methods = array_change_key_case($instance->implementedMethods());

foreach($finders as $finder => &$methodName) {
$finder = strtolower($finder);
if (isset($this->_finderMap[$finder])) {
$duplicate = $this->_finderMap[$finder];
$error = __d(
Expand All @@ -161,8 +150,6 @@ protected function _getMethods(Behavior $instance, $class, $alias) {
}

foreach($methods as $method => &$methodName) {
$method = strtolower($method);

if (isset($this->_methodMap[$method])) {
$duplicate = $this->_methodMap[$method];
$error = __d(
Expand Down

0 comments on commit 9fdc819

Please sign in to comment.