AMQP 및 CQRS를 사용한 이벤트 중심 슬림 4 프레임 워크 골격
기본 설치 프로필에는 예제가 없습니다. 무슨 일인지 알고 깨끗한 슬레이트로 시작하려면이 프로필을 사용해야합니다.
> composer create-project robiningelbrecht/php-slim-skeleton [app-name] --no-install --ignore-platform-reqs --stability=dev
# Build docker containers
> docker-compose up -d --build
# Install dependencies
> docker-compose run --rm php-cli composer install전체 설치 프로필에는 완전한 작업 예제가 있습니다.
> composer create-project robiningelbrecht/php-slim-skeleton:dev-master-with-examples [app-name] --no-install --ignore-platform-reqs --stability=dev
# Build docker containers
> docker-compose up -d --build
# Install dependencies
> docker-compose run --rm php-cli composer install
# Initialize example
> docker-compose run --rm php-cli composer example:init
# Start consuming the voting example queue
> docker-compose run --rm php-cli bin/console app:amqp:consume add-vote-command-queue namespace App Controller ;
class UserOverviewRequestHandler
{
public function __construct (
private readonly UserOverviewRepository $ userOverviewRepository ,
) {
}
public function handle (
ServerRequestInterface $ request ,
ResponseInterface $ response ): ResponseInterface
{
$ users = $ this -> userOverviewRepository -> findonyBy ( /*...*/ );
$ response -> getBody ()-> write ( /*...*/ );
return $ response ;
}
} config/routes.php 로 가서 요청 처리기에 대한 경로를 추가하십시오.
return function ( App $ app ) {
// Set default route strategy.
$ routeCollector = $ app -> getRouteCollector ();
$ routeCollector -> setDefaultInvocationStrategy ( new RequestResponseArgs ());
$ app -> get ( ' /user/overview ' , UserOverviewRequestHandler::class. ' :handle ' );
};전체 문서
콘솔 응용 프로그램은 Symfony 콘솔 구성 요소를 사용하여 CLI 기능을 활용합니다.
#[AsCommand(name: ' app:user:create ' )]
class CreateUserConsoleCommand extends Command
{
protected function execute ( InputInterface $ input , OutputInterface $ output ): int
{
// ...
return Command:: SUCCESS ;
}
}전체 문서
골격을 사용하면 명령 및 명령 처리기를 사용하여 작업을 수행 할 수 있습니다. 이 2는 항상 쌍으로 제공되며, 쓰기 모델에서 새 명령을 만들 때 해당 명령 핸들러도 생성해야합니다.
namespace App Domain WriteModel User CreateUser ;
class CreateUser extends DomainCommand
{
} namespace App Domain WriteModel User CreateUser ;
#[AsCommandHandler]
class CreateUserCommandHandler implements CommandHandler
{
public function __construct (
) {
}
public function handle ( DomainCommand $ command ): void
{
assert ( $ command instanceof CreateUser);
// Do stuff.
}
}전체 문서
이 프로젝트의 아이디어는 모든 것이 이벤트 중심이 될 수 있다는 것입니다. 이벤트 소싱은 기본적으로 제공되지 않습니다.
class UserWasCreated extends DomainEvent
{
public function __construct (
private UserId $ userId ,
) {
}
public function getUserId (): UserId
{
return $ this -> userId ;
}
} class User extends AggregateRoot
{
private function __construct (
private UserId $ userId ,
) {
}
public static function create (
UserId $ userId ,
): self {
$ user = new self ( $ userId );
$ user -> recordThat ( new UserWasCreated ( $ userId ));
return $ user ;
}
} class UserRepository extends DbalAggregateRootRepository
{
public function add ( User $ user ): void
{
$ this -> connection -> insert ( /*...*/ );
$ this -> publishEvents ( $ user -> getRecordedEvents ());
}
}#[AsEventListener(type: EventListenerType:: PROCESS_MANAGER )]
class UserNotificationManager extends ConventionBasedEventListener
{
public function reactToUserWasCreated ( UserWasCreated $ event ): void
{
// Send out some notifications.
}
}전체 문서
이 프로젝트에서 선택한 AMQP 구현은 RabbitMQ이지만 Amazon의 AMQP 솔루션과 같은 쉽게 쉽게 전환 할 수 있습니다.
#[AsEventListener(type: EventListenerType:: PROCESS_MANAGER )]
class UserCommandQueue extends CommandQueue
{
} class YourService
{
public function __construct (
private readonly UserCommandQueue $ userCommandQueue
) {
}
public function aMethod (): void
{
$ this -> userCommandQueue -> queue ( new CreateUser ( /*...*/ ));
}
} > docker-compose run --rm php-cli bin/console app:amqp:consume user-command-queue전체 문서
데이터베이스 마이그레이션을 관리하기 위해 교리/마이그레이션 패키지가 사용됩니다.
#[Entity]
class User extends AggregateRoot
{
private function __construct (
#[Id, Column(type: ' string ' , unique: true , nullable: false )]
private readonly UserId $ userId ,
#[Column(type: ' string ' , nullable: false )]
private readonly Name $ name ,
) {
}
// ...
}데이터베이스 스키마의 현재 상태를 ORM을 사용하여 정의 된 매핑 정보와 비교 한 다음 해당 마이그레이션을 실행하여 교리가 마이그레이션을 생성 할 수 있습니다.
> docker-compose run --rm php-cli vendor/bin/doctrine-migrations diff
> docker-compose run --rm php-cli vendor/bin/doctrine-migrations migrate전체 문서
이 프로젝트에서 선택한 템플릿 엔진은 Twig이며 HTML과 관련된 모든 것을 렌더링하는 데 사용할 수 있습니다.
< h1 >Users</ h1 >
< ul >
{% for user in users %}
< li >{{ user . username | e }}</ li >
{% endfor %}
</ ul > class UserOverviewRequestHandler
{
public function __construct (
private readonly Environment $ twig ,
) {
}
public function handle (
ServerRequestInterface $ request ,
ResponseInterface $ response ): ResponseInterface
{
$ template = $ this -> twig -> load ( ' users.html.twig ' );
$ response -> getBody ()-> write ( $ template -> render ( /*...*/ ));
return $ response ;
}
}전체 문서
이 링크에서 자세히 알아보십시오.
자세한 내용은 기여를 참조하십시오.