Skip to content

Commit

Permalink
allow behavior method aliasing
Browse files Browse the repository at this point in the history
and change the way finders are stored to remove the find prefix
  • Loading branch information
AD7six committed Nov 10, 2013
1 parent e0ce895 commit c75915e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
47 changes: 20 additions & 27 deletions Cake/ORM/BehaviorRegistry.php
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions Cake/ORM/Table.php
Expand Up @@ -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)
);
}

Expand Down
10 changes: 5 additions & 5 deletions Cake/Test/TestCase/ORM/BehaviorRegistryTest.php
Expand Up @@ -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'));
}

/**
Expand All @@ -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'));
Expand All @@ -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);
}

Expand Down

0 comments on commit c75915e

Please sign in to comment.