Skip to content
Permalink
Browse files

allow behavior method aliasing

and change the way finders are stored to remove the find prefix
  • Loading branch information...
AD7six committed Nov 4, 2013
1 parent e0ce895 commit c75915eeb5dc9356c6b92564fbb04b03817f986e
Showing with 28 additions and 35 deletions.
  1. +20 −27 Cake/ORM/BehaviorRegistry.php
  2. +3 −3 Cake/ORM/Table.php
  3. +5 −5 Cake/Test/TestCase/ORM/BehaviorRegistryTest.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));
@@ -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)
);
}
@@ -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);
}

0 comments on commit c75915e

Please sign in to comment.
You can’t perform that action at this time.