arraypress / wp-slurp
Include every PHP file in a directory, in a fixed order.
Requires
- php: >=8.3
Requires (Dev)
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.13.5
- wp-coding-standards/wpcs: ^3.4
README
Include every PHP file in a directory, in a fixed order.
What it does
A plugin that grows past a handful of files ends up with a bootstrap listing every one of them by hand, and a merge conflict every time two people add a class. This replaces that list with the directory it lives in.
The ordering is the point. DirectoryIterator and glob( ..., GLOB_NOSORT )
hand back whatever the filesystem holds — creation order on APFS, hashed
elsewhere — so a class extending another in the same folder loads cleanly on
your machine and fatals on the server. Files here are always loaded in name
order, which is something you control.
Features
- Load every PHP file in a directory, sorted by name
- Load several directories in the order you list them
- Recurse into subdirectories, each still sorted
- Skips the
index.phpsilence stubs - Returns the paths it loaded, so a bootstrap can be asserted on
- Reports what a directory would load without loading it
- A directory that is not there is skipped, not fatal
Installation
composer require arraypress/wp-slurp
Quick start
use ArrayPress\Slurp\Slurp; // One directory. Slurp::dir( PLUGIN_DIR . 'includes/utils' ); // Several, in the order they have to load. Slurp::dirs( [ PLUGIN_DIR . 'includes/database/schemas', PLUGIN_DIR . 'includes/database/tables', PLUGIN_DIR . 'includes/database/queries', ] );
Prefix filenames to pin order within a directory — 10-base.php,
20-extends-base.php. Sorting is by string, so numbers need padding.
A note on side effects
Files are require_onced, so anything at file scope runs the moment you call
this. Declare your classes and functions in these files and hook from a
bootstrap that runs afterwards, or load order becomes hook order.
Requirements
- PHP 8.3 or later
License
GPL-2.0-or-later