scafera/layered 问题修复 & 功能扩展

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

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

scafera/layered

Composer 安装命令:

composer require scafera/layered

包简介

Layered architecture conventions for the Scafera framework

README 文档

README

Scafera Layered is an opinionated architecture package for the Scafera framework. It defines and enforces a strict, layered approach for building applications.

Provides: Opinionated layered architecture for Scafera — defines six layers (Controller/, Service/, Repository/, Integration/, Entity/, Command/) with downward-only dependencies, and enforces them at build time via validators, advisors, and code generators run through scafera validate and scafera make:*.

Depends on: A Scafera host project where the kernel discovers this package via extra.scafera-architecture in composer.json. User code lives in the six layer directories under src/.

Extension points:

  • Layer directories — user code goes in src/Controller/, src/Service/, src/Repository/, src/Integration/, src/Entity/, src/Command/; each has naming/location rules enforced at build time
  • Generators — scafera make:controller, make:service, make:command scaffold a class + test with conventions baked in; support nested names (e.g. Order/Create)
  • Validators and advisors — contributed via kernel's scafera.validator and scafera.advisor tags; run by scafera validate in CI
  • Kernel primitives — user controllers use #[Route] (kernel), commands use #[AsCommand] (kernel)

Not responsible for: Capability concerns — persistence (scafera/database), presentation (scafera/frontend), authentication (scafera/auth), forms (scafera/form), logging (scafera/log), assets (scafera/asset), external integrations (scafera/integration), translations (scafera/translate), file handling (scafera/file) · implicit execution via event subscribers or listeners (blocked by ImplicitExecutionValidator) · flexible or non-layered architectures (use a different architecture package).

Philosophy

In Scafera, architecture is not a guideline — it is an installed package.

This package enforces every convention at validation time via scafera validate, designed to run in CI. Violations are caught before code ships, not after.

All execution is explicit: no event subscribers, no listeners, no auto-discovered side effects. If behavior is not visible in the code being read, it does not happen.

Architecture model

The package organizes application code into six layers:

Layer Purpose
Controller/ Single-action invokables — delegate to services, no business logic
Service/ All business logic lives here
Repository/ Data access repositories
Integration/ Third-party service wrappers (Stripe, Mailgun, external APIs)
Entity/ Domain data
Command/ CLI entry points via #[AsCommand]

Dependencies flow downward. Repository and Integration are siblings — both called by Service, neither depends on the other:

Controller → Service → Repository   → Entity
Command  ↗         ↘ Integration ↗

Validators

Twelve validators enforce the layered conventions:

Validator Rule
Tests directory Tests must be in tests/ only
Controller location Controllers must live in src/Controller/
Controller naming No Controller suffix; single-word names at root, multi-word in subfolders
Single-action controllers Must use __invoke(), no other public methods (except __construct)
Controller test parity Every controller must have a matching test
Command test parity Every command must have a matching test
Service location Only recognized directories under src/
Service final All services must be declared final
Namespace conventions PSR-4 namespace must match file path
Layer dependencies Enforces downward-only dependency flow; Integration sits alongside Repository as a sibling
Integration naming Must be in vendor subfolder; class name must not repeat vendor prefix
No implicit execution No EventSubscriberInterface or #[AsEventListener] in userland

Advisors

Non-blocking hints that never affect the exit code:

Advisor What it checks
Test sync Warns when a controller or command is modified in git but its test is not

The test sync advisor requires git and gracefully skips with a reason when prerequisites are not met.

Generators

Scaffold new files with conventions baked in:

Generator Command What it creates
Controller scafera make:controller <Name> Single-action controller + test
Service scafera make:service <Name> Final service class + test
Command scafera make:command <Name> Console command + test

All generators support nested names (e.g. Order/Create, Report/Generate) and reject convention-violating suffixes like Controller or Command.

Project structure

src/
├── Controller/    ← single-action, attribute routing
│   ├── Home.php
│   └── Order/
│       ├── Create.php
│       └── List.php
├── Command/       ← #[AsCommand], delegate to services
├── Service/       ← all business logic
├── Repository/    ← data access
├── Integration/   ← third-party service wrappers
└── Entity/        ← domain data
tests/
├── Controller/    ← one test per controller
├── Command/       ← one test per command
└── Service/       ← unit tests where needed

When to use

Use this package when you want:

  • Strong conventions and predictable structure
  • Clear separation of responsibilities
  • Automated enforcement of architectural rules

Avoid this package if you require a flexible or custom architecture with minimal constraints.

Installation

composer require scafera/layered

The kernel discovers it automatically via the scafera-architecture Composer extra field.

Requirements

  • PHP >= 8.4
  • scafera/kernel

License

MIT

scafera/layered 适用场景与选型建议

scafera/layered 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 40
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-13