diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c61fcaab..6a2fb5c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,9 @@ jobs: - name: Build Dockerfile run: docker build . + - name: Verify nginx redirects + run: docker run --rm -v "$PWD:/app:ro" -w /app nginx:alpine sh tests/nginx.sh + cs-stan: name: Coding Standard runs-on: ubuntu-latest @@ -50,8 +53,12 @@ jobs: - name: Composer install run: composer install - - name: Run loader regression checks - run: php tests/loader.php + - name: Run regression checks + run: | + php tests/loader.php + php tests/generator.php + php tests/releases.php + sh tests/build-release.sh - name: Run PHP CodeSniffer run: vendor/bin/phpcs -q --report=checkstyle src/ | cs2pr diff --git a/Dockerfile b/Dockerfile index 1d82407e..3213df0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -# Build api docs with php 8.1 requirements +# syntax=docker/dockerfile:1 +# Build API docs with PHP 8.2 FROM alpine:3.19 AS builder RUN apk add --no-cache \ @@ -7,7 +8,7 @@ RUN apk add --no-cache \ git \ make \ openssh-client \ - php81 \ + php82 \ php82-bz2 \ php82-curl \ php82-dom \ @@ -24,29 +25,23 @@ RUN apk add --no-cache \ RUN ln -sf /usr/bin/php82 /usr/bin/php -RUN mkdir /root/.ssh \ - && ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts - WORKDIR /data -COPY . /data -RUN git clone https://github.com/cakephp/cakephp.git /cakephp \ - && git clone https://github.com/cakephp/authentication.git /authentication \ - && git clone https://github.com/cakephp/authorization.git /authorization \ - && git clone https://github.com/cakephp/chronos.git /chronos \ - && git clone https://github.com/cakephp/elastic-search.git /elastic \ - && git clone https://github.com/cakephp/queue.git /queue +# Install generator dependencies independently of source, template and asset edits. +COPY composer.json composer.lock ./ +RUN --mount=type=cache,target=/root/.composer/cache \ + curl -sS https://getcomposer.org/installer | php \ + && php composer.phar install --no-interaction --prefer-dist + +COPY . /data -RUN ls -lah \ - && make build-cakephp3-all CAKEPHP_SOURCE_DIR=/cakephp \ - && make build-cakephp4-all CAKEPHP_SOURCE_DIR=/cakephp \ - && make build-cakephp5-all CAKEPHP_SOURCE_DIR=/cakephp \ - && make build-cakephp6-all CAKEPHP_SOURCE_DIR=/cakephp \ - && make build-authentication-all AUTHENTICATION_SOURCE_DIR=/authentication \ - && make build-authorization-all AUTHORIZATION_SOURCE_DIR=/authorization \ - && make build-chronos-all CHRONOS_SOURCE_DIR=/chronos \ - && make build-elastic-all ELASTIC_SOURCE_DIR=/elastic \ - && make build-queue-all QUEUE_SOURCE_DIR=/queue +# Change DOCS_CACHE_REFRESH to re-resolve dependencies even for unchanged source refs. +ARG DOCS_CACHE_REFRESH= +ENV DOCS_CACHE_DIR=/cache/docs +RUN --mount=type=cache,target=/sources,sharing=locked \ + --mount=type=cache,target=/cache/docs,sharing=locked \ + --mount=type=cache,target=/root/.composer/cache,sharing=locked \ + DOCS_CACHE_REFRESH="$DOCS_CACHE_REFRESH" sh bin/build-all.sh # nginx server FROM alpine:3.19 diff --git a/Makefile b/Makefile index 0c6c43a3..33673aef 100644 --- a/Makefile +++ b/Makefile @@ -9,65 +9,36 @@ BUILD_DIR ?= ./build/api DEPLOY_DIR ?= ./website PHP ?= php +DOCS_CACHE_DIR ?= +export DOCS_CACHE_REFRESH COMPOSER := $(CURDIR)/composer.phar COMPOSER_UPDATE_FLAGS := --no-plugins --no-audit --no-security-blocking --ignore-platform-reqs .DEFAULT_GOAL := help -CAKEPHP3_RELEASES := \ - 3.0|3.0.19|no-vendor \ - 3.1|3.1.14|no-vendor \ - 3.2|3.2.14|update \ - 3.3|3.3.16|update \ - 3.4|3.4.14|update \ - 3.5|3.5.18|update \ - 3.6|3.6.15|update \ - 3.7|3.7.9|update \ - 3.8|3.8.13|update \ - 3.9|3.9.10|update \ - 3.10|origin/3.x|update - -CAKEPHP4_RELEASES := \ - 4.0|4.0.9|update \ - 4.1|4.1.7|update \ - 4.2|4.2.10|update \ - 4.3|4.3.10|update \ - 4.4|4.4.18|update \ - 4.5|4.5.10|update \ - 4.6|origin/4.x|update - -CAKEPHP5_RELEASES := \ - 5.0|5.0.11|update \ - 5.1|5.1.9|update \ - 5.2|5.2.15|update \ - 5.3|5.3.7|update \ - 5.4|origin/5.x|update - -CAKEPHP6_RELEASES := \ - 6.0|origin/6.x|update - -CHRONOS_RELEASES := \ - 1.x|origin/1.x|update \ - 2.x|origin/2.x|update \ - 3.x|origin/3.x|update - -ELASTIC_RELEASES := \ - 2.x|origin/2.x|update \ - 3.x|origin/3.x|update \ - 4.x|origin/4.x|update - -QUEUE_RELEASES := \ - 1.x|origin/1.x|update \ - 2.x|origin/2.x|update - -AUTHENTICATION_RELEASES := \ - 2.x|origin/2.x|update \ - 3.x|origin/3.x|update \ - 4.x|origin/4.x|update - -AUTHORIZATION_RELEASES := \ - 2.x|origin/2.x|update \ - 3.x|origin/3.x|update +CAKEPHP3_RELEASES := $(shell $(PHP) bin/releases.php cakephp3) + +CAKEPHP4_RELEASES := $(shell $(PHP) bin/releases.php cakephp4) + +CAKEPHP5_RELEASES := $(shell $(PHP) bin/releases.php cakephp5) + +CAKEPHP6_RELEASES := $(shell $(PHP) bin/releases.php cakephp6) + +CHRONOS_RELEASES := $(shell $(PHP) bin/releases.php chronos) + +ELASTIC_RELEASES := $(shell $(PHP) bin/releases.php elastic) + +QUEUE_RELEASES := $(shell $(PHP) bin/releases.php queue) + +AUTHENTICATION_RELEASES := $(shell $(PHP) bin/releases.php authentication) + +AUTHORIZATION_RELEASES := $(shell $(PHP) bin/releases.php authorization) + +RELEASE_GROUPS := CAKEPHP3 CAKEPHP4 CAKEPHP5 CAKEPHP6 CHRONOS ELASTIC QUEUE AUTHENTICATION AUTHORIZATION +$(foreach group,$(RELEASE_GROUPS),$(if $($(group)_RELEASES),,$(error Unable to load $(group) releases; check PHP and config/releases.php))) + +# Releases share source checkouts; concurrent builds would race on git checkout. +.NOTPARALLEL: release_version = $(word 1,$(subst |, ,$1)) release_tag = $(word 2,$(subst |, ,$1)) @@ -134,6 +105,8 @@ help: @echo " AUTHENTICATION_SOURCE_DIR Location of the authentication clone." @echo " AUTHORIZATION_SOURCE_DIR Location of the authorization clone." @echo " BUILD_DIR Output directory. Default: $(BUILD_DIR)" + @echo " DOCS_CACHE_DIR Optional cache directory for completed release builds." + @echo " DOCS_CACHE_REFRESH Change this value to refresh cached dependency resolution." @echo " DEPLOY_DIR Deployment directory. Default: $(DEPLOY_DIR)" @echo "" @echo "NOTE: Source directories will have their checkout branch changed." @@ -158,18 +131,15 @@ deploy: $(DEPLOY_DIR) composer.phar: curl -sS https://getcomposer.org/installer | php -install: composer.phar +install: vendor/autoload.php + +vendor/autoload.php: composer.json composer.lock composer.phar $(PHP) $(COMPOSER) install define build_docs build-$(1)-$(5): install - cd $($(2)) && git checkout -f $(6) - $(if $(filter no-vendor,$(7)),cd $($(2)) && rm -rf ./vendor,cd $($(2)) && $(PHP) $(COMPOSER) update $(COMPOSER_UPDATE_FLAGS)) - mkdir -p $(BUILD_DIR)/$(3)/$(5) - cp -r static/assets/* $(BUILD_DIR)/$(3)/$(5) - - $(PHP) bin/apitool.php generate --config $(4) --version $(5) --tag $(6) \ - --output-dir $(BUILD_DIR)/$(3)/$(5) $($(2)) + sh bin/build-release.sh "$(PHP)" "$(COMPOSER)" "$($(2))" "$(BUILD_DIR)/$(3)/$(5)" \ + "$(4)" "$(5)" "$(6)" "$(7)" "$(COMPOSER_UPDATE_FLAGS)" "$(DOCS_CACHE_DIR)" endef build-cakephp3-all: $(foreach version,$(CAKEPHP3_VERSIONS),build-cakephp-$(version)) diff --git a/README.md b/README.md index aa9af516..bdd02cf2 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,57 @@ mkdir -p build/api/cakephp/6.0 cp -r static/assets/* build/api/cakephp/6.0/ php bin/apitool.php generate --config cakephp6 --version 6.0 --tag origin/6.x --output-dir build/api/cakephp/6.0/ ../cakephp ``` + +### Release definitions and cached builds + +`config/releases.php` defines the versions, Git refs and dependency modes used by +Make targets. All CakePHP configurations derive their version selector from this +manifest; legacy 1.x and 2.x links are retained in `config/cakephp-versions.php`. +PHP is required when reading the Makefile, including for `make help`. + +Docker uses BuildKit caches for source clones, Composer downloads and completed +release documentation. Generator dependencies are installed before application +sources are copied, so template edits reuse the Composer layer. Rendered releases +are keyed by the source commit, generator code, templates, assets, configuration, +Composer lock file, PHP major/minor version and dependency update flags. Only +successful builds are cached, and a complete output directory replaces the old one. + +Local Make builds can opt into the same release cache: + +```sh +make build-cakephp-6.0 DOCS_CACHE_DIR="$HOME/.cache/cakephp-api-docs" +``` + +Cached documentation retains its resolved source dependencies. To re-resolve them +without changing the source ref, set a new refresh value: + +```sh +make build-cakephp-6.0 DOCS_CACHE_DIR="$HOME/.cache/cakephp-api-docs" DOCS_CACHE_REFRESH=2026-11-29 +# Also invalidates Docker's build step so remote refs are fetched again: +docker build --build-arg DOCS_CACHE_REFRESH=2026-11-29 . +``` + +BuildKit caches are local to the builder. A fresh CI runner performs a full build +unless its builder cache is persisted. Changing templates or shared version links +correctly regenerates all affected release pages. README and test edits do not +change the rendered-release cache keys. + +### Public URL routing + +The `site-api` proxy forwards public `/6.0/...` requests to this image's internal +`/cakephp/6.0/...` path. Missing HTML pages redirect to the public `/6.0/` overview, +with a relative Location header so the internal prefix and upstream host do not +leak into public URLs. Missing assets retain a 404 response. + +### Regression checks + +After installing Composer dependencies: + +```sh +php tests/loader.php +php tests/generator.php +php tests/releases.php +sh tests/build-release.sh +# Runs nginx and HTTP requests inside a disposable container: +docker run --rm -v "$PWD:/app:ro" -w /app nginx:alpine sh tests/nginx.sh +``` diff --git a/bin/build-all.sh b/bin/build-all.sh new file mode 100644 index 00000000..bd98e1f6 --- /dev/null +++ b/bin/build-all.sh @@ -0,0 +1,24 @@ +#!/bin/sh +set -eu + +# Source and rendered-documentation directories are Docker BuildKit cache mounts. +for project in cakephp authentication authorization chronos elastic-search queue; do + source_dir="/sources/$project" + if [ ! -d "$source_dir/.git" ]; then + git clone "https://github.com/cakephp/$project.git" "$source_dir" + else + git -C "$source_dir" fetch --prune --tags origin + fi + case "$project" in + cakephp) + for major in 3 4 5 6; do + make "build-cakephp$major-all" CAKEPHP_SOURCE_DIR="$source_dir" + done + ;; + elastic-search) make build-elastic-all ELASTIC_SOURCE_DIR="$source_dir" ;; + *) + variable=$(printf '%s_SOURCE_DIR' "$project" | tr '[:lower:]' '[:upper:]') + make "build-$project-all" "$variable=$source_dir" + ;; + esac +done diff --git a/bin/build-release.sh b/bin/build-release.sh new file mode 100644 index 00000000..fe8948a3 --- /dev/null +++ b/bin/build-release.sh @@ -0,0 +1,57 @@ +#!/bin/sh +set -eu + +php=$1 +composer=$2 +source_dir=$(cd "$3" && pwd -P) +output=$4 +config=$5 +version=$6 +tag=$7 +mode=$8 +update_flags=$9 +cache_dir=${10} + +commit=$(git -C "$source_dir" rev-parse "$tag^{commit}") +cache_entry= +if [ -n "$cache_dir" ]; then + mkdir -p "$cache_dir" + cache_dir=$(cd "$cache_dir" && pwd -P) + fingerprint=$("$php" bin/cache-key.php "$commit" "$config" "$version" "$mode" "$update_flags" "${DOCS_CACHE_REFRESH:-}") + cache_entry="$cache_dir/$config-$version-$fingerprint" +fi + +mkdir -p "$(dirname "$output")" +stage=$(mktemp -d "$(dirname "$output")/.docs-XXXXXX") +trap 'rm -rf "$stage"' EXIT HUP INT TERM + +if [ -n "$cache_entry" ] && [ -f "$cache_entry/.complete" ]; then + echo "Using cached documentation for $config $version ($commit)" + cp -a "$cache_entry/." "$stage/" + rm "$stage/.complete" +else + git -C "$source_dir" checkout -f "$tag" + if [ "$mode" = no-vendor ]; then + rm -rf "$source_dir/vendor" + else + # Flags are deliberately split into individual Composer arguments. + # shellcheck disable=SC2086 + (cd "$source_dir" && "$php" "$composer" update $update_flags) + fi + cp -R static/assets/. "$stage/" + "$php" bin/apitool.php generate --config "$config" --version "$version" --tag "$tag" \ + --output-dir "$stage" "$source_dir" + if [ -n "$cache_entry" ]; then + cache_stage=$(mktemp -d "$cache_dir/.docs-XXXXXX") + cp -a "$stage/." "$cache_stage/" + touch "$cache_stage/.complete" + rm -rf "$cache_entry" + mv "$cache_stage" "$cache_entry" + fi +fi + +# Publish only after every page has been generated successfully. +# mktemp creates a private directory; nginx workers must be able to traverse it. +chmod 755 "$stage" +rm -rf "$output" +mv "$stage" "$output" diff --git a/bin/cache-key.php b/bin/cache-key.php new file mode 100644 index 00000000..e7960076 --- /dev/null +++ b/bin/cache-key.php @@ -0,0 +1,27 @@ +isFile()) { + $files[] = $file->getPathname(); + } + } +} +sort($files, SORT_STRING); +$hash = hash_init('sha256'); +foreach ($files as $file) { + $digest = hash_file('sha256', $file); + if ($digest === false) { + throw new RuntimeException(sprintf('Unable to fingerprint `%s`.', $file)); + } + hash_update($hash, $file . "\0" . $digest . "\0"); +} +hash_update($hash, json_encode(array_slice($argv, 1), JSON_THROW_ON_ERROR)); +hash_update($hash, PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION); +echo hash_final($hash), "\n"; diff --git a/bin/releases.php b/bin/releases.php new file mode 100644 index 00000000..030d115f --- /dev/null +++ b/bin/releases.php @@ -0,0 +1,13 @@ + [ 'project' => 'CakePHP', 'release' => 'Red Velvet', - 'versions' => [ - '6.0' => '../6.0/', - '5.4' => '../5.4/', - '5.3' => '../5.3/', - '5.2' => '../5.2/', - '5.1' => '../5.1/', - '5.0' => '../5.0/', - '4.6' => '../4.6/', - '4.5' => '../4.5/', - '4.4' => '../4.4/', - '4.3' => '../4.3/', - '4.2' => '../4.2/', - '4.1' => '../4.1/', - '4.0' => '../4.0/', - '3.10' => '../3.10', - '3.9' => '../3.9/', - '3.8' => '../3.8/', - '3.7' => '../3.7/', - '3.6' => '../3.6/', - '3.5' => '../3.5/', - '3.4' => '../3.4/', - '3.3' => '../3.3/', - '3.2' => '../3.2/', - '3.1' => '../3.1/', - '3.0' => '../3.0/', - '2.10' => '../2.10/', - '2.9' => '../2.9/', - '2.8' => '../2.8/', - '2.7' => '../2.7/', - '2.6' => '../2.6/', - '2.5' => '../2.5/', - '2.4' => '../2.4/', - '2.3' => '../2.3/', - '2.2' => '../2.2/', - '2.1' => '../2.1/', - '2.0' => '../2.0/', - '1.3' => '../1.3/', - '1.2' => '../1.2/', - ], + 'versions' => require __DIR__ . '/cakephp-versions.php', ], ], ]; diff --git a/config/cakephp4.php b/config/cakephp4.php index 386cbaed..9b6e7de8 100644 --- a/config/cakephp4.php +++ b/config/cakephp4.php @@ -21,45 +21,7 @@ 'globals' => [ 'project' => 'CakePHP', 'release' => 'Strawberry', - 'versions' => [ - '6.0' => '../6.0/', - '5.4' => '../5.4/', - '5.3' => '../5.3/', - '5.2' => '../5.2/', - '5.1' => '../5.1/', - '5.0' => '../5.0/', - '4.6' => '../4.6/', - '4.5' => '../4.5/', - '4.4' => '../4.4/', - '4.3' => '../4.3/', - '4.2' => '../4.2/', - '4.1' => '../4.1/', - '4.0' => '../4.0/', - '3.10' => '../3.10', - '3.9' => '../3.9/', - '3.8' => '../3.8/', - '3.7' => '../3.7/', - '3.6' => '../3.6/', - '3.5' => '../3.5/', - '3.4' => '../3.4/', - '3.3' => '../3.3/', - '3.2' => '../3.2/', - '3.1' => '../3.1/', - '3.0' => '../3.0/', - '2.10' => '../2.10/', - '2.9' => '../2.9/', - '2.8' => '../2.8/', - '2.7' => '../2.7/', - '2.6' => '../2.6/', - '2.5' => '../2.5/', - '2.4' => '../2.4/', - '2.3' => '../2.3/', - '2.2' => '../2.2/', - '2.1' => '../2.1/', - '2.0' => '../2.0/', - '1.3' => '../1.3/', - '1.2' => '../1.2/', - ], + 'versions' => require __DIR__ . '/cakephp-versions.php', ], ], ]; diff --git a/config/cakephp5.php b/config/cakephp5.php index d86ad03c..3c839c40 100644 --- a/config/cakephp5.php +++ b/config/cakephp5.php @@ -21,45 +21,7 @@ 'globals' => [ 'project' => 'CakePHP', 'release' => 'Chiffon', - 'versions' => [ - '6.0' => '../6.0/', - '5.4' => '../5.4/', - '5.3' => '../5.3/', - '5.2' => '../5.2/', - '5.1' => '../5.1/', - '5.0' => '../5.0/', - '4.6' => '../4.6/', - '4.5' => '../4.5/', - '4.4' => '../4.4/', - '4.3' => '../4.3/', - '4.2' => '../4.2/', - '4.1' => '../4.1/', - '4.0' => '../4.0/', - '3.10' => '../3.10', - '3.9' => '../3.9/', - '3.8' => '../3.8/', - '3.7' => '../3.7/', - '3.6' => '../3.6/', - '3.5' => '../3.5/', - '3.4' => '../3.4/', - '3.3' => '../3.3/', - '3.2' => '../3.2/', - '3.1' => '../3.1/', - '3.0' => '../3.0/', - '2.10' => '../2.10/', - '2.9' => '../2.9/', - '2.8' => '../2.8/', - '2.7' => '../2.7/', - '2.6' => '../2.6/', - '2.5' => '../2.5/', - '2.4' => '../2.4/', - '2.3' => '../2.3/', - '2.2' => '../2.2/', - '2.1' => '../2.1/', - '2.0' => '../2.0/', - '1.3' => '../1.3/', - '1.2' => '../1.2/', - ], + 'versions' => require __DIR__ . '/cakephp-versions.php', ], ], ]; diff --git a/config/cakephp6.php b/config/cakephp6.php index ac0eeb61..9f0c051c 100644 --- a/config/cakephp6.php +++ b/config/cakephp6.php @@ -21,45 +21,7 @@ 'globals' => [ 'project' => 'CakePHP', 'release' => '', - 'versions' => [ - '6.0' => '../6.0/', - '5.4' => '../5.4/', - '5.3' => '../5.3/', - '5.2' => '../5.2/', - '5.1' => '../5.1/', - '5.0' => '../5.0/', - '4.6' => '../4.6/', - '4.5' => '../4.5/', - '4.4' => '../4.4/', - '4.3' => '../4.3/', - '4.2' => '../4.2/', - '4.1' => '../4.1/', - '4.0' => '../4.0/', - '3.10' => '../3.10', - '3.9' => '../3.9/', - '3.8' => '../3.8/', - '3.7' => '../3.7/', - '3.6' => '../3.6/', - '3.5' => '../3.5/', - '3.4' => '../3.4/', - '3.3' => '../3.3/', - '3.2' => '../3.2/', - '3.1' => '../3.1/', - '3.0' => '../3.0/', - '2.10' => '../2.10/', - '2.9' => '../2.9/', - '2.8' => '../2.8/', - '2.7' => '../2.7/', - '2.6' => '../2.6/', - '2.5' => '../2.5/', - '2.4' => '../2.4/', - '2.3' => '../2.3/', - '2.2' => '../2.2/', - '2.1' => '../2.1/', - '2.0' => '../2.0/', - '1.3' => '../1.3/', - '1.2' => '../1.2/', - ], + 'versions' => require __DIR__ . '/cakephp-versions.php', ], ], ]; diff --git a/config/releases.php b/config/releases.php new file mode 100644 index 00000000..44564e9a --- /dev/null +++ b/config/releases.php @@ -0,0 +1,60 @@ + [ + ['version' => '3.0', 'tag' => '3.0.19', 'mode' => 'no-vendor'], + ['version' => '3.1', 'tag' => '3.1.14', 'mode' => 'no-vendor'], + ['version' => '3.2', 'tag' => '3.2.14', 'mode' => 'update'], + ['version' => '3.3', 'tag' => '3.3.16', 'mode' => 'update'], + ['version' => '3.4', 'tag' => '3.4.14', 'mode' => 'update'], + ['version' => '3.5', 'tag' => '3.5.18', 'mode' => 'update'], + ['version' => '3.6', 'tag' => '3.6.15', 'mode' => 'update'], + ['version' => '3.7', 'tag' => '3.7.9', 'mode' => 'update'], + ['version' => '3.8', 'tag' => '3.8.13', 'mode' => 'update'], + ['version' => '3.9', 'tag' => '3.9.10', 'mode' => 'update'], + ['version' => '3.10', 'tag' => 'origin/3.x', 'mode' => 'update'], + ], + 'cakephp4' => [ + ['version' => '4.0', 'tag' => '4.0.9', 'mode' => 'update'], + ['version' => '4.1', 'tag' => '4.1.7', 'mode' => 'update'], + ['version' => '4.2', 'tag' => '4.2.10', 'mode' => 'update'], + ['version' => '4.3', 'tag' => '4.3.10', 'mode' => 'update'], + ['version' => '4.4', 'tag' => '4.4.18', 'mode' => 'update'], + ['version' => '4.5', 'tag' => '4.5.10', 'mode' => 'update'], + ['version' => '4.6', 'tag' => 'origin/4.x', 'mode' => 'update'], + ], + 'cakephp5' => [ + ['version' => '5.0', 'tag' => '5.0.11', 'mode' => 'update'], + ['version' => '5.1', 'tag' => '5.1.9', 'mode' => 'update'], + ['version' => '5.2', 'tag' => '5.2.15', 'mode' => 'update'], + ['version' => '5.3', 'tag' => '5.3.7', 'mode' => 'update'], + ['version' => '5.4', 'tag' => 'origin/5.x', 'mode' => 'update'], + ], + 'cakephp6' => [ + ['version' => '6.0', 'tag' => 'origin/6.x', 'mode' => 'update'], + ], + 'chronos' => [ + ['version' => '1.x', 'tag' => 'origin/1.x', 'mode' => 'update'], + ['version' => '2.x', 'tag' => 'origin/2.x', 'mode' => 'update'], + ['version' => '3.x', 'tag' => 'origin/3.x', 'mode' => 'update'], + ], + 'elastic' => [ + ['version' => '2.x', 'tag' => 'origin/2.x', 'mode' => 'update'], + ['version' => '3.x', 'tag' => 'origin/3.x', 'mode' => 'update'], + ['version' => '4.x', 'tag' => 'origin/4.x', 'mode' => 'update'], + ], + 'queue' => [ + ['version' => '1.x', 'tag' => 'origin/1.x', 'mode' => 'update'], + ['version' => '2.x', 'tag' => 'origin/2.x', 'mode' => 'update'], + ], + 'authentication' => [ + ['version' => '2.x', 'tag' => 'origin/2.x', 'mode' => 'update'], + ['version' => '3.x', 'tag' => 'origin/3.x', 'mode' => 'update'], + ['version' => '4.x', 'tag' => 'origin/4.x', 'mode' => 'update'], + ], + 'authorization' => [ + ['version' => '2.x', 'tag' => 'origin/2.x', 'mode' => 'update'], + ['version' => '3.x', 'tag' => 'origin/3.x', 'mode' => 'update'], + ], +]; diff --git a/nginx.conf b/nginx.conf index 85d78d7c..d8a3b6e0 100644 --- a/nginx.conf +++ b/nginx.conf @@ -2,20 +2,19 @@ server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; - index index.html index.; + index index.html; server_name _; - location ~ "^/([3456]\.\d+)/.*\.html$" { - try_files $uri @overview; - } + # site-api proxies public /6.0/... URLs to /cakephp/6.0/... here. + # Keep redirects relative to the public host, without the internal prefix. + absolute_redirect off; - location ~ "^/4\.next/.*\.html$" { + location ~ "^/cakephp/([3456]\.\d+|4\.next)/.*\.html$" { try_files $uri @overview; } location @overview { - rewrite "^/([3456]\.\d+)/.*$" /$1/ redirect; - rewrite "^/4\.next/.*$" /$1/ redirect; + rewrite "^/cakephp/([3456]\.\d+|4\.next)/.*$" /$1/ redirect; return 404; } } diff --git a/src/Generator.php b/src/Generator.php index e60b2b1a..7634b4e2 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -21,6 +21,7 @@ use Cake\ApiDocs\Reflection\ReflectedInterface; use Cake\ApiDocs\Reflection\ReflectedTrait; use Cake\Log\LogTrait; +use RuntimeException; use Twig\Environment; class Generator @@ -41,7 +42,14 @@ class Generator public function __construct(Environment $twig, string $outputDir) { $this->twig = $twig; - $this->outputDir = $outputDir; + if ($outputDir === '') { + throw new RuntimeException('The output directory must not be empty.'); + } + // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Convert filesystem warnings into useful exceptions. + if (!is_dir($outputDir) && !@mkdir($outputDir, 0777, true) && !is_dir($outputDir)) { + throw new RuntimeException(sprintf('Unable to create output directory `%s`.', $outputDir)); + } + $this->outputDir = realpath($outputDir); } /** @@ -186,7 +194,11 @@ protected function renderSearch(array $namespaces): void */ protected function renderTemplate(string $template, string $filename, array $context = []): void { - $path = getcwd() . DS . $this->outputDir . DS . $filename; - file_put_contents($path, $this->twig->render($template, $context)); + $path = $this->outputDir . DIRECTORY_SEPARATOR . $filename; + $contents = $this->twig->render($template, $context); + // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Convert filesystem warnings into useful exceptions. + if (@file_put_contents($path, $contents) !== strlen($contents)) { + throw new RuntimeException(sprintf('Unable to write generated file `%s`.', $path)); + } } } diff --git a/src/Loader.php b/src/Loader.php index dfe59fea..c926a9f9 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -19,12 +19,14 @@ use Cake\ApiDocs\Reflection\ReflectedClassLike; use FilesystemIterator; +use PhpParser\Error; use PhpParser\NodeTraverser; use PhpParser\Parser; use PhpParser\ParserFactory; use PhpParser\PhpVersion; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; +use RuntimeException; class Loader { @@ -77,7 +79,16 @@ public function loadDirectory(string $path, bool $inProject): array */ public function loadFile(string $path, bool $inProject): array { - $stmts = $this->parser->parse(file_get_contents($path)); + // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Convert filesystem warnings into useful exceptions. + $contents = @file_get_contents($path); + if ($contents === false) { + throw new RuntimeException(sprintf('Unable to read source file `%s`.', $path)); + } + try { + $stmts = $this->parser->parse($contents); + } catch (Error $error) { + throw new RuntimeException(sprintf('Unable to parse `%s`: %s', $path, $error->getMessage()), 0, $error); + } $traverser = new NodeTraverser(); $visitor = new FileVisitor(new Factory(), substr($path, strlen($this->projectPath) + 1), $inProject); diff --git a/tests/build-release.sh b/tests/build-release.sh new file mode 100644 index 00000000..17b190df --- /dev/null +++ b/tests/build-release.sh @@ -0,0 +1,89 @@ +#!/bin/sh +set -eu + +root=$(pwd) +workspace=$(mktemp -d) +cleanup() { + status=$? + if [ "$status" -ne 0 ] && [ -f "$workspace/log" ]; then + cat "$workspace/log" >&2 + fi + rm -rf "$workspace" + exit "$status" +} +trap cleanup EXIT +trap 'exit 1' HUP INT TERM +source_dir="$workspace/source" +output="$workspace/output" +cache_dir="$workspace/cache" +# Exercise generator changes in an isolated copy, without modifying the checkout. +mkdir "$workspace/generator" +cp -R src templates static config bin composer.json composer.lock "$workspace/generator/" +ln -s "$root/vendor" "$workspace/generator/vendor" +mkdir -p "$source_dir/src" "$source_dir/config" +git -C "$source_dir" init -q +git -C "$source_dir" config user.name 'API docs test' +git -C "$source_dir" config user.email 'test@example.invalid' +printf '%s\n' ' "$source_dir/src/Example.php" +git -C "$source_dir" add . +git -C "$source_dir" commit -qm 'Initial fixture' + +build() { + (cd "$workspace/generator" && sh bin/build-release.sh php "$root/composer.phar" "$source_dir" "$output" \ + cakephp6 6.0 HEAD no-vendor '' "$cache_dir") > "$workspace/log" 2>&1 +} + +build +if grep -q 'Using cached documentation' "$workspace/log"; then + echo 'Changed inputs unexpectedly reused cached documentation.' >&2 + exit 1 +fi +test -s "$output/class-Cake.Example.html" +# PHP expressions must reach the PHP interpreter unchanged. +# shellcheck disable=SC2016 +php -r 'if ((fileperms($argv[1]) & 0005) !== 0005) { throw new RuntimeException("Published docs must be accessible to nginx workers."); }' "$output" +# Restoring the cache replaces old output rather than retaining stale pages. +touch "$output/stale.html" +build +grep -q 'Using cached documentation' "$workspace/log" +test ! -e "$output/stale.html" +test ! -e "$output/.complete" +# PHP expressions must reach the PHP interpreter unchanged. +# shellcheck disable=SC2016 +php -r 'if ((fileperms($argv[1]) & 0005) !== 0005) { throw new RuntimeException("Cached docs must be accessible to nginx workers."); }' "$output" + +sed 's/

/

template changed /' "$workspace/generator/templates/pages/overview.twig" > "$workspace/overview.twig" +mv "$workspace/overview.twig" "$workspace/generator/templates/pages/overview.twig" +build +if grep -q 'Using cached documentation' "$workspace/log"; then + echo 'A template edit unexpectedly reused cached documentation.' >&2 + exit 1 +fi +grep -q 'template changed' "$output/index.html" + +printf '%s\n' ' "$source_dir/src/Example.php" +git -C "$source_dir" add . +git -C "$source_dir" commit -qm 'Change source' +build +if grep -q 'Using cached documentation' "$workspace/log"; then + echo 'Changed inputs unexpectedly reused cached documentation.' >&2 + exit 1 +fi +grep -q 'second' "$output/class-Cake.Example.html" +DOCS_CACHE_REFRESH=refresh build +if grep -q 'Using cached documentation' "$workspace/log"; then + echo 'Changed inputs unexpectedly reused cached documentation.' >&2 + exit 1 +fi + +# A failed build must retain the last complete output and its useful diagnostics. +printf '%s\n' ' "$source_dir/src/Example.php" +git -C "$source_dir" add . +git -C "$source_dir" commit -qm 'Invalid source' +if build; then + echo 'Invalid source unexpectedly succeeded.' >&2 + exit 1 +fi +grep -q 'Example.php' "$workspace/log" +grep -q 'second' "$output/class-Cake.Example.html" +echo 'Release build cache checks passed.' diff --git a/tests/generator.php b/tests/generator.php new file mode 100644 index 00000000..def23ebb --- /dev/null +++ b/tests/generator.php @@ -0,0 +1,59 @@ + 'Generated overview'])); + +try { + chdir($directory); + foreach (['relative/nested', $directory . '/absolute/nested'] as $output) { + (new Generator($twig, $output))->renderOverview(); + if (file_get_contents($output . '/index.html') !== 'Generated overview') { + throw new RuntimeException('The overview must be written into the requested directory.'); + } + } + + file_put_contents('blocked', 'file'); + try { + new Generator($twig, 'blocked/nested'); + throw new LogicException('Creating a directory under a file must fail.'); + } catch (RuntimeException $error) { + if (!str_contains($error->getMessage(), 'blocked/nested')) { + throw $error; + } + } + + mkdir('unwritable/index.html', 0777, true); + try { + (new Generator($twig, 'unwritable'))->renderOverview(); + throw new LogicException('Writing a page over a directory must fail.'); + } catch (RuntimeException $error) { + if (!str_contains($error->getMessage(), '/unwritable/index.html')) { + throw $error; + } + } + + echo "Generator regression checks passed.\n"; +} finally { + chdir($originalDirectory); + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getPathname()); + } else { + unlink($file->getPathname()); + } + } + rmdir($directory); +} diff --git a/tests/loader.php b/tests/loader.php index 272fd69e..cf1f250b 100644 --- a/tests/loader.php +++ b/tests/loader.php @@ -49,6 +49,20 @@ public function name(): string throw new RuntimeException('Modern syntax must still be parsed and reflected.'); } + file_put_contents($directory . '/invalid.php', "loadFile($directory . '/invalid.php', true); + throw new LogicException('Invalid source must fail.'); + } catch (RuntimeException $error) { + if ( + !str_contains($error->getMessage(), '/invalid.php') || + !str_contains($error->getMessage(), 'line 2') || + !$error->getPrevious() instanceof PhpParser\Error + ) { + throw $error; + } + } + echo "Loader regression checks passed.\n"; } finally { foreach (glob($directory . '/*.php') as $file) { diff --git a/tests/nginx.sh b/tests/nginx.sh new file mode 100644 index 00000000..5b97318d --- /dev/null +++ b/tests/nginx.sh @@ -0,0 +1,33 @@ +#!/bin/sh +set -eu + +workspace=$(mktemp -d) +mkdir -p "$workspace/logs" +trap 'nginx -c "$workspace/nginx.conf" -p "$workspace" -s quit 2>/dev/null || true; rm -rf "$workspace"' EXIT HUP INT TERM +cat > "$workspace/nginx.conf" < "/var/www/html/cakephp/$version/index.html" + printf 'existing page' > "/var/www/html/cakephp/$version/class-Existing.html" +done +nginx -t -c "$workspace/nginx.conf" -p "$workspace" +nginx -c "$workspace/nginx.conf" -p "$workspace" +for version in 3.0 6.0 4.next; do + wget -q -O "$workspace/page" "http://127.0.0.1/cakephp/$version/class-Existing.html" + grep -q 'existing page' "$workspace/page" + # wget follows the redirect; inspect the first response's public Location. + wget -S -O /dev/null "http://127.0.0.1/cakephp/$version/class-Missing.html?test=1" 2> "$workspace/headers" || true + grep -q 'HTTP/1.1 302' "$workspace/headers" + grep -Fq "Location: /$version/?test=1" "$workspace/headers" +done +wget -S -O /dev/null 'http://127.0.0.1/cakephp/6.0/missing.css' 2> "$workspace/headers" || true +grep -q 'HTTP/1.1 404' "$workspace/headers" +echo 'Nginx public redirect checks passed.' diff --git a/tests/releases.php b/tests/releases.php new file mode 100644 index 00000000..3b7448f7 --- /dev/null +++ b/tests/releases.php @@ -0,0 +1,24 @@ + version_compare($b, $a)); +if ($ordered !== $expectedOrder || !isset($versions['1.2'], $versions['2.10'])) { + throw new RuntimeException('The selector must retain descending versions and legacy documentation links.'); +} +echo "Release manifest checks passed.\n";