All notes
Composer

Installation



curl -sS https://getcomposer.org/installer | php -- --install-dir=${HOME}/bin/ --filename=composer

# Use it: php ${HOME}/bin/composer

China mirror

pkg.phpcomposer.com.

修改 composer 的全局配置文件(推荐方式)


composer config -g repo.packagist composer https://packagist.phpcomposer.com

修改当前项目的 composer.json 配置文件:


composer config repo.packagist composer https://packagist.phpcomposer.com

上述命令将会在当前项目中的 composer.json 文件的末尾自动添加镜像的配置信息(你也可以自己手工添加):


"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}

Command lines

create-project

This is the equivalent of doing a git clone / svn checkout followed by a "composer install" of the vendors.



composer create-project doctrine/orm pathName 2.2.*

global

It's a helper to manage a project stored in a central location that can hold CLI tools or Composer plugins that you want to have available everywhere.



composer global require friendsofphp/php-cs-fixer
# Now the php-cs-fixer binary is available globally.

# Get the global vendor binaries directory
composer global config bin-dir --absolute

# Update the binary later on you can just run a global update:
composer global update

install

It reads the composer.json file from the current directory, resolves the dependencies, and installs them into "vendor".

If there is a composer.lock file in the current directory, it will use the exact versions from there.

update

It's equivalent to deleting the composer.lock file and running install again.

Composer will display a Warning when executing an install command if composer.lock and composer.json are not synchronized.

But these conflict with your requirements or minimum-stability

Add minimum-stability and prefer-stable to your composer.json:



{
    "name": "coderstephen/slack-client",
    "keywords": ["slack", "api", "realtime"],
    "license": "MIT",
    "description": "A better Slack client, with RTM API support",
    "authors": [{
        "name": "Stephen Coakley",
        "email": "[email protected]"
    }],
    "require": {
        "php": ">=5.5",
        "devristo/phpws": "dev-master",
        "evenement/evenement": "2.0.*",
        "guzzlehttp/guzzle": "~6.0",
        "react/event-loop": "^0.4.1",
        "react/promise": "^2.2"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.6",
        "fzaninotto/faker": "~1.4",
        "apigen/apigen": "^4.1"
    },
    "autoload": {
        "psr-4": {
            "Slack\\": "src"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Slack\\Tests\\": "tests"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

Plugins

composer-asset-plugin

The Composer Asset Plugin allows you to manage project assets (css, js, etc.) in your composer.json without installing NPM or Bower.

Exception: Invalid version string

github.com: Unable to install google-closure-compiler.

[UnexpectedValueException]
  Could not parse version constraint >=20171112.0.0: Invalid version string "20171112.0.0"

Fix:



// As a quickhack, I added these lines to the Converter/SemverConverter.php:convertVersion() right before the return:

if (preg_match('/^\d{7,}\./', $version)) {
  // composer can only handle 6 digits, convert somehow

  // cut off least significant digits
  $version = substr($version, -6);
}