Luminova Framework

Server Infomation

Last updated: 2024-05-06 16:37:39

The Server class serves as a wrapper for the PHP $_SERVER superglobal, providing a more structured and manageable way to interact with server-related information. By initializing Server with an array representation of $_SERVER, it becomes easier to manipulate and access server data.


  • Class namespace: \Luminova\Http\Server

Properties

variables

Server variables.

protected array $variables

Methods

constructor

Initializes the server constructor.

public __construct(array $variables): mixed

Parameters:

ParameterTypeDescription
$variablesarray

get

Get server variables.

public get(string|null $name = null, mixed $default = null): mixed|array|string|null

Parameters:

ParameterTypeDescription
$namestring|nullOptional name of the server variable.
$defaultmixedDefault value for the server key.

Return Value:

mixed|array|string|null - The value of the specified server variable, or all server variables if $name is null.


set

Set server variable.

public set(string $key, string $value): void

Parameters:

ParameterTypeDescription
$keystringThe server variable key to set.
$valuestringThe server variable value.

remove

Removes a server variable by key

public remove(string $key): void

Parameters:

ParameterTypeDescription
$keystringThe key to remove.

Attempt to find a key in HTTP request headers.

public search(string $key, mixed $default = false): mixed

This method searches for a key in the request headers, including normalized and stripped versions.

Parameters:

ParameterTypeDescription
$keystringThe key to search for.
$defaultmixed

Return Value:

mixed - The value of the found header or false if not found.


has

Check if request header key exist.

public has(string $key): bool

Parameters:

ParameterTypeDescription
$keystringHeader key to check.

Return Value:

bool - Return true if key exists, false otherwise.


count

Get the total number of server variables.

public count(): int

Return Value:

int - Number of server variables


Example Usage:

The server object is available in request class object as public property request()->server

Get value for a specific key.

$value = $server->get('SERVER_NAME');

Set a new value for a key.

$server->set('QUERY_STRING', 'Default Query String');

Remove a key.

$server->remove('SERVER_PORT');

Search for a value

$key = $server->search('localhost');

Check if a key exists

if ($server->has('SERVER_SIGNATURE')) {
    // Key exists
}

Count the number of elements.

$count = $server->count();