codepoet/opcache-profiler-bundle
Install via Composer:
composer require codepoet/opcache-profiler-bundle:^1.0
(Note: For Symfony 2.0.x, use the gist workaround.)
Enable the Bundle:
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
new Codepoet\OpcacheProfilerBundle\CodepoetOpcacheProfilerBundle(),
First Use Case:
Access any Symfony profiler page (e.g., /_profiler/). The Opcache tab will appear in the toolbar, showing:
Debugging Performance Bottlenecks:
Monitoring During Deployment:
Environment-Specific Config:
# config/packages/dev/codepoet_opcache_profiler.yaml
codepoet_opcache_profiler:
data_collector:
show_filelist: true # Only enable in dev/staging
cache_stats: true # Show detailed cache stats
Integration with CI/CD:
Custom Metrics:
Extend the OpcacheDataCollector to log cache events to a monitoring system (e.g., Datadog):
// src/EventListener/OpcacheListener.php
use Codepoet\OpcacheProfilerBundle\DataCollector\OpcacheDataCollector;
public function onKernelRequest(GetResponseEvent $event) {
$opcache = $event->getKernel()->getContainer()
->get('codepoet_opcache_profiler.data_collector');
if ($opcache->getMissCount() > 10) {
// Trigger alert
}
}
Dynamic Filelist Filtering:
Override the getFileList() method to exclude vendor/ or cache/ directories:
// config/services.yaml
Codepoet\OpcacheProfilerBundle\DataCollector\OpcacheDataCollector:
arguments:
$showFilelist: true
$filelistFilter: '~^(?!.*(vendor|cache))~'
Memory Overhead:
show_filelist: true can consume hundreds of MB for large apps. Enable only in dev/staging.opcache.get_status() directly in CLI scripts for lightweight checks.Symfony 4+ Compatibility:
config/bundles.php is used instead of AppKernel.Opcache Disabled:
php -i | grep opcache.enable
php.ini (opcache.enable=1) and restart PHP-FPM.Filelist Inconsistencies:
opcache_reset()).opcache_reset() in CLI to sync.CLI Debugging:
Use opcache_get_status() in a script to bypass the toolbar:
$status = opcache_get_status();
var_dump($status['opcache_statistics']);
Log Cache Events:
Add this to config/packages/monolog.yaml to log Opcache misses:
handlers:
opcache:
type: stream
path: "%kernel.logs_dir%/opcache.log"
level: debug
channels: ["opcache"]
Then log misses in a subscriber:
public function onKernelRequest(RequestEvent $event) {
$misses = opcache_get_status()['opcache']['miss_count'];
if ($misses > 0) {
$this->logger->debug('Opcache misses', ['misses' => $misses]);
}
}
Custom Data Collection:
Extend OpcacheDataCollector to add metrics like:
Toolbar Integration:
Override the Twig template (templates/CodepoetOpcacheProfilerBundle:Toolbar:opcache.html.twig) to:
opcache_reset()).Asynchronous Monitoring: Use a cron job to log Opcache stats to a database:
*/5 * * * * php /path/to/script.php opcache:stats >> /var/log/opcache.log
Script example:
// script/opcache:stats.php
$stats = opcache_get_status();
file_put_contents(
'/var/log/opcache.json',
json_encode($stats['opcache_statistics'])
);
How can I help you explore Laravel packages today?