Skip to content

Another Boilerplate for Well-designed Symfony API Projects (DDD, SOLID, Symfony5/PHP7, TDD, Docker...)

License

Notifications You must be signed in to change notification settings

gonzaloplaza/myphp-ddd-demo

Repository files navigation

MyPHP-DDD-Demo - Another Boilerplate for DDD Symfony API Projects

php 7.4 Symfony 5.2.* GitHub Repo Size Github CI codecov License MIT


This is a PHP7.4 - Symfony 5 boilerplate/example for JSON REST APIs, backend services, microservices or console/terminal applications. It's focused on simplicity, decoupling from infrastructure (Hexagonal Architecture) and Domain Driven Design.

Ideas and Feature Requests are welcomed!


Tech Stack

  • PHP-FPM 7.4
  • Symfony 5.2.*
  • Docker & Docker Compose (Alpine, NGINX, php-fpm, supervisord ...)
  • Rabbitmq/AMQP integration with Symfony Messenger component
  • Good practices: DDD (Domain Driven Design), Bounded Contexts, SOLID principles, TDD&BDD...
  • Work in progress...

Development quality/testing tools

  • PHPStan (PHP Static Analysis)

  • PHPMD (PHP Mess Detector)

  • PHP-CS-Fixer (PHP Coding Standards Fixer)

  • PHPUnit (Testing framework for PHP)

  • Behat (Behavior-Driven Development framework)

  • Codecov.io Integration (Code coverage)


Install

Install composer vendors

$ composer install

Copy .env to .env.local to override environment variables

Run development

You have to install Symfony CLI locally to run dev server (https://symfony.com/download)

$ composer dev

or

$ symfony server:start -d

Development endpoint is: http://127.0.0.1:8000

HealtCheck Endpoint is: http://127.0.0.1:8000/health_check

Run quality tools checking (Static Analysis, Mess Detector...)

$ composer phpstan
$ composer phpmd

Run tests (Behat & PHPUnit)

Note: Starting local server is not needed to run tests (including e2e api tests) due to Symfony application bootstraping in tests/src/bootstrap.php file and Mink Extension. This is fine to work with CI tools such as Github Actions or Jenkins pipelines.

$ composer tests

or separately

$ composer behat
$ composer phpunit

Working with Docker Compose for development (recommended):

You can build and run locally the application stack with Docker Compose (php-fpm app + nginx + rabbitmq). A shared volume between code and containers will be created (Check docker-compose.yaml).

$ docker-compose build
$ docker-compose up -d

You can stop the stack and remove containers:

$ docker-compose down

The application endpoint is running at: http://localhost:8081

RabbitMQ access and configuration (Docker Compose)

If you are using Docker Compose runtime during development, be sure you have created the following environment variables:

#.env file
AMPQ_TRANSPORT_DSN=amqp://user:password@myphp-ddd-demo-rabbitmq:5672/%2f/messages
DOCKER_RABBITMQ_DEFAULT_USER=user
DOCKER_RABBITMQ_DEFAULT_PASS=password

Once Rabbitmq cluster is created and running, run this command to start the async message consumer (We use Symfony Messenger implementation): https://symfony.com/doc/current/messenger.html#transports-async-queued-messages

# Start listening messages (consumer)
$ docker exec -it myphp-ddd-demo php bin/console messenger:consume async

# Stop consumer
$ docker exec -it myphp-ddd-demo php bin/console messenger:stop

Also, you can access the RabbitMQ's management UI through: http://localhost:15672 using the provided credentials.

Standalone Docker container

If you want to build the standalone application container (running php-fpm on port 9000/tcp).

Note: you will need a fast-cgi proxy like Apache or NGINX (https://symfony.com/doc/current/setup/web_server_configuration.html)

$ docker build -t myphp-ddd-demo .

$ docker run --rm -d -p 9000:9000 \
    --name myphp-ddd-demo myphp-ddd-demo

Docker, infrastructure stack configuration

In etc/ you can find default configuration files for php7.4, php-fpm, nginx, rabbitmq and supervisor. If you want to extend or customize that, you can do so by mounting a configuration file in the correct folder.

Next Steps

  • Adds DDD/Hexagonal architecture with shared bounded context
  • Adds Github CI + CI testings
  • Docker for local development (Docker Compose)
  • RabbitMQ, Symfony Messenger, DDD-integration
  • Better documentation
  • AWS CodeBuild yaml specification (buildspec)
  • Kubernetes manifests.

About

Another Boilerplate for Well-designed Symfony API Projects (DDD, SOLID, Symfony5/PHP7, TDD, Docker...)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published