|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +/* |
| 13 | + *--------------------------------------------------------------- |
| 14 | + * Sample file for Preloading |
| 15 | + *--------------------------------------------------------------- |
| 16 | + * See https://www.php.net/manual/en/opcache.preloading.php |
| 17 | + * |
| 18 | + * How to Use: |
| 19 | + * 1. Set Preload::$paths. |
| 20 | + * 2. Set opcache.preload in php.ini. |
| 21 | + * php.ini: |
| 22 | + * opcache.preload=/path/to/preload.php |
| 23 | + */ |
| 24 | + |
| 25 | +// Load the paths config file |
| 26 | +require __DIR__ . '/app/Config/Paths.php'; |
| 27 | + |
| 28 | +// Path to the front controller |
| 29 | +define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR); |
| 30 | + |
| 31 | +/** |
| 32 | + * See https://www.php.net/manual/en/function.str-contains.php#126277 |
| 33 | + */ |
| 34 | +if (! function_exists('str_contains')) { |
| 35 | + /** |
| 36 | + * Polyfill of str_contains() |
| 37 | + */ |
| 38 | + function str_contains(string $haystack, string $needle): bool |
| 39 | + { |
| 40 | + return empty($needle) || strpos($haystack, $needle) !== false; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class Preload |
| 45 | +{ |
| 46 | + /** |
| 47 | + * @var array Paths to preload. |
| 48 | + */ |
| 49 | + private array $paths = [ |
| 50 | + [ |
| 51 | + 'include' => // __DIR__ . '/vendor/codeigniter4/framework/system' |
| 52 | + __DIR__ . '/system', |
| 53 | + 'exclude' => [ |
| 54 | + // Not needed if you don't use them. |
| 55 | + '/system/Database/OCI8/', |
| 56 | + '/system/Database/Postgre/', |
| 57 | + '/system/Database/SQLSRV/', |
| 58 | + // Not needed. |
| 59 | + '/system/Database/Seeder.php', |
| 60 | + '/system/Test/', |
| 61 | + '/system/Language/', |
| 62 | + '/system/CLI/', |
| 63 | + '/system/Commands/', |
| 64 | + '/system/Publisher/', |
| 65 | + '/system/ComposerScripts.php', |
| 66 | + '/Views/', |
| 67 | + // Errors occur. |
| 68 | + '/system/Config/Routes.php', |
| 69 | + '/system/ThirdParty/', |
| 70 | + '/system/Events/Events.php', |
| 71 | + ], |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + public function __construct() |
| 76 | + { |
| 77 | + $this->loadAutoloader(); |
| 78 | + } |
| 79 | + |
| 80 | + private function loadAutoloader() |
| 81 | + { |
| 82 | + $paths = new Config\Paths(); |
| 83 | + require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Load PHP files. |
| 88 | + */ |
| 89 | + public function load() |
| 90 | + { |
| 91 | + foreach ($this->paths as $path) { |
| 92 | + $directory = new RecursiveDirectoryIterator($path['include']); |
| 93 | + $fullTree = new RecursiveIteratorIterator($directory); |
| 94 | + $phpFiles = new RegexIterator( |
| 95 | + $fullTree, |
| 96 | + '/.+((?<!Test)+\.php$)/i', |
| 97 | + RecursiveRegexIterator::GET_MATCH |
| 98 | + ); |
| 99 | + |
| 100 | + foreach ($phpFiles as $key => $file) { |
| 101 | + foreach ($path['exclude'] as $exclude) { |
| 102 | + if (str_contains($file[0], $exclude)) { |
| 103 | + continue 2; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + require_once $file[0]; |
| 108 | + echo 'Loaded: ' . $file[0] . "\n"; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +(new Preload())->load(); |
0 commit comments