Download the PHP package axy/env without Composer
On this page you can find all versions of the php package axy/env. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package env
Short Description Abstracting access to the environment
License MIT
Homepage https://github.com/axypro/env
Informations about the package env
axy\env
Abstracting access to the environment (PHP).
- The library does not require any dependencies (except composer packages).
- Tested on PHP 5.4+, PHP 7, HHVM (on Linux), PHP 5.5 (on Windows).
- Install:
composer require axy/env
. - License: MIT.
Documentation
The library provides the abstract layer for access to the environment. The environment is
- The current time and timezone
- PHP runtime settings
- Input data (
$_GET
,$_POST
...) - Server data (
$_SERVER
,$_ENV
) - HTTP headers and cookies
- and etc... any parameter whose modification may affect other parts of the system
If some code has direct access to super-global arrays or calls functions like time()
or header()
then it code becomes tied to the environment.
This code is difficult to test and configure.
The library provides the environment wrapper (an instance of axy\env\Env
class).
The application code gets this wrapper from the outside and works with the environment via it.
By default the wrapper just delegates requests to standard functions.
But this behaviour can be redefined (for test or an other purpose).
Example
By default will be used the standard environment and $this->env->getCurrentTime()
returns the current time.
But this behaviour can be changed.
Config
The environment wrapper with standard behaviour:
The non-standard behavior is specified in the config:
The config specified via an array (as above) or via an instance of axy\env\Config
:
An array is simply, but the object supports autocomplete in IDE.
Specific parameters of the config are described in the relevant sections below.
Cloning
Current Time
Check the current time (returns a timestamp):
Parameter time
specified the current time.
It can be an int or a numeric string (timestamp) or a string for strtotime.
- 1234567890 - a timestamp (2009-02-14 02:31:30)
- "1234567890" - similarly
- "2015-11-04 10:11:12" - a time in the current timezone
- "2015-11-04" - "2015-11-04 00:00:00"
- "+1 month" - relative time for
strtotime()
Changing time
$env->getCurrentTime()
in the above example always returns the same time.
For long-lived scenarios, the time change can be important.
If specified timeChanging
then the time will be changing.
Custom function instead time()
If time
is not specified getCurrentTime()
calls a wrapper of the global function time()
(see below section "Global Functions").
It can override.
Super-Globals
The super-globals arrays $_SERVER
, $_ENV
, $_GET
, $_POST
, $_REQUEST
, $_COOKIE
, $_FILES
available via
$env->server
$env->env
$env->get
$env->post
$env->request
$env->cookie
$env->files
Their can override in the config:
Global Functions
Magic __call
delegates to global functions.
Overriding:
Checking the existence of functions $env->isFunctionExists(string $name):bool
.
echo()
can also be overridden.
Example
Function getallheaders()
not exist in all environments.
Override:
Or
Functions list
It is intended for functions that access the environment. In phpdoc for autocomplete lists the following:
- header
- setcookie
- getallheaders
- headers_list
- http_response_code
- ini_set
- ini_get
- getenv
- error_reporting
- date_default_timezone_get
- date_default_timezone_set
- set_error_handler
- set_exception_handler
echo()
But theoretically it can be used for any function.
Streams
Access to php I/O streams.
Container $env->streams
contains following object:
stdin
stdout
stderr
input
output
These objects implement interface IStream.
You can create object with the interface IStream and spoof a stream:
You can use classes Stream
and StreamMock
:
Stream
is wrapper on a stream resource.
StreamMock
StreamMock is a wrapper on a string.
In addition to the methods of IStream
, StreamMock
supports following methods:
setContent(string $content [, int $position])
setPosition(int $position)
getContent(): string
getPosition(): int
Factory
The method getStandard()
returns an env wrapper for standard behaviour.
The method create($config)
creates a wrapper with overriding behaviour.
It can taken:
an array
a Config
instance
or a Env
instance itself
By default (NULL
) specifies the standard wrapper.
The target service can take wrapper in different formats and not to deal with them.
Exceptions
The library can throw the following exceptions:
axy\errors\InvalidConfig
- Unknown fields in a config (
['time' => 12345, 'unknown' => 67890]
) - Wrong type of fields (
['server' => 5]
, must be an array) - Wrong data in fields (
[time => 'wrong time string']
). - Function wrapper is not callable (detected during a call).
- Unknown fields in a config (
axy\errors\FieldNotFound
- Field not found (
echo $env->unknown;
). - Function not found (
echo $env->unknown(5);
).
- Field not found (
axy\errors\Disabled
- Function disabled in the config
axy\errors\ContainerReadOnly
$env->server = []
orunset($env->server)
.