glhd/special 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

glhd/special

Composer 安装命令:

composer require glhd/special

包简介

关键字:

README 文档

README

Build Status Latest Stable Release MIT Licensed Follow @cmorrell.com on bsky

Special✨

Sometimes, certain database records are just special✨, and you need to reference them inside your code.

You might have a few special vendors that have special handling in a few special places, and maybe their own special artisan commands run from time-to-time.

special✨ lets you use backed enums to reference Eloquent models. Rather than backing your enum with a string or integer, think of it as backing your enum with a database record.

If the record is missing, you can let special✨ automatically create it for you. This is especially great in testing, where you may have a few special records that need to exist for a few special tests, but you don't want to track which tests need to run special seeders at setup.

Installation

composer require glhd/special

Usage

To start, create a new enum and use the EloquentBacking trait provided by this package.

You can optionally add a CreateWith attribute to any of your enum cases, and special✨ will use those values to automatically create the model record for you if it's missing.

use Glhd\Special\EloquentBacking;

enum SpecialOrganizations: string
{
	use EloquentBacking;
	
	#[CreateWith(['name' => 'Laravel', 'url' => 'https://laravel.com/'])]
	case Laravel = 'laravel';
	
	#[CreateWith(['name' => 'Spatie', 'url' => 'https://spatie.be/'])]
	case Spatie = 'spatie';
	
	#[CreateWith(['name' => 'Thunk', 'url' => 'http://thunk.dev/'])]
	case Thunk = 'kathunk';
	
	// If your enum name is the same as the model name, this is optional.
	public static function modelClass(): string
	{
		return Organization::class;
	}
}

Now, you can use those enums to access the backing models. By default, strings are assumed to be a slug column, and integers are assumed to be the id column, but this can be configured at the project level or the individual enum level.

SpecialOrganizations::Laravel->toArray();

// [
//   'id' => 1337,
//   'slug' => 'laravel',
//   'name' => 'Laravel',
//   'url' => 'https://laravel.com/',
//   'created_at' => [...],
//   'updated_at' => [...],
// ]

Special enums decorate the underlying model, so you can often just call the enum as though it were the model itself. But sometimes you want the actual copy of the model instance, which you can do with:

// Get a copy of the model — only loads from DB once, but clones each time
SpecialOrganizations::Laravel->get();

// Get a single, shared copy — same instance each time
SpecialOrganizations::Laravel->singleton();

// Get a fresh copy — always loads from the DB
SpecialOrganizations::Laravel->fresh();

Using the primary key cache

Often, the only reason you need a special enum is to use its primary key in another query or to set up a relationship. Special✨ keeps a cache of the 50 most recently-used primary keys so that in many cases, you don't have to do a single database lookup. You can configure the number of keys cached and the cache TTL by publishing the package config.

PullRequest::create([
    'organization_id' => SpecialOrganizations::Laravel->getKey(),
    'ref_number' => 47785,
    'title' => '[10.x] Add Collection::enforce() method',
]);

As long as SpecialOrganizations::Laravel has been used in the last hour, the 'organization_id' value can be set without making a single query to the database.

Due to the nature of these kinds of enums, this is usually pretty safe, since they're used with the types of records that aren't likely to change in your application ever. That said, you can always clear the cache at any time with php artisan cache:clear-special-keys.

Using with Laravel relations

Often times you want to use a special enum to look up related models. We provide a few convenient ways to do this:

PullRequests::query()
  ->forSpecial(SpecialOrganizations::Laravel)
  ->dumpRawSql();

// select *
// from `pull_requests`
// where `organization_id` = 1337

Or, you can use a special enum to constrain an existing query. The exact same query can be generated with:

SpecialOrganizations::Laravel
  ->constrain(PullRequests::query())
  ->dumpRawSql();

The constrain() method (and forSpecial macro) both use the primary key cache under the hood. This means that most relational queries using special enums will not trigger any additional database queries.

Automatic Model Observation

Special✨ automatically registers model observers for any model that you retrieve. This means that if you update or delete a special model during a request, subsequent calls to the enum will automatically reflect those changes.

This works regardless of whether the updated model was retrieved using the package.

// Get a copy of the Laravel organization, which causes it to be
// cached for the rest of the request.
$laravel = SpecialOrganizations::Laravel->singleton();
assert($laravel->name === 'Laravel');

// Now we'll update it without using our enum
$org = Organizations::where('slug', 'laravel')->first();
$org->update(['name' => 'Laravel LLC']);

// Later calls to the enum will reflect the changes
$laravel = SpecialOrganizations::Laravel->singleton();
assert($laravel->name === 'Laravel LLC');

glhd/special 适用场景与选型建议

glhd/special 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.54k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2023 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 glhd/special 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 glhd/special 我们能提供哪些服务?
定制开发 / 二次开发

基于 glhd/special 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 33.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 20
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-11