File
File
- Full name:
\Hazaar\Controller\Response\File
- Parent class:
\Hazaar\Controller\Response\HTTP\OK
Properties
__defaultCacheControlDirectives
Default cache control.
public static array<string,bool|int|string> $__defaultCacheControlDirectives
Public with max age is 5 minutes
- This property is static.
file
private ?\Hazaar\File $file
manager
private ?\Hazaar\File\Manager $manager
fmtime
private ?\Hazaar\Date $fmtime
bom
Byte-Order-Mark.
private ?string $bom
This allows a byte-order-mark to be output at the beginning of the file content if one does not already exist.
charsetMap
Charset map.
private array<string,string> $charsetMap
This is a map of charsets to their respective byte-order-marks.
Methods
__construct
\Hazaar\File Constructor.
public __construct(null|\Hazaar\File|string $file = null, ?\Hazaar\File\Manager $manager = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$file | null|\Hazaar\File|string | either a string filename to use or a \Hazaar\File object |
$manager | ?\Hazaar\File\Manager |
initialiseCacheControl
public initialiseCacheControl(): bool
load
public load(\Hazaar\File|string $file, ?\Hazaar\File\Manager $manager = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$file | \Hazaar\File|string | |
$manager | ?\Hazaar\File\Manager |
setContent
Sets the content for the response.
public setContent(mixed $data, ?string $contentType = null): void
Parameters:
Parameter | Type | Description |
---|---|---|
$data | mixed | |
$contentType | ?string |
getContent
Retrieves the content of the response.
public getContent(): string
Return Value:
an empty string as the content
getContentLength
Retrieves the content length.
public getContentLength(): int
Return Value:
the length of the content, which is currently always 0
hasContent
Checks if the response has content.
public hasContent(): bool
Return Value:
returns false indicating that the response does not have content
setUnmodified
public setUnmodified(\Hazaar\Date $ifModifiedSince): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$ifModifiedSince | \Hazaar\Date |
setLastModified
public setLastModified(\Hazaar\Date|int $fmtime): void
Parameters:
Parameter | Type | Description |
---|---|---|
$fmtime | \Hazaar\Date|int |
getLastModified
public getLastModified(): string
match
public match(string $pattern, null|array& $matches = null, 0|256|512|768 $flags, int $offset): false|int
Parameters:
Parameter | Type | Description |
---|---|---|
$pattern | string | |
$matches | null|array | |
$flags | 0|256|512|768 | |
$offset | int |
replace
public replace(string $pattern, string $replacement, int $limit = -1, ?int& $count = null): void
Parameters:
Parameter | Type | Description |
---|---|---|
$pattern | string | |
$replacement | string | |
$limit | int | |
$count | ?int |
setDownloadable
public setDownloadable(bool $toggle = true, ?string $filename = null): void
Parameters:
Parameter | Type | Description |
---|---|---|
$toggle | bool | |
$filename | ?string |
setContentType
Sets the content type for the response.
public setContentType(?string $type = null): void
If no content type is provided, it attempts to detect the MIME type of the current content using the finfo
extension.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | ?string | The MIME type to set. If null, the MIME type will be auto-detected. |
getContentType
Retrieves the content type of the response.
public getContentType(): string
Return Value:
the content type of the response
getFile
public getFile(): \Hazaar\File
fileExists
public fileExists(): bool
Inherited methods
__construct
public __construct(string $content_type = 'text/html'): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$content_type | string |
__sleep
public __sleep(): mixed
writeOutput
Writes the output content to the response.
public writeOutput(): void
This method performs the following steps:
- Initializes the content to an empty string.
- Checks if the content has been modified and retrieves it if so.
- If an encryption key is set, encrypts the content using the default encryption cipher and sets the appropriate header.
- If the script is not running in CLI mode and headers have not been set, writes the headers.
- Outputs the content and flushes the output buffer.
Throws:
if headers have already been sent
setHeader
Sets a header for the response.
public setHeader(string $key, string $value, bool $overwrite = true): bool
This method allows setting a header key-value pair for the response. If the header key is 'content-length', the method will return false and will not set the header. Otherwise, it will set the header based on the provided parameters.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the header key |
$value | string | the header value |
$overwrite | bool | Optional. Whether to overwrite the existing header value if the key already exists. Default is true. |
Return Value:
returns true if the header was set successfully, false if the header key is 'content-length'
setHeaders
Add multiple headers.
public setHeaders((string[]|string)[] $headers, bool $overwrite = true): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$headers | (string[]|string)[] | |
$overwrite | bool |
removeHeader
Removes a header from the response.
public removeHeader(string $key): void
This method checks if a header with the specified key exists in the headers array. If it does, the header is removed.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the header to be removed |
getHeaders
Retrieves the headers associated with the response.
public getHeaders(): array<string,string[]|string>
Return Value:
an array of headers
getHeader
Retrieves the header value associated with the specified key.
public getHeader(string $key): null|string[]|string
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the header to retrieve |
Return Value:
the value of the header if found, or null if not found
clearHeaders
Clears all the headers from the response.
public clearHeaders(): void
This method resets the headers array, effectively removing any headers that have been set previously. It is useful when you need to start with a clean slate for headers before setting new ones.
modified
Checks if the response has been modified.
public modified(): bool
This method determines if the response status code indicates that the content has been modified. A status code of 304 means "Not Modified", so this method returns true if the status code is anything other than 304.
Return Value:
true if the response has been modified, false otherwise
setContentType
Sets the content type for the response.
public setContentType(null|string $type = null): void
If no content type is provided, it attempts to detect the MIME type of the current content using the finfo
extension.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | null|string | The MIME type to set. If null, the MIME type will be auto-detected. |
getContentType
Retrieves the content type of the response.
public getContentType(): string
Return Value:
the content type of the response
getContent
Retrieves the content of the response.
public getContent(): string
Return Value:
an empty string as the content
setContent
Sets the content for the response.
public setContent(mixed $content): void
Parameters:
Parameter | Type | Description |
---|---|---|
$content | mixed | the content to be set for the response |
getContentLength
Retrieves the content length.
public getContentLength(): int
Return Value:
the length of the content, which is currently always 0
hasContent
Checks if the response has content.
public hasContent(): bool
Return Value:
returns false indicating that the response does not have content
addContent
Adds content to the response.
public addContent(mixed $content): void
Parameters:
Parameter | Type | Description |
---|---|---|
$content | mixed | the content to be added to the response |
setStatus
Sets the HTTP status code for the response.
public setStatus(int $status): void
Parameters:
Parameter | Type | Description |
---|---|---|
$status | int | the HTTP status code to set |
getStatus
Retrieves the current status code of the response.
public getStatus(): int
Return Value:
the status code of the response
getStatusMessage
Retrieves the status message corresponding to the current HTTP status code.
public getStatusMessage(): string
This method uses the http_response_text
function to convert the status code stored in the $statusCode
property to its associated textual representation.
Return Value:
the status message corresponding to the current HTTP status code
ignoreHeaders
Marks the headers as set, effectively ignoring any further header modifications.
public ignoreHeaders(): void
This method sets the internal flag $headersSet
to true
, which indicates that headers have already been sent or should be ignored. This can be useful in scenarios where you want to prevent any additional headers from being added to the response.
writeHeaders
Sets the HTTP response headers.
private writeHeaders(null|int $content_length = null): void
This method sets the HTTP status code, content type, content length, and any additional headers specified in the $this->headers
array. If the content length is not provided, it will be determined by the getContentLength
method.
Parameters:
Parameter | Type | Description |
---|---|---|
$content_length | null|int | The length of the content. If null, it will be determined automatically. |
Automatically generated on 2024-11-14