awonderphp / fileresource
An abstract class for resources embedded in a web page (images, scripts, whatever)
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/awonderphp/fileresource
Requires
- php: ~7.1.0 || ~7.2.0
- awonderphp/filewrapper: ^1.1
Requires (Dev)
- phpunit/phpunit: ^7
- squizlabs/php_codesniffer: 3.*
- vimeo/psalm: ^0
This package is not auto-updated.
Last update: 2025-10-18 02:34:57 UTC
README
An abstract class for resources embedded in a web page (images, scripts, whatever)
This package is intended to provide a standard means by which web applications can treat resources embedded in the web page as objects.
The advantage of treating resources as objects is it allows them to have both properties and methods that can do stuff with those properties and/or parameters fed to those methods.
This package also includes an abstract class for using PHP to serve the resource described by a FileResource object.
- Install Package
- Class Properties
- Class Methods
- FileWrapper Class
- ResourceServer Class
- GenericFileResource Class
- Unit Testing
Install Package
You can install this class with composer require awonderphp/fileresource:^1.0
Class Properties
The following properties are defined by the abstract class:
-
protected $mime = null;
The MIME type the file should be served with when a client requests it. -
protected $checksum = null;
The algorithm used to calculate a checksum digest, followed by a colon, followed by either a hex or base64 representation of the checksum digest. It is recommended to use one of the SHA-2 algorithms that browsers are required to support for use with theintegrityattribute. -
protected $validIntegrityAlgo = array('sha256', 'sha384', 'sha512');
Algorithms that are supported by all web browsers that support theintegrityattribute. -
protected $filepath = null;
The full path on the local server to the file. Please note this property should remainnullwhen the file described by the object is not present on the local filesystem. -
protected $crossorigin = null;
The contents of thecrossoriginattribute that should be used when creating an (X)HTML node that references the resource. -
$validCrossOrigin = array('anonymous', 'use-credentials');
The valid options that can be used with acrossoriginattribute. -
protected $lastmod = null;
The last time the file was modified, using ISO 8601 inY-m-d\TH:i:sO, which can be achieve in PHP asdate('c'). This does not need to match the file timestamp on the local filesystem, which often is not accurate. -
protected $urlscheme = null;
Should be one ofnull,http, orhttps. The scheme to use when creating asrcorhrefattribute to embed the resource in a web page. This should benullwhen the resource will be served from the same server that is serving the web page. -
protected $urlhost = null;
Should benullor the hostname to use when creating asrcorhrefattribute to embed the resource in a web page. This should benullwhen the resource will be served from the same server as that is serving the web page. -
protected $urlpath = null;
The path to the resource on the server that is serving the resource when creating asrcorhrefattribute to embed the resource in a web page. -
protected $urlquery = null;
In the event a URL query is needed (the part after the?in a URL), the query that should be appended at the end of the URL when construction asrcorhrefattribute to embed the resource in a web page.
Notes About the Class Properties
For many files, additional properties are needed to adequately embed the
resource within a web page (such as alt tag and possibly caption for an
image)
This abstract class is intended to define most properties commonly needed for embedding a resource in a web page and everything that is needed for a PHP wrapper to correctly serve the file.
The four url* properties correspond with the the PHP function
parse_url with the
following ommissions:
-
port
Only applicable if the resource is remote, in which case HTTPS should be preferred and only the default port 443 should be used for HTTPS. -
user
Not an appropriate component of a URL for embedding resources in a web page. -
pass
Not an appropriate component of a URL for embedding resources in a web page. -
fragment
Not an appropriate component of a URL for embedding resources in a web page.
Class Methods
The following methods are defined by the abstract class:
-
public function getMimeType()
Returns the$mimeproperty. -
public function getChecksum()
Returns the$checksumproperty. -
public function getCrossOrigin()
Returns the$crossoriginproperty. -
public function getFilePath()
Returns the$filepathproperty. -
public function validateFile()
If the$checksumproperty is set and the$filepathproperty is set and the file exists, returnstrueif the file matches the checksum andfalseif it does not. -
public function getSrcAttribute($prefix = null)
Builds the contents of thesrcorhrefattribute needed to embed the resource in a web page. Note that this will returnnullif the$urlschemeproperty ishttpand the file does not have a$checksumproperty that uses an algorithm in the$validIntegrityAlgoproperty. The optional parameter$prefixis a filesystem path to put in front of the$urlpathproperty, useful for web applications using a wrapper to serve the file. -
public function getIntegrityAttribute()
Builds the contents of anintegrityattribute, if the$checksumproperty uses a suitable algorithm. -
public function getTimestamp()
If the$lastmodeproperty is not null, returns a UNIX timestamp (seconds from UNIX epoch).
FileWrapper Class
This is a class that extends my \AWonderPHP\FileWrapper\FileWrapper class.
The class it extends was written to be a download wrapper including support
for client cache validation and partial content requests.
However that class uses the timestamp of the file on the filesystem for the
Last-Modified header and uses the inode of the file on the filesystem in
its generation of the ETag header.
The extended class allows the modification timestamp and the ETag to be set independent of the file on the filesystem.
ResourceServer Class
This is an abstract class that provides a public method for serving a file
based from a FileResource object.
The idea is to extend the class adding a method construct the necessary
FileResource object and then serve it.
GenericFileResource Class
This is a very generic class that extends the FileResource abstract class. It
exists to provide for easy unit testing of the public methods in the abstract
class but it can be used in web applications.
The __construct function takes a single argument, an array of key => value
pairs where the key corresponds to the FileResource property you wish to set.
Unit Testing
Unit Testing is performed using PHPUnit version 7. The results can be viewed in the file UnitTestResults.txt
EOF