userfrosting / support
UserFrosting和UF模块的支持类
Requires
- php: >=7.1
- illuminate/config: ^5.8
- jackiedo/dotenv-editor: ~1.0.7
- symfony/yaml: >=2.1
- userfrosting/uniformresourcelocator: ~4.2.1 | ~4.3.0 | ~4.4.0 | ~4.5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- phpunit/phpunit: ^7.5 | ^8.5
README
分支 | 构建 | 覆盖率 | 风格 |
---|---|---|---|
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的config
、translator
和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 配置文件(或具有相同结构和语法的文件)。