FROM php:7.4-apache AS base
WORKDIR /var/www/html

RUN apt update && \
    apt install -y \
    libicu-dev \
    libgd-dev \
    libonig-dev \
    unzip && \
    apt purge -y --auto-remove && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

RUN docker-php-ext-install \
    exif \
    gd \
    intl \
    mbstring \
    mysqli \
    pdo_mysql

FROM composer:2 AS build
WORKDIR /var/www/html

COPY composer.json .
COPY composer.lock .
RUN composer install --no-dev --no-scripts --ignore-platform-reqs

COPY . .
RUN composer dumpautoload --optimize

FROM base AS final
COPY --from=build /var/www/html /var/www/html

RUN sed -ri -e 's!/var/www/!/var/www/html!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN sed -ri -e 's!AllowOverride None!AllowOverride All!g' /etc/apache2/apache2.conf

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN sed -ri -e 's!upload_max_filesize = 2M!upload_max_filesize = 8M!g' $PHP_INI_DIR/php.ini

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

VOLUME /var/www/html