# syntax=docker/dockerfile:1
FROM node:12 as assets

WORKDIR /appsource

# First copy only dependencies related info for better caching
COPY package.json yarn.lock ./
COPY assets/fontawesome/fontawesome-pro-5.15.4-web assets/fontawesome/fontawesome-pro-5.15.4-web

RUN yarn

COPY . .
RUN yarn run build

FROM php:7.4-fpm-alpine

# Timezone
RUN ln -snf /usr/share/zoneinfo/Europe/Warsaw /etc/localtime
RUN echo "date.timezone = Europe/Warsaw" >> /usr/local/etc/php/conf.d/datetime.ini;

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
COPY --from=composer:1 /usr/bin/composer /usr/local/bin/

# Box
RUN wget https://github.com/humbug/box/releases/download/3.8.4/box.phar \
    && mv box.phar /usr/local/bin/box \
    && chmod +x /usr/local/bin/box

RUN install-php-extensions sockets redis zip curl;

ARG BUILD_MODE=prod
RUN set -eux; \
    if [ "$BUILD_MODE" = "dev" ]; then \
        install-php-extensions xdebug; \
        { \
            echo "xdebug.discover_client_host=1"; \
            echo "xdebug.mode=debug"; \
        } > "$PHP_INI_DIR"/conf.d/90-xdebug.ini; \
    elif [ "$BUILD_MODE" = "prod" ]; then \
        install-php-extensions opcache; \
        { \
            echo "opcache.enable_cli=1"; \
            echo "opcache.max_accelerated_files=40000"; \
        } > "$PHP_INI_DIR"/conf.d/90-opcache.ini; \
    fi

WORKDIR /var/www

COPY composer.* ./

RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --no-progress --no-interaction && \
    composer check-platform-reqs

COPY . .

RUN composer dump-autoload --optimize && \
    composer run-script post-install-cmd

COPY --link --from=assets /appsource/public/build ./public/build
