axy/envnorm

Initial normalization of the environment

0.1.5 2016-06-24 12:40 UTC

This package is auto-updated.

Last update: 2024-12-08 02:32:17 UTC


README

Initial normalization of the environment

Latest Stable Version Minimum PHP Version Build Status Coverage Status License

  • 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/envnorm.
  • License: MIT.

Documentation

How to use

use axy\envnorm\Normalizer;

$normalizer = new Normalizer();
$normalizer->normalize();

Or (do not litter in the global scope):

Normalizer::createInstance()->normalize();

Or (custom configuration):

$config = [
    'datetime' => null,
    'encoding' => 'Windows-1251',
];

Normalizer::createInstance($config)->normalize();

The library is intended for rearrangement of the PHP-environment before the application start.

The configuration

The default configuration is this

[
    'errors' => [
        'level' => E_ALL,
        'display' => null,
        'handler' => true,
        'ErrorException' => 'ErrorException',
        'exceptionHandler' => null,
        'allowSuppression' => true,
    ],
    'datetime' => [
        'timezone' => 'UTC',
        'keepTimezone' => true,
    ],
    'encoding' => 'UTF-8',
    'options' => [],
];

You can override the parameters that you need. A custom configuration merges with the default by array_replace_recursive().

NULL in a value means that action should not be performed.

$custom = [
    'errors' => [
        'handler' => null, // do not use a custom error handler
    ],
    'datetime' => null, // do not perform any action on the date and time settings
];

Errors

Default means the following behavior:

  • All errors (including warnings, notices and etc) lead to the stop (by an exception).
  • Show or hide the error depends on the platform (development or production).

The following describes the fields of the configuration section errors.

level (int)

The bitmask of handled error types. By default is E_ALL.

display (boolean)

The value for the display_errors option. By default is NULL: a script shouldn't set display_errors, it must be set in php.ini (depends on the platform).

handler (callback)

The callback for the error handler (see set_error_handler()). IF NULL then do not set a custom handler. IF TRUE then set the handler from the library.

The library handler throws an exception (ErrorException or its child) for each error.

$a = [];
$a['a'] = $a['b']; // ErrorException
ErrorException (string)

The library handler uses this option. This is the class name of the exception. It is ErrorException by default and can be a child of ErrorException.

allowSuppression (bool)

This option allows suppression of error by @-operator.

$a = [];
$a['a'] = @$a['b'];

The default (allowSuppression=TRUE) it do not lead to the exception. If set this option to FALSE the exception will be thrown regardless of @.

exceptionHandler (callback)

The top level handler for exceptions. See set_exception_handler().

The default is NULL: the handler will not set.

Timezone

datetime.timezone (string)

The default timezone. UTC by default.

datetime.keepTimezone (bool)

The default (keepTimezone=TRUE) remains the timezone from php.ini. datetime.timezone only used if the timezone is not defined in php.ini.

Encoding

The encoding option used in mbstring (if it is installed).

Options

All other PHP-options that should be changed.

$config = [
    'options' => [
        'sendmail_from' => 'me@server.loc',
        'mysqli.default_port' => 3307,
    ],
];