Files
Virtnet.bond/preload.php

296 lines
9.4 KiB
PHP

<?php
/**
* OPcache Preload Script for PHP 8.3-FPM
* Optimized for Paymenter/VirtNet - Memory-efficient version
*
* Configure in php.ini:
* opcache.preload=/var/www/paymenter/preload.php
* opcache.preload_user=www-data
* opcache.memory_consumption=256
* opcache.interned_strings_buffer=64
* opcache.max_accelerated_files=30000
* opcache.validate_timestamps=0
* opcache.save_comments=1
* opcache.jit=1255
* opcache.jit_buffer_size=128M
*/
declare(strict_types=1);
if (!function_exists('opcache_compile_file')) {
return;
}
$basePath = __DIR__;
$count = 0;
/**
* Safely compile a file into OPcache
*/
function preload(string $path): bool
{
global $count;
if (!is_file($path) || !is_readable($path)) {
return false;
}
try {
if (@opcache_compile_file($path)) {
$count++;
return true;
}
} catch (Throwable $e) {
// Skip files with errors
}
return false;
}
/**
* Preload a directory (non-recursive for control)
*/
function preloadDir(string $dir): void
{
if (!is_dir($dir)) {
return;
}
foreach (glob($dir . '/*.php') as $file) {
preload($file);
}
}
/**
* Preload a directory recursively
*/
function preloadDirRecursive(string $dir, int $maxDepth = 10): void
{
if (!is_dir($dir) || $maxDepth < 0) {
return;
}
foreach (glob($dir . '/*.php') as $file) {
preload($file);
}
foreach (glob($dir . '/*', GLOB_ONLYDIR) as $subdir) {
$basename = basename($subdir);
// Skip test/dev directories
if (in_array(strtolower($basename), ['tests', 'test', 'testing', 'stubs', 'fixtures', 'mock', 'mocks'])) {
continue;
}
preloadDirRecursive($subdir, $maxDepth - 1);
}
}
// =============================================================================
// 1. LARAVEL CACHED FILES (Most Important)
// =============================================================================
$cacheFiles = [
'/bootstrap/cache/packages.php',
'/bootstrap/cache/services.php',
'/bootstrap/cache/config.php',
'/bootstrap/cache/routes-v7.php',
'/bootstrap/cache/events.php',
'/bootstrap/cache/filament.php',
];
foreach ($cacheFiles as $file) {
preload($basePath . $file);
}
// =============================================================================
// 2. COMPOSER AUTOLOADER
// =============================================================================
preload($basePath . '/vendor/composer/ClassLoader.php');
preload($basePath . '/vendor/composer/autoload_static.php');
// =============================================================================
// 3. APPLICATION MODELS (High Priority)
// =============================================================================
preloadDir($basePath . '/app/Models');
// =============================================================================
// 4. LARAVEL CORE (Selective - Most Used Classes Only)
// =============================================================================
$laravelCore = [
// Foundation
'/vendor/laravel/framework/src/Illuminate/Foundation/Application.php',
'/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php',
'/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php',
// Container
'/vendor/laravel/framework/src/Illuminate/Container/Container.php',
'/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php',
// HTTP
'/vendor/laravel/framework/src/Illuminate/Http/Request.php',
'/vendor/laravel/framework/src/Illuminate/Http/Response.php',
'/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php',
'/vendor/laravel/framework/src/Illuminate/Http/RedirectResponse.php',
// Routing
'/vendor/laravel/framework/src/Illuminate/Routing/Router.php',
'/vendor/laravel/framework/src/Illuminate/Routing/Route.php',
'/vendor/laravel/framework/src/Illuminate/Routing/Controller.php',
'/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php',
'/vendor/laravel/framework/src/Illuminate/Routing/Redirector.php',
// Database
'/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php',
'/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
'/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php',
'/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php',
'/vendor/laravel/framework/src/Illuminate/Database/Connection.php',
'/vendor/laravel/framework/src/Illuminate/Database/MySqlConnection.php',
// Support
'/vendor/laravel/framework/src/Illuminate/Support/Collection.php',
'/vendor/laravel/framework/src/Illuminate/Support/Str.php',
'/vendor/laravel/framework/src/Illuminate/Support/Arr.php',
'/vendor/laravel/framework/src/Illuminate/Support/Carbon.php',
'/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php',
// Auth
'/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php',
'/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php',
// Session
'/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php',
'/vendor/laravel/framework/src/Illuminate/Session/Store.php',
// View
'/vendor/laravel/framework/src/Illuminate/View/Factory.php',
'/vendor/laravel/framework/src/Illuminate/View/View.php',
'/vendor/laravel/framework/src/Illuminate/View/Component.php',
// Cache
'/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php',
'/vendor/laravel/framework/src/Illuminate/Cache/Repository.php',
'/vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php',
// Validation
'/vendor/laravel/framework/src/Illuminate/Validation/Validator.php',
'/vendor/laravel/framework/src/Illuminate/Validation/Factory.php',
// Pipeline
'/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php',
];
foreach ($laravelCore as $file) {
preload($basePath . $file);
}
// =============================================================================
// 5. SYMFONY HTTP (Used by Laravel)
// =============================================================================
$symfonyHttp = [
'/vendor/symfony/http-foundation/Request.php',
'/vendor/symfony/http-foundation/Response.php',
'/vendor/symfony/http-foundation/HeaderBag.php',
'/vendor/symfony/http-foundation/ParameterBag.php',
'/vendor/symfony/http-foundation/InputBag.php',
'/vendor/symfony/http-foundation/FileBag.php',
'/vendor/symfony/http-foundation/ServerBag.php',
'/vendor/symfony/http-foundation/Session/Session.php',
'/vendor/symfony/http-foundation/Cookie.php',
'/vendor/symfony/http-kernel/HttpKernel.php',
];
foreach ($symfonyHttp as $file) {
preload($basePath . $file);
}
// =============================================================================
// 6. LIVEWIRE CORE
// =============================================================================
$livewireCore = [
'/vendor/livewire/livewire/src/Livewire.php',
'/vendor/livewire/livewire/src/Component.php',
'/vendor/livewire/livewire/src/LivewireManager.php',
'/vendor/livewire/livewire/src/ComponentHook.php',
];
foreach ($livewireCore as $file) {
preload($basePath . $file);
}
// =============================================================================
// 7. FILAMENT CORE
// =============================================================================
$filamentCore = [
'/vendor/filament/filament/src/FilamentManager.php',
'/vendor/filament/filament/src/Panel.php',
'/vendor/filament/support/src/Facades/FilamentView.php',
'/vendor/filament/forms/src/Form.php',
'/vendor/filament/tables/src/Table.php',
'/vendor/filament/actions/src/Action.php',
];
foreach ($filamentCore as $file) {
preload($basePath . $file);
}
// =============================================================================
// 8. APPLICATION CODE
// =============================================================================
// Controllers
preloadDir($basePath . '/app/Http/Controllers');
// Middleware
preloadDir($basePath . '/app/Http/Middleware');
// Livewire Components
preloadDirRecursive($basePath . '/app/Livewire', 3);
// Providers
preloadDir($basePath . '/app/Providers');
// Classes & Helpers
preloadDir($basePath . '/app/Classes');
preloadDir($basePath . '/app/Helpers');
preloadDir($basePath . '/app/Traits');
// Admin Resources (Filament)
preloadDir($basePath . '/app/Admin/Resources');
preloadDir($basePath . '/app/Admin/Pages');
// Jobs
preloadDir($basePath . '/app/Jobs');
// =============================================================================
// 9. COMPILED VIEWS (Top 100)
// =============================================================================
$viewsPath = $basePath . '/storage/framework/views';
if (is_dir($viewsPath)) {
$views = glob($viewsPath . '/*.php');
if ($views) {
usort($views, fn($a, $b) => filemtime($b) <=> filemtime($a));
foreach (array_slice($views, 0, 100) as $view) {
preload($view);
}
}
}
// =============================================================================
// SUMMARY
// =============================================================================
if (PHP_SAPI === 'cli') {
$mem = round(memory_get_usage(true) / 1024 / 1024, 1);
echo "OPcache Preload: {$count} files, {$mem}MB memory\n";
} else {
error_log("OPcache Preload: {$count} files");
}