- name: Install apt-transport-https required for sury repo
  apt: pkg=apt-transport-https state=present

- name: Add sury repo
  copy: src=sury-php.list dest=/etc/apt/sources.list.d/sury-php.list
  register: dotdeb_repo
  when: ansible_distribution == 'Debian'

- name: Remove old dotdeb repo
  file: path=/etc/apt/sources.list.d/dotdeb.list state=absent

- name: Install sury gpg key
  apt_key: url=https://packages.sury.org/php/apt.gpg
  when: ansible_distribution == 'Debian'

- name: Update apt cache
  apt: update_cache=yes
  when: dotdeb_repo.changed

- name: Install ppa:ondrej/php
  apt_repository: repo="ppa:ondrej/php" state=present
  when: ansible_distribution == 'Ubuntu'

- name: Install php5.6 (debian default-php)
  include: php-packages.yml php_package_prefix=php5
  when: (ansible_distribution == 'Debian' and '5.6' in php_install_versions)

- name: Install php5.6 (ubuntu)
  include: php-packages.yml php_package_prefix=php5.6
  when: ansible_distribution == 'Ubuntu' and '5.6' in php_install_versions

- name: Install php7.0
  include: php-packages.yml php_package_prefix=php7.0
  when: >
    '7.0' in php_install_versions

- name: Set default cli version
  alternatives: name=php path=/usr/bin/{{ (php_default_cli_version == '5.6' and (ansible_distribution == 'Debian')) | ternary('php5', 'php' + php_default_cli_version) }}

# Fix https://github.com/oerdnj/deb.sury.org/issues/266
- name: Disable php*-fpm apache config
  command: a2disconf {{ item }}
  with_items:
    - php5.6-fpm
    - php7.0-fpm
    - php5-fpm
  changed_when: False
  failed_when: False

- name: Install curl for composer installation
  apt: pkg=curl state=present

- name: Install composer
  shell:
    curl -sS https://getcomposer.org/installer | /usr/bin/php -- --install-dir=/usr/local/bin && /bin/mv -f /usr/local/bin/composer.phar /usr/local/bin/composer
    creates=/usr/local/bin/composer

- name: Enable php5.6-fpm apache config
  command: a2enconf php5.6-fpm
  notify:
    - apache-restart