php slim skeleton
1.0.0
使用AMQP和CQRS的事件驱动的Slim 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 ,并为您的请求handler添加路由:
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完整的文档
该项目选择的模板引擎是树枝,可用于渲染与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 ;
}
}完整的文档
在这些链接上了解更多信息:
请有关详细信息,请参阅贡献。