FROM php:8.4-apache

# Variables
ARG SYMFONY_PROJECT_NAME="symfony"
ARG SYMFONY_VERSION=7.1
ARG SYMFONY_DEFAULT_PACKAGES="webapp"
ARG SYMFONY_SERVER_MODE="dev"
ARG PHP_MEMORY_LIMIT="512M"
ARG PHP_MAX_EXECUTION_TIME=120
ARG PHP_POST_MAX_SIZE="512M"
ARG PHP_UPLOAD_MAX_FILESIZE="512M"

# Set environment variables
ENV SYMFONY_PROJECT_NAME=${SYMFONY_PROJECT_NAME}
ENV SYMFONY_VERSION=${SYMFONY_VERSION}
ENV SYMFONY_DEFAULT_PACKAGES=${SYMFONY_DEFAULT_PACKAGES}
ENV SYMFONY_SERVER_MODE=${SYMFONY_SERVER_MODE}
ENV PHP_MEMORY_LIMIT=${PHP_MEMORY_LIMIT}
ENV PHP_MAX_EXECUTION_TIME=${PHP_MAX_EXECUTION_TIME}
ENV PHP_POST_MAX_SIZE=${PHP_POST_MAX_SIZE}
ENV PHP_UPLOAD_MAX_FILESIZE=${PHP_UPLOAD_MAX_FILESIZE}

# User configuration for permissions
ENV USER_ID=1000
ENV GROUP_ID=1000

# Dependencies
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    libzip-dev \
    libpng-dev \
    libicu-dev \
    libpq-dev \
    libxml2-dev \
    libjpeg-dev \
    libfreetype6-dev \
    locales \
    zlib1g-dev \
    libonig-dev \
    curl \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install intl opcache zip gd pdo pdo_mysql pdo_pgsql \
    && a2enmod rewrite \
    && a2enmod headers

# Code coverage driver of the tests (composer test:coverage). Installed but off, so that the requests of the
# development server are not slowed down: the coverage run turns it on with `-d pcov.enabled=1`.
RUN pecl install pcov \
    && docker-php-ext-enable pcov \
    && echo "pcov.enabled=0" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini

# Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

# Symfony Install
RUN curl -sS https://get.symfony.com/cli/installer | bash \
    && mv /root/.symfony*/bin/symfony /usr/local/bin/symfony

# Working directory
WORKDIR /var/www/html

# Configure Apache
COPY ./config/apache-symfony.conf /etc/apache2/sites-available/000-default.conf
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# Copy entrypoint script
COPY ./scripts /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/setup.sh
RUN chmod +x /usr/local/bin/run.sh

RUN chown -R www-data:www-data /var/www/html

# Expose port
EXPOSE 80

# Set the entrypoint
ENTRYPOINT ["entrypoint.sh"]
