preload.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * This file is part of CodeIgniter 4 framework.
  4. *
  5. * (c) CodeIgniter Foundation <admin@codeigniter.com>
  6. *
  7. * For the full copyright and license information, please view
  8. * the LICENSE file that was distributed with this source code.
  9. */
  10. use CodeIgniter\Boot;
  11. use Config\Paths;
  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. * 0. Copy this file to your project root folder.
  20. * 1. Set the $paths property of the preload class below.
  21. * 2. Set opcache.preload in php.ini.
  22. * php.ini:
  23. * opcache.preload=/path/to/preload.php
  24. */
  25. // Load the paths config file
  26. require __DIR__ . '/app/Config/Paths.php';
  27. // Path to the front controller
  28. define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
  29. class preload
  30. {
  31. /**
  32. * @var array Paths to preload.
  33. */
  34. private array $paths = [
  35. [
  36. 'include' => __DIR__ . '/vendor/codeigniter4/framework/system', // Change this path if using manual installation
  37. 'exclude' => [
  38. // Not needed if you don't use them.
  39. '/system/Database/OCI8/',
  40. '/system/Database/Postgre/',
  41. '/system/Database/SQLite3/',
  42. '/system/Database/SQLSRV/',
  43. // Not needed for web apps.
  44. '/system/Database/Seeder.php',
  45. '/system/Test/',
  46. '/system/CLI/',
  47. '/system/Commands/',
  48. '/system/Publisher/',
  49. '/system/ComposerScripts.php',
  50. // Not Class/Function files.
  51. '/system/Config/Routes.php',
  52. '/system/Language/',
  53. '/system/bootstrap.php',
  54. '/system/util_bootstrap.php',
  55. '/system/rewrite.php',
  56. '/Views/',
  57. // Errors occur.
  58. '/system/ThirdParty/',
  59. ],
  60. ],
  61. ];
  62. public function __construct()
  63. {
  64. $this->loadAutoloader();
  65. }
  66. private function loadAutoloader(): void
  67. {
  68. $paths = new Paths();
  69. require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php';
  70. Boot::preload($paths);
  71. }
  72. /**
  73. * Load PHP files.
  74. */
  75. public function load(): void
  76. {
  77. foreach ($this->paths as $path) {
  78. $directory = new RecursiveDirectoryIterator($path['include']);
  79. $fullTree = new RecursiveIteratorIterator($directory);
  80. $phpFiles = new RegexIterator(
  81. $fullTree,
  82. '/.+((?<!Test)+\.php$)/i',
  83. RecursiveRegexIterator::GET_MATCH,
  84. );
  85. foreach ($phpFiles as $key => $file) {
  86. foreach ($path['exclude'] as $exclude) {
  87. if (str_contains($file[0], $exclude)) {
  88. continue 2;
  89. }
  90. }
  91. require_once $file[0];
  92. // Uncomment only for debugging (to inspect which files are included).
  93. // Never use this in production - preload scripts must not generate output.
  94. // echo 'Loaded: ' . $file[0] . "\n";
  95. }
  96. }
  97. }
  98. }
  99. (new preload())->load();