name: "Setup PHP Project"
description: "Sets up a PHP project with GitHub Actions, including detecting PHP version and running composer install."
branding:
  color: "orange"
  icon: "arrow-up-circle"

inputs:
  working_directory:
    description: "The directory where the project is located."
    required: false
    default: "."
  php_tools:
    description: "The PHP tools to install. Comma seperated list from shivammathur/setup-php"
    required: false
    default: ""

outputs:
  php_version:
    description: "The PHP version that was installed, influenced by composer.json"
    value: ${{ steps.emit-php-detected.outputs.php_version }}

runs:
  using: "composite"
  steps:
    - uses: actions/checkout@v4
    - id: read-php-version
      name: "Read PHP Version"
      shell: bash
      working-directory: ${{ inputs.working_directory }}
      run: |
        # if composer.json exists, set the php_version output
        if [ -f composer.json ]; then
          # And that the require php key exists
            echo -n "composer.json exists"
            if jq -e '.require["php"]' composer.json > /dev/null; then
                php_version=$(jq -r '.require["php"]' composer.json | sed -E 's/[^0-9.]//g')
                echo "and detected PHP version $php_version from composer.json!"
                echo "php_version=$php_version" >> $GITHUB_OUTPUT
                echo "has_composer=true" >> $GITHUB_OUTPUT
                exit 0
            else
                echo " ... But does not specify a PHP version!"
                echo "Please go set a PHP version in the require key of your composer.json"
                exit 1;
            fi
        fi
        echo "No PHP version detected"
        exit 0;
    - uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ steps.read-php-version.outputs.php_version }}
        tools: ${{ inputs.php_tools }}
    - id: composer-cache-find
      name: "Find Composer Cache"
      if: ${{ steps.read-php-version.outputs.has_composer }}
      shell: bash
      run: |
        {
          echo "dir=$(composer config cache-files-dir)" 
          echo "key=${{ runner.os }}-${{ inputs.working_directory }}-composer-${{ hashFiles('**/composer.lock') }}" 
          echo "restore-key=${{ runner.os }}-${{ inputs.working_directory }}-composer-"
        } >> $GITHUB_OUTPUT
    - id: composer-cache-restore
      if: ${{ steps.read-php-version.outputs.has_composer }}
      uses: actions/cache/restore@v4
      with:
        path: ${{ steps.composer-cache-find.outputs.dir }}
        key: ${{ steps.composer-cache-find.outputs.key }}
        restore-keys: ${{ steps.composer-cache-find.outputs.restore-key }}
    - working-directory: ${{ inputs.working_directory }}
      if: ${{ steps.read-php-version.outputs.has_composer }}
      shell: bash
      run: composer install --ignore-platform-reqs --prefer-dist
    - id: composer-cache-save
      if: ${{ always() && steps.read-php-version.outputs.has_composer }}
      uses: actions/cache/save@v4
      with:
        path: ${{ steps.composer-cache-find.outputs.dir }}
        key: ${{ steps.composer-cache-restore.outputs.cache-primary-key }}
    - id: emit-php-detected
      name: "Emit PHP Detected"
      shell: bash
      run: |
        PHP_VERSION=$(php -v | head -n 1 | cut -d ' ' -f 2 | cut -d '.' -f 1,2)
        echo "PHP Version: $PHP_VERSION"
        echo "PHP Tools: ${{ inputs.php_tools }}"
        if [ ${{ steps.read-php-version.outputs.has_composer }} == "true" ]; then
          echo "Composer Cache Dir: ${{ steps.composer-cache-find.outputs.dir }}"
          echo "Composer Cache Key: ${{ steps.composer-cache-find.outputs.key }}"
          echo "Composer Cache Restore Key: ${{ steps.composer-cache-find.outputs.restore-key }}"
        fi
        # Emit installed php version by major release
        echo "PHP_VERSION=$PHP_VERSION" >> $GITHUB_ENV
        echo "php_version=$PHP_VERSION" >> $GITHUB_OUTPUT