Skip to content

Commit a802788

Browse files
committed
Cleanup and Doc
1 parent 94071c7 commit a802788

26 files changed

+297
-189
lines changed

.github/workflows/docker-build.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
name: Docker Build & Push
22

33
on:
4-
push:
5-
branches:
6-
- main
7-
pull_request:
8-
branches:
9-
- main
4+
workflow_run:
5+
workflows: ["run-tests"]
6+
types:
7+
- completed
108
workflow_dispatch: # manual trigger
119

1210
jobs:

.github/workflows/run-tests.yml

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
pull_request:
66
schedule:
7-
- cron: '0 0 * * *'
7+
- cron: "0 0 * * *"
88

99
jobs:
1010
run-tests:
@@ -14,39 +14,79 @@ jobs:
1414
matrix:
1515
php: [8.4]
1616

17-
name: P${{ matrix.php }}
17+
name: PHP ${{ matrix.php }}
1818

1919
steps:
20+
# --------------------------
21+
# Update APT for runtime & build dependencies
22+
# --------------------------
2023
- name: Update apt
2124
run: sudo apt-get update --fix-missing
2225

26+
# --------------------------
27+
# Checkout code
28+
# --------------------------
2329
- name: Checkout code
2430
uses: actions/checkout@v4
2531

32+
# --------------------------
33+
# Setup PHP with all extensions
34+
# --------------------------
2635
- name: Setup PHP
2736
uses: shivammathur/setup-php@v2
2837
with:
2938
php-version: ${{ matrix.php }}
30-
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
39+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick, sockets, opcache, redis, xdebug
3140
coverage: none
32-
tools: composer, cs2pr # cs2pr converts phpcs/phpstan results into PR annotations
41+
tools: composer, cs2pr
3342

43+
# --------------------------
44+
# Install runtime & build dependencies from Dockerfile
45+
# --------------------------
46+
- name: Install required system libraries
47+
run: |
48+
sudo apt-get install -y git unzip libzip-dev libssl-dev libcurl4-openssl-dev \
49+
libnghttp2-dev libsqlite3-dev libpq-dev libbrotli-dev libc-ares-dev supervisor \
50+
autoconf build-essential pkg-config \
51+
&& sudo rm -rf /var/lib/apt/lists/*
52+
53+
# --------------------------
54+
# Cache Composer dependencies
55+
# --------------------------
56+
- name: Cache Composer
57+
uses: actions/cache@v3
58+
with:
59+
path: ~/.composer/cache
60+
key: composer-${{ matrix.php }}-${{ hashFiles('composer.lock') }}
61+
62+
# --------------------------
63+
# Install PHP dependencies
64+
# --------------------------
65+
- name: Install dependencies
66+
run: composer install --no-dev --optimize-autoloader --prefer-dist
67+
68+
# --------------------------
69+
# Setup Problem Matchers
70+
# --------------------------
3471
- name: Setup Problem Matches
3572
run: |
3673
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
3774
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
3875
39-
- name: Install dependencies
40-
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
41-
76+
# --------------------------
77+
# Run PHP code quality checks
78+
# --------------------------
4279
- name: Run PHPCS
4380
run: vendor/bin/phpcs ./src --encoding=utf-8 --standard=phpcs.xml --runtime-set testVersion 8.4 --parallel=1 --report=checkstyle | cs2pr
4481

4582
- name: Run PHPMD
4683
run: vendor/bin/phpmd ./src text phpmd.xml | cs2pr
4784

4885
- name: Run PHPStan
49-
run: vendor/bin/phpstan analyse --configuration=phpstan.neon --memory-limit=-1 --error-format=checkstyle| cs2pr
86+
run: vendor/bin/phpstan analyse --configuration=phpstan.neon --memory-limit=-1 --error-format=checkstyle | cs2pr
5087

88+
# --------------------------
89+
# Execute PHPUnit tests
90+
# --------------------------
5191
- name: Execute tests
5292
run: vendor/bin/phpunit

.php-cs-fixer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,6 @@
8282
// Misc spacing & braces
8383
'no_extra_blank_lines' => ['tokens' => ['extra','curly_brace_block','square_brace_block','parenthesis_brace_block','throw','use','return']],
8484
'braces' => ['allow_single_line_closure' => true,'position_after_functions_and_oop_constructs' => 'same'],
85+
// disable only the new_expression_parentheses fixer
86+
'new_expression_parentheses' => false,
8587
]);

Dockerfile

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
# Dockerfile for PHP Swoole CRUD Microservice
22
#
33
# This Dockerfile builds a PHP 8.4 CLI image with Swoole, Redis, Xdebug, Opcache, and other required extensions.
4-
# It uses a multi-stage build to compile Swoole and extensions, then creates a lightweight runtime image.
4+
# It uses a multi-stage build to compile Swoole and extensions, then creates a runtime image with all extensions included.
55
#
6-
# Stages:
7-
# 1. build: Compiles Swoole and PHP extensions, installs Composer.
8-
# 2. runtime: Copies compiled extensions and Composer, installs runtime dependencies, and sets up the app.
6+
# Supervisor is used to run the PHP process and manage restarts.
97
#
108
# Exposes ports for Swoole HTTP servers.
119
#
1210
# Usage:
1311
# docker build -t php-swoole-crud-microservice .
1412
# docker run -p 9501:9501 php-swoole-crud-microservice
1513

16-
# ================= Build Stage =================
17-
FROM php:8.4-cli AS build
14+
# ================= Base Stage =================
15+
# Contains common runtime dependencies
16+
FROM php:8.4-cli AS base
1817

19-
# --- System dependencies ---
20-
# Installs build tools and libraries required for compiling Swoole and PHP extensions.
21-
# Includes nghttp2 and zlib to ensure HTTP/2 and compression support are built in automatically.
18+
# --- Runtime system dependencies ---
2219
RUN apt-get update && apt-get install -y \
23-
autoconf \
24-
build-essential \
2520
git \
2621
libbrotli-dev \
2722
libc-ares-dev \
@@ -31,104 +26,81 @@ RUN apt-get update && apt-get install -y \
3126
libsqlite3-dev \
3227
libssl-dev \
3328
libzip-dev \
34-
pkg-config \
29+
supervisor \
3530
unzip \
36-
zlib1g-dev \
3731
&& rm -rf /var/lib/apt/lists/*
3832

39-
RUN docker-php-ext-install sockets opcache
33+
# Create supervisor log directory
34+
RUN mkdir -p /var/log/supervisor
35+
36+
# ================= Build Stage =================
37+
FROM base AS build
4038

41-
# --- Build Swoole from source with required features ---
42-
# Deprecated options removed; HTTP/2, zlib, mysqlnd auto-enabled if dependencies are present.
39+
# --- Build-only dependencies ---
40+
RUN apt-get update && apt-get install -y \
41+
autoconf \
42+
build-essential \
43+
pkg-config \
44+
&& rm -rf /var/lib/apt/lists/*
45+
46+
# --- PHP extensions ---
47+
RUN docker-php-ext-install sockets opcache pdo pdo_mysql pdo_sqlite
48+
49+
# --- Build Swoole from source with caching ---
4350
ARG SWOOLE_VERSION=6.0.0
44-
RUN git clone -b v${SWOOLE_VERSION} https://github.com/swoole/swoole-src.git /usr/src/swoole \
51+
RUN --mount=type=cache,target=/tmp/swoole-build \
52+
git clone -b v${SWOOLE_VERSION} https://github.com/swoole/swoole-src.git /usr/src/swoole \
4553
&& cd /usr/src/swoole \
4654
&& phpize \
47-
&& ./configure \
48-
--enable-openssl \
49-
--enable-sockets \
50-
--enable-swoole-curl \
51-
--enable-cares \
52-
--enable-mysqlnd \
55+
&& ./configure --enable-openssl --enable-sockets --enable-swoole-curl --enable-cares --enable-mysqlnd \
5356
&& make -j$(nproc) && make install \
5457
&& docker-php-ext-enable swoole opcache
5558

56-
# --- PHP extensions ---
57-
# Installs PDO extensions for MySQL and SQLite.
58-
RUN docker-php-ext-install pdo pdo_mysql pdo_sqlite
59-
60-
# In build stage
61-
# RUN git clone https://github.com/OpenSwoole/mysql.git /usr/src/openswoole-mysql \
62-
# && cd /usr/src/openswoole-mysql \
63-
# && phpize \
64-
# && ./configure \
65-
# && make -j$(nproc) \
66-
# && make install \
67-
# && docker-php-ext-enable openswoole_mysql
68-
69-
# --- phpredis extension ---
70-
# Installs and enables phpredis via PECL.
71-
RUN pecl install redis \
72-
&& docker-php-ext-enable redis
73-
74-
# --- Xdebug extension ---
75-
# Installs and enables Xdebug for debugging.
76-
RUN pecl install xdebug \
77-
&& docker-php-ext-enable xdebug
59+
# --- PECL extensions with caching ---
60+
RUN --mount=type=cache,target=/tmp/pecl \
61+
pecl install redis xdebug \
62+
&& docker-php-ext-enable redis xdebug
7863

7964
# --- Composer ---
80-
# Copies Composer binary from official Composer image.
8165
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
8266

83-
# ================= Runtime Stage =================
84-
FROM php:8.4-cli
67+
# --- Set working directory ---
68+
WORKDIR /app
8569

86-
# --- Runtime system dependencies ---
87-
# Installs only the libraries required at runtime.
88-
RUN apt-get update && apt-get install -y \
89-
git \
90-
libbrotli-dev \
91-
libc-ares-dev \
92-
libcurl4-openssl-dev \
93-
libnghttp2-dev \
94-
libpq-dev \
95-
libsqlite3-dev \
96-
libssl-dev \
97-
libzip-dev \
98-
pkg-config \
99-
unzip \
100-
zlib1g-dev \
101-
&& rm -rf /var/lib/apt/lists/*
70+
# --- Copy only composer files first for caching ---
71+
COPY composer.json composer.lock* ./
10272

103-
# --- Copy compiled PHP extensions and Composer ---
104-
COPY --from=build /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
105-
COPY --from=build /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
106-
COPY --from=build /usr/bin/composer /usr/bin/composer
73+
# --- Install application dependencies with caching ---
74+
RUN --mount=type=cache,target=/root/.composer/cache \
75+
composer install --no-dev --optimize-autoloader --prefer-dist
76+
77+
# --- Ensure logs directory exists ---
78+
RUN mkdir -p /app/logs
79+
80+
# ================= Runtime Stage =================
81+
FROM base AS runtime
10782

10883
# --- Set working directory ---
10984
WORKDIR /app
11085

111-
# --- Composer install ---
112-
# Copies composer.json and optionally composer.lock, then installs dependencies.
113-
COPY composer.json ./
114-
RUN if [ -f composer.lock ]; then \
115-
cp composer.lock composer.lock && \
116-
composer install --no-dev --optimize-autoloader || true; \
117-
fi
86+
# --- Copy compiled PHP extensions and Composer/vendor from build stage ---
87+
COPY --from=build /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
88+
COPY --from=build /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
89+
COPY --from=build /app/vendor /app/vendor
90+
# COPY --from=build /app /app
11891

11992
# --- PHP configuration ---
120-
# Copies custom PHP configuration including opcache tuning.
12193
COPY docker/php/php.ini /usr/local/etc/php/conf.d/zz-preload.ini
12294
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
12395

124-
# --- Copy application source code ---
125-
COPY . .
96+
# --- Supervisor configuration ---
97+
COPY docker/supervisord.conf /etc/supervisor/conf.d/swoole.conf
12698

127-
# --- Ensure logs directory exists ---
128-
RUN mkdir -p /app/logs
99+
# --- Copy all application source code in a single step ---
100+
COPY . .
129101

130102
# --- Expose Swoole HTTP and custom ports ---
131103
EXPOSE 9501 9310 9502
132104

133105
# --- Default command ---
134-
CMD ["php","public/index.php"]
106+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/swoole.conf"]

0 commit comments

Comments
 (0)