Method
StaticFiles::wrap
A handler that answers the requests naming a file from the directory, and calls $app for every other.
Description#
public StaticFiles::wrap(
Closure $app
): ClosureParameters#
- $app Closure
- The application's handler
Return values#
Closure
Examples#
use Swerve\ClientRequest;
use Swerve\RequestHandler;
use Swerve\StaticFiles;
$app = new RequestHandler((new StaticFiles(__DIR__ . '/../StaticFiles/public'))->wrap(function (ClientRequest $request) {
$request->sendResponseHeaders(404);
$request->end();
}));
if (debug_backtrace()) {
return $app; // swerve loads this file: it is the application's swerve.php
}
// Run directly: serve the application with swerve, and send it requests
require __DIR__ . '/../serve.php';
touch(__DIR__ . '/../StaticFiles/public/hello.txt', 1767225600); // a fixed date, for the same output every run
serve(__FILE__, [
"GET /hello.txt HTTP/1.1\r\nHost: example\r\nRange: bytes=0-4\r\n\r\n", // 206, the first 5 bytes
"GET /hello.txt HTTP/1.1\r\nHost: example\r\nIf-None-Match: \"6955b900-12\"\r\n\r\n", // 304, the client has it
"GET /serve.php HTTP/1.1\r\nHost: example\r\n\r\n", // never a PHP source
]);Output
HTTP/1.1 206 Partial Content Content-Range: bytes 0-4/18 Content-Type: text/plain; charset=utf-8 Last-Modified: Thu, 01 Jan 2026 00:00:00 GMT ETag: "6955b900-12" Accept-Ranges: bytes Server: Swerve Content-Length: 5 Connection: close Hello HTTP/1.1 304 Not Modified Content-Type: text/plain; charset=utf-8 Last-Modified: Thu, 01 Jan 2026 00:00:00 GMT ETag: "6955b900-12" Accept-Ranges: bytes Server: Swerve Connection: close HTTP/1.1 404 Not Found Server: Swerve Content-Length: 0 Connection: close