From f10af5ab0dc9ffb141897b7c09a283029fc696ba Mon Sep 17 00:00:00 2001
From: thePanz <thepanz@gmail.com>
Date: Wed, 13 Mar 2024 21:09:55 +0100
Subject: [PATCH 1/4] Refactor(docker): initial refactor of Docker setup

---
 .docker/Dockerfile                | 82 ++++++++-----------------------
 .docker/php-config/10-symfony.ini |  5 ++
 .gitattributes                    |  3 ++
 .gitignore                        |  2 +
 Justfile                          | 60 ++++++++++++++++++++++
 compose.override.example.yaml     |  9 ++++
 compose.yaml                      | 13 +++++
 7 files changed, 112 insertions(+), 62 deletions(-)
 create mode 100644 .docker/php-config/10-symfony.ini
 create mode 100644 Justfile
 create mode 100644 compose.override.example.yaml
 create mode 100644 compose.yaml

diff --git a/.docker/Dockerfile b/.docker/Dockerfile
index a9d410dbf..73026c5b1 100644
--- a/.docker/Dockerfile
+++ b/.docker/Dockerfile
@@ -1,68 +1,26 @@
 ARG PHP_VERSION
-FROM php:${PHP_VERSION}-cli
+FROM php:${PHP_VERSION}-fpm-alpine
 
-RUN docker-php-ext-install pdo
-RUN docker-php-ext-install pdo_mysql
+COPY --from=composer:latest --link /usr/bin/composer /usr/local/bin/composer
+COPY --from=phario/phive:0.15.2 --link /usr/local/bin/phive /usr/local/bin/phive
+ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
 
-# Install mbstring PHP extension
-#
-RUN set -eux; \
-  apt-get update; \
-  apt-get install -y --no-upgrade --no-install-recommends \
-    libonig-dev \
-  ; \
-  \
-  apt-get clean; \
-  rm -rf /var/lib/apt/lists/*; \
-  \
-  docker-php-ext-install mbstring
+# Install PHP extensions
+RUN RUN set -eux; \
+    install-php-extensions \
+      apcu \
+      memcache \
+      pdo \
+      pdo_mysql \
+      zip \
+    ;
 
-# Install APCu PHP extension
-#
-ARG APCU_VERSION
-RUN set -eux; \
-  \
-  test x"" = x"${APCU_VERSION}" || { \
-    pecl install apcu-${APCU_VERSION}; \
-    docker-php-ext-enable apcu; \
-    \
-    rm -r /tmp/pear; \
-  }
+COPY --link --chmod=0644 php-config/*.ini /usr/local/etc/php/conf.d/
 
-# Install memcache PHP extension
-#
-ARG MEMCACHE_VERSION
-RUN set -eux; \
-  buildDeps=' \
-    libzip-dev \
-  '; \
-  apt-get update; \
-  apt-get install -y --no-upgrade --no-install-recommends \
-    $buildDeps \
-  ; \
-  \
-  pecl install memcache-${MEMCACHE_VERSION}; \
-  docker-php-ext-enable memcache; \
-  \
-  apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=true \
-    $buildDeps \
-  ; \
-  apt-get clean; \
-  rm -rf /var/lib/apt/lists/*; \
-  rm -r /tmp/pear
+RUN RUN set -eux; \
+    apk add file --no-cache;
 
-# For consistent mime type file guesser
-RUN set -eux; \
-  distFilePath=`which file`; \
-  \
-  mv ${distFilePath} ${distFilePath}.dist; \
-  { \
-    echo '#! /bin/sh -eu'; \
-    echo ''; \
-    echo "${distFilePath}"'.dist "$@" | sed -e s,application/x-pie-executable,application/x-executable,g'; \
-  } | tee ${distFilePath}; \
-  \
-  chmod +x ${distFilePath}; \
-  \
-  file /bin/ls --mime | grep application/x-executable; \
-  :;
+# Configure Composer folders
+RUN RUN set -eux; \
+    mkdir /var/composer; \
+    chmod a+rwX /var/composer;
diff --git a/.docker/php-config/10-symfony.ini b/.docker/php-config/10-symfony.ini
new file mode 100644
index 000000000..f7a548529
--- /dev/null
+++ b/.docker/php-config/10-symfony.ini
@@ -0,0 +1,5 @@
+display_error = on
+error_reporting = E_ALL
+memory_limit = 512M
+short_open_tag = off
+date.timezone = "UTC"
diff --git a/.gitattributes b/.gitattributes
index 9645b77e9..30bd6482a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -4,7 +4,10 @@
 .gitattributes         export-ignore
 .github/               export-ignore
 .gitignore             export-ignore
+compose.yaml           export-ignore
+compose.override.yaml  export-ignore
 .php-cs-fixer.dist.php export-ignore
+Justfile               export-ignore
 docker-compose.yml     export-ignore
 phpstan.neon           export-ignore
 phpunit.xml            export-ignore
diff --git a/.gitignore b/.gitignore
index 1c5624fa2..01d5091b9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
 .cache/
 
+compose.override.yaml
+
 /test/functional/fixtures/cache
 /test/functional/fixtures/log
 /lib/plugins/sfDoctrinePlugin/test/functional/fixtures/lib/*/doctrine/base/
