FROM docker.io/php:8.3-zts-alpine3.18 AS UNIT_BUILDER

ARG UNIT_VERSION=1.31.1

RUN apk --no-cache add pcre2-dev gcc git musl-dev make && \
    mkdir -p /usr/lib/unit/modules && \
    git clone https://github.com/nginx/unit.git && \
    cd unit && \
    git checkout $UNIT_VERSION && \
    ./configure --prefix=/var --statedir=/var/lib/unit --runstatedir=/var/run --control=unix:/run/unit/control.unit.sock --log=/var/log/unit.log --user=www-data --group=www-data --tmpdir=/tmp --modulesdir=/var/lib/unit/modules && \
    ./configure php && \
    make && \
    make install

FROM docker.io/php:8.3-zts-alpine3.18 as BUILD

COPY --from=UNIT_BUILDER /var/sbin/unitd /usr/sbin/unitd
COPY --from=UNIT_BUILDER /var/lib/unit/ /var/lib/unit/

COPY docker/unit-config.json /docker-entrypoint.d/config.json

RUN apk --no-cache add pcre2 libbz2 libpng libwebp libjpeg-turbo icu-libs freetype oniguruma libzip \
    && apk add --no-cache --virtual .phpize-deps icu-dev libpng-dev bzip2-dev libwebp-dev libjpeg-turbo-dev freetype-dev oniguruma-dev libzip-dev pcre2-dev ${PHPIZE_DEPS} \
    && docker-php-ext-configure intl --enable-intl && \
    docker-php-ext-configure bcmath --enable-bcmath && \
    docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp && \
    docker-php-ext-install -j$(nproc) gd && \
    docker-php-ext-install bcmath &&\
    docker-php-ext-install pdo \
        mysqli pdo_mysql \
        intl mbstring \
        zip pcntl \
        exif opcache bz2 \
        calendar \
    && pear update-channels && pecl update-channels \
    && pecl install redis && docker-php-ext-enable redis \
    && rm -rf /tmp/pear \
    && docker-php-source delete \
    && apk del .phpize-deps \
    && rm -rf /var/cache/apk/* && rm -rf /etc/apk/cache \
    && rm -rf /usr/share/php && rm -rf /tmp/* \
    && rm "$PHP_INI_DIR/php.ini-development" \
    && mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
    && mkdir -p /tmp/php/upload \
    && mkdir -p /tmp/php/sys \
    && mkdir -p /tmp/php/session \
    && chown -R www-data:www-data /tmp/php \
    && ln -sf /dev/stdout /var/log/unit.log \
    && addgroup -S unit && adduser -S unit -G unit

FROM BUILD as APP_BUILD_FOR_PRODUCTION
WORKDIR /home/app

COPY application /home/app

RUN apk --no-cache add git nodejs npm \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && composer install --optimize-autoloader --no-dev \
    && npm install && npm run build \
    && rm -rf /home/app/node_modules /home/app/.env


FROM BUILD AS PRODUCTION

COPY --from=APP_BUILD_FOR_PRODUCTION /home/app /var/www/html
COPY docker/docker-entrypoint_prod.sh /home/unit/docker-entrypoint.sh

WORKDIR /var/www/html

RUN chmod 755 /home/unit/docker-entrypoint.sh

STOPSIGNAL SIGTERM

ENTRYPOINT ["/home/unit/docker-entrypoint.sh"]
EXPOSE 9000
COPY docker/start_prod.sh /usr/local/bin/start
CMD ["/usr/local/bin/start"]


FROM BUILD AS DEVELOP

WORKDIR /var/www/html

COPY docker/docker-entrypoint_dev.sh /home/unit/docker-entrypoint.sh

STOPSIGNAL SIGTERM

RUN chmod 755 /home/unit/docker-entrypoint.sh \
    && apk --no-cache add git nodejs npm \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

ENTRYPOINT ["/home/unit/docker-entrypoint.sh"]

EXPOSE 9000

COPY docker/start_dev.sh /usr/local/bin/start
CMD ["/usr/local/bin/start"]

FROM BUILD AS ARTISAN
WORKDIR /var/www/html
STOPSIGNAL SIGTERM
ENTRYPOINT ["php", "/var/www/html/artisan"]


FROM BUILD AS COMPOSER
WORKDIR /var/www/html
STOPSIGNAL SIGTERM
RUN apk --no-cache add git \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && mkdir /.composer && chmod 0777 /.composer
ENTRYPOINT ["composer"]



FROM BUILD AS NPM
RUN mkdir "/.npm" && chmod -R 0777 "/.npm"
WORKDIR /var/www/html
STOPSIGNAL SIGTERM
RUN apk --no-cache add nodejs npm
ENTRYPOINT ["npm"]
