Skip to content

Commit

Permalink
Mount the application in /var/www instead of /home/app
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Nov 13, 2023
1 parent 276fe00 commit f559853
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 27 deletions.
10 changes: 5 additions & 5 deletions .castor/qa.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
1 change: 0 additions & 1 deletion .castor/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

# Tools
.php-cs-fixer.cache
/tools/phpstan/var
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
47 changes: 38 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -53,7 +50,6 @@ function create_default_parameters(): Context
"admin.{$projectName}.{$tld}",
"api.{$projectName}.{$tld}",
],
'project_directory' => 'application',
'php_version' => 8.2,
];
)
Expand Down Expand Up @@ -127,6 +123,39 @@ Since `tools/bin` path is appended to the `$PATH`, tools will be available globa

</details>

### How to change the layout of the project

<details>

<summary>Read the cookbook</summary>

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
```

</details>

### How to use with Webpack Encore

<details>
Expand All @@ -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"
Expand Down Expand Up @@ -679,7 +708,7 @@ services:
context: services/php
target: cron
volumes:
- "../../${PROJECT_DIRECTORY}:/home/app/application:cached"
- "../..:/var/www:cached"
```

</details>
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
Expand All @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions infrastructure/docker/docker-compose.builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions infrastructure/docker/docker-compose.worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion infrastructure/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
depends_on:
- postgres
volumes:
- "../../${PROJECT_DIRECTORY}:/home/app/application:cached"
- "../..:/var/www:cached"
environment:
- "PHP_VERSION=${PHP_VERSION}"
labels:
Expand Down
6 changes: 4 additions & 2 deletions infrastructure/docker/services/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
level: 6
paths:
#- application/src
- application/public
- castor.php
- .castor/
scanFiles:
Expand All @@ -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: '''
Expand Down
1 change: 1 addition & 0 deletions tools/phpstan/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/var/
/vendor/

0 comments on commit f559853

Please sign in to comment.