diff --git a/Justfile b/Justfile
new file mode 100644
index 000000000..6874ccc12
--- /dev/null
+++ b/Justfile
@@ -0,0 +1,60 @@
+dockerCompose := "docker compose"
+dockerExec := dockerCompose + " exec php"
+defaultPhp := "7.4"
+
+default:
+    @just --list --justfile {{ justfile() }}
+
+# Build the docker image with the given PHP version
+build version=defaultPhp *options="":
+    {{ dockerCompose }} build --build-arg=PHP_VERSION={{ version }} {{ options }}
+
+# Build the docker image (and pull new images) with the given PHP version
+build-pull version=defaultPhp: (build version "--pull")
+
+# Build the docker image (and pull new images, with no docker cache) with the given PHP version
+rebuild version=defaultPhp: (build version "--pull" "--no-cache")
+
+# Start the docker containers in detached mode (no logs) and waits for the dependencies to be up and running.
+up:
+    {{ dockerCompose }} up --detach --wait
+
+# Start the docker containers and keep the daemon attached
+up-foreground:
+    {{ dockerCompose }} up
+
+# Stop the running containers
+down:
+    {{ dockerCompose }} down --remove-orphans
+
+# Display and follow the containers logs
+logs:
+    {{ dockerCompose }} logs --follow
+
+# Get a terminal within the running PHP container
+shell:
+    {{ dockerExec }} ash
+
+cs-check: (run-cs-fix "--dry-run")
+cs-fix: run-cs-fix
+[private]
+run-cs-fix *options:
+    {{ dockerExec }} tools/php-cs-fixer.phar fix --verbose {{ options }}
+
+# Run the legacy Symfony1 tests on the currently running docker instance
+tests-legacy:
+    {{ dockerExec }} php data/bin/symfony symfony:test --trace
+
+# Show the given PHP extensions's configuration from the running PHP container
+php-ext-config extname:
+    {{ dockerExec }} php --ri {{ extname }}
+
+# Setup and initialize the project (docker image must be running)
+setup:
+    git submodule update --checkout --recursive --force
+    {{ dockerExec }} composer update --optimize-autoloader
+
+# Cleanup the local code from vendor and composer.lock file
+cleanup:
+    rm -fr vendor/
+    rm -fr composer.lock
diff --git a/compose.override.example.yaml b/compose.override.example.yaml
new file mode 100644
index 000000000..52f5bb11e
--- /dev/null
+++ b/compose.override.example.yaml
@@ -0,0 +1,9 @@
+services:
+    php:
+        # Use the following user withing the image, this should help with file permissions
+        user: 1000:1000
+        volumes:
+            # Mount additional volumes from the host system to share Composer cache and authentication
+            - "${COMPOSER_CACHE_DIR:-${HOME}/.cache/composer}:/var/composer/cache:z"
+            - "${COMPOSER_HOME:-${HOME}/.composer}/auth.json:/var/composer/auth.json:ro,z"
+            - "${COMPOSER_HOME:-${HOME}/.composer}/config.json:/var/composer/config.json:ro,z"
diff --git a/compose.yaml b/compose.yaml
new file mode 100644
index 000000000..84a2849b6
--- /dev/null
+++ b/compose.yaml
@@ -0,0 +1,13 @@
+services:
+
+    php:
+        environment:
+            COMPOSER_HOME: "/var/composer"
+            COMPOSER_CACHE_DIR: "/var/composer/cache"
+        build:
+            context: .docker/
+        volumes:
+            - ".:/var/www/html:rw,z"
+
+#     memcached:
+#         image: memcached:1.6.13-alpine3.15

