bear/projection
Composer 安装命令:
composer require bear/projection
包简介
Read-optimized query library for BEAR.Resource #[Embed]
README 文档
README
Read-optimized query library for BEAR.Resource #[Embed]
Overview
BEAR.Projection implements the BDR Pattern for the Read side, providing query:// scheme resources designed for #[Embed]. A companion to Ray.MediaQuery.
Features
- Embed-first design - Optimized for
#[Embed]in ResourceObject - Parallel SQL execution - Multiple
#[Embed]queries run concurrently - DI-powered Factory - Inject dependencies for data transformation
Requirements
- PHP 8.2+
- MySQL (uses mysqli for async query execution, not PDO)
Installation
composer require bear/projection
Usage
1. Define a Projection
namespace MyApp\Projection; final class UserProfile { public function __construct( public readonly string $id, public readonly string $name, public readonly int $age, public readonly string $avatarUrl, ) { } }
2. Create a Factory with DI
Factory can inject dependencies for data transformation and validation:
namespace MyApp\Projection; final class UserProfileFactory { public function __construct( private readonly AgeCalculator $ageCalculator, private readonly ImageUrlResolver $imageResolver, ) { } public function __invoke( string $id, string $name, string $birthDate, string $avatarPath, ): UserProfile { return new UserProfile( id: $id, name: $name, age: $this->ageCalculator->fromBirthDate($birthDate), avatarUrl: $this->imageResolver->resolve($avatarPath), ); } }
3. Write the SQL Query
-- var/sql/query/user_profile.sql SELECT id, name, birth_date, avatar_path FROM users WHERE id = :id
4. Configure the Module
use BEAR\Projection\Module\ProjectionModule; class AppModule extends AbstractModule { protected function configure(): void { $this->install(new ProjectionModule( sqlDir: __DIR__ . '/../var/sql/query', projectionNamespace: 'MyApp\\Projection\\', )); } }
5. Embed in Resource
use BEAR\Resource\Annotation\Embed; use BEAR\Resource\ResourceObject; class User extends ResourceObject { #[Embed(rel: 'profile', src: 'query://self/user_profile{?id}')] #[Embed(rel: 'orders', src: 'query://self/user_orders{?id}')] public function onGet(string $id): static { // Both queries execute in parallel return $this; } }
File Structure
var/sql/query/
user_profile.sql
user_orders.sql
src/Projection/
UserProfile.php
UserProfileFactory.php
UserOrders.php
UserOrdersFactory.php
Naming Conventions
| SQL file | Factory class | URI |
|---|---|---|
user_profile.sql |
UserProfileFactory |
query://self/user_profile |
user_orders.sql |
UserOrdersFactory |
query://self/user_orders |
bear/projection 适用场景与选型建议
bear/projection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bear/projection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bear/projection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-25