Skip to content

Commit 346c18f

Browse files
authored
Merge pull request #5974 from kenjis/add-preload
Add sample file for preloading
2 parents f63e6e5 + 9f6b338 commit 346c18f

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

preload.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
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+
],
71+
],
72+
];
73+
74+
public function __construct()
75+
{
76+
$this->loadAutoloader();
77+
}
78+
79+
private function loadAutoloader()
80+
{
81+
$paths = new Config\Paths();
82+
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
83+
}
84+
85+
/**
86+
* Load PHP files.
87+
*/
88+
public function load()
89+
{
90+
foreach ($this->paths as $path) {
91+
$directory = new RecursiveDirectoryIterator($path['include']);
92+
$fullTree = new RecursiveIteratorIterator($directory);
93+
$phpFiles = new RegexIterator(
94+
$fullTree,
95+
'/.+((?<!Test)+\.php$)/i',
96+
RecursiveRegexIterator::GET_MATCH
97+
);
98+
99+
foreach ($phpFiles as $key => $file) {
100+
foreach ($path['exclude'] as $exclude) {
101+
if (str_contains($file[0], $exclude)) {
102+
continue 2;
103+
}
104+
}
105+
106+
require_once $file[0];
107+
echo 'Loaded: ' . $file[0] . "\n";
108+
}
109+
}
110+
}
111+
}
112+
113+
(new Preload())->load();

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Others
9494
- RouteCollection::addRedirect() can now use placeholders.
9595
- Debugbar enhancements
9696
- Debug toolbar is now using ``microtime()`` instead of ``time()``.
97+
- Added a sample file for `Preloading <https://www.php.net/manual/en/opcache.preloading.php>`_. See **preload.php**.
9798

9899
Changes
99100
*******

0 commit comments

Comments
 (0)