ez-php/opcache

OPcache preload script generator for the ez-php framework — scans class paths and writes a ready-to-use preload.php

Maintainers

Package info

github.com/ez-php/opcache

pkg:composer/ez-php/opcache

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.3.0 2026-03-29 22:47 UTC

This package is auto-updated.

Last update: 2026-03-29 23:20:53 UTC


README

OPcache preload script generator for the ez-php framework.

Scans configured class directories and generates a ready-to-use preload.php that can be referenced by PHP's opcache.preload ini directive. Preloading compiles framework classes into shared memory at server startup, eliminating per-request file I/O and compilation overhead.

Installation

composer require ez-php/opcache

Registration

Add the service provider in provider/modules.php:

$app->register(\EzPhp\OPCache\PreloaderServiceProvider::class);

Configuration

Add config/opcache.php:

<?php

return [
    // Absolute path for the generated preload script.
    'output_file' => base_path('preload.php'),

    // Directories to scan for PHP files.
    'paths' => [
        base_path('vendor/ez-php/framework/src'),
        base_path('vendor/ez-php/contracts/src'),
        base_path('app'),
    ],

    // Filename glob patterns to exclude.
    'exclude' => [
        '*Test.php',
        '*TestCase.php',
        '*Interface.php',
    ],

    // Set to true to use require_once instead of opcache_compile_file.
    // Useful when classes have cross-file dependencies that must be resolved
    // in the correct order. Defaults to false.
    'require_once' => false,
];

Generating the preload script

Use the Preloader directly — for example from a Composer script or a deploy step:

use EzPhp\OPCache\PreloadConfig;
use EzPhp\OPCache\Preloader;

$config = new PreloadConfig(
    outputFile: '/var/www/html/preload.php',
    paths: [
        '/var/www/html/vendor/ez-php/framework/src',
        '/var/www/html/vendor/ez-php/contracts/src',
    ],
    excludePatterns: ['*Test.php', '*TestCase.php'],
);

$count = (new Preloader($config))->generate();

echo "Generated preload script with {$count} files.\n";

Or resolve it from the container after bootstrapping the application:

$preloader = $app->make(\EzPhp\OPCache\Preloader::class);
$count = $preloader->generate();

Enabling preloading in php.ini

After generating the script, point PHP to it:

opcache.preload=/var/www/html/preload.php
opcache.preload_user=www-data

Restart PHP-FPM or the web server after each change to the preload script.

Generated script format

<?php

// Auto-generated OPcache preload script.
// Generated by ez-php/opcache at 2026-03-29 12:00:00
// Total files: 42
// Usage: set opcache.preload=<this-file> in php.ini

if (function_exists('opcache_compile_file')) {
    opcache_compile_file('/var/www/html/vendor/ez-php/framework/src/Application.php');
    opcache_compile_file('/var/www/html/vendor/ez-php/framework/src/Container.php');
    // ...
}

Testing

No external services required — all tests run in-process using temporary directories.

composer test

License

MIT