ocramius/proxy-manager
ProxyManager generates and manages PHP proxy classes (virtual proxies, lazy-loading value holders, etc.) to implement the Proxy Pattern. Useful for lazy-loading, interceptors, and advanced DI/ORM scenarios. Install via Composer and use factory helpers to create proxies.
An access interceptor value holder is a smart reference proxy that allows you to dynamically define the logic that will be executed before or after any of the wrapped object's methods logic.
It wraps around a real instance of the object to be proxied and can be useful for things like:
Here's an example of how you can create and use an access interceptor value holder:
<?php
use ProxyManager\Factory\AccessInterceptorValueHolderFactory as Factory;
require_once __DIR__ . '/vendor/autoload.php';
class Foo
{
public function doFoo()
{
echo "Foo!\n";
}
}
$factory = new Factory();
$proxy = $factory->createProxy(
new Foo(),
['doFoo' => function () { echo "PreFoo!\n"; }],
['doFoo' => function () { echo "PostFoo!\n"; }]
);
$proxy->doFoo();
This sends something like following to your output:
PreFoo!
Foo!
PostFoo!
A proxy produced by the
ProxyManager\Factory\AccessInterceptorValueHolderFactory
implements the ProxyManager\Proxy\AccessInterceptorValueHolderInterface.
Therefore, you can set an access interceptor callback by calling:
$proxy->setMethodPrefixInterceptor('methodName', function () { echo 'pre'; });
$proxy->setMethodSuffixInterceptor('methodName', function () { echo 'post'; });
You can also listen to public properties access by attaching interceptors to __get, __set, __isset and __unset.
A prefix interceptor (executed before method logic) should have the following signature:
/**
* [@var](https://github.com/var) object $proxy the proxy that intercepted the method call
* [@var](https://github.com/var) object $instance the wrapped instance within the proxy
* [@var](https://github.com/var) string $method name of the called method
* [@var](https://github.com/var) array $params sorted array of parameters passed to the intercepted
* method, indexed by parameter name
* [@var](https://github.com/var) bool $returnEarly flag to tell the interceptor proxy to return early, returning
* the interceptor's return value instead of executing the method logic
*
* [@return](https://github.com/return) mixed
*/
$prefixInterceptor = function ($proxy, $instance, $method, $params, & $returnEarly) {};
A suffix interceptor (executed after method logic) should have the following signature:
/**
* [@var](https://github.com/var) object $proxy the proxy that intercepted the method call
* [@var](https://github.com/var) object $instance the wrapped instance within the proxy
* [@var](https://github.com/var) string $method name of the called method
* [@var](https://github.com/var) array $params sorted array of parameters passed to the intercepted
* method, indexed by parameter name
* [@var](https://github.com/var) mixed $returnValue the return value of the intercepted method
* [@var](https://github.com/var) bool $returnEarly flag to tell the proxy to return early, returning the interceptor's
* return value instead of the value produced by the method
*
* [@return](https://github.com/return) mixed
*/
$suffixInterceptor = function ($proxy, $instance, $method, $params, $returnValue, & $returnEarly) {};
func_get_args(), func_get_arg() and func_num_arg() will not function properly for parameters that are
not part of the proxied object interface: use
variadic arguments instead.How can I help you explore Laravel packages today?