diff --git a/.gitattributes b/.gitattributes index 0bc7bf6..3e0b0bc 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,8 @@ /.github export-ignore /config export-ignore +/docker export-ignore +/make export-ignore /tests export-ignore /.gitattributes export-ignore /.gitignore export-ignore +/Makefile export-ignore diff --git a/.github/workflows/php-analyze.yml b/.github/workflows/php-analyze.yml index ef1b367..bc8ace5 100644 --- a/.github/workflows/php-analyze.yml +++ b/.github/workflows/php-analyze.yml @@ -36,7 +36,7 @@ jobs: run: composer php-analyze - name: Run PHPUnit Tests - run: composer paratest + run: composer php-tests - name: Upload coverage reports to Codecov uses: codecov/codecov-action@main diff --git a/.gitignore b/.gitignore index cd280d8..6672a87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ /* !/.github !/config +!/docker !/docs +!/make !/src !/tests /tests/Kernel/var @@ -13,4 +15,5 @@ !/*.dist !/LICENSE.md +!/Makefile !/README.md diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2c1caca --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +$(shell cp -n make/dev/infrastructure/.env.dist .env && mkdir -p ./var) + +include ./.env +export $(shell sed 's/=.*//' ./.env) + +-include ./make/${APP_ENV}/**/*.mk +-include ./make/${APP_ENV}/*.mk + +RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) +$(eval $(RUN_ARGS):;@:) + +env: + cp -n make/${RUN_ARGS}/infrastructure/.env.dist .env + +down-clear: ## Down service and remove volumes. + docker-compose down --remove-orphans -v + rm -rf var vendor composer.lock docker-compose.yml + +.PHONY: help + +help: ## Display this help + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-12s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) + +.DEFAULT_GOAL := help diff --git a/composer.json b/composer.json index d2f5e6b..a282cec 100644 --- a/composer.json +++ b/composer.json @@ -56,13 +56,13 @@ "scripts": { "cs-check": "vendor/bin/php-cs-fixer fix --config config/php_cs.dist.php --dry-run", "cs-fix": "vendor/bin/php-cs-fixer fix --config config/php_cs.dist.php", - "paratest": "XDEBUG_MODE=coverage vendor/bin/paratest -c config/phpunit.xml.dist --colors --runner=WrapperRunner --coverage-clover ./coverage.xml", "php-analyze": [ "@psalm", "@cs-check", "@phplint", "@phpstan" ], + "php-tests": "XDEBUG_MODE=coverage vendor/bin/paratest -c config/phpunit.xml.dist --colors --runner=WrapperRunner --coverage-clover ./coverage.xml", "phplint": "vendor/bin/phplint -c config/phplint.yaml.dist", "phpstan": "vendor/bin/phpstan analyse -c config/phpstan.neon.dist --no-progress --memory-limit=-1", "psalm": "vendor/bin/psalm -c config/psalm.xml.dist --no-cache --threads=6 --memory-limit=-1 --shepherd", diff --git a/docker/php-cli/conf.d/timezone.ini b/docker/php-cli/conf.d/timezone.ini new file mode 100644 index 0000000..c7e09fc --- /dev/null +++ b/docker/php-cli/conf.d/timezone.ini @@ -0,0 +1 @@ +date.timezone=Europe/Moscow diff --git a/docker/php-cli/conf.d/xdebug.ini b/docker/php-cli/conf.d/xdebug.ini new file mode 100644 index 0000000..3dd6188 --- /dev/null +++ b/docker/php-cli/conf.d/xdebug.ini @@ -0,0 +1,7 @@ +xdebug.mode=${XDEBUG_MODE} +xdebug.client_host=host.docker.internal +xdebug.client_port=9003 +xdebug.idekey=PHPSTORM +xdebug.start_with_request=yes +xdebug.log_level=3 +xdebug.log="/tmp/xdebug.log" diff --git a/docker/php-cli/php-cli.dockerfile b/docker/php-cli/php-cli.dockerfile new file mode 100644 index 0000000..183ade5 --- /dev/null +++ b/docker/php-cli/php-cli.dockerfile @@ -0,0 +1,56 @@ +# syntax=docker/dockerfile:experimental +ARG php_cli_image +FROM $php_cli_image AS php-common + +ARG pear_ext_dir_date +ENV PHP_EXT_DIR "/usr/local/lib/php/extensions/no-debug-non-zts-$pear_ext_dir_date" +RUN set -ex \ + && if [ `pear config-get ext_dir` != ${PHP_EXT_DIR} ]; then echo PHP_EXT_DIR must be `pear config-get ext_dir` && exit 1; fi + +FROM php-common AS php-build +RUN --mount=type=cache,target=/var/cache/apk set -ex \ + && apk add --update-cache $PHPIZE_DEPS + +FROM php-build AS php-ext-intl +RUN --mount=type=cache,target=/var/cache/apk set -ex \ + && apk add \ + icu-dev \ + && docker-php-ext-install intl + +FROM php-build AS php-ext-xdebug +RUN set -ex \ + && apk add --update linux-headers \ + && pecl install xdebug + +FROM php-build AS php-ext-pcntl +RUN set -ex \ + && docker-php-ext-install pcntl + +FROM php-common AS php-base +COPY --from=php-ext-intl ${PHP_EXT_DIR}/intl.so ${PHP_EXT_DIR}/ +COPY --from=php-ext-intl /usr/local /usr/local +COPY --from=php-ext-pcntl ${PHP_EXT_DIR}/pcntl.so ${PHP_EXT_DIR}/ +COPY --from=php-ext-xdebug ${PHP_EXT_DIR}/xdebug.so ${PHP_EXT_DIR}/ +RUN --mount=type=cache,target=/var/cache/apk \ + set -ex \ + && apk add libpq icu shadow gettext git \ + && docker-php-ext-enable intl pcntl xdebug \ + && mv $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini + +COPY ./php-cli/conf.d /usr/local/etc/php/conf.d + +ARG user +ARG uid + +RUN addgroup $user \ + && adduser -DS -h /home/$user -u 1000 -G $user $user \ + && adduser www-data $user \ + && mkdir -p /home/$user/.composer \ + && chown -R $user:$user /home/$user + +COPY --chown=$user:$user --from=composer:latest /usr/bin/composer /usr/local/bin/composer + +USER $user:$user + +ARG app_dir +WORKDIR $app_dir diff --git a/make/dev/analyze.mk b/make/dev/analyze.mk new file mode 100644 index 0000000..73e625c --- /dev/null +++ b/make/dev/analyze.mk @@ -0,0 +1,5 @@ +php-analyze: ## Run static analyze - phpcs, phplint, phpstan, psalm. + docker-compose run --rm php-cli composer php-analyze + +php-cs-fix: + docker-compose run --rm php-cli composer cs-fix diff --git a/make/dev/composer.mk b/make/dev/composer.mk new file mode 100644 index 0000000..65ad976 --- /dev/null +++ b/make/dev/composer.mk @@ -0,0 +1,8 @@ +composer-install: ## Install composer dependecies. + docker-compose run --rm php-cli composer install --no-interaction --no-progress + +composer-normalize: + docker-compose run --rm php-cli composer normalize + +composer-require: + docker-compose run --rm php-cli composer req ${RUN_ARGS} diff --git a/make/dev/docker.mk b/make/dev/docker.mk new file mode 100644 index 0000000..dac7d03 --- /dev/null +++ b/make/dev/docker.mk @@ -0,0 +1,8 @@ +docker-compose: + envsubst < make/${APP_ENV}/infrastructure/docker-compose.yml > docker-compose.yml + +docker-build: ## Buid dev images + docker-compose build + +docker-up: ## Start service. + docker-compose up -d diff --git a/make/dev/infrastructure/.env.dist b/make/dev/infrastructure/.env.dist new file mode 100644 index 0000000..aa99085 --- /dev/null +++ b/make/dev/infrastructure/.env.dist @@ -0,0 +1,19 @@ +# app. +APP_NAME=jwt-core +APP_VERSION=0.3.1 +APP_DIR=/app +APP_ENV=dev +APP_DEBUG=true +APP_SECRET=11b2c9dfc2dbecf56941327b7c84cdf9 +# php. +PHP_VERSION=8.3.1 +PHP_CLI_IMAGE=php:${PHP_VERSION}-cli-alpine +XDEBUG_MODE=develop,debug,coverage +# php 8.2.1 +# PEAR_EXT_DIR_DATE=20220829 +# php 8.3.1 +PEAR_EXT_DIR_DATE=20230831 +# docker. +COMPOSE_DOCKER_CLI_BUILD=1 +DOCKER_BUILDKIT=1 +UID=1000 diff --git a/make/dev/infrastructure/docker-compose.yml b/make/dev/infrastructure/docker-compose.yml new file mode 100644 index 0000000..9ecced2 --- /dev/null +++ b/make/dev/infrastructure/docker-compose.yml @@ -0,0 +1,22 @@ +version: "3.9" + +services: + php-cli: + build: + context: ./docker + dockerfile: php-cli/php-cli.dockerfile + args: + php_cli_image: ${PHP_CLI_IMAGE} + app_dir: ${APP_DIR} + user: ${USER} + uid: ${UID} + pear_ext_dir_date: ${PEAR_EXT_DIR_DATE} + env_file: [ .env ] + networks: [ backend ] + dns: [ 8.8.4.4, 8.8.8.8 ] + extra_hosts: [ "host.docker.internal:host-gateway" ] + volumes: + - .:${APP_DIR}:rw + +networks: + backend: diff --git a/make/dev/install.mk b/make/dev/install.mk new file mode 100644 index 0000000..3a17a89 --- /dev/null +++ b/make/dev/install.mk @@ -0,0 +1,5 @@ +prepare: + @make env dev + @make docker-compose dev + +install: prepare docker-build docker-up composer-install ## Build & run app developments containers. diff --git a/make/dev/phpunit.mk b/make/dev/phpunit.mk new file mode 100644 index 0000000..35d690f --- /dev/null +++ b/make/dev/phpunit.mk @@ -0,0 +1,2 @@ +php-tests: ## Run phpunit tests. + docker-compose run --rm php-cli composer php-tests diff --git a/tests/Kernel/Infrastructure/Http/Security/Authentication/EntryPoint.php b/tests/Kernel/Infrastructure/Http/Security/Authentication/EntryPoint.php index 0b1f694..5bae5fe 100644 --- a/tests/Kernel/Infrastructure/Http/Security/Authentication/EntryPoint.php +++ b/tests/Kernel/Infrastructure/Http/Security/Authentication/EntryPoint.php @@ -17,7 +17,7 @@ public function __construct(private UrlGeneratorInterface $urlGenerator) { } - public function start(Request $request, AuthenticationException $authException = null): RedirectResponse + public function start(Request $request, ?AuthenticationException $authException = null): RedirectResponse { return new RedirectResponse($this->urlGenerator->generate(Login\Controller::class)); }