From f5598535e4edb422e687c328a9c04d14eeef593a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 13 Nov 2023 18:15:11 +0100 Subject: [PATCH] Mount the application in `/var/www` instead of `/home/app` --- .castor/qa.php | 10 ++-- .castor/utils.php | 1 - .gitignore | 1 - CHANGELOG.md | 1 + README.md | 47 +++++++++++++++---- castor.php | 3 +- .../docker/docker-compose.builder.yml | 3 +- .../docker/docker-compose.worker.yml | 4 +- infrastructure/docker/docker-compose.yml | 2 +- infrastructure/docker/services/php/Dockerfile | 6 ++- .../php/frontend/etc/nginx/nginx.conf | 2 +- phpstan.neon | 3 +- tools/phpstan/.gitignore | 1 + 13 files changed, 57 insertions(+), 27 deletions(-) diff --git a/.castor/qa.php b/.castor/qa.php index 5905df6..3380dbe 100644 --- a/.castor/qa.php +++ b/.castor/qa.php @@ -17,22 +17,22 @@ function all(): int #[AsTask(description: 'Installs tooling')] function install(): void { - docker_compose_run('composer install -o', workDir: '/home/app/root/tools/php-cs-fixer'); - docker_compose_run('composer install -o', workDir: '/home/app/root/tools/phpstan'); + docker_compose_run('composer install -o', workDir: '/var/www/tools/php-cs-fixer'); + docker_compose_run('composer install -o', workDir: '/var/www/tools/phpstan'); } #[AsTask(description: 'Runs PHPStan', aliases: ['phpstan'])] function phpstan(): int { - return docker_exit_code('phpstan --configuration=/home/app/root/phpstan.neon', workDir: '/home/app/application'); + return docker_exit_code('phpstan', workDir: '/var/www'); } #[AsTask(description: 'Fixes Coding Style', aliases: ['cs'])] function cs(bool $dryRun = false): int { if ($dryRun) { - return docker_exit_code('php-cs-fixer fix --dry-run --diff', workDir: '/home/app/root'); + return docker_exit_code('php-cs-fixer fix --dry-run --diff', workDir: '/var/www'); } - return docker_exit_code('php-cs-fixer fix', workDir: '/home/app/root'); + return docker_exit_code('php-cs-fixer fix', workDir: '/var/www'); } diff --git a/.castor/utils.php b/.castor/utils.php index 36c5bdf..b5b4005 100644 --- a/.castor/utils.php +++ b/.castor/utils.php @@ -172,7 +172,6 @@ function docker_compose(array $subCommand, Context $c = null, bool $withBuilder ->withTimeout(null) ->withEnvironment([ 'PROJECT_NAME' => variable('project_name'), - 'PROJECT_DIRECTORY' => variable('project_directory'), 'PROJECT_ROOT_DOMAIN' => variable('root_domain'), 'PROJECT_DOMAINS' => $domains, 'USER_ID' => variable('user_id'), diff --git a/.gitignore b/.gitignore index a9acfd7..27dfc25 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ # Tools .php-cs-fixer.cache -/tools/phpstan/var diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1e640..0b4040a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Add some PHP tooling (PHP-CS-Fixer, PHPStan) * Add `castor init` command to initialize a new project * Add `castor symfony` to install a Symfony application +* Mount the application in `/var/www` instead of `/home/app` ## 3.11.0 (2023-05-30) diff --git a/README.md b/README.md index 705e99c..05e974d 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,6 @@ domain where the application will be available; * `extra_domains` (optional): This contains extra domains where the application will be available; -* `project_directory` (optional, default: `application`): This is the host -directory containing your PHP application; - * `php_version` (optional, default: `8.2`): This is PHP version. For example: @@ -53,7 +50,6 @@ function create_default_parameters(): Context "admin.{$projectName}.{$tld}", "api.{$projectName}.{$tld}", ], - 'project_directory' => 'application', 'php_version' => 8.2, ]; ) @@ -127,6 +123,39 @@ Since `tools/bin` path is appended to the `$PATH`, tools will be available globa +### How to change the layout of the project + +
+ +Read the cookbook + +If you want to rename the `application` directory, or even move its content to +the root directory, you have to edit each reference to it. Theses references +represent each application entry point, whether it be over HTTP or CLI. +Usually, there is three places where you need to do it: + +* In Nginx configuration file: + `infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf`. You need + to update `http.server.root` option to the new path. For example: + ```diff + - root /var/www/application/public; + + root /var/www/public; + ``` +* In all workers configuration file: + `infrastructure/docker/docker-compose.worker.yml`: + ```diff + - command: php -d memory_limit=1G /var/www/application/bin/console messenger:consume async --memory-limit=128M + + command: php -d memory_limit=1G /var/www/bin/console messenger:consume async --memory-limit=128M + ``` +* In the builder, to land in the right directory directly: + `infrastructure/docker/services/php/Dockerfile`: + ```diff + - WORKDIR /var/www/application + + WORKDIR /var/www + ``` + +
+ ### How to use with Webpack Encore
@@ -153,7 +182,7 @@ If you want to use Webpack Encore in a Symfony project, context: services/php target: builder volumes: - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" + - "../..:/var/www:cached" command: "yarn run dev-server --hot --host 0.0.0.0 --allowed-hosts encore.${PROJECT_ROOT_DOMAIN} --allowed-hosts ${PROJECT_ROOT_DOMAIN} --client-web-socket-url-hostname encore.${PROJECT_ROOT_DOMAIN} --client-web-socket-url-port 443 --client-web-socket-url-protocol wss" labels: - "traefik.enable=true" @@ -679,7 +708,7 @@ services: context: services/php target: cron volumes: - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" + - "../..:/var/www:cached" ```
@@ -696,7 +725,7 @@ In order to set up workers, you should define their services in the `docker-comp services: worker_my_worker: <<: *worker_base - command: /home/app/application/my-worker + command: /var/www/application/my-worker worker_date: <<: *worker_base @@ -813,7 +842,7 @@ index 2eda814..59f8fed 100644 + - mysql #- rabbitmq volumes: - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" + - "../..:/var/www:cached" diff --git a/infrastructure/docker/docker-compose.yml b/infrastructure/docker/docker-compose.yml index 49a2661..1804a01 100644 --- a/infrastructure/docker/docker-compose.yml @@ -834,7 +863,7 @@ index 49a2661..1804a01 100644 - - postgres + - mysql volumes: - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" + - "../..:/var/www:cached" labels: @@ -24,10 +24,7 @@ services: # Comment the next line to be able to access frontend via HTTP instead of HTTPS diff --git a/castor.php b/castor.php index 9f0cc9f..d0235cf 100644 --- a/castor.php +++ b/castor.php @@ -23,7 +23,6 @@ function create_default_variables(): array 'extra_domains' => [ "www.{$projectName}.{$tld}", ], - 'project_directory' => 'application', 'php_version' => $_SERVER['DS_PHP_VERSION'] ?? '8.2', ]; } @@ -49,7 +48,7 @@ function start(): void #[AsTask(description: 'Installs the application (composer, yarn, ...)', namespace: 'app', aliases: ['install'])] function install(): void { - $basePath = sprintf('%s/%s', variable('root_dir'), variable('project_directory')); + $basePath = sprintf('%s/application', variable('root_dir')); if (is_file("{$basePath}/composer.json")) { docker_compose_run('composer install -n --prefer-dist --optimize-autoloader'); diff --git a/infrastructure/docker/docker-compose.builder.yml b/infrastructure/docker/docker-compose.builder.yml index b79f0cf..f2b4807 100644 --- a/infrastructure/docker/docker-compose.builder.yml +++ b/infrastructure/docker/docker-compose.builder.yml @@ -22,5 +22,4 @@ services: volumes: - "builder-data:/home/app" - "${COMPOSER_CACHE_DIR}:/home/app/.composer/cache" - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" - - "../..:/home/app/root:cached" + - "../..:/var/www:cached" diff --git a/infrastructure/docker/docker-compose.worker.yml b/infrastructure/docker/docker-compose.worker.yml index f03de48..315f713 100644 --- a/infrastructure/docker/docker-compose.worker.yml +++ b/infrastructure/docker/docker-compose.worker.yml @@ -10,11 +10,11 @@ x-services-templates: - postgres #- rabbitmq volumes: - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" + - "../..:/var/www:cached" labels: - "docker-starter.worker.${PROJECT_NAME}" # services: # worker_messenger: # <<: *worker_base -# command: php -d memory_limit=1G /home/app/application/bin/console messenger:consume async --memory-limit=128M +# command: php -d memory_limit=1G /var/www/application/bin/console messenger:consume async --memory-limit=128M diff --git a/infrastructure/docker/docker-compose.yml b/infrastructure/docker/docker-compose.yml index 1e92c13..230a21a 100644 --- a/infrastructure/docker/docker-compose.yml +++ b/infrastructure/docker/docker-compose.yml @@ -18,7 +18,7 @@ services: depends_on: - postgres volumes: - - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" + - "../..:/var/www:cached" environment: - "PHP_VERSION=${PHP_VERSION}" labels: diff --git a/infrastructure/docker/services/php/Dockerfile b/infrastructure/docker/services/php/Dockerfile index e97d1b0..7228c50 100644 --- a/infrastructure/docker/services/php/Dockerfile +++ b/infrastructure/docker/services/php/Dockerfile @@ -51,7 +51,7 @@ RUN addgroup --gid $USER_ID app && \ # Configuration COPY base/php-configuration /etc/php/${PHP_VERSION} -WORKDIR /home/app/application +WORKDIR /var/www ENTRYPOINT [ "/entrypoint" ] FROM php-base as frontend @@ -115,4 +115,6 @@ RUN mkdir -p "/home/app/.composer/cache" \ && chown app: /home/app/.composer -R # Third party tools -ENV PATH="$PATH:/home/app/root/tools/bin" +ENV PATH="$PATH:/var/www/tools/bin" + +WORKDIR /var/www/application diff --git a/infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf b/infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf index 9bba62c..2d69c60 100644 --- a/infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf +++ b/infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf @@ -27,7 +27,7 @@ http { server { listen 0.0.0.0:80; - root /home/app/application/public; + root /var/www/application/public; location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg)$ { access_log off; diff --git a/phpstan.neon b/phpstan.neon index e819040..d6f2dd0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,7 @@ parameters: level: 6 paths: #- application/src + - application/public - castor.php - .castor/ scanFiles: @@ -10,7 +11,7 @@ parameters: inferPrivatePropertyTypeFromConstructor: true # symfony: - # container_xml_path: '%rootDir%/../../../../../application/var/cache/dev/App_KernelDevDebugContainer.xml' + # container_xml_path: application/var/cache/dev/App_KernelDevDebugContainer.xml' typeAliases: ContextData: ''' diff --git a/tools/phpstan/.gitignore b/tools/phpstan/.gitignore index 57872d0..31b30cc 100644 --- a/tools/phpstan/.gitignore +++ b/tools/phpstan/.gitignore @@ -1 +1,2 @@ +/var/ /vendor/