timostamm/web-resource

PHP Web Resources

v1.2.1 2023-10-08 14:09 UTC

This package is auto-updated.

Last update: 2024-04-08 18:38:35 UTC


README

build Packagist PHP Version GitHub tag License

A collection of classes that represent resources, suited for delivery to a HTTP client.

The benefit of these classes is a common interface that bundles the data stream with a bunch of metadata like mimetype, modification date, etc.

This abstraction makes caching, lazy processing, media type conversion and other pipelined operations much more convenient than handling files.

Example

// In a Symfony Controller:
$res = Resource::fromFile('dir/my-file.txt');
$res->getMimetype(); // guessed mimetype
return new ResourceResponse($res); 

// Fetch a resource from an URL:
$res = Resource::fromUrl('http://foo.bar/my-file.txt');
$res->getMimetype(); // mimetype from server
$res->getLastModified(); // modification date from server

// Overriding a mimetype and the file name:
$res = Resource::fromFile('dir/my-file.txt', [
	'mimetype' => 'applicatio/octet-stream', 
	'filename' => 'text.txt'
]);
return new ResourceResponse($res); // will also set the appropriate content-disposition header