Class Yiisoft\Proxy\ObjectProxy
| Inheritance | Yiisoft\Proxy\ObjectProxy |
|---|---|
| Uses Traits | Yiisoft\Proxy\ProxyTrait |
Base proxy class for objects to use in {@see ProxyManager}. A concrete implementation can be provided too.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Proxy\ObjectProxy | |
| getCurrentError() | Gets current error. | Yiisoft\Proxy\ProxyTrait |
| getInstance() | Gets instance. | Yiisoft\Proxy\ObjectProxy |
| hasCurrentError() | Whether a proxy has current error. | Yiisoft\Proxy\ProxyTrait |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| afterCall() | An event executed after each call of a method. Can be used for handling errors, logging, etc. $result must be
always returned. |
Yiisoft\Proxy\ObjectProxy |
| call() | Calls a method in the {@see $instance} additionally allowing to process result afterwards (even in case of error). | Yiisoft\Proxy\ObjectProxy |
| getNewStaticInstance() | Gets new instance of {@see $instance} class. | Yiisoft\Proxy\ObjectProxy |
| repeatError() | Throws current error again. | Yiisoft\Proxy\ProxyTrait |
| resetCurrentError() | Resets current error. | Yiisoft\Proxy\ProxyTrait |
Method Details
| public mixed __construct ( object $instance ) | ||
| $instance | object | |
public function __construct(
/**
* @var object An instance of the class for proxying method calls.
*/
private object $instance
) {
}
An event executed after each call of a method. Can be used for handling errors, logging, etc. $result must be
always returned.
| protected mixed afterCall ( string $methodName, array $arguments, mixed $result, float $timeStart ) | ||
| $methodName | string |
A called method in the {@see $instance}. |
| $arguments | array |
A list of arguments passed to a called method. The order must be maintained. |
| $result | mixed |
Return value of a called method. |
| $timeStart | float |
UNIX timestamp right before proxy method call. For example: |
| return | mixed |
Return value of a called method. |
|---|---|---|
protected function afterCall(
string $methodName,
array $arguments,
mixed $result,
float $timeStart
): mixed {
return $result;
}
Calls a method in the {@see $instance} additionally allowing to process result afterwards (even in case of error).
| protected $this|mixed call ( string $methodName, array $arguments ) | ||
| $methodName | string |
A called method in the {@see $instance}. |
| $arguments | array |
A list of arguments passed to a called method. The order must be maintained. |
| return | $this|mixed |
Either a new instance of {@see $instance} class or return value of a called method. |
|---|---|---|
| throws | Throwable |
In case of error happen during the method call. |
protected function call(string $methodName, array $arguments): mixed
{
$this->resetCurrentError();
$result = null;
$timeStart = microtime(true);
try {
/** @var mixed $result */
$result = $this->callInternal($methodName, $arguments);
} catch (Throwable $e) {
$this->repeatError($e);
} finally {
/** @var mixed $result */
$result = $this->afterCall($methodName, $arguments, $result, $timeStart);
}
return $this->processResult($result);
}
Defined in: Yiisoft\Proxy\ProxyTrait::getCurrentError()
Gets current error.
| public Throwable|null getCurrentError ( ) |
public function getCurrentError(): ?Throwable
{
return $this->currentError;
}
Gets instance.
| public object getInstance ( ) |
public function getInstance(): object
{
return $this->instance;
}
Gets new instance of {@see $instance} class.
| protected $this getNewStaticInstance ( object $instance ) | ||
| $instance | object |
{@see $instance}. |
| return | $this |
A new instance of the same class |
|---|---|---|
protected function getNewStaticInstance(object $instance): self
{
/**
* @psalm-suppress UnsafeInstantiation Constructor should be consistent to `getNewStaticInstance()`.
*/
return new static($instance);
}
Defined in: Yiisoft\Proxy\ProxyTrait::hasCurrentError()
Whether a proxy has current error.
| public boolean hasCurrentError ( ) | ||
| return | boolean |
|
|---|---|---|
public function hasCurrentError(): bool
{
return $this->currentError !== null;
}
Defined in: Yiisoft\Proxy\ProxyTrait::repeatError()
Throws current error again.
| protected void repeatError ( Throwable $error ) | ||
| $error | Throwable |
A throwable object. |
| throws | Throwable |
An exact error previously stored in {@see $currentError}. |
|---|---|---|
protected function repeatError(Throwable $error): void
{
$this->currentError = $error;
throw $error;
}
Defined in: Yiisoft\Proxy\ProxyTrait::resetCurrentError()
Resets current error.
| protected void resetCurrentError ( ) |
protected function resetCurrentError(): void
{
$this->currentError = null;
}
Signup or Login in order to comment.