Examples

小さなコードに、設計の境界を残す。

BEAR.Sundayのコードは、リソースが中心です。入力、依存、リンク、横断的処理が見える場所に残るため、読み手はアプリケーションの意味をたどれます。

Resource

URIに対応するリソースは、状態を決めて返す。

final class Profile extends ResourceObject
{
    public function onGet(int $id): static
    {
        $this->body = $this->profileQuery->item($id);

        return $this;
    }
}

Dependency Injection

必要な依存はコンストラクタで明示する。

public function __construct(
    private readonly ProfileQuery $profileQuery,
    private readonly ClockInterface $clock,
) {
}

AOP

横断的関心は属性とInterceptorへ逃がす。

#[Transactional]
#[Cacheable(expirySecond: 30)]
public function onPost(string $name): static
{
    $this->body = $this->command->create($name);

    return $this;
}

Hypermedia

次に遷移できるリソースをリンクとして宣言する。

#[Link(rel: 'orders', href: 'app://self/orders{?id}')]
#[Embed(rel: 'profile', src: 'app://self/profile{?id}')]
public function onGet(int $id): static
{
    return $this;
}

What this enables

コード例は、使い方ではなく境界を示す。

リソースが状態を持ち、表現や転送は外側へ分かれる。依存は注入され、 横断的関心はAOPへ分かれる。この単純な分離が、テストと拡張の土台になります。

  • リソース単位でテストを書ける
  • 同じリソースをWeb API、HTML、CLIで使える
  • キャッシュやトランザクションを業務ロジックから分離できる
  • リンクとスキーマからAPIドキュメントを組み立てやすい

Start with one resource

まずは小さく作り、構造を残す。