Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Proxy Manager Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Access Interceptor Value Holder Proxy

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:

  • caching execution of slow and heavy methods
  • log method calls
  • debugging
  • event triggering
  • handling of orthogonal logic, and AOP in general

Example

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!

Implementing pre- and post- access interceptors

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

Known limitations

  • methods using 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.

Tuning performance for production

See Tuning ProxyManager for Production.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport