mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
b273f3b524
- Updated AspectProxyService to handle multiple handlers for each proxied object's methods. - Changed BeforeCallAspect to allow for providing a return value that should be returned to the caller instead of the proxied return value - Changed AfterCallAspect behaviour to allow for returning the value of the aspect to the caller instead of the proxied return value
69 lines
1.5 KiB
PHP
69 lines
1.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* A class that proxies another, allowing various functionality to be
|
|
* injected.
|
|
*
|
|
* @package framework
|
|
* @subpackage injector
|
|
*/
|
|
class AopProxyService {
|
|
public $beforeCall = array();
|
|
|
|
public $afterCall = array();
|
|
|
|
public $proxied;
|
|
|
|
/**
|
|
* Because we don't know exactly how the proxied class is usually called,
|
|
* provide a default constructor
|
|
*/
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
public function __call($method, $args) {
|
|
if (method_exists($this->proxied, $method)) {
|
|
$continue = true;
|
|
$result = null;
|
|
|
|
if (isset($this->beforeCall[$method])) {
|
|
$methods = $this->beforeCall[$method];
|
|
if (!is_array($methods)) {
|
|
$methods = array($methods);
|
|
}
|
|
foreach ($methods as $handler) {
|
|
$alternateReturn = null;
|
|
$proceed = $handler->beforeCall($this->proxied, $method, $args, $alternateReturn);
|
|
if ($proceed === false) {
|
|
$continue = false;
|
|
// if something is set in, use it
|
|
if ($alternateReturn) {
|
|
$result = $alternateReturn;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($continue) {
|
|
$result = call_user_func_array(array($this->proxied, $method), $args);
|
|
|
|
if (isset($this->afterCall[$method])) {
|
|
$methods = $this->afterCall[$method];
|
|
if (!is_array($methods)) {
|
|
$methods = array($methods);
|
|
}
|
|
foreach ($methods as $handler) {
|
|
$return = $handler->afterCall($this->proxied, $method, $args, $result);
|
|
if (!is_null($return)) {
|
|
$result = $return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
}
|