# Stage 1 : PHP build
# php:8.5.10-alpine amd64
FROM php@sha256:458a35f5b39d64293acde1c7cdc3af19864e60929570cc329fc44d7a5d45ae9e AS builder

ARG http_proxy
ENV http_proxy=${http_proxy} \
    https_proxy=${http_proxy} \
    HTTP_PROXY=${http_proxy} \
    HTTPS_PROXY=${http_proxy}

RUN apk add --no-cache \
    $PHPIZE_DEPS \
    linux-headers \
    zlib-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    libwebp-dev \
    libxpm-dev \
    icu-dev \
    gmp-dev \
    libxslt-dev \
    gettext-dev \
    libzip-dev \
    openldap-dev \
    git

RUN docker-php-ext-configure gd \
        --with-freetype \
        --with-jpeg \
        --with-webp \
        --with-xpm \
    && docker-php-ext-configure ldap \
    && docker-php-ext-install -j"$(nproc)" \
        bcmath \
        exif \
        gettext \
        gmp \
        mysqli \
        pcntl \
        pdo_mysql \
        posix \
        sockets \
        xsl \
        zip \
        gd \
        intl \
        ldap

# redis install
RUN curl -fsSL https://github.com/phpredis/phpredis/archive/refs/tags/6.3.0.tar.gz -o /tmp/redis.tar.gz \
    && tar -xzf /tmp/redis.tar.gz -C /tmp \
    && cd /tmp/phpredis-6.3.0 \
    && phpize \
    && ./configure \
    && make -j"$(nproc)" \
    && make install \
    && rm -rf /tmp/redis*

# pinned PCOV install
ARG PCOV_COMMIT=e8b16fae2dbb56e1cfd6f2be6d56c74c4eb1823b
RUN mkdir -p /tmp/pcov \
    && cd /tmp/pcov \
    && git init \
    && git remote add origin https://github.com/krakjoe/pcov.git \
    && git fetch --depth 1 origin "${PCOV_COMMIT}" \
    && git checkout FETCH_HEAD \
    && phpize \
    && ./configure \
    && make -j"$(nproc)" \
    && make install \
    && strip --strip-all /usr/local/lib/php/extensions/no-debug-non-zts-*/*.so \
    && rm -rf /tmp/pcov


# Stage 2 : Image Build
# php:8.5.10-alpine amd64
FROM php@sha256:458a35f5b39d64293acde1c7cdc3af19864e60929570cc329fc44d7a5d45ae9e

# Composer install
COPY --from=composer/composer:2-bin /composer /usr/local/bin/composer

# Runtime libs + git + tini for entry point
RUN apk add --no-cache \
    git \
    tini \
    zlib \
    freetype \
    libjpeg-turbo \
    libpng \
    libwebp \
    libxpm \
    libx11 \
    icu-libs \
    gmp \
    libxslt \
    libintl \
    libzip \
    libldap \
    # Php minimal configuration
    && printf '%s\n' \
        '# composer php cli ini settings' \
        'date.timezone=UTC' \
        'memory_limit=-1' \
        'opcache.enable_cli=1' \
        > "$PHP_INI_DIR/php-cli.ini"

ENV COMPOSER_ALLOW_SUPERUSER=1 \
    COMPOSER_HOME=/tmp

# Git ownership fix
RUN git config --global --add safe.directory '*'

# Copy php extensions
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/

RUN docker-php-ext-enable \
    bcmath \
    exif \
    gd \
    gettext \
    gmp \
    intl \
    ldap \
    mysqli \
    pcntl \
    pdo_mysql \
    posix \
    sockets \
    xsl \
    zip \
    redis \
    pcov

# Copy entry point
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

WORKDIR /project

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["composer"]
