preload.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /*
  11. *---------------------------------------------------------------
  12. * Sample file for Preloading
  13. *---------------------------------------------------------------
  14. * See https://www.php.net/manual/en/opcache.preloading.php
  15. *
  16. * How to Use:
  17. * 0. Copy this file to your project root folder.
  18. * 1. Set the $paths property of the preload class below.
  19. * 2. Set opcache.preload in php.ini.
  20. * php.ini:
  21. * opcache.preload=/path/to/preload.php
  22. */
  23. // Load the paths config file
  24. require __DIR__ . '/app/Config/Paths.php';
  25. // Path to the front controller
  26. define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
  27. class preload
  28. {
  29. /**
  30. * @var array Paths to preload.
  31. */
  32. private array $paths = [
  33. [
  34. 'include' => __DIR__ . '/vendor/codeigniter4/framework/system', // Change this path if using manual installation
  35. 'exclude' => [
  36. '/system/bootstrap.php',
  37. // Not needed if you don't use them.
  38. '/system/Database/OCI8/',
  39. '/system/Database/Postgre/',
  40. '/system/Database/SQLite3/',
  41. '/system/Database/SQLSRV/',
  42. // Not needed.
  43. '/system/Database/Seeder.php',
  44. '/system/Test/',
  45. '/system/Language/',
  46. '/system/CLI/',
  47. '/system/Commands/',
  48. '/system/Publisher/',
  49. '/system/ComposerScripts.php',
  50. '/Views/',
  51. // Errors occur.
  52. '/system/Config/Routes.php',
  53. '/system/ThirdParty/',
  54. ],
  55. ],
  56. ];
  57. public function __construct()
  58. {
  59. $this->loadAutoloader();
  60. }
  61. private function loadAutoloader(): void
  62. {
  63. $paths = new Config\Paths();
  64. require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php';
  65. CodeIgniter\Boot::preload($paths);
  66. }
  67. /**
  68. * Load PHP files.
  69. */
  70. public function load(): void
  71. {
  72. foreach ($this->paths as $path) {
  73. $directory = new RecursiveDirectoryIterator($path['include']);
  74. $fullTree = new RecursiveIteratorIterator($directory);
  75. $phpFiles = new RegexIterator(
  76. $fullTree,
  77. '/.+((?<!Test)+\.php$)/i',
  78. RecursiveRegexIterator::GET_MATCH
  79. );
  80. foreach ($phpFiles as $key => $file) {
  81. foreach ($path['exclude'] as $exclude) {
  82. if (str_contains($file[0], $exclude)) {
  83. continue 2;
  84. }
  85. }
  86. require_once $file[0];
  87. echo 'Loaded: ' . $file[0] . "\n";
  88. }
  89. }
  90. }
  91. }
  92. (new preload())->load();