clean-arch-knowledge
9
总安装量
9
周安装量
#32220
全站排名
安装命令
npx skills add https://github.com/dykyi-roman/awesome-claude-code --skill clean-arch-knowledge
Agent 安装分布
opencode
9
claude-code
7
github-copilot
7
codex
7
gemini-cli
7
cursor
7
Skill 文档
Clean Architecture Knowledge Base
Quick reference for Clean Architecture / Hexagonal Architecture patterns and PHP implementation guidelines.
Core Principles
The Dependency Rule
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â FRAMEWORKS & DRIVERS â
â (Web, UI, DB, External Services, Devices) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â INTERFACE ADAPTERS â
â (Controllers, Gateways, Presenters, Repositories) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â APPLICATION BUSINESS RULES â
â (Use Cases, Application Services) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â ENTERPRISE BUSINESS RULES â
â (Entities, Value Objects, Domain Services) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â²
â
Dependencies point INWARD only
Rule: Source code dependencies must point INWARD. Inner layers know nothing about outer layers.
Hexagonal Architecture (Ports & Adapters)
âââââââââââââââââââ
â Primary â
â Adapters â
â (Controllers) â
ââââââââââ¬âââââââââ
â
â¼
âââââââââââââââââââ
ââââââââââââºâ PORTS âââââââââââââ
â â (Interfaces) â â
â ââââââââââ¬âââââââââ â
â â â
â â¼ â
â âââââââââââââââââââ â
â â APPLICATION â â
â â (Use Cases) â â
â ââââââââââ¬âââââââââ â
â â â
â â¼ â
â âââââââââââââââââââ â
â â DOMAIN â â
â â (Entities) â â
â âââââââââââââââââââ â
â â
â âââââââââââââââââââ â
âââââââââââââ Secondary âââââââââââââ
â Adapters â
â (Repositories, â
â External APIs) â
âââââââââââââââââââ
Rule: Application core defines Ports (interfaces). Adapters implement them.
Quick Checklists
Domain Layer Checklist
- No imports from outer layers
- No framework dependencies
- Pure business logic
- Value Objects for concepts
- Entities with behavior
- Repository interfaces only
Application Layer Checklist
- Use Cases orchestrate domain
- Defines Ports (interfaces) for external services
- DTOs for input/output
- No infrastructure details
- No framework dependencies
Interface Adapters Checklist
- Implements domain/application interfaces
- Controllers call Use Cases
- Presenters format output
- No business logic
Frameworks & Drivers Checklist
- Configuration only
- Wiring/DI setup
- Framework-specific code isolated
Common Violations Quick Reference
| Violation | Where to Look | Severity |
|---|---|---|
| Inner layer imports outer | Domain/Application importing Infrastructure | Critical |
| Framework in core | Doctrine/Symfony in Domain | Critical |
| Use Case with HTTP details | Request/Response in Application | Critical |
| Business logic in Controller | if/switch on domain state | Warning |
| Missing Port | Direct external service call | Warning |
| Adapter with logic | Repository doing validation | Warning |
PHP 8.4 Clean Architecture Patterns
Port (Driven Port)
// Application layer - defines the contract
namespace Application\Order\Port;
interface PaymentGatewayInterface
{
public function charge(PaymentRequest $request): PaymentResponse;
public function refund(string $transactionId, Money $amount): RefundResponse;
}
Adapter (Driven Adapter)
// Infrastructure layer - implements the contract
namespace Infrastructure\Payment;
final readonly class StripePaymentGateway implements PaymentGatewayInterface
{
public function __construct(
private StripeClient $stripe
) {}
public function charge(PaymentRequest $request): PaymentResponse
{
$charge = $this->stripe->charges->create([
'amount' => $request->amount->cents(),
'currency' => $request->currency->value,
'source' => $request->token,
]);
return new PaymentResponse(
transactionId: $charge->id,
status: PaymentStatus::from($charge->status)
);
}
}
Use Case (Application Service)
namespace Application\Order\UseCase;
final readonly class ProcessPaymentUseCase
{
public function __construct(
private OrderRepositoryInterface $orders,
private PaymentGatewayInterface $paymentGateway, // Port
private EventDispatcherInterface $events
) {}
public function execute(ProcessPaymentCommand $command): PaymentResult
{
$order = $this->orders->findById($command->orderId);
$payment = $this->paymentGateway->charge(
new PaymentRequest($order->total(), $command->paymentToken)
);
if ($payment->isSuccessful()) {
$order->markAsPaid($payment->transactionId());
$this->orders->save($order);
}
return new PaymentResult($payment->transactionId(), $payment->status());
}
}
Controller (Driving Adapter)
namespace Presentation\Api\Order;
final readonly class PaymentController
{
public function __construct(
private ProcessPaymentUseCase $processPayment
) {}
public function process(Request $request): JsonResponse
{
$command = new ProcessPaymentCommand(
orderId: new OrderId($request->get('order_id')),
paymentToken: $request->get('payment_token')
);
$result = $this->processPayment->execute($command);
return new JsonResponse([
'transaction_id' => $result->transactionId,
'status' => $result->status->value,
]);
}
}
References
For detailed information, load these reference files:
references/dependency-rule.mdâ The Dependency Rule explainedreferences/layer-boundaries.mdâ Layer responsibilities and boundariesreferences/port-adapter-patterns.mdâ Hexagonal Architecture patternsreferences/antipatterns.mdâ Common violations with detection patterns