PHP code example of dealroadshow / proximity

1. Go to this page and download the library: Download dealroadshow/proximity library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dealroadshow / proximity example snippets




$factory = new \Dealroadshow\Proximity\ProxyFactory(
    new \Dealroadshow\Proximity\ProxyGenerator(),
    '/path/where/proxy/classes/will/be/stored'
);

$myObject = new Foo();
$proxy = $factory->proxy($myObject);

use Dealroadshow\Proximity\ProxyFactory;
use Dealroadshow\Proximity\ProxyGenerator;
use Dealroadshow\Proximity\MethodsInterception\BodyInterceptorInterface;
use Dealroadshow\Proximity\ProxyInterface;
use Dealroadshow\Proximity\MethodsInterception\BodyInterceptionResult;
use Dealroadshow\Proximity\ProxyOptions;
use Dealroadshow\Proximity\MethodsInterception\ResultInterceptorInterface;
use Dealroadshow\Proximity\MethodsInterception\InterceptionContext;

class Foo
{
    public function bar(): void
    {
        echo 'Bar!', PHP_EOL;
    }
}

$factory = new ProxyFactory(
    new ProxyGenerator(),
    '/path/where/proxy/classes/will/be/stored'
);

$bodyInterceptor = new class implements BodyInterceptorInterface 
{
    public function beforeMethodBody(ProxyInterface $proxy, object $object, string $methodName, array $methodArgs) : BodyInterceptionResult
    {
        echo "Method $methodName() is about to be executed!\n";
        
        return new BodyInterceptionResult(preventMethodBody: false);
    }
};

$resultInterceptor = new class implements ResultInterceptorInterface
{
    public function afterMethodBody(ProxyInterface $proxy, object $object, string $methodName, array $methodArgs, InterceptionContext $context): void
    {
        echo "Method $methodName() just finished execution!\n";
    }
};

$foo = new Foo();
$proxy = $factory->proxy(
    $foo,
    new ProxyOptions(
        ['bar' => [$bodyInterceptor]],
        ['bar' => [$resultInterceptor]],
    )
);

$proxy->bar();

public function afterMethodBody(ProxyInterface $proxy, object $object, string $methodName, array $methodArgs, InterceptionContext $context): void
{
    // do some stuff, then:
    $context->returnValue = 'New value';
}