From e4d07142cf3059dcb3cbc01e94524d57c2bfc38a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 May 2022 09:13:27 +0900 Subject: [PATCH 1/2] chore: add sample for preloading --- preload.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 preload.php diff --git a/preload.php b/preload.php new file mode 100644 index 000000000000..7e1a04956fa9 --- /dev/null +++ b/preload.php @@ -0,0 +1,113 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +/* + *--------------------------------------------------------------- + * Sample file for Preloading + *--------------------------------------------------------------- + * See https://www.php.net/manual/en/opcache.preloading.php + * + * How to Use: + * 1. Set Preload::$paths. + * 2. Set opcache.preload in php.ini. + * php.ini: + * opcache.preload=/path/to/preload.php + */ + +// Load the paths config file +require __DIR__ . '/app/Config/Paths.php'; + +// Path to the front controller +define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR); + +/** + * See https://www.php.net/manual/en/function.str-contains.php#126277 + */ +if (! function_exists('str_contains')) { + /** + * Polyfill of str_contains() + */ + function str_contains(string $haystack, string $needle): bool + { + return empty($needle) || strpos($haystack, $needle) !== false; + } +} + +class Preload +{ + /** + * @var array Paths to preload. + */ + private array $paths = [ + [ + 'include' => // __DIR__ . '/vendor/codeigniter4/framework/system', + __DIR__ . '/system', + 'exclude' => [ + // Not needed if you don't use them. + '/system/Database/OCI8/', + '/system/Database/Postgre/', + '/system/Database/SQLSRV/', + // Not needed. + '/system/Database/Seeder.php', + '/system/Test/', + '/system/Language/', + '/system/CLI/', + '/system/Commands/', + '/system/Publisher/', + '/system/ComposerScripts.php', + '/Views/', + // Errors occur. + '/system/Config/Routes.php', + '/system/ThirdParty/', + ], + ], + ]; + + public function __construct() + { + $this->loadAutoloader(); + } + + private function loadAutoloader() + { + $paths = new Config\Paths(); + require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; + } + + /** + * Load PHP files. + */ + public function load() + { + foreach ($this->paths as $path) { + $directory = new RecursiveDirectoryIterator($path['include']); + $fullTree = new RecursiveIteratorIterator($directory); + $phpFiles = new RegexIterator( + $fullTree, + '/.+((? $file) { + foreach ($path['exclude'] as $exclude) { + if (str_contains($file[0], $exclude)) { + continue 2; + } + } + + require_once $file[0]; + echo 'Loaded: ' . $file[0] . "\n"; + } + } + } +} + +(new Preload())->load(); From 9f6b338960453bf6f81fac69163b18217908b352 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 23 May 2022 18:56:23 +0900 Subject: [PATCH 2/2] docs: add changelog --- user_guide_src/source/changelogs/v4.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index b314dd892794..f81d3368e458 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -85,6 +85,7 @@ Others - :ref:`select() `, :ref:`where() `, :ref:`like() ` accept the ``CodeIgniter\Database\RawSql`` instance. - Debugbar enhancements - Debug toolbar is now using ``microtime()`` instead of ``time()``. +- Added a sample file for `Preloading `_. See **preload.php**. Changes *******