include .makefiles/common.mk
include .makefiles/auth.mk

PROJECT = cloudflare-bundle

DOCKER_COMPOSE_OVERRIDE ?= dev
export COMPOSER_PROJECT_NAME=cloudflare-bundle

ifeq (,$(shell which docker))
TB = bash -c
TB_NODEPS = ${TB}
else
TB_OPTS = $(shell [ -t 0 ] || echo '-T')
TB = docker-compose exec $(TB_OPTS) php /scripts/toolbox.sh bash -c
TB_NODEPS = docker-compose run $(TB_OPTS) -e USER_ID=${USER_ID} -e GROUP_ID=${GROUP_ID} --rm --no-deps --entrypoint=/scripts/toolbox.sh php bash -c
endif

## —— Docker 🐳 ———————————————————————————————————————————————————————————————————————

.PHONY: up
up: docker-compose.override.yml ## start the containers
	docker-compose up --detach

.PHONY: build
build: docker-compose.override.yml ## build docker images
	docker-compose build --pull

start: build up ## build and start the containers

.PHONY: down
down: ## Stop local stack
	docker-compose down --remove-orphans

.PHONY: down-full
down-full: docker-compose.override.yml ## Stop local stack and remove volumes and local images
	docker-compose down --remove-orphans --volumes --rmi local

.PHONY: logs
logs:
	docker-compose logs --tail=100 --follow

docker-compose.override.yml: docker-compose.$(DOCKER_COMPOSE_OVERRIDE).yml
	@test -f $@ \
		&& $(call color_text,/!\ $< might have been modified - remove $@ to be up-to-date,31) \
		|| ( echo "Copy $@ from $<"; cp $< $@ )

## —— Application 🖥️  ——————————————————————————————————————————————————————————————————

.PHONY: php_install
php_install:
	$(TB) "composer install"

.PHONY: install
install: php_install

.PHONY: sh
sh: ## open a shell into php container
	$(TB) zsh

## —— Tests ✅ ————————————————————————————————————————————————————————————————————————

test: phpunit.xml.dist ## Run tests
	@$(eval suite ?= 'all')
	@$(eval filter ?= '.')
	$(TB) './vendor/bin/.phpunit/phpunit/bin/simple-phpunit --testsuite=$(suite) --filter=$(filter) --stop-on-failure'

## —— Coding standards / Quality ✨ ————————————————————————————————————————————————————

.PHONY: lint
lint: ## Dry-run PHP lint
	$(TB) 'DRY_RUN=1 .php-cs-fixer/format.sh;'

.PHONY: lint-fix
lint-fix: ## Fix PHP lint
	$(TB) '.php-cs-fixer/format.sh;'

.PHONY: phpstan
phpstan: ## Run PHPStan
	$(TB) 'vendor/bin/phpstan analyse src/ tests --level 7'

.PHONY: security
security:
	$(TB) 'local-php-security-checker  --path=./composer.lock'

.PHONY: rector
rector: ## Run Rector
	$(TB) 'bin/rector'
