https://symfonycasts.com/screencast/messenger
Sandbox for getting to know and learn Symfony Messenger component, based on https://symfonycasts.com/screencast/messenger/, but run on Docker, utilizing docker compose, with PHP 8.2 + Symfony 7.1 + nginx 1.19 + PostgreSQL 15 + Vue 3.
Sandboxes in previous Symfony versions are available at legacy/symfony<VERSION> branches - check them out if you need.
Status: WIP
- Chapter 1
- Chapter 2
- Chapter 3
- Chapter 4
- Chapter 5
- Chapter 6
- Chapter 7
- Chapter 8
- Chapter 9
- Chapter 10
- Chapter 11
- Chapter 12
- Chapter 13
- Chapter 14
- Chapter 15
- Chapter 16
- Chapter 17
- Chapter 18
- Chapter 19
- Chapter 20
- Chapter 21
- Chapter 22
- Chapter 23
- Chapter 24
- Chapter 25
- Chapter 26
- Chapter 27
- Chapter 28
- Chapter 29
- Chapter 30
- Chapter 31
- Chapter 32
- Chapter 33
- Chapter 34
- Chapter 35
- Chapter 36
- Chapter 37
- Chapter 38
- Chapter 39
- Chapter 40
- Chapter 41
- Chapter 42
- Chapter 43
- Chapter 44
- Chapter 45
- Chapter 46
- Chapter 47
- Chapter 48
By default, includes xdebug extension and PHP_CodeSniffer for easy development and basic configuration for opcache for production. Includes instruction for setting it in PhpStorm.
- https://symfony.com/
- https://www.docker.com/
- https://docs.docker.com/compose/
- https://www.php.net/
- https://www.nginx.com/
- https://www.postgresql.org/
- https://xdebug.org/
- https://github.com/squizlabs/PHP_CodeSniffer
- https://www.php.net/manual/en/intro.opcache.php
- https://www.jetbrains.com/phpstorm/
Clone and tweak it to your needs. Tested on Linux (Ubuntu 20.04):
Dockerversion 24.0.7docker composeversion 2.21.0
set up in WSL2, on Ubuntu 22.04.3 LTS (Jammy).
- Clone repository,
cdinside. - Create
.envfile indocker/phpdirectory according to your environment, one of -dev,test,prod- just copy correct template.env.dist, but remember to define your ownAPP_SECRET! - Review
compose.yamland change it according to the comments inside. - You can change PHP memory limit in
docker/php/config/docker-php-memlimit.initfile if you want.
Afterwards run:
docker compose build docker compose up
After that log into container with docker exec -it messenger.php bash, where messenger.php is the default container name from compose.yaml. Then run:
composer install npm install npm run dev php bin/console doctrine:migrations:migrate
From this point forward, application should be available under http://localhost:8050/, where port 8050 is default defined in compose.yaml.
If browser shows empty page / ERR_EMPTY_RESPONSE and containers are running, check whether your VPN/firewall blocks local Docker port-forward traffic.
Example (Mullvad): if Local network sharing is set to block, http://localhost:8050/ may fail with connection reset.
Enable local network access and retry:
mullvad lan set allow curl -i http://127.0.0.1:8050/
To mimic production environment and to follow Chapter 24 repository contains Supervisor - tool to control processes on your system.
It is used to run - constantly - Symfony's Messenger's consumer. Configuration is stored in docker/php/supervisor/messenger-worker.conf.
But since we are using dockerized environment there are a few issues with that:
- we can have (and probably should) have separate container for Supervisor - but since Messenger's
messenger:consumecommand is integral part of Symfony we would need to include another copy of whole application in that container - we can have Supervisor installed in the same container as PHP but, because of the way it starts (
CMDcommand) it blocks PHP-FPM process from starting (only oneCMDmay be used inDockerfile) (and this way you are treating Docker more like virtual machine for everything - and you should not)
For sandbox-learning purposes I have decided to go with the second approach and resolve the issue of not-starting PHP-FPM by starting it
automatically on docker compose up by using... Supervisor ;) - check docker/php/supervisor/php-fpm.conf file.
And, since it's a sandbox and I don't want to mess with application flow, it's disabled by default - but you can always uncomment following two lines in docker/php/Dockerfile:
COPY supervisor/* /etc/supervisor/conf.d/CMD ["/usr/bin/supervisord"]if you want to try it out.
Environment variable APP_ENV must be set to test to be able to run Kernel-/Web-TestCases based tests because
Real environment variables win over .env files and this is the case in docker-based environments.
All PHP extensions can be installed via docker-php-ext-install command in docker/php/Dockerfile. Examples and usage:
https://gist.github.com/giansalex/2776a4206666d940d014792ab4700d80.
Based on PhpStorm version: 2024.1.3
Open directory including cloned repository as directory in PhpStorm.
Settings->PHP->Servers: create server with namedocker(the same as in ENV variablePHP_IDE_CONFIG), hostlocalhost, port8050(default fromcompose.yaml).- Tick
Use path mappings-> setFile/Directory<->Absolute path on the serveras:</absolute/path>/app<->/var/www/app(default fromcompose.yaml). Settings->PHP: three dots next to the fieldCLI interpreter->+button ->From Docker, Vagrant(...)-> tickDocker compose, choose serverDocker, setConfiguration filesto./compose.yaml. After thatServicelist should be reloaded - pickphpfrom there. InLifecyclesection ensure to pickAlways start a new container (...), inGeneralrefresh interpreter data.
Settings->PHP->Debug->Xdebug->Debug port:9003(set by default) and checkCan accept external connections.- Click
Start Listening for PHP Debug connections->+button, set breakpoints and refresh website.
- Copy
app/phpcs.xml.distand name itphpcs.xml. Tweak it to your needs. Settings->PHP->Quality Tools->PHP_CodeSniffer->Configuration: three dots, add interpreter with+and validate paths. By default, there should be correct path mappings and paths already set to/var/www/app/vendor/bin/phpcsand/var/www/app/vendor/bin/phpcbf.Settings->Editor->Inspections->PHP->Quality tools-> tickPHP_CodeSniffer validation-> tickShow sniff name-> set coding standard toCustom-> three dots and type/var/www/app/phpcs.xml(path in container).
Open Database section on the right bar of IDE -> Data Source -> PostgreSQL -> set host to localhost, set user to app_user, pass app_pass, database to messenger_app (defaults from compose.yaml) Set url to jdbc:postgresql://localhost:5432/app.
- Copy
phpunit.xml.distintophpunit.xml. - Login into
messenger.phpcontainer wheremessenger.phpis the default container name fromcompose.yaml, and run./bin/phpunit. Settings->PHP->Test frameworks. Click+andPHPUnit by Remote Intepreter-> pick interpreter. InPHPUnit librarytickPath to phpunit.pharand typebin/phpunit. Click refresh icon. InTest runnersection setDefault configuration filetophpunit.xmlandDefault bootstrap filetotests/bootstrap.php.
Although there are present different files for prod and dev environments these are only stubs - the idea was to create as much integral, self-contained and flexible environment for development as possible and these files are here merely to easily mimic prod env and point out differences in configuration.