Repository: ansible-collections/community.general

Issue:
Composer plugin cannot run working_dir dependent commands. e.g. `symfony:dump-env`
### Summary

When I try to use the `symfony:dump-env` composer command I get an error that the command does not exist. This is caused by [`get_available_options`](https://github.com/ansible-collections/community.general/blob/c3cab7c68c4eb0d0f80d49356c8be52d0bb849ef/plugins/modules/packaging/language/composer.py#L256) method calling `composer help` [without setting the `working-dir` option](https://github.com/ansible-collections/community.general/blob/c3cab7c68c4eb0d0f80d49356c8be52d0bb849ef/plugins/modules/packaging/language/composer.py#L177).

This is what is executed by ansible:
```console
$ composer help symfony:dump-env

                                                                  
  [Symfony\Component\Console\Exception\CommandNotFoundException]  
  There are no commands defined in the "symfony" namespace.       
                                                                  

help [--xml] [--format FORMAT] [--raw] [--] [<command_name>]
```

This should be executed instead:
```console
$ composer help symfony:dump-env --working-dir /path/to/my/working/dir
Usage:
  symfony:dump-env [options] [--] [<env>]
  dump-env

Arguments:
  env                            The application environment to dump .env files for - e.g. "prod".

Options:
      --empty                    Ignore the content of .env files
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Compiles .env files to .env.local.php.
```

### Issue Type

Bug Report

### Component Name

composer

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.11.6] 
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['~/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = ~/.pyenv/versions/3.9.7/envs/ansible-venv-3.9.7/lib/python3.9/site-packages/ansible
  ansible collection location = ~/.ansible/collections:/usr/share/ansible/collections
  executable location = ~/.pyenv/versions/ansible-venv-3.9.7/bin/ansible
  python version = 3.9.7 (default, Nov  5 2021, 08:41:26) [GCC 4.8.5]
  jinja version = 3.0.2
  libyaml = True

```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general
# ~/.pyenv/versions/3.9.7/envs/ansible-venv-3.9.7/lib/python3.9/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 3.8.1  

```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = ['/etc/ansible/hosts']
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
```


### OS / Environment

Target OS is SLES 15-SP3, but that should not be of concern. I installed composer using the [`ansible role by geerlingguy`](https://github.com/geerlingguy/ansible-role-composer)

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
- name: Setup env file with composer/symfony flex
  composer:
    command: symfony:dump-env
    arguments: prod
    working_dir: "{{ project_checkout_path }}"
```


### Expected Results

I expected the command to be executed properly.

### Actual Results

```console (paste below)
fatal: [project]: FAILED! => {"changed": false, "msg": "[Symfony\\Component\\Console\\Exception\\CommandNotFoundException] There are no commands defined in the \"symfony\" namespace. help [--xml] [--format FORMAT] [--raw] [--] [<command_name>]"}

```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/packaging/language/composer.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/packaging/language/composer.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @dmtrs
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
* [`plugins/modules/composer.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/composer.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
+1, I have to use the `ansible` `command` or `shell` module instead because of this issue.
Both this issue and #5204 seem to be waiting for someone to pick them up.
Hey folks, can you test #7405 and tell us if it fix the issue? Thanks

Resolving patch:
diff --git a/plugins/modules/composer.py b/plugins/modules/composer.py
--- a/plugins/modules/composer.py
+++ b/plugins/modules/composer.py
@@ -170,10 +170,15 @@ def get_available_options(module, command='install'):
     return command_help_json['definition']['options']
 
 
-def composer_command(module, command, arguments="", options=None, global_command=False):
+def composer_command(module, command, arguments="", options=None):
     if options is None:
         options = []
 
+    global_command = module.params['global_command']
+
+    if not global_command:
+        options.extend(['--working-dir', "'%s'" % module.params['working_dir']])
+
     if module.params['executable'] is None:
         php_path = module.get_bin_path("php", True, ["/usr/local/bin"])
     else:
@@ -217,7 +222,6 @@ def main():
         module.fail_json(msg="Use the 'arguments' param for passing arguments with the 'command'")
 
     arguments = module.params['arguments']
-    global_command = module.params['global_command']
     available_options = get_available_options(module=module, command=command)
 
     options = []
@@ -234,9 +238,6 @@ def main():
             option = "--%s" % option
             options.append(option)
 
-    if not global_command:
-        options.extend(['--working-dir', "'%s'" % module.params['working_dir']])
-
     option_params = {
         'prefer_source': 'prefer-source',
         'prefer_dist': 'prefer-dist',
@@ -260,7 +261,7 @@ def main():
         else:
             module.exit_json(skipped=True, msg="command '%s' does not support check mode, skipping" % command)
 
-    rc, out, err = composer_command(module, command, arguments, options, global_command)
+    rc, out, err = composer_command(module, command, arguments, options)
 
     if rc != 0:
         output = parse_out(err)


Test patch: