Laravel 8.81 发布
Laravel 团队发布了 8.81,其中包含字符串扫描方法、在 属性
访问器中禁用缓存的能力、getOrPut()
集合方法等等:
可字符串化扫描方法
Amir Rami 为 可字符串化
贡献了一个 scan 方法,使用 PHP 的 sscanf 函数实现
$this->assertSame(
[123456],
$this->stringable('SN/123456')
->scan('SN/%d')
->toArray()
);
$this->assertSame(
['Otwell', 'Taylor'],
$this->stringable('Otwell, Taylor')
->scan('%[^,],%s')
->toArray()
);
$this->assertSame(
['filename', 'jpg'],
$this->stringable('filename.jpg')
->scan('%[^.].%s')
->toArray()
);
在属性访问器中禁用缓存
Declan Norton 贡献了禁用缓存的能力在使用属性访问器时访问新的属性时. 可以通过以下几种方式使用:
public function finish(): Attribute
{
return Attribute::getWithoutCaching(
fn () => $this->start->addSeconds($this->duration)
);
}
// or
public function finish(): Attribute
{
return Attribute::get(
fn () => $this->start->addSeconds($this->duration)
)->disableObjectCaching();
}
以下是 PR 描述中对用例的解释:
我经常发现自己添加了虚拟访问器来基于模型中的其他属性(最常见的是 DateTime 实例)来构造对象。
目前,如果访问器是对象,则访问器的结果将被缓存,并且仅在调用属性的 mutator 时才无效。 标量类型不受影响。
Get or Put 集合方法
@eusonlito 为集合提供了一个 getOrPut
方法,这简化了当想要获取现有键或存放值(如果它不存在)并返回值的用例:
// 以下方式仍然可用,
if ($this->collection->has($key) === false) {
$this->collection->put($key, $this->create($data));
}
return $this->collection->get($key);
// 在 `getOrPut()` 方法中使用闭包函数
return $this->collection->getOrPut($key, fn () => $this->create($data));
// 或者传递一个固定参数
return $this->collection->getOrPut($key, $value);
发行说明
你可以看到下面的新功能和更新的完整列表以及 [8.80.0 和 8.81.0 之间的差异 8.80.0 和 8.81.0 在 GitHub. 以下发行说明直接来自 changelog:
v8.81.0
新增
- 添加了
Illuminate/Support/Stringable::scan()
(#40472) - 允许为返回对象的虚拟属性访问器禁用缓存 (#40519)
- 添加了更好的按位运算符支持 (#40529, def671d)
- 为集合添加了 getOrPut 方法 (#40535)
- 改进 PhpRedis 刷新 (#40544)
- 添加了
Illuminate/Support/Str::flushCache()
(#40620) 方法
修复的
- 修复了
Str::headline
/Str::studly
使用 unicode 并添加Str::ucsplit
方法 (#40499) - 使用 MailFake 修复了忘记邮件程序 (#40495)
- 修剪模型:从方法中获取模型的默认路径 (#40539)
- 修复 redis 集群的刷新数据库 (#40446)
- 避免未定义的数组键 0 错误 (#40571)
修改的
- 在 PostgreSQL 的 PDO dbname 中允许空格 (#40483)
- 允许 authorizeResource 方法接收模型和参数的数组 (#40516)
- 逆向可变形类型和 id 过滤语句,以防止 SQL 错误 (#40523)
- 将 voku/portable-ascii 升级到 v1.6.1 (#40588, #40610)
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: