Skip to content

Commit

Permalink
Add docs for the new bin-dir access and bump runtime-api, refs #10402
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Dec 28, 2021
1 parent 6f5baab commit a8ed352
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions doc/07-runtime.md
Expand Up @@ -162,4 +162,13 @@ This is set by the binary proxy and as such is not made available to projects
by Composer's `vendor/autoload.php`, which would be useless as it would point back
to itself.

## Binary (bin-dir) path in binaries

composer-runtime-api 2.2.2 introduced a new `$_composer_bin_dir` global
variable set when running binaries installed with Composer. Read more
about this [on the vendor binaries docs](articles/vendor-binaries.md#finding-the-composer-bin-dir-from-a-binary).

This is set by the binary proxy and as such is not made available to projects
by Composer's `vendor/autoload.php`.

← [Config](06-config.md) | [Community](08-community.md) →
34 changes: 34 additions & 0 deletions doc/articles/vendor-binaries.md
Expand Up @@ -93,6 +93,40 @@ If you want to rely on this in your package you should however make sure to
also require `"composer-runtime-api": "^2.2"` to ensure that the package
gets installed with a Composer version supporting the feature.

## Finding the Composer bin-dir from a binary

As of Composer 2.2.2, a new `$_composer_bin_dir` global variable
is defined by the bin proxy file, so that when your binary gets executed
it can use it to easily locate the project's autoloader.

For non-PHP binaries, the bin proxy sets a `COMPOSER_BIN_DIR` environment
variable.

This global variable will not be available however when running binaries defined
by the root package itself, so you need to have a fallback in place.

This can look like this for example:

```php
<?php

$binDir = $_composer_bin_dir ?? __DIR__ . '/../vendor/bin';
```

```php
#!/bin/bash

if [[ -z "$COMPOSER_BIN_DIR" ]]; then
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
else
BIN_DIR="$COMPOSER_BIN_DIR"
fi
```

If you want to rely on this in your package you should however make sure to
also require `"composer-runtime-api": "^2.2.2"` to ensure that the package
gets installed with a Composer version supporting the feature.

## What about Windows and .bat files?

Packages managed entirely by Composer do not *need* to contain any
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Composer.php
Expand Up @@ -66,7 +66,7 @@ class Composer
*
* @var string
*/
const RUNTIME_API_VERSION = '2.2.0';
const RUNTIME_API_VERSION = '2.2.2';

/**
* @return string
Expand Down

0 comments on commit a8ed352

Please sign in to comment.