PHP Class HTTP_ConditionalGet, minify

E.g. Content from DB with update time: list($updateTime, $content) = getDbUpdateAndContent(); $cg = new HTTP_ConditionalGet(array( 'lastModifiedTime' => $updateTime ,'isPublic' => true )); $cg->sendHeaders(); if ($cg->cacheIsValid) { exit(); } echo $content; E.g. Shortcut for the above HTTP_ConditionalGet::check($updateTime, true); // exits if client has cache echo $content; E.g. Content from DB with no update time: $content = getContentFromDB(); $cg = new HTTP_ConditionalGet(array( 'contentHash' => md5($content) )); $cg->sendHeaders(); if ($cg->cacheIsValid) { exit(); } echo $content; E.g. Static content with some static includes: before content $cg = new HTTP_ConditionalGet(array( 'lastUpdateTime' => max( filemtime(__FILE__) ,filemtime('/path/to/header.inc') ,filemtime('/path/to/footer.inc') ) )); $cg->sendHeaders(); if ($cg->cacheIsValid) { exit(); }
Author: Stephen Clay ([email protected])
Datei anzeigen Open project: mrclay/minify Class Usage Examples

Public Properties

Property Type Description
$cacheIsValid boolean You'll want to check this after instantiating the object. If true, do not send content, just call sendHeaders() if you haven't already.

Protected Properties

Property Type Description
$_etag
$_headers
$_lmTime
$_stripEtag

Public Methods

Method Description
__construct ( array $spec )
check ( integer $lastModifiedTime = null, boolean $isPublic = false, array $options = [] ) Exit if the client's cache is valid for this resource
getHeaders ( ) : array Get array of output headers to be sent
gmtDate ( integer $time ) : string Get a GMT formatted date for use in HTTP headers
sendHeaders ( ) : null Send headers
setContentLength ( integer $bytes ) : integer Set the Content-Length header in bytes

Protected Methods

Method Description
_isCacheValid ( ) : boolean Determine validity of client cache and queue 304 header if valid
_setEtag ( string $hash, string $scope )
_setLastModified ( integer $time )
normalizeEtag ( string $etag ) : string
resourceMatchedEtag ( ) : boolean
resourceNotModified ( ) : boolean

Method Details

__construct() public method

public __construct ( array $spec )
$spec array options 'isPublic': (bool) if false, the Cache-Control header will contain "private", allowing only browser caching. (default false) 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers will be sent with content. This is recommended. 'encoding': (string) if set, the header "Vary: Accept-Encoding" will always be sent and a truncated version of the encoding will be appended to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient checking of the client's If-None-Match header, as the encoding portion of the ETag will be stripped before comparison. 'contentHash': (string) if given, only the ETag header can be sent with content (only HTTP1.1 clients can conditionally GET). The given string should be short with no quote characters and always change when the resource changes (recommend md5()). This is not needed/used if lastModifiedTime is given. 'eTag': (string) if given, this will be used as the ETag header rather than values based on lastModifiedTime or contentHash. Also the encoding string will not be appended to the given value as described above. 'invalidate': (bool) if true, the client cache will be considered invalid without testing. Effectively this disables conditional GET. (default false) 'maxAge': (int) if given, this will set the Cache-Control max-age in seconds, and also set the Expires header to the equivalent GMT date. After the max-age period has passed, the browser will again send a conditional GET to revalidate its cache.

_isCacheValid() protected method

Determine validity of client cache and queue 304 header if valid
protected _isCacheValid ( ) : boolean
return boolean

_setEtag() protected method

protected _setEtag ( string $hash, string $scope )
$hash string
$scope string

_setLastModified() protected method

protected _setLastModified ( integer $time )
$time integer

check() public static method

This is a convenience method for common use of the class
public static check ( integer $lastModifiedTime = null, boolean $isPublic = false, array $options = [] )
$lastModifiedTime integer if given, both ETag AND Last-Modified headers will be sent with content. This is recommended.
$isPublic boolean (default false) if true, the Cache-Control header will contain "public", allowing proxies to cache the content. Otherwise "private" will be sent, allowing only browser caching.
$options array (default empty) additional options for constructor

getHeaders() public method

In the case of 304 responses, this array will only contain the response code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified') Otherwise something like: array( 'Cache-Control' => 'max-age=0, public' ,'ETag' => '"foobar"' )
public getHeaders ( ) : array
return array

gmtDate() public static method

header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
public static gmtDate ( integer $time ) : string
$time integer unix timestamp
return string

normalizeEtag() protected method

protected normalizeEtag ( string $etag ) : string
$etag string
return string

resourceMatchedEtag() protected method

protected resourceMatchedEtag ( ) : boolean
return boolean

resourceNotModified() protected method

protected resourceNotModified ( ) : boolean
return boolean

sendHeaders() public method

Send headers
See also: getHeaders() Note this doesn't "clear" the headers. Calling sendHeaders() will call header() again (but probably have not effect) and getHeaders() will still return the headers.
public sendHeaders ( ) : null
return null

setContentLength() public method

With most PHP configs, as long as you don't flush() output, this method is not needed and PHP will buffer all output and set Content-Length for you. Otherwise you'll want to call this to let the client know up front.
public setContentLength ( integer $bytes ) : integer
$bytes integer
return integer copy of input $bytes

Property Details

$_etag protected_oe property

protected $_etag

$_headers protected_oe property

protected $_headers

$_lmTime protected_oe property

protected $_lmTime

$_stripEtag protected_oe property

protected $_stripEtag

$cacheIsValid public_oe property

You'll want to check this after instantiating the object. If true, do not send content, just call sendHeaders() if you haven't already.
public bool $cacheIsValid
return boolean