codememory/reflection
Cacheable alternative to PHP’s Reflection API. Uses a Symfony Cache adapter to store class metadata (names, methods, properties, types, attributes) for faster repeat reflection in production, with a dev mode toggle via ReflectorManager.
Installation:
composer require codememory/reflection
Requires PHP 8.3+ and Symfony Cache component (^7.1).
Basic Initialization:
use Codememory\Reflection\ReflectorManager;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new FilesystemAdapter('codememory', 'cache');
$reflectorManager = new ReflectorManager($cache, false); // Disable dev mode for production
First Use Case: Fetch a class reflector and inspect properties/methods:
$classReflector = $reflectorManager->getReflector(MyClass::class);
$properties = $classReflector->getProperties(); // Cached result
Reflector Caching:
new ReflectionClass() with $reflectorManager->getReflector(ClassName::class).First).Attribute Introspection:
foreach ($classReflector->getProperties() as $property) {
foreach ($property->getAttributes() as $attribute) {
$instance = $attribute->getInstance(); // Cached attribute instance
}
}
Environment-Aware Caching:
true as the second argument to ReflectorManager in development to disable caching (forces live reflection).Integration with Laravel:
AppServiceProvider:
$this->app->singleton(ReflectorManager::class, fn() =>
new ReflectorManager(app(Cache::class)->getAdapter('codememory'), app()->isLocal())
);
public function __construct(private ReflectorManager $reflectorManager) {}
Bulk Reflection:
$reflectors = $reflectorManager->getReflectors([ClassA::class, ClassB::class]);
Cache Invalidation:
$cache->clear();
isDev mode during development to bypass cache.Attribute Resolution:
Performance Tradeoffs:
Symfony Cache Dependency:
FilesystemAdapter).Cache Inspection:
$cache->getItem('First')->get(); // Dump cached data
Dev Mode:
isDev mode to debug live reflection:
$reflectorManager = new ReflectorManager($cache, true);
Fallback to Native Reflection:
ReflectorManager to delegate to ReflectionClass when needed.Custom Cache Adapter:
Psr\Cache\CacheItemPoolInterface for database/Redis caching.Reflector Decorators:
ReflectorManager to add pre/post-processing:
class DecoratedReflectorManager {
public function __construct(private ReflectorManager $manager) {}
public function getReflector(string $class) {
$reflector = $this->manager->getReflector($class);
// Add custom logic (e.g., logging)
return $reflector;
}
}
Attribute Handlers:
Codememory\Reflection\Reflectors\ClassReflector.How can I help you explore Laravel packages today?