You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.0KB

  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. * 1. Set Preload::$paths.
  18. * 2. Set opcache.preload in php.ini.
  19. * php.ini:
  20. * opcache.preload=/path/to/preload.php
  21. */
  22. // Load the paths config file
  23. require __DIR__ . '/app/Config/Paths.php';
  24. // Path to the front controller
  25. define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
  26. /**
  27. * See https://www.php.net/manual/en/function.str-contains.php#126277
  28. */
  29. if (! function_exists('str_contains')) {
  30. /**
  31. * Polyfill of str_contains()
  32. */
  33. function str_contains(string $haystack, string $needle): bool
  34. {
  35. return empty($needle) || strpos($haystack, $needle) !== false;
  36. }
  37. }
  38. class preload
  39. {
  40. /**
  41. * @var array Paths to preload.
  42. */
  43. private array $paths = [
  44. [
  45. 'include' => __DIR__ . '/vendor/codeigniter4/framework/system',
  46. 'exclude' => [
  47. // Not needed if you don't use them.
  48. '/system/Database/OCI8/',
  49. '/system/Database/Postgre/',
  50. '/system/Database/SQLSRV/',
  51. // Not needed.
  52. '/system/Database/Seeder.php',
  53. '/system/Test/',
  54. '/system/Language/',
  55. '/system/CLI/',
  56. '/system/Commands/',
  57. '/system/Publisher/',
  58. '/system/ComposerScripts.php',
  59. '/Views/',
  60. // Errors occur.
  61. '/system/Config/Routes.php',
  62. '/system/ThirdParty/',
  63. ],
  64. ],
  65. ];
  66. public function __construct()
  67. {
  68. $this->loadAutoloader();
  69. }
  70. private function loadAutoloader()
  71. {
  72. $paths = new Config\Paths();
  73. require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
  74. }
  75. /**
  76. * Load PHP files.
  77. */
  78. public function load()
  79. {
  80. foreach ($this->paths as $path) {
  81. $directory = new RecursiveDirectoryIterator($path['include']);
  82. $fullTree = new RecursiveIteratorIterator($directory);
  83. $phpFiles = new RegexIterator(
  84. $fullTree,
  85. '/.+((?<!Test)+\.php$)/i',
  86. RecursiveRegexIterator::GET_MATCH
  87. );
  88. foreach ($phpFiles as $key => $file) {
  89. foreach ($path['exclude'] as $exclude) {
  90. if (str_contains($file[0], $exclude)) {
  91. continue 2;
  92. }
  93. }
  94. require_once $file[0];
  95. echo 'Loaded: ' . $file[0] . "\n";
  96. }
  97. }
  98. }
  99. }
  100. (new preload())->load();