顯示廣告
隱藏 ✕
看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [PHP] 安裝套件管理器 Composer
時間 2023-09-24 Sun. 23:56:18


[圖]


Composer 是PHP的軟體套件管理系統

用來管理套件彼此相依的問題
會在單個專案的目錄中安裝所有需要的程式庫

目前版本 2.6.3 需要 PHP 7.2+
PHP 5.3~7.1 要另外安裝 2.2.x 版

官網: https://getcomposer.org/


在 Linux 安裝 Composer

安裝環境: Rocky Linux 9

先安裝需要用到的程式
$ sudo yum install php-cli php-zip wget unzip


方法1 依照官網的說明使用 php 下載

下載 Composer 安裝程式
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

使用 php 執行 copy 將安裝程式下載為 composer-setup.php

檢查下載的 php 檔案完整性
$ HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

顯示 Installer verified 代表沒問題
顯示 Installer corrupt 的話可能要重新下載

使用 php 執行安裝程式
$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

--install-dir=/usr/local/bin 代表將執行檔放在執行路徑中
--filename=composer 代表將執行檔的檔名設定為 composer


方法2 使用 curl 下載

直接下載安裝程式並執行
$ curl -sS https://getcomposer.org/installer -o composer-setup.php
$ php composer-setup.php --install-dir=/usr/local/bin --filename=composer


安裝好後執行看看
$ composer

若找不到檔案,代表 /usr/local/bin 不在執行路徑中
再加個軟連結到執行路徑,例如 /usr/bin
$ sudo ln -s /usr/local/bin/composer /usr/bin/composer

執行結果:
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.6.3 2023-09-15 09:38:21

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display help for the given command. When no command is given display help for the list command
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi|--no-ansi           Force (or disable --no-ansi) ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
      --no-scripts               Skips the execution of all scripts defined in composer.json file.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
...

安裝成功後可將安裝程式移除
$ php -r "unlink('composer-setup.php');"


在 Windows 安裝 Composer

在官網下載安裝檔執行即可
https://getcomposer.org/Composer-Setup.exe

詢問是否要用 Developer mode 安裝時,不需勾選


在新專案使用 Composer

開一個新的資料夾來測試
$ mkdir test_composer
$ cd test_composer

下載一個套件看看,在 https://packagist.org/ 可以選擇想要安裝的 PHP 套件
例如要安裝 monolog/monolog 套件的話
在專案的資料夾裡執行
$ composer require monolog/monolog

執行結果:
Info from https://repo.packagist.org: #StandWithUkraine
Cannot use monolog/monolog's latest version 3.4.0 as it requires php >=8.1 which is not satisfied by your platform.
./composer.json has been created
Running composer update monolog/monolog
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
  - Locking monolog/monolog (2.9.1)
  - Locking psr/log (3.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Downloading psr/log (3.0.0)
  - Downloading monolog/monolog (2.9.1)
  - Installing psr/log (3.0.0): Extracting archive
  - Installing monolog/monolog (2.9.1): Extracting archive
10 package suggestions were added by new dependencies, use composer suggest to see details.
Generating autoload files
1 package you are using is looking for funding.
Use the composer fund command to find out more!
No security vulnerability advisories found.
Using version ^2.9 for monolog/monolog

自動依目前 PHP 的版本安裝了適合的 2.9 版,而不是最新的 3.4 版
自動安裝了相依的套件 psr/log

查看資料夾,多了兩個檔案和一個 vendor 資料夾
composer.json  用來指定要使用哪些套件以及套件的版本
composer.lock  已下載安裝的套件資訊
vendor         下載的套件都放在這個資料夾裡
vendor/autoload.php  在PHP程式中require這個檔就可以使用所有套件

$ vim composer.json
{
    "require": {
        "monolog/monolog": "^2.9"
    }
}
可看到指定了一個套件,以及版本為 ^2.9
版本號 ^1.2.3 代表 >=1.2.3 且 <2.0.0 (第一位數固定)
       ~1.2.3 代表 >=1.2.3 且 <1.3.0 (前兩位數固定)

可以自己編輯 composer.json 檔,加上想要的套件及版本號後
執行 composer install 就會自動安裝套件了


寫一個使用套件的 PHP 程式
$ vim test_monolog.php
<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('text.log', Logger::WARNING));

// add records to the log
$log->warning('Foo');
$log->error('Bar');

執行這個 PHP 程式
$ php test_monolog.php

資料夾下多了一個 test.log 檔
[2023-09-24T23:47:29.798026+08:00] name.WARNING: Foo [] []
[2023-09-24T23:47:29.799463+08:00] name.ERROR: Bar [] []


Composer 指令

安裝指定的套件
$ composer require 套件名稱

安裝 composer.json 中有指定,但 composer.lock 中沒有的套件
$ composer install

更新所有的套件
$ composer update

更新指定的套件
$ composer update 套件1 套件2

移除指定的套件
$ composer remove 套件名稱

更新 composer 版本
$ sudo composer self-update


參考
https://phoenixnap.com/kb/how-to-install-and-use-php-composer-on-centos-7


--
※ 作者: Knuckles 時間: 2023-09-24 23:56:18 (台灣)
※ 編輯: Knuckles 時間: 2023-11-28 19:58:06 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 31 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