---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    - name: Check if requested version parameters are valid
      fail:
        msg: You cannot request a specific version AND keep the composer up to date.
          Set either composer_version or composer_keep_updated, but not both.
      when: composer_version != '' and composer_keep_updated
    
    - name: Set php_executable variable to a default if not defined.
      set_fact:
        php_executable: php
      when: php_executable is not defined
    
    - name: Check if Composer is installed.
      stat: "path={{ composer_path }}"
      register: composer_bin
    
    - name: Get Composer installer signature.
      uri:
        url: https://composer.github.io/installer.sig
        return_content: true
      register: composer_installer_signature
      when: not composer_bin.stat.exists
    
    - name: Download Composer installer.
      get_url:
        url: https://getcomposer.org/installer
        dest: /tmp/composer-installer.php
        mode: 0755
        checksum: "sha384:{{ composer_installer_signature.content }}"
      when: not composer_bin.stat.exists
    
    - name: Run Composer installer.
      command: >
        {{ php_executable }} composer-installer.php {% if composer_version_branch %} {{ composer_version_branch }}{% elif composer_version %} --version={{ composer_version }}{% endif %}
        chdir=/tmp
      when: not composer_bin.stat.exists
    
    - name: Move Composer into globally-accessible location.
      command: >
        mv /tmp/composer.phar {{ composer_path }}
        creates={{ composer_path }}
      when: not composer_bin.stat.exists
    
    - name: Update Composer to latest version (if configured).
      command: >
        {{ php_executable }} {{ composer_path }} self-update {{ composer_version_branch }}
      register: composer_update
      changed_when: "'Updating to version' in composer_update.stdout"
      when: composer_keep_updated | bool
    
    - name: Ensure composer directory exists.
      become: true
      become_user: "{{ composer_home_owner }}"
      file:
        path: "{{ composer_home_path }}"
        owner: "{{ composer_home_owner }}"
        group: "{{ composer_home_group }}"
        state: directory
        mode: 0755
    
    - name: Add GitHub OAuth token for Composer (if configured).
      become: true
      become_user: "{{ composer_home_owner }}"
      template:
        src: "auth.json.j2"
        dest: "{{ composer_home_path }}/auth.json"
        owner: "{{ composer_home_owner }}"
        group: "{{ composer_home_group }}"
        mode: 0644
      when: composer_github_oauth_token | length > 0
    
    - include_tasks: global-require.yml
      when: composer_global_packages | length > 0
    
    - include_tasks: project-bin.yml
      when: composer_add_project_to_path | bool