From fb222b78a6b26e5bf771afb1d542659e11a4eed7 Mon Sep 17 00:00:00 2001
From: thePanz <thepanz@gmail.com>
Date: Wed, 20 Mar 2024 18:19:28 +0100
Subject: [PATCH 2/4] Docker: remove legacy docker-compose.yml file

---
 .gitattributes     |  1 -
 docker-compose.yml | 92 ----------------------------------------------
 2 files changed, 93 deletions(-)
 delete mode 100644 docker-compose.yml

diff --git a/.gitattributes b/.gitattributes
index 30bd6482a..db4716824 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -8,7 +8,6 @@ compose.yaml           export-ignore
 compose.override.yaml  export-ignore
 .php-cs-fixer.dist.php export-ignore
 Justfile               export-ignore
-docker-compose.yml     export-ignore
 phpstan.neon           export-ignore
 phpunit.xml            export-ignore
 tests/                 export-ignore
diff --git a/docker-compose.yml b/docker-compose.yml
deleted file mode 100644
index 326393e41..000000000
--- a/docker-compose.yml
+++ /dev/null
@@ -1,92 +0,0 @@
-version: '3.5'
-
-volumes:
-  db_socket:
-
-services:
-  composer:
-    image: composer
-    working_dir: /app
-    volumes:
-      - .:/app
-    entrypoint:
-      - sh
-      - -c
-      - |
-        exec tail -f /dev/null
-
-  php74: &services_php74
-    build:
-      context: .docker
-      args:
-        PHP_VERSION: '7.4'
-        MEMCACHE_VERSION: '4.0.5.2'
-        APCU_VERSION: '5.1.23'
-    environment:
-        MEMCACHED_HOST: memcached
-    working_dir: /app
-    volumes:
-        - .:/app
-        - db_socket:/var/run/mysqld
-    entrypoint:
-        - sh
-        - -c
-        - |
-            {
-              echo 'pdo_mysql.default_socket = /var/run/mysqld/mysql.sock'
-              echo 'memory_limit = -1'
-              echo 'short_open_tag = off'
-              echo 'magic_quotes_gpc = off'
-              echo 'date.timezone = "UTC"'
-              echo 'apc.enable_cli = on'
-              echo 'apc.use_request_time = 0'
-            } | tee -a /usr/local/etc/php/php.ini
-
-            exec tail -f /dev/null
-    depends_on:
-        - db
-        - memcached
-
-  php81:
-    <<: *services_php74
-    build:
-      args:
-        PHP_VERSION: '8.1'
-        MEMCACHE_VERSION: '8.0'
-        APCU_VERSION: '5.1.23'
-
-  php82:
-    <<: *services_php74
-    build:
-      args:
-        PHP_VERSION: '8.2'
-        MEMCACHE_VERSION: '8.0'
-        APCU_VERSION: '5.1.23'
-
-  php83:
-    <<: *services_php74
-    build:
-      args:
-        PHP_VERSION: '8.3'
-        MEMCACHE_VERSION: '8.0'
-        APCU_VERSION: '5.1.23'
-
-
-  db:
-    image: mysql:5.5.62
-    environment:
-      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
-    volumes:
-      - db_socket:/tmp
-    entrypoint:
-      - bash
-      - -c
-      - |
-        {
-            echo "CREATE DATABASE IF NOT EXISTS test;"
-        } | tee /docker-entrypoint-initdb.d/init.sql
-
-        exec /usr/local/bin/docker-entrypoint.sh mysqld
-
-  memcached:
-    image: memcached:1.6.13-alpine3.15

