bredmor/phreloader

An automatic preloader for PHP 7.4+

0.0.6 2019-11-27 00:09 UTC

This package is auto-updated.

Last update: 2024-05-07 09:53:23 UTC


README

Packagist Version PHP from Packagist License from GitHub

Phreloader is a simple auto-preloader for the PHP preloader available from version 7.4 and later.

This preloader will automatically preload every PHP file defined by your composer classmap, as long as it can be found within the parent directory.

Work In Progress

This package is a heavy work in progress. As of the time of this writing, PHP 7.4 has not yet been released, nor has this package been extensively tested. USE AT YOUR OWN RISK

This package is a stopgap solution until Composer supports preloading.

Planned Features (Not Yet Implemented)

  • Support more custom vendor directory configurations
  • Publish configured preloading script to project root
  • Tests

Installation

From Source:

Clone the repository from GitHub or unzip into your vendor directory. Phreloader is packaged for PSR-4 autoloading.

From Composer:

composer require bredmor/phreloader

Basic Usage

Create a file preloader.php in your project root directory with the following contents:

require_once(__DIR__ . '/vendor/autoload.php');

use bredmor/phreloader/Preloader;
(new Preloader(__DIR__))->go(); // Where `__DIR__` refers to the root directory of your project's code.

Optional - Specify vendor directory location

require_once(__DIR__ . '/vendor/autoload.php');

use bredmor/phreloader/Preloader;
(new Preloader(__DIR__, '/my/custom/vendor/dir'))->go(); 

Add the following line to your php.ini configuration file(s):

opcache.preload=/path/to/your/project/preload.php

That's it! The next time you restart your server, PHP will preload your project files in memory.

Ignoring specific files or directories

In the same directory as your project root, create a new file .nopreload. Each line in this file should be the (relative or absolute) path to files or directories not to be preloaded.

Example:

tests/
some_rarely_used_dir/
badfile.php
another/directory/file.php

Caveats

  • Preloading requires more available RAM the larger your project is. Preloading your entire classmap may not be desirable for larger projects, especially on smaller servers/containers. It is strongly recommended to use the ignore list to ignore rarely-loaded files, or directories that contain them..

  • Because preloading stores the opcodes for your files in memory, you must restart your server every time you change one of these files - the new code will not be picked up until you do, your server will continue to use the version cached in RAM.

Requirements

Phreloader is built for PHP 7.4 and later.

Authors

Contributing

Pull requests, bug reports and feature requests are welcome.

License

Phreloader is licensed under the MIT License - see the LICENSE file for details