BUG Fix extend() failing on protected extend-prefixed methods

This commit is contained in:
Damian Mooyman 2018-02-20 13:48:20 +13:00 committed by Guy Sartorelli
parent 76fc8f1596
commit f2211d690f
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
2 changed files with 31 additions and 14 deletions

View File

@ -419,7 +419,7 @@ trait Extensible
* The extension methods are defined during {@link __construct()} in {@link defineMethods()}.
*
* @param string $method the name of the method to call on each extension
* @param mixed ...$arguments
* @param mixed &...$arguments
* @return array
*/
public function extend($method, &...$arguments)
@ -438,19 +438,9 @@ trait Extensible
foreach ($this->getExtensionInstances() as $instance) {
// Prefer `extend` prefixed methods
$instanceMethod = method_exists($instance, "extend{$method}")
? "extend{$method}"
: (method_exists($instance, $method) ? $method : null);
if ($instanceMethod) {
try {
$instance->setOwner($this);
$value = $instance->$instanceMethod(...$arguments);
} finally {
$instance->clearOwner();
}
if ($value !== null) {
$values[] = $value;
}
$value = $instance->invokeExtension($this, $method, ...$arguments);
if ($value !== null) {
$values[] = $value;
}
}

View File

@ -115,4 +115,31 @@ abstract class Extension
// Split out both args and service name
return strtok(strtok($extensionStr ?? '', '(') ?? '', '.');
}
/**
* Invoke extension point. This will prefer explicit `extend` prefixed
* methods.
*
* @param object $owner
* @param string $method
* @param array &...$arguments
* @return mixed
*/
public function invokeExtension($owner, $method, &...$arguments)
{
// Prefer `extend` prefixed methods
$instanceMethod = method_exists($this, "extend{$method}")
? "extend{$method}"
: (method_exists($this, $method) ? $method : null);
if (!$instanceMethod) {
return null;
}
try {
$this->setOwner($owner);
return $this->$instanceMethod(...$arguments);
} finally {
$this->clearOwner();
}
}
}