2012-05-09 14:26:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class that proxies another, allowing various functionality to be
|
2013-05-20 12:18:07 +02:00
|
|
|
* injected.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-06-20 23:59:16 +02:00
|
|
|
* @package framework
|
2012-05-09 14:26:29 +02:00
|
|
|
* @subpackage injector
|
|
|
|
*/
|
|
|
|
class AopProxyService {
|
|
|
|
public $beforeCall = array();
|
|
|
|
|
|
|
|
public $afterCall = array();
|
|
|
|
|
|
|
|
public $proxied;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-12-05 02:21:31 +01:00
|
|
|
/**
|
|
|
|
* Because we don't know exactly how the proxied class is usually called,
|
|
|
|
* provide a default constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-12-05 02:21:31 +01:00
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public function __call($method, $args) {
|
|
|
|
if (method_exists($this->proxied, $method)) {
|
|
|
|
$continue = true;
|
2013-12-05 02:21:31 +01:00
|
|
|
$result = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
if (isset($this->beforeCall[$method])) {
|
2013-12-05 02:21:31 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($continue) {
|
|
|
|
$result = call_user_func_array(array($this->proxied, $method), $args);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
if (isset($this->afterCall[$method])) {
|
2013-12-05 02:21:31 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-05 02:21:31 +01:00
|
|
|
|
|
|
|
return $result;
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|