Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Composer Vendor Binaries


May 25, 2021 Composer


Table of contents


Binary supply library and vendor/bin

What is a binary supply library?

A Composer resource pack, any command-line script that you want to pass to the user who installed it, should be included 二进制供应库

If a resource pack contains other scripts that are not required by the user, such as build or compile scripts, the code should not be included in the binary repository.

How is it defined?

It is defined by adding a bin key to the bin composer.json It is defined as an array of files so that any given item can add multiple binary files.

{
    "bin": ["bin/my-script", "bin/my-other-script"]
}

What is the role of defining binary supply libraries in composer.json?

For any project that depends on the resource pack, it will direct Composer to install the bin files in the resource pack into vendor/bin directory.

This is a convenient way to expose useful scripts, otherwise they will be hidden deep vendor/

What happens when Composer runs to the composer.json file that defines the binary supply library?

Nothing happens to a binary supply library that is directly defined by a resource pack.

What happens when Composer runs to the Composer.json file that lists the binary supply library dependencies?

Composer checks all binary files defined in the dependency package. And set up a soft connection to vendor/bin for each dependent binary library.

For example, this is how the binary library of the my-vendor/project-a resource pack is installed:

{
    "name": "my-vendor/project-a",
    "bin": ["bin/project-a-bin"]
}

Executing the composer install command on composer install has bin/project-a-bin composer.json

Another example is the my-vendor/project-b such a require definition:

{
    "name": "my-vendor/project-b",
    "require": {
        "my-vendor/project-a": "*"
    }
}

When you execute the composer install command on composer.json checks all the dependent packages for project-b and installs their binary vendor/bin composer install

In this case, Composer vendor/bin/project-a-bin those access paths that vendor/my-vendor/project-a/bin/project-a-bin can access. On a Class Unix platform, this is achieved by creating soft links.

What about windows .bat and files?

Packages that are fully managed by Composer do not need to contain any .bat files. When running in a Windows environment, Composer handles the installation of binary files in a special way:

  • A file that references this binary is .bat automatically
  • A Unix-style proxy file with the same name as the binary is also automatically generated (easy for Cygwin or Git Bash)

Sometimes a resource pack needs to maintain .bat to support the workflow, which can not be included in Composer. In this case, the resource pack should not list them as binary files because Composer does not need to know them.

Can a binary supply library be installed somewhere other than vendor/bin?

Of course, there are two ways to specify other optional locations for binary supply libraries:

  1. Set at the bin-dir composer.json file
  2. Set the environment COMPOSER_BIN_DIR

Examples of the former are as follows:

{
    "config": {
        "bin-dir": "scripts"
    }
}

Running composer composer install composer.json install installs all binary supply scripts/ instead of vendor/bin/