0 follower

Class Yiisoft\Proxy\ObjectProxy

InheritanceYiisoft\Proxy\ObjectProxy
Uses TraitsYiisoft\Proxy\ProxyTrait

Base proxy class for objects to use in {@see ProxyManager}. A concrete implementation can be provided too.

Public Methods

Hide inherited 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

Hide inherited 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

Hide inherited methods

__construct() public method

public mixed __construct ( object $instance )
$instance object

                public function __construct(
    /**
     * @var object An instance of the class for proxying method calls.
     */
    private object $instance
) {
}

            
afterCall() protected method

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: 1656657586.4849.

return mixed

Return value of a called method.

                protected function afterCall(
    string $methodName,
    array $arguments,
    mixed $result,
    float $timeStart
): mixed {
    return $result;
}

            
call() protected method

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);
}

            
getCurrentError() public method

Defined in: Yiisoft\Proxy\ProxyTrait::getCurrentError()

Gets current error.

public Throwable|null getCurrentError ( )

                public function getCurrentError(): ?Throwable
{
    return $this->currentError;
}

            
getInstance() public method

Gets instance.

public object getInstance ( )

                public function getInstance(): object
{
    return $this->instance;
}

            
getNewStaticInstance() protected method

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);
}

            
hasCurrentError() public method

Defined in: Yiisoft\Proxy\ProxyTrait::hasCurrentError()

Whether a proxy has current error.

public boolean hasCurrentError ( )
return boolean

true if it has current error and false otherwise.

                public function hasCurrentError(): bool
{
    return $this->currentError !== null;
}

            
repeatError() protected method

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;
}

            
resetCurrentError() protected method

Defined in: Yiisoft\Proxy\ProxyTrait::resetCurrentError()

Resets current error.

protected void resetCurrentError ( )

                protected function resetCurrentError(): void
{
    $this->currentError = null;
}