nikic/include-interceptor
PHP extension and library to intercept and virtualize include/require. Lets you provide custom file contents, rewrite paths, or instrument included code without touching the filesystem—useful for loaders, build tools, testing, and sandboxing.
Install via Composer: composer require nikic/include-interceptor. Start by wrapping your entrypoint with the interceptor to capture all include/require calls:
use IceWind\Interceptor\IncludeInterceptor;
$interceptor = new IncludeInterceptor();
$interceptor->install();
// Your application or test bootstrapping logic here
require 'app.php';
$interceptor->uninstall();
In tests or CLI tools, you might use it to log or alter included paths for analysis:
$interceptor->onInclude(function ($type, $path, $resolvedPath) {
error_log("Included {$type}: {$path} → {$resolvedPath}");
return $resolvedPath; // return modified path or original to proceed
});
require calls to block access to sensitive paths (e.g., /etc/passwd) by returning false or throwing exceptions.config.php with a test fixture by resolving to a different path in the callback.Best practice: Keep callbacks fast and idempotent. Avoid heavy I/O or recursion (e.g., do not include inside the callback unless deliberate).
include/require, not fopen or direct file reads: It won’t catch file_get_contents('config.php'), only explicit PHP include constructs.install() before any file is included. If you include a file before installing, it’ll bypass the interceptor entirely.uninstall() restores PHP’s default behavior. Skipping this may cause issues in long-running processes (e.g., ReactPHP, Swoole).false from the callback skips the include entirely. Returning a string replaces the path. Omitting return defaults to using the resolved path.spl_autoload_register() for full coverage: use autoloading for classes, include-interceptor for procedural includes.static $depth = 0; $depth++), and decrement on __FILE__-based checks to trace recursive includes.How can I help you explore Laravel packages today?