class Environment (View source)

Consolidates access and modification of PHP global variables and settings.

This class should be used sparingly, and only if information cannot be obtained from a current {@link HTTPRequest} object.

Acts as the primary store for environment variables, including those loaded from .env files. Applications should use Environment::getEnv() instead of php's getenv in order to include .env configuration, as the system's actual environment variables are treated immutably.

Methods

static array
getVariables()

Extract env vars prior to modification

static 
setVariables(array $vars)

Restore a backed up or modified list of vars to $globals

static bool
increaseMemoryLimitTo(string|float|int $memoryLimit = -1)

Increase the memory limit to the given level if it's currently too low.

static 
setMemoryLimitMax(string|float $memoryLimit)

Set the maximum allowed value for {@link increaseMemoryLimitTo()}.

static int
getMemoryLimitMax()

No description

static bool
increaseTimeLimitTo(int $timeLimit = null)

Increase the time limit of this script. By default, the time will be unlimited.

static 
setTimeLimitMax(int $timeLimit)

Set the maximum allowed value for {@link increaseTimeLimitTo()};

static int
getTimeLimitMax()

No description

static mixed
getEnv(string $name)

Get value of environment variable

static 
putEnv(string $string)

Set environment variable using php.ini syntax.

static 
setEnv(string $name, string $value)

Set environment variable via $name / $value pair

static bool
isCli()

Returns true if this script is being run from the command line rather than the web server

Details

static array getVariables()

Extract env vars prior to modification

Return Value

array

List of all super globals

static setVariables(array $vars)

Restore a backed up or modified list of vars to $globals

Parameters

array $vars

static bool increaseMemoryLimitTo(string|float|int $memoryLimit = -1)

Increase the memory limit to the given level if it's currently too low.

Only increases up to the maximum defined in {@link setMemoryLimitMax()}, and defaults to the 'memory_limit' setting in the PHP configuration.

Parameters

string|float|int $memoryLimit

A memory limit string, such as "64M". If omitted, unlimited memory will be set.

Return Value

bool

true indicates a successful change, false a denied change.

static setMemoryLimitMax(string|float $memoryLimit)

Set the maximum allowed value for {@link increaseMemoryLimitTo()}.

The same result can also be achieved through 'suhosin.memory_limit' if PHP is running with the Suhosin system.

Parameters

string|float $memoryLimit

Memory limit string or float value

static int getMemoryLimitMax()

Return Value

int

Memory limit in bytes

static bool increaseTimeLimitTo(int $timeLimit = null)

Increase the time limit of this script. By default, the time will be unlimited.

Only works if 'safe_mode' is off in the PHP configuration. Only values up to {@link getTimeLimitMax()} are allowed.

Parameters

int $timeLimit

The time limit in seconds. If omitted, no time limit will be set.

Return Value

bool

TRUE indicates a successful change, FALSE a denied change.

static setTimeLimitMax(int $timeLimit)

Set the maximum allowed value for {@link increaseTimeLimitTo()};

Parameters

int $timeLimit

Limit in seconds

static int getTimeLimitMax()

Return Value

int

Limit in seconds

static mixed getEnv(string $name)

Get value of environment variable

Parameters

string $name

Return Value

mixed

Value of the environment variable, or false if not set

static putEnv(string $string)

Set environment variable using php.ini syntax.

Acts as a process-isolated version of putenv() Note: This will be parsed via parse_ini_string() which handles quoted values

Parameters

string $string

Setting to assign in KEY=VALUE or KEY="VALUE" syntax

static setEnv(string $name, string $value)

Set environment variable via $name / $value pair

Parameters

string $name
string $value

static bool isCli()

Returns true if this script is being run from the command line rather than the web server

Return Value

bool