linio/common

Linio项目的通用库

4.1.0 2023-03-03 00:43 UTC

README

Latest Stable Version License Build Status Scrutinizer Code Quality

Linio Common包含一些小型组件,它们可以扩展PHP的功能或为Linio组件提供一个连贯的基座

  • 通用类型
  • 基本异常
  • Monolog处理器和处理器

安装

安装Linio Common的推荐方法是通过composer

$ composer require linio/common

测试

要运行测试套件,您需要通过composer安装依赖项,然后运行PHPUnit。

$ composer install
$ vendor/bin/phpunit

集合

此组件直接依赖于doctrine/collections。由于它们也被用作大多数我们自定义集合类型的基础,因此鼓励您使用它们。

字典

这种数据结构允许您创建连贯的键值对,可以以规范的方式使用

<?php

use Linio\Common\Type\Dictionary;

$dict = new Dictionary(['foo' => 'bar']);

if ($dict->has('foo')) {
    echo $dict->get('foo');
}

echo $dict->get('i_dont_exist'); // null
echo $dict->get('i_dont_exist', 'default'); // default

if ($dict->contains('bar')) {
    echo 'We have a bar value somewhere!';
}

异常

这些异常为所有领域异常提供了一个良好的基座。包含的接口作为标签,允许Monolog处理器和处理器以指定方式与之交互。

DomainException

这是所有其他库和应用程序(领域)异常应扩展的核心异常。它被我们通用库中包含的不同框架的异常处理器所使用。有了它,我们可以轻松地支持按字段翻译消息和输入错误。

<?php

throw new \Linio\Common\Exception\DomainException(
    'ORDER_COULD_NOT_BE_PROCESSED',
    500,
    'The order could not be processed because the processor is not responding'
);

ClientException

<?php

throw new \Linio\Common\Exception\ClientException(
    'ORDER_INCOMPLETE',
    400,
    'The order could not be processed because the request is incomplete'
);

EntityNotFoundException

<?php

throw new \Linio\Common\Exception\EntityNotFoundException(
    'Customer',
    'b3ed5dec-a152-4f38-8726-4c4628a6fdbd'
);
<?php

throw new \Linio\Common\Exception\EntityNotFoundException(
    'Postcode',
    ['region' => 'Region 1', 'municipality' => 'Municipality 1']
);
<?php

class CustomerNotFoundException extends \Linio\Common\Exception\EntityNotFoundException
{
    public function __construct(string $identifier)
    {
        parent::__construct('Customer', $identifier, 'CUSTOMER_NOT_FOUND');
    }
}

接口

可用的接口有

  • Linio\Common\Exception\DoNotLog - 告诉Monolog忽略异常
  • Linio\Common\Exception\ForceLogging - 告诉Monolog无论DoNotLog如何都要记录异常
  • Linio\Common\Exception\CriticalError - 告诉Monolog将异常记录为CRITICAL,无论其当前级别如何

日志记录(Monolog)

此组件包含各种与Monolog集成的类。

处理器

  • Linio\Common\Logging\CriticalErrorProcessor - 将CriticalError异常升级为CRITICAL,无论日志级别如何。
  • Linio\Common\Logging\ExceptionTokenProcessor - 将异常令牌添加到记录中。

处理器

DoNotLogHandler

  • Linio\Common\Logging\DoNotLogHandler - 忽略实现此接口的异常。