- How does Codememory/Reflection improve Laravel application performance?
- This package caches reflection metadata (classes, methods, properties, attributes) to avoid repeated expensive PHP Reflection API calls. In Laravel, this is especially useful for ORM operations, attribute scanning (e.g., middleware, validation), or dynamic proxies, where reflection is a hot path. Benchmarks show 50–300% speedups in high-traffic production environments.
- Can I use Codememory/Reflection in Laravel 9 or older versions?
- No, this package requires PHP 8.3+ and Symfony Cache v7.1+, which aligns with Laravel 10+. For older Laravel versions, you’d need to fork the package or use a polyfill for ReflectionAttribute, but this isn’t officially supported. Laravel 10+ users get seamless integration with Symfony’s cache adapters already bundled in the framework.
- How do I configure Codememory/Reflection in Laravel?
- Register the `ReflectorManager` in your `AppServiceProvider` using Laravel’s container. Example: `$this->app->singleton(ReflectorManager::class, fn() => new ReflectorManager(Cache::store('file')->getAdapter(), app()->environment('local')));`. The `isDev` flag defaults to `true` in local environments, disabling caching for development. Use `Cache::tags(['reflection'])` for easy invalidation.
- What cache adapters does Codememory/Reflection support in Laravel?
- It works with any Symfony Cache adapter, including Laravel’s built-in `FileCache`, `RedisCache`, or `DatabaseCache`. For production, Redis or APCu (if available) are recommended for lower I/O latency. The package auto-detects Laravel’s cache configuration, so no extra setup is needed beyond `composer require codememory/reflection`.
- How do I handle cache invalidation when classes change in Laravel?
- Use tagged caching with `Cache::tags(['reflection'])` and invalidate during deployments by calling `Cache::tags(['reflection'])->clear()`. For dynamic class loading (e.g., plugins), listen to Laravel’s `updated` events or integrate with the Optimizer. The package also supports manual invalidation via `ReflectorManager::clearCache()`.
- Does Codememory/Reflection work with Laravel’s attributes (e.g., #[Middleware])?
- Yes, it fully supports PHP 8.3+ attributes and Laravel’s attribute system (e.g., `#[Cacheable]`, `#[Middleware]`). The cached metadata includes attribute arguments and targets, reducing redundant reflection calls during request processing. This is particularly useful for frameworks like Livewire or Forge where attributes are heavily used.
- What’s the memory overhead of caching reflection data in Laravel?
- Caching reflection metadata increases memory usage, but the impact is minimal for most applications. Monitor with `Cache::getAdapter()->getStats()` or Laravel’s memory debug bar. For large codebases, use Redis or APCu to offload cache storage. The tradeoff is justified by the performance gains, especially in high-concurrency environments like queues or APIs.
- Can I fall back to native Reflection if caching fails?
- Yes, the package includes a fallback mechanism. If the cache adapter fails (e.g., disk full, permission issues), it gracefully degrades to PHP’s native Reflection API. Configure this via `ReflectorManager`’s constructor or use Laravel’s `app()->makeWith()` to wrap reflection calls with a fallback strategy.
- How do I test Codememory/Reflection in Laravel’s CI/CD pipeline?
- Mock the `ReflectorManager` in unit tests using `ReflectorManager::shouldReturnMock()` to bypass caching. For integration tests, ensure cache invalidation is tested by triggering `updated` events or manually clearing the cache. Use Laravel’s `Artisan::call('cache:clear')` in test setups to simulate production behavior.
- Are there alternatives to Codememory/Reflection for Laravel?
- For Laravel, alternatives include APCu (if enabled) for native reflection caching or custom solutions like `roave/better-reflection` (though it lacks Laravel-specific optimizations). Codememory/Reflection stands out by leveraging Symfony Cache (already in Laravel) and offering seamless integration with Laravel’s DI container, attributes, and cache tags. It’s the most Laravel-native option for reflection performance.