name: Main workflow
on:
  workflow_dispatch:
  pull_request:
    branches:
      - master
      - develop
      - verbose
    paths-ignore:
      - '**.md'
      - 'examples/**'
  push:
    branches:
      - master
      - develop
      - verbose
    paths-ignore:
      - '**.md'
      - 'examples/**'
permissions:
  contents: read
env:
  default-php-version: '8.2'
jobs:
  run:
    name: Run
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-24.04, ubuntu-22.04, ubuntu-20.04, windows-2022, macos-13]
        php-versions: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
        include:
          - operating-system: ubuntu-24.04
            php-versions: ''
            php-version-file: 'php-version-file'
    env:
      extensions: xml, opcache, xdebug, pcov, gd
      key: cache-v5
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@develop
        with:
          php-version: ${{ matrix.php-versions || env.default-php-version }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v4
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Stage php-version-file
        if: ${{ matrix.php-version-file == 'php-version-file' }}
        run: |
          echo ${{ env.default-php-version }} > php-version-file

      - name: Setup PHP with extensions and custom config
        run: node dist/index.js
        env:
          php-version: ${{ matrix.php-versions }}
          php-version-file: ${{ matrix.php-version-file }}
          extensions: ${{ env.extensions }}
          ini-values: post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata

      - name: Testing PHP version
        run: |
          php -v
          php -r "if(strpos(phpversion(), '${{ matrix.php-versions || env.default-php-version }}') === false) {throw new Exception('Wrong PHP version Installed');}"

      - name: Testing Composer version
        run: |
          composer -V
          php -r "if(strpos(@exec('composer -V'), 'Composer version') === false) {throw new Exception('Composer not found');}"
      - name: Testing Extensions
        run: |
          php -m
          php -r "if(! extension_loaded('gd')) {throw new Exception('gd not found');}"
          php -r "if(! extension_loaded('xml')) {throw new Exception('xml not found');}"
          php -r "if(! extension_loaded('Xdebug')) {throw new Exception('Xdebug not found');}"
          php -r "if(phpversion()>=7.1 && ! extension_loaded('pcov')) {throw new Exception('PCOV not found');}"
      - name: Testing ini values
        run: |
          php -r "if(ini_get('memory_limit')!='-1') {throw new Exception('memory_limit not disabled');}"
          php -r "if(ini_get('post_max_size')!='256M') {throw new Exception('post_max_size not added');}"
          php -r "if(ini_get('short_open_tag')!=1) {throw new Exception('short_open_tag not added');}"
          php -r "if(ini_get('date.timezone')!='Asia/Kolkata') {throw new Exception('date.timezone not added');}"