diff --git a/Cake/ORM/BehaviorRegistry.php b/Cake/ORM/BehaviorRegistry.php index 1cb4e3bafb2..7fc32eca902 100644 --- a/Cake/ORM/BehaviorRegistry.php +++ b/Cake/ORM/BehaviorRegistry.php @@ -141,50 +141,43 @@ protected function _create($class, $alias, $settings) { * @throws Cake\Error\Exception when duplicate methods are connected. */ protected function _getMethods(Behavior $instance, $class, $alias) { - if (isset(static::$_methodCache[$class])) { - return static::$_methodCache[$class]; - } - - $methodMap = $finderMap = []; $finders = $instance->implementedFinders(); $methods = $instance->implementedMethods(); - foreach($finders as $methodName) { - $methodName = strtolower($methodName); - - if (isset($this->_finderMap[$methodName])) { - $duplicate = $this->_finderMap[$methodName]; + foreach($finders as $finder => &$methodName) { + $finder = strtolower($finder); + if (isset($this->_finderMap[$finder])) { + $duplicate = $this->_finderMap[$finder]; $error = __d( 'cake_dev', - '%s contains duplicate method "%s" which is already provided by "%s"', + '%s contains duplicate finder "%s" which is already provided by "%s"', $class, - $methodName, - $duplicate + $finder, + $duplicate[0] ); throw new Error\Exception($error); } - $finderMap[$methodName] = $alias; + $methodName = [$alias, $methodName]; } - foreach($methods as $methodName) { - $methodName = strtolower($methodName); + foreach($methods as $method => &$methodName) { + $method = strtolower($method); - if (isset($this->_methodMap[$methodName])) { - $duplicate = $this->_methodMap[$methodName]; + if (isset($this->_methodMap[$method])) { + $duplicate = $this->_methodMap[$method]; $error = __d( 'cake_dev', '%s contains duplicate method "%s" which is already provided by "%s"', $class, - $methodName, - $duplicate + $method, + $duplicate[0] ); throw new Error\Exception($error); } - $methodMap[$methodName] = $alias; + $methodName = [$alias, $methodName]; } - static::$_methodCache[$class] = ['methods' => $methodMap, 'finders' => $finderMap]; - return static::$_methodCache[$class]; + return compact('methods', 'finders'); } /** @@ -226,13 +219,13 @@ public function hasFinder($method) { public function call($method, array $args = []) { $method = strtolower($method); if ($this->hasMethod($method)) { - $alias = $this->_methodMap[$method]; - return call_user_func_array([$this->_loaded[$alias], $method], $args); + list($behavior, $callMethod) = $this->_methodMap[$method]; + return call_user_func_array([$this->_loaded[$behavior], $callMethod], $args); } if ($this->hasFinder($method)) { - $alias = $this->_finderMap[$method]; - return call_user_func_array([$this->_loaded[$alias], $method], $args); + list($behavior, $callMethod) = $this->_finderMap[$method]; + return call_user_func_array([$this->_loaded[$behavior], $callMethod], $args); } throw new Error\Exception(__d('cake_dev', 'Cannot call "%s" it does not belong to any attached behaviors.', $method)); diff --git a/Cake/ORM/Table.php b/Cake/ORM/Table.php index f9e1c7a4049..7519a0f7e69 100644 --- a/Cake/ORM/Table.php +++ b/Cake/ORM/Table.php @@ -1026,12 +1026,12 @@ public function callFinder($type, Query $query, $options = []) { return $this->{$finder}($query, $options); } - if ($this->_behaviors && $this->_behaviors->hasFinder($finder)) { - return $this->_behaviors->call($finder, [$query, $options]); + if ($this->_behaviors && $this->_behaviors->hasFinder($type)) { + return $this->_behaviors->callFinder($type, [$query, $options]); } throw new \BadMethodCallException( - __d('cake_dev', 'Unknown finder method "%s"', $finder) + __d('cake_dev', 'Unknown finder method "%s"', $type) ); } diff --git a/Cake/Test/TestCase/ORM/BehaviorRegistryTest.php b/Cake/Test/TestCase/ORM/BehaviorRegistryTest.php index 5ff87ead3ae..838d7f2376d 100644 --- a/Cake/Test/TestCase/ORM/BehaviorRegistryTest.php +++ b/Cake/Test/TestCase/ORM/BehaviorRegistryTest.php @@ -150,7 +150,7 @@ public function testHasMethod() { $this->assertFalse($this->Behaviors->hasMethod('nope')); $this->assertFalse($this->Behaviors->hasMethod('beforeFind')); - $this->assertFalse($this->Behaviors->hasMethod('findNoSlug')); + $this->assertFalse($this->Behaviors->hasMethod('noSlug')); } /** @@ -161,9 +161,9 @@ public function testHasMethod() { public function testHasFinder() { $this->Behaviors->load('Sluggable'); - $this->assertTrue($this->Behaviors->hasFinder('findNoSlug')); - $this->assertTrue($this->Behaviors->hasFinder('findnoslug')); - $this->assertTrue($this->Behaviors->hasFinder('FINDNOSLUG')); + $this->assertTrue($this->Behaviors->hasFinder('noSlug')); + $this->assertTrue($this->Behaviors->hasFinder('noslug')); + $this->assertTrue($this->Behaviors->hasFinder('NOSLUG')); $this->assertFalse($this->Behaviors->hasFinder('slugify')); $this->assertFalse($this->Behaviors->hasFinder('beforeFind')); @@ -181,7 +181,7 @@ public function testCall() { $this->assertEquals('some_value', $result); $query = $this->getMock('Cake\ORM\Query', [], [null, null]); - $result = $this->Behaviors->call('findNoSlug', [$query]); + $result = $this->Behaviors->call('noSlug', [$query]); $this->assertEquals($query, $result); }