composer [Docs]

User Tools

Site Tools


composer

https://github.com/hirak/prestissimo

COMPOSER

3 - Libraries

This chapter will tell you how to make your library installable through Composer.

Every project is a package

As soon as you have a composer.json in a directory, that directory is a package

{

"name": "acme/hello-world",
"require": {
    "monolog/monolog": "1.0.*"
}

}

Composer infers versions from your VCS and you should not specify a version in your composer.json file

4 - CLI

Global options (--verbose, --no-plugins, --profile, --ansi, --version,....) composer init composer install

--prefer-source: There are two ways of downloading a package: source and dist. For stable versions Composer will use the dist by default. The source is a version control repository. If --prefer-source is enabled, Composer will install from source if there is one. This is useful if you want to make a bugfix to a project and get a local git clone of the dependency directly. --prefer-dist: Reverse of --prefer-source, Composer will install from dist if possible. This can speed up installs substantially on build servers and other use cases where you typically do not run updates of the vendors. It is also a way to circumvent problems with git if you do not have a proper setup. --dry-run: If you want to run through an installation without actually installing a package, you can use --dry-run. This will simulate the installation and show you what would happen. --dev: Install packages listed in require-dev (this is the default behavior). --apcu-autoloader: Use APCu to cache found/not-found classes.

composer update

--lock: Only updates the lock file hash to suppress warning about the lock file being out of date.

composer require

--sort-packages: Keep packages sorted in

composer remove composer show

php composer.phar show monolog/monolog

name : monolog/monolog versions : master-dev, 1.0.2, 1.0.1, 1.0.0, 1.0.0-RC1 type : library names : monolog/monolog source : [git] https://github.com/Seldaek/monolog.git 3d4e60d0cbc4b888fe5ad223d77964428b1978da dist : [zip] https://github.com/Seldaek/monolog/zipball/3d4e60d0cbc4b888fe5ad223d77964428b1978da 3d4e60d0cbc4b888fe5ad223d77964428b1978da license : MIT

autoload psr-0 Monolog : src/

requires php >=5.3.0

composer check-platform-reqs

This can be used to verify that a production server has all the extensions needed to run a project after installing it for example.

composer depends (why) composer prohibits (why-not) composer validate

You should always run the validate command before you commit your composer.json file, and before you tag a release. It will check if your composer.json is valid.

composer status If you often need to modify the code of your dependencies and they are installed from source, the status command allows you to check if you have local changes in any of them.

composer self-update composer dump-autoload

If you need to update the autoloader because of new classes in a classmap package for example, you can use dump-autoload to do that without having to go through an install or update.

composer clear-cache composer run-script composer exec composer diagnose cli completion: https://github.com/bamarni/symfony-console-autocomplete

4 - SCHEMA

Root Package: The root package is the package defined by the composer.json at the root of your project

Schema

name, description, keywords, license, authors, support version: The version of the package. In most cases this is not required and should be omitted (see below). This must follow the format of X.Y.Z or vX.Y.Z with an optional suffix of dev, -patch (-p), -alpha (-a), -beta (-b) or -RC. The patch, alpha, beta and RC suffixes can also be followed by a number.

   Packagist uses VCS repositories, so the statement above is very much true for Packagist as well. Specifying the version yourself will most likely end up creating problems at some point due to human error.

type: library, project, metapackage, composer-plugin

Package links (require, require-dev (root-only), conflict, replace, provide, suggest)

  • require: Lists packages required by this package. The package will not be installed unless those requirements can be met.

-require-dev: Lists packages required for developing this package, or running tests, etc.

autoload (PSR-4, PSR-0, Classmap, Files, Exclude files from classmaps, Optimizing the autoloader) autoload-dev (root-only) include-path target-dir minimum-stability (root-only) prefer-stable (root-only) repositories (root-only): Custom package repositories to use. (composer, vcs, pear, package) config (root-only) scripts (root-only) extra bin archive abandoned non-feature-branches

5 - Repositories

package:

Composer is a dependency manager. It installs packages locally. A package is essentially a directory containing something. Composer sees every version as a separate package.

The information most relevant for installation is the source definition, which describes where to get the package contents. The package data points to the contents of the package. And there are two options here: dist and source.

Dist: The dist is a packaged version of the package data. Usually a released version, usually a stable release.

Source: The source is used for development. This will usually originate from a source code repository, such as git. You can fetch this when you want to modify the downloaded package.

repository:

A repository is a package source. It's a list of packages/versions. Composer will look in all your repositories to find the packages your project requires.

6 - Scripts

A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Scripts are useful for executing a package's custom code or package-specific commands during the Composer execution process.

{

"scripts": {
    "post-update-cmd": "MyVendor\\MyClass::postUpdate",
    "post-package-install": [
        "MyVendor\\MyClass::postPackageInstall"
    ],
    "post-install-cmd": [
        "MyVendor\\MyClass::warmCache",
        "phpunit -c app/"
    ],
    "post-autoload-dump": [
        "MyVendor\\MyClass::postAutoloadDump"
    ],
    "post-create-project-cmd": [
        "php -r \"copy('config/local-example.php', 'config/local.php');\""
    ]
}

}

"scripts": {
    "test": [
        "@clearCache",
        "phpunit"
    ],
    "clearCache": "rm -rf cache/*"
}

}

{

"scripts": {
    "test": [
        "@php script.php",
        "phpunit"
    ]
}

}

7 - Vendor bin {

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

}

8 - Versions composer checks git tags if you want to check the branches use dev-*

version range examples:

>=1.0 >=1.0 <2.0 >=1.0 <1.1 || >=1.2

*

1.0.* == >=1.0 <1.1

~

~1.2 == >=1.2 <2.0.0 ~1.2.3 == >=1.2.3 <1.3.0

^

^1.2.3 == >=1.2.3 <2.0.0

// * | wildcard
"vendor/package": "1.3.*", // >=1.3.0 <1.4.0

// ~ | allows last digit specified to go up
"vendor/package": "~1.3.2", // >=1.3.2 <1.4.0
"vendor/package": "~1.3", // >=1.3.0 <2.0.0

// ^ | doesn't allow breaking changes (major version fixed - following semver)
"vendor/package": "^1.3.2", // >=1.3.2 <2.0.0
"vendor/package": "^0.3.2", // >=0.3.2 <0.4.0 // except if major version is 0
composer.txt · Last modified: 2020/07/02 16:06 (external edit)