Usual Makefile commands on symfony project


Aug 12, 2023 Symfony


Use Makefile to work faster on symfony project using docker


When you are working on symfony app as a senior developer you need to know about Makefile.

A Makefile contains shortcut commands that help you gain time for certain actions. Example : instead of connecting to ssh to your container and running command “./bin/console cache:clear” you will just type “make cc” from the root directory of your project.

Let us see this in action :

First, install make utility on your computer, just type “install make” on google to install make utility (https://www.gnu.org/software/make/#download)

Then create on the root directory of your project a file named : Makefile

Below is a list of usual actions that I have used on symfony projects I worked on :

We suppose that we have a php container named my_php

####### initialize docker-compose command in a variable
DOCKER_COMPOSE  = docker-compose
# initialize docker-compose exec command on php container in a variable
EXEC_PHP            = docker exec -it  my_php

####### print text on different colors
GREEN = echo
"\x1b[32m\#\# $1\x1b[0m"
RED = echo
"\x1b[31m\#\# $1\x1b[0m"
define showMessage
        @echo
"\033[92m#######"
        @echo
"#######  $(1)"
        @echo
"#######\033[39m"
endef

################################
####### Docker commands
################################

# build : build containers with the latest version
.PHONY: build
build:
        
$(DOCKER_COMPOSE) build --no-cache --pull

# up: build and start all docker container
.PHONY: up
up:
        
$(DOCKER_COMPOSE) up -d

# down : stop and remove containers, networks
.PHONY: down
down:
        @
$(DOCKER_COMPOSE) down  --volumes --remove-orphans

# kill : force stop service containers.
.PHONY: kill
kill:
        @
$(DOCKER_COMPOSE) kill

# kill_all : remove containers for services not defined in the Compose file, remove images used by services, remove named volumes declared
.PHONY: kill_all
kill_all:
        @
$(DOCKER_COMPOSE) kill
        @
$(DOCKER_COMPOSE) down --volumes --remove-orphans --rmi all

# clean : remove all unused images not just dangling ones
.PHONY: clean
clean:
        
$(call RED, " Cleaning started ")
        docker system prune --all
        
$(call GREEN, " Cleaning finished ")

# ssh_my_php : login in ssh into the php containe
.PHONY: ssh_my_php
ssh_my_php:
        @
$(EXEC_PHP) bash

################################
####### SYMFONY commands
################################

# clear_cache : clear symfony cache
.PHONY: clear_cache
clear_cache: # Clear cache symfony
        @
$(EXEC_PHP) php -d memory_limit=-1 bin/console cache:clear --env=dev

################################
####### COMPOSER commands
################################

# update_specific_vendor : update specific library version from the vendor
.PHONY: update_specific_vendor
update_specific_vendor:
        @
$(EXEC_PHP) composer update -i

# update_all_vendor : update all vendors
.PHONY: update_all_vendor
update_all_vendor:
        @
$(EXEC_PHP) composer update

################################
####### INIT PROJECTS
################################
# init : initialize project
.PHONY: init
init:
        @
$(call showMessage, "Deleting containers if exist")
        
$(MAKE) down
        @
$(call showMessage, "Building images")
        
$(MAKE) build
        @
$(call showMessage, "Starting containers")
        
$(MAKE) up
        @
$(call showMessage, "Project is successfully initialized ")

################################
####### HELPERS
################################
# list : list all available target
.PHONY: list
list:
   
# make sure that the next line is indented by exactly 1 tab char (spaces do not work).
        @LC_ALL=C
$(MAKE) -pRrq -f $(firstword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/(^|\n)# Files(\n|$$)/,/(^|\n)# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | grep -E -v -e '^[^[:alnum:]]' -e '^$@$$'

# help : show documentation of all targets
.PHONY: help
help:
        @grep '^
# ' Makefile | sed 's/#.* \(.*\) # \(.*\)/\1 \2/' | expand -t20 | sort


symfony
makefile
php