$ show Ocramius/ProxyManager
ProxyManager is a PHP library for generating proxy classes at runtime. It provides implementations for several proxy patterns: lazy loading (ghost and value holder), access interceptors (with value holder or scope localizer), null objects, and remote objects. The library uses Laminas Code for code generation and reflection to dynamically create proxy classes that wrap or substitute real objects. It is designed as a standalone Composer package with a configuration-driven architecture: a central Configuration object manages autoloading, generator strategies (evaluating or file-writing), class name inflectors, and signature verification. Factories (e.g., LazyLoadingValueHolderFactory, NullObjectFactory) serve as the primary entry points for users, each producing proxies for a given class or interface. The generated proxies include methods for initialization control, interceptor callbacks, and property access simulation. The library is extensively tested with unit tests, functional tests, and mutation testing, and includes benchmarks for performance. External dependencies are limited to Laminas Code and Composer's installed versions API.
The LLM identified 5 files worth highlighting for reuse:
Generates deterministic unique identifiers (e.g., for property names) by appending a hash suffix based on installed package versions. This ensures that generated proxy code remains unique across different dependency versions, preventing collisions. The pattern is reusable for any code generation system that needs stable, version-dependent identifiers.
Generates PHP code that simulates property access (read, write, isset, unset) from the correct scope using debug_backtrace. This is used in magic methods of generated proxies to correctly handle public, protected, and private property access. The approach is non-trivial and reusable for any runtime proxy or AOP system that needs to respect PHP's visibility rules.
A data transfer object that extracts and categorizes properties from a class reflection: public, protected, private, typed, nullable, referenceable. It provides methods to filter properties by visibility and type, and is used by multiple proxy generators to decide which properties to initialize or skip. This utility is self-contained and can be reused in any PHP code generation or reflection-based tool.
Generates code to unset all instance properties of an object. This is used in static proxy constructors and __wakeup methods to reset the object state. The generated code handles typed properties correctly (using unset on non-initialized typed properties). This pattern is reusable for any code generation that needs to clear object state without relying on default values.
Generates the method body for intercepted methods, wrapping the original method call with prefix and suffix interceptor callbacks. It handles return values, void methods, and by-reference parameters. This is a core building block for access interceptor proxies and can be extracted for any AOP or middleware system that needs to inject behavior around method calls.