userfrosting/support

此包已被弃用且不再维护。未建议替代包。

UserFrosting和UF模块的支持类

4.5.0 2021-04-21 00:22 UTC

This package is auto-updated.

Last update: 2024-05-06 06:59:49 UTC


README

Latest Version PHP Version Software License Join the chat at https://chat.userfrosting.com/channel/support Donate

分支 构建 覆盖率 风格
master
develop  

本模块包含UserFrosting及相关模块的支持类。

异常

userfrosting/support为主要的UserFrosting项目提供了一些自定义异常类型

  • RuntimeException
    • FileNotFoundException
    • JsonException
  • HttpException
    • BadRequestException
    • ForbiddenException
    • NotFoundException

HttpException

UserFrosting的大多数异常类型都继承自HttpException类。

HttpException类像典型的异常一样工作,但它在内部维护了两个额外的参数:异常处理程序可能显示给客户端的消息列表(UserMessage)以及应随响应返回的状态码。以下是一个简单的示例:BadRequestException

<?php
class BadRequestException extends HttpException
{
    protected $defaultMessage = 'Bad data!';
    protected $httpErrorCode = 400;
}

它定义了一个默认消息'Bad data!',注册的异常处理程序可以在错误页面上显示或在警报流中推送。它还设置了一个默认的HTTP状态码,用于随错误响应返回。

当异常在您的代码中抛出时,可以覆盖默认消息

$e = new BadRequestException("This is the exception message that will be logged for the dev/sysadmin.");
$e->addUserMessage("This is a custom error message that will be sent back to the client.  Hello, client!");
throw $e;

仓库

userfrosting/support提供了一个通用的Repository类,它扩展了Laravel的基本Repository类,并且各种其他UserFrosting组件都依赖于它。例如,UserFrosting的configtranslator和Fortress的schema都将其数据存储在UserFrosting的Repository或子类中。

Repository类提供了以下方法

has(string $key)

确定给定的配置值是否存在。

get(string $key, mixed $default = null)

获取指定的配置值。

set(array|string $key, mixed $value = null)

设置给定的配置值。

prepend(string $key, mixed $value)

将值拼接到数组配置值上。

push(string $key, mixed $value)

将值推送到数组配置值上。

all()

获取应用程序的所有配置项。

mergeItems(string $key = null, mixed $items)

使用array_replace_recursive将值或值数组合并到仓库中,使用$key作为键。如果$key为null,它将合并到整个仓库中。

加载器

加载器类允许您从多个来源加载数据仓库,并将它们合并到一个公共数据结构中。抽象类FileRepositoryLoader管理一个有序的文件路径列表。当在FileRepositoryLoader的具体实现上调用load方法时,它将使用parseFile的实现来读取每个文件的正文并将它们合并在一起,返回合并内容的数组。load方法使用array_replace_recursive函数来执行此合并。

以下是一个示例,考虑了YamlFileLoader实现,该实现加载了两个架构文件

# core/schema/contact.yaml

name:
    validators:
        length:
            min: 1
            max: 200
            message: Please enter a name between 1 and 200 characters.
        required :
            message : Please specify your name.

email:
    validators:
        length:
            min: 1
            max: 150
            message: Please enter an email address between 1 and 150 characters.

        email:
            message : That does not appear to be a valid email address.

# account/schema/contact.yaml

email:
    validators:
        required:
            message: Please specify your email address.

message:
    validators:
        required:
            message: Please enter a message

为了将这两个架构文件加载并合并到一个仓库中

$loader = new \UserFrosting\Support\Repository\Loader\YamlFileLoader([
    'core/schema/contact.yaml',
    'account/schema/contact.yaml'
]);
$schema = new \UserFrosting\Support\Repository\Repository($loader->load());

路径构建器

抽象的PathBuilder类使用UniformResourceLocator的一个实例来构建一个定制的路径列表,该列表可以传递给一个Loader类。

例如,StreamPathBuilder 类接受一个 UniformResourceLocator 和一个已经注册到定位器的 流路径,当你调用 buildPaths 方法时,它会返回一个匹配路径的列表。

$builder = new \UserFrosting\Support\Repository\PathBuilder\StreamPathBuilder($this->locator, 'owls://megascops.php');
$paths = $builder->buildPaths();

// Returns a list of paths matching owls://megascops.php:
[
    '/core/owls/megascops.php',
    '/account/owls/megascops.php',
    '/admin/owls/megascops.php'
]

你可以定义其他的 PathBuilder 类来自定义构建这个路径列表的方式。只需实现 buildPaths 方法,返回一个数组,其中包含由 Loader 类按顺序加载的生成路径。

DotenvEditor

DotenvEditor 类为 UserFrosting 实现了 JackieDo Laravel-Dotenv-Editor 包。这可以用来读取和写入 .env 配置文件(或具有相同结构和语法的文件)。

有关 DotenvEditor 类的完整用法,请参阅

风格指南

测试