From a91b89982f5bed97a9880c6ef9c59c6ec80445be Mon Sep 17 00:00:00 2001
From: thePanz <thepanz@gmail.com>
Date: Wed, 13 Mar 2024 21:32:18 +0100
Subject: [PATCH 3/4] Fix(docker): Fix tests for file-type checks on new docker
 alpine images

---
 test/unit/validator/sfValidatorFileTest.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test/unit/validator/sfValidatorFileTest.php b/test/unit/validator/sfValidatorFileTest.php
index 6d9f524b2..54b1d5e10 100755
--- a/test/unit/validator/sfValidatorFileTest.php
+++ b/test/unit/validator/sfValidatorFileTest.php
@@ -90,7 +90,7 @@ public function getMimeTypesFromCategory($category)
 $v = new testValidatorFile();
 $t->is($v->guessFromFileBinary($tmpDir.'/test.txt'), 'text/plain', '->guessFromFileBinary() guesses the type of a given file');
 $t->is($v->guessFromFileBinary($tmpDir.'/foo.txt'), null, '->guessFromFileBinary() returns null if the file type is not guessable');
-$t->like($v->guessFromFileBinary('/bin/ls'), (PHP_OS != 'Darwin') ? '/^application\/x-(pie-executable|executable|sharedlib)$/' : '/^application/octet-stream$/', '->guessFromFileBinary() returns correct type if file is guessable');
+$t->is($v->guessFromFileBinary(PHP_BINARY), 'application/x-pie-executable', '->guessFromFileBinary() returns correct type if file is guessable');
 $t->is($v->guessFromFileBinary('-test'), null, '->guessFromFileBinary() returns null if file path has leading dash');
 
 // ->getMimeType()

From d3d624b007a256607ef378e8ad876c58763698cb Mon Sep 17 00:00:00 2001
From: thePanz <thepanz@gmail.com>
Date: Tue, 19 Mar 2024 11:51:21 +0100
Subject: [PATCH 4/4] WIP: add MySQL to docker setup

---
 .docker/php-config/11-mysql-socket.ini | 1 +
 compose.yaml                           | 9 +++++++++
 2 files changed, 10 insertions(+)
 create mode 100644 .docker/php-config/11-mysql-socket.ini

diff --git a/.docker/php-config/11-mysql-socket.ini b/.docker/php-config/11-mysql-socket.ini
new file mode 100644
index 000000000..b472c2a1f
--- /dev/null
+++ b/.docker/php-config/11-mysql-socket.ini
@@ -0,0 +1 @@
+; pdo_mysql.default_socket = /var/run/mysqld/mysql.sock'
diff --git a/compose.yaml b/compose.yaml
index 84a2849b6..8b78510b9 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -8,6 +8,15 @@ services:
             context: .docker/
         volumes:
             - ".:/var/www/html:rw,z"
+            - "mysql_socket:/var/run/mysqld/mysqld.sock:rw"
 
 #     memcached:
 #         image: memcached:1.6.13-alpine3.15
+    mysql:
+        image: mysql:8.3
+        profiles:
+            - full
+        volumes:
+            - "mysql_socket:/var/run/mysqld/mysqld.sock:rw"
+volumes:
+    mysql_socket: