name: "Install Composer deps"

inputs:
  flags:
    description: 'Composer flags'
    required: false

runs:
  using: "composite"
  steps:
    # Warning!
    # Use $GITHUB_ENV instead of $GITHUB_OUTPUT.
    #
    # The actions/cache is, on calling it in this composite action, reading the cache,
    # and then also register a shutdown function for the whole workflow, to write the cache afterwards.
    #
    # That registered shutdown function does _NOT_ have access to inputs.* nor steps.*.outputs for
    # `path` parameter, which is evaluated on both stages (read and write), yet `key` parameters
    # are evaluated on start only and are not affected by this issue.
    #
    # Unfortunately, when the issue is faced, it does not break the build, simply raises a warning
    # and completing the build successfully, yet without storing the cache:
    # > Warning: Input required and not supplied: path
    #
    # Refs:
    # - https://github.com/actions/cache/issues/638#issuecomment-1793564996
    # - https://github.com/actions/cache/issues/803#issuecomment-1793565071
    # - https://github.com/actions/runner/issues/2009#issuecomment-1793565031

    - name: Get Composer cache params
      shell: bash
      run: |
        echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
        echo "COMPOSER_CACHE_PHP=$(php -r 'echo PHP_VERSION;')" >> $GITHUB_ENV
        echo "PHP_VERSION_ID=$(php -r 'echo PHP_VERSION_ID;')" >> $GITHUB_ENV

    - name: Cache Composer dependencies
      uses: actions/cache@v4
      with:
        path: ${{ env.COMPOSER_CACHE_DIR }}
        key: Composer-${{ runner.os }}-${{ env.COMPOSER_CACHE_PHP }}-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          Composer-${{ runner.os }}-${{ env.COMPOSER_CACHE_PHP }}-${{ hashFiles('**/composer.json') }}-
          Composer-${{ runner.os }}-${{ env.COMPOSER_CACHE_PHP }}-
          Composer-${{ runner.os }}-

    - name: Install dependencies
      uses: nick-invision/retry@v3
      with:
        timeout_minutes: 5
        max_attempts: 5
        retry_wait_seconds: 30
        # `--no-scripts` to avoid side-effects (e.g. installing dev-tools for all jobs on CI level),
        # all executed scripts should be explicit and run only when needed.
        command: composer update --optimize-autoloader --no-interaction --no-progress --no-scripts ${{ inputs.flags }}

    - name: Show versions of packages
      shell: bash
      run: composer info -D
