axy/envnorm

环境初始标准化

0.1.5 2016-06-24 12:40 UTC

This package is auto-updated.

Last update: 2024-09-08 02:00:32 UTC


README

环境初始标准化

Latest Stable Version Minimum PHP Version Build Status Coverage Status License

  • 该库不需要任何依赖项(除了composer包)。
  • 在PHP 5.4+、PHP 7、HHVM(Linux上)、PHP 5.5(Windows上)上进行了测试。
  • 安装: composer require axy/envnorm
  • 许可证: MIT

文档

如何使用

use axy\envnorm\Normalizer;

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

或者(不要在全局作用域中留下垃圾)

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

或者(自定义配置)

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

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

该库旨在在应用启动之前重新排列PHP环境。

配置

默认配置如下

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

您可以覆盖您需要的参数。自定义配置通过 array_replace_recursive() 与默认配置合并。

值中的 NULL 表示不应执行操作。

$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 的字段。

level (int)

处理错误类型的掩码。默认为 E_ALL

display (boolean)

display_errors 选项设置的值。默认为 NULL:脚本不应该设置 display_errors,它必须在 php.ini 中设置(取决于平台)。

handler (callback)

为错误处理程序设置的回调(见 set_error_handler())。如果为 NULL,则不设置自定义处理程序。如果为 TRUE,则设置库中的处理程序。

库处理程序为每个错误抛出异常(ErrorException 或其子类)。

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

handler 使用此选项。这是异常的类名。默认为 ErrorException,可以是 ErrorException 的子类。

allowSuppression (bool)

此选项允许通过 @-运算符抑制错误。

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

默认(allowSuppression=TRUE)它不会导致异常。如果将此选项设置为 FALSE,则无论是否有 @,都将抛出异常。

exceptionHandler (callback)

异常的最高级处理程序。见 set_exception_handler()

默认为 NULL:处理程序将不会设置。

时区

datetime.timezone (string)

默认时区。默认为 UTC

datetime.keepTimezone (bool)

默认(keepTimezone=TRUE)保持 php.ini 中的时区。只有当 php.ini 中未定义时区时,才使用 datetime.timezone

编码

mbstring 中使用的 encoding 选项(如果已安装)。

选项

应更改的所有其他PHP选项。

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