composer

最後更新: 2020-12-21

介紹

HomePage: https://getcomposer.org/

Php Composer = composer.phar

目錄

 


Install

 

Install

wget https://getcomposer.org/download/2.7.6/composer.phar

# 2.7.6

sha256sum composer.phar

29dc9a19ef33535db061b31180b2a833a7cf8d2cf4145b33a2f83504877bba08  composer.phar

 

Check Version

php composer.phar --version

Composer version 2.7.6 2024-05-04 23:03:15
PHP version 8.1.2-1ubuntu2.17 (/usr/bin/php8.1)
Run the "diagnose" command to get more detailed diagnostics output.

 

 


Usage

 

 

# Updates composer.phar to the latest version

php composer.phar selfupdate

You are already using the latest available Composer version 2.7.6 (stable channel).

 

# 查看安裝了那些 Package

php composer.phar info

doctrine/instantiator             1.5.0   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
endroid/qr-code                   1.6.6   Endroid QR Code
guzzlehttp/guzzle                 6.5.8   Guzzle is a PHP HTTP client library
...

 

# 根據 composer.json 去安裝所有 Package

php composer.phar install

 

# Validates a composer.json

php composer.phar validate

./composer.json is valid

 * You should always run the validatecommand before you commit your composer.jsonfile

當有以下 output 時

# Lock file errors
- The lock file is not up to date with the latest changes in composer.json,
 it is recommended that you run `composer update` or `composer update <package name>`.

建議 backup 後行 `php composer.phar update`

 

# Updates your dependencies to the latest version

php composer.phar update

./composer.json is valid

 * 會根據 composer.json 去更新 package

 * 它會同時更新 composer.lock

 * This command is also aliased as upgrade

 

查看依賴 Package 的 Info

php composer.phar fund

The following packages were found in your dependencies which publish funding information:

doctrine
  instantiator
    https://www.doctrine-project.org/sponsorship.html
    https://www.patreon.com/phpdoctrine
    https://tidelift.com/funding/github/packagist/doctrine%2Finstantiator

...

它基本上係 "require": {} 內的 Package 資料

 

# Create an archive of this composer package

./composer.phar archive

 


安裝新 Package (roundcube)

 

php composer.phar update

php composer.phar search mobiledetect/mobiledetectlib

mobiledetect/mobiledetectlib Mobile_Detect is a lightweight PHP class for detecting mobile devices. ...
kwenziwa/devicedetector      Device Detector is a versatile ...

查看 Packages Info.

# 對於未安裝的 Package 要加上 "--available / -a" 才看到 Info

php composer.phar info -a mobiledetect/mobiledetectlib

name     : mobiledetect/mobiledetectlib
descrip. : ...
keywords : ...
versions : ...
...

Adds new packages to the composer.json

Syntax

php composer.phar require vendorA/package2:dev-master

i.e.

php composer.phar require mobiledetect/mobiledetectlib

...
Using version ^4.8 for mobiledetect/mobiledetectlib

php composer.phar require roundcube/elastic4mobile

Could not find a version of package roundcube/elastic4mobile matching your minimum-stability (stable).
Require it with an explicit version constraint allowing its desired stability.

php composer.phar info -a roundcube/elastic4mobile

php composer.phar require roundcube/elastic4mobile

...
versions : dev-master
...

php composer.phar require roundcube/elastic4mobile:dev-master

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires roundcube/elastic4mobile dev-master -> satisfiable by roundcube/elastic4mobile[dev-master].
    - roundcube/elastic4mobile dev-master requires mobiledetect/mobiledetectlib ^2.8
       -> found mobiledetect/mobiledetectlib[2.8.0, ..., 2.8.45]
      but it conflicts with your root composer.json require (^4.8).

最終安裝方法

php composer.phar rm mobiledetect/mobiledetectlib

php composer.phar require roundcube/elastic4mobile:dev-master

Notes

 * 在 composer.json 內 "require" 只有  "roundcube/elastic4mobile": "dev-master"
    沒有 "mobiledetect/mobiledetectlib"

 

 

Creative Commons license icon Creative Commons license icon