php-symfony
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSymfony Framework Skill
Symfony框架技能
Atomic skill for mastering enterprise-grade Symfony applications
掌握企业级Symfony应用的原子技能
Overview
概述
Comprehensive skill for building robust Symfony applications. Covers Symfony 6.4 LTS and 7.x with focus on Doctrine ORM, dependency injection, and API Platform.
全面掌握构建稳健Symfony应用的技能。涵盖Symfony 6.4 LTS和7.x版本,重点讲解Doctrine ORM、依赖注入和API Platform。
Skill Parameters
技能参数
Input Validation
输入验证
typescript
interface SkillParams {
topic:
| "doctrine" // ORM, DBAL, migrations
| "di-container" // Services, autowiring, tags
| "routing" // Routes, controllers
| "security" // Voters, firewalls
| "messenger" // Async messaging
| "api-platform" // REST/GraphQL APIs
| "forms"; // Form types, validation
level: "beginner" | "intermediate" | "advanced";
symfony_version?: "6.4" | "7.0" | "7.1" | "7.2";
project_type?: "webapp" | "api" | "microservice";
}typescript
interface SkillParams {
topic:
| "doctrine" // ORM, DBAL, migrations
| "di-container" // Services, autowiring, tags
| "routing" // Routes, controllers
| "security" // Voters, firewalls
| "messenger" // Async messaging
| "api-platform" // REST/GraphQL APIs
| "forms"; // Form types, validation
level: "beginner" | "intermediate" | "advanced";
symfony_version?: "6.4" | "7.0" | "7.1" | "7.2";
project_type?: "webapp" | "api" | "microservice";
}Validation Rules
验证规则
yaml
validation:
topic:
required: true
allowed: [doctrine, di-container, routing, security, messenger, api-platform, forms]
level:
required: true
symfony_version:
default: "7.1"yaml
validation:
topic:
required: true
allowed: [doctrine, di-container, routing, security, messenger, api-platform, forms]
level:
required: true
symfony_version:
default: "7.1"Learning Modules
学习模块
Module 1: Doctrine ORM
模块1:Doctrine ORM
yaml
beginner:
- Entity creation with attributes
- Basic relationships
- Repository basics
intermediate:
- DQL and QueryBuilder
- Lifecycle events
- Custom repository methods
advanced:
- Inheritance mapping
- Second-level cache
- Event subscribersyaml
beginner:
- Entity creation with attributes
- Basic relationships
- Repository basics
intermediate:
- DQL and QueryBuilder
- Lifecycle events
- Custom repository methods
advanced:
- Inheritance mapping
- Second-level cache
- Event subscribersModule 2: Dependency Injection
模块2:依赖注入
yaml
beginner:
- Service basics
- Autowiring
- Constructor injection
intermediate:
- Service tags
- Factory services
- Decorators
advanced:
- Compiler passes
- Service subscribers
- Lazy servicesyaml
beginner:
- Service basics
- Autowiring
- Constructor injection
intermediate:
- Service tags
- Factory services
- Decorators
advanced:
- Compiler passes
- Service subscribers
- Lazy servicesModule 3: Messenger Component
模块3:Messenger组件
yaml
beginner:
- Messages and handlers
- Sync vs async transports
intermediate:
- Message middleware
- Retry strategies
- Multiple transports
advanced:
- Custom transports
- Saga patternsyaml
beginner:
- Messages and handlers
- Sync vs async transports
intermediate:
- Message middleware
- Retry strategies
- Multiple transports
advanced:
- Custom transports
- Saga patternsExecution Flow
执行流程
mermaid
graph TD
A[Skill Invoked] --> B{Validate}
B -->|Invalid| C[Return Error]
B -->|Valid| D[Load Content]
D --> E{Topic Router}
E --> F[Generate Examples]
F --> G[Add Console Commands]mermaid
graph TD
A[Skill Invoked] --> B{Validate}
B -->|Invalid| C[Return Error]
B -->|Valid| D[Load Content]
D --> E{Topic Router}
E --> F[Generate Examples]
F --> G[Add Console Commands]Error Handling & Retry Logic
错误处理与重试逻辑
yaml
errors:
CONTAINER_ERROR:
code: "SYMFONY_001"
recovery: "Run cache:clear, check services.yaml"
DOCTRINE_ERROR:
code: "SYMFONY_002"
recovery: "Run doctrine:schema:validate"
retry:
max_attempts: 3
backoff:
type: exponential
initial_delay_ms: 100yaml
errors:
CONTAINER_ERROR:
code: "SYMFONY_001"
recovery: "Run cache:clear, check services.yaml"
DOCTRINE_ERROR:
code: "SYMFONY_002"
recovery: "Run doctrine:schema:validate"
retry:
max_attempts: 3
backoff:
type: exponential
initial_delay_ms: 100Code Examples
代码示例
Doctrine Entity
Doctrine实体
php
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private string $name;
#[ORM\ManyToOne(inversedBy: 'products')]
#[ORM\JoinColumn(nullable: false)]
private Category $category;
#[ORM\Column]
private \DateTimeImmutable $createdAt;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
}
}php
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private string $name;
#[ORM\ManyToOne(inversedBy: 'products')]
#[ORM\JoinColumn(nullable: false)]
private Category $category;
#[ORM\Column]
private \DateTimeImmutable $createdAt;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
}
}Service with DI
依赖注入服务
php
<?php
declare(strict_types=1);
namespace App\Service;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final readonly class ProductService
{
public function __construct(
private ProductRepository $repository,
private EntityManagerInterface $entityManager,
private LoggerInterface $logger,
#[Autowire('%app.tax_rate%')]
private float $taxRate,
) {}
public function createProduct(array $data): Product
{
$product = new Product();
$product->setName($data['name']);
$this->entityManager->persist($product);
$this->entityManager->flush();
$this->logger->info('Product created', ['id' => $product->getId()]);
return $product;
}
}php
<?php
declare(strict_types=1);
namespace App\Service;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final readonly class ProductService
{
public function __construct(
private ProductRepository $repository,
private EntityManagerInterface $entityManager,
private LoggerInterface $logger,
#[Autowire('%app.tax_rate%')]
private float $taxRate,
) {}
public function createProduct(array $data): Product
{
$product = new Product();
$product->setName($data['name']);
$this->entityManager->persist($product);
$this->entityManager->flush();
$this->logger->info('Product created', ['id' => $product->getId()]);
return $product;
}
}Messenger Handler
Messenger消息处理器
php
<?php
declare(strict_types=1);
namespace App\MessageHandler;
use App\Message\SendOrderConfirmation;
use App\Service\EmailService;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
final readonly class SendOrderConfirmationHandler
{
public function __construct(
private EmailService $emailService,
) {}
public function __invoke(SendOrderConfirmation $message): void
{
$this->emailService->sendOrderConfirmation($message->orderId);
}
}php
<?php
declare(strict_types=1);
namespace App\MessageHandler;
use App\Message\SendOrderConfirmation;
use App\Service\EmailService;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
final readonly class SendOrderConfirmationHandler
{
public function __construct(
private EmailService $emailService,
) {}
public function __invoke(SendOrderConfirmation $message): void
{
$this->emailService->sendOrderConfirmation($message->orderId);
}
}Test Templates
测试模板
php
<?php
declare(strict_types=1);
namespace App\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class ProductControllerTest extends WebTestCase
{
public function testListProducts(): void
{
$client = static::createClient();
$client->request('GET', '/api/products');
$this->assertResponseIsSuccessful();
}
}php
<?php
declare(strict_types=1);
namespace App\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class ProductControllerTest extends WebTestCase
{
public function testListProducts(): void
{
$client = static::createClient();
$client->request('GET', '/api/products');
$this->assertResponseIsSuccessful();
}
}Troubleshooting
故障排除
| Problem | Solution |
|---|---|
| Service not found | Run |
| Doctrine mapping error | Run |
| Route not found | Run |
| 问题 | 解决方案 |
|---|---|
| 服务未找到 | 执行 |
| Doctrine映射错误 | 执行 |
| 路由未找到 | 执行 |
Quality Metrics
质量指标
| Metric | Target |
|---|---|
| Code accuracy | 100% |
| Symfony conventions | 100% |
| DI best practices | 100% |
| 指标 | 目标值 |
|---|---|
| 代码准确性 | 100% |
| Symfony规范符合性 | 100% |
| 依赖注入最佳实践 | 100% |
Usage
使用方法
Skill("php-symfony", {topic: "messenger", level: "advanced"})Skill("php-symfony", {topic: "messenger", level: "advanced"})