Kerangka kerangka kerja Slim 4 yang digerakkan oleh acara menggunakan AMQP dan CQRS
Profil instalasi default tidak memiliki contoh. Anda harus menggunakan profil ini jika Anda tahu apa yang terjadi dan ingin memulai dengan batu tulis yang bersih.
> 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 installProfil instalasi lengkap memiliki contoh kerja yang lengkap.
> 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 ;
}
} Pergilah ke config/routes.php dan tambahkan rute untuk request handler Anda:
return function ( App $ app ) {
// Set default route strategy.
$ routeCollector = $ app -> getRouteCollector ();
$ routeCollector -> setDefaultInvocationStrategy ( new RequestResponseArgs ());
$ app -> get ( ' /user/overview ' , UserOverviewRequestHandler::class. ' :handle ' );
};Dokumentasi lengkap
Aplikasi konsol menggunakan komponen konsol Symfony untuk memanfaatkan fungsionalitas CLI.
#[AsCommand(name: ' app:user:create ' )]
class CreateUserConsoleCommand extends Command
{
protected function execute ( InputInterface $ input , OutputInterface $ output ): int
{
// ...
return Command:: SUCCESS ;
}
}Dokumentasi lengkap
Kerangka memungkinkan Anda menggunakan perintah dan penangan perintah untuk melakukan tindakan. 2 ini selalu datang berpasangan, saat membuat perintah baru dalam model tulis, pawang perintah yang sesuai juga harus dibuat.
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.
}
}Dokumentasi lengkap
Gagasan proyek ini adalah bahwa semuanya, atau bisa, didorong oleh peristiwa. Sumber Acara tidak disediakan secara default.
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.
}
}Dokumentasi lengkap
Implementasi AMQP yang dipilih untuk proyek ini adalah RabbitMQ, tetapi dapat dengan mudah dialihkan ke misalnya solusi AMQP Amazon.
#[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-queueDokumentasi lengkap
Untuk mengelola migrasi basis data, paket doktrin/migrasi digunakan.
#[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 ,
) {
}
// ...
}Anda dapat memiliki doktrin menghasilkan migrasi untuk Anda dengan membandingkan keadaan saat ini dari skema basis data Anda dengan informasi pemetaan yang ditentukan dengan menggunakan ORM dan kemudian menjalankan migrasi itu.
> docker-compose run --rm php-cli vendor/bin/doctrine-migrations diff
> docker-compose run --rm php-cli vendor/bin/doctrine-migrations migrateDokumentasi lengkap
Mesin template pilihan untuk proyek ini adalah ranting dan dapat digunakan untuk membuat apa pun yang terkait dengan 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 ;
}
}Dokumentasi lengkap
Pelajari lebih lanjut di tautan ini:
Silakan lihat berkontribusi untuk detailnya.