◆ 雰囲気で使ってるくらいの知識だったので(略

これまで composer って npm みたいなもんでしょ と雰囲気で使ってた (あれ 最近どこかで……) ので少しちゃんと使ってみました

全体的なこと

composer は PHP のパッケージマネージャです
Node.js でいう npm みたいなものです

npm は Node.js についてきますが composer は PHP についてはきません
標準ではなくあくまでサードパーティのものという感じです

Node.js は npm のパッケージをロードするのが前提とした作りですが PHP だとそういうものはなくて require でファイルを単純にロードするものがあるくらいです
それと autoload というクラスが無いときにファイルを require する機能があって これらをうまく使ってパッケージをロードさせるという仕組みです
なのでパッケージマネージャでインストールしたパッケージを使うためには 最初に vendor/autoload.php というファイルを require しないといけないとかちょっと手順が増えます

インストールするには公式のインストーラを使ったり直接 composer.phar ファイルをダウンロードしたり dnf でインストール (fedora) したりです

npm の package.json と package-lock.json にあたるものが composer.json と composer.lock です

パッケージの一覧は composer のサイトじゃなくて packagist というサイトで見れます
https://packagist.org/

npm だと気軽に作りかけみたいのや個人の自己紹介みたいなものまで何でもありますが packagist はそれに比べるともう少しちゃんとした有用なものだけって印象です
npm ほど有名じゃないからというのもありそうですけど

とは言っても PHP のパッケージマネージャなのに jQuery や underscore.js や highlightjs など JavaScript ファイルもあったりよくわからない部分でもあります

composer は PHP だけでなく HHVM でも使います
パッケージによっては HHVM で動かさないとインストールできないのもあります

install

composer 自体も PHP で作られています
composer.phar というファイルを PHP で実行すれば動きます

公式サイトにダウンロードやインストール方法が書いてます

fedora なら

sudo dnf install composer

でインストールできるのでこれでいいと思います

使い方は dnf でインストールしたなど composer コマンドがあるなら

composer install

composer.phar を php で実行するなら

php composer.phar install

のようになります

composer.phar の shebang は 「#!/usr/bin/env php」 なので php にパスが通ってたら直接呼び出しても実行できると思います

composer init

npm と同じく設定ファイルを interactive に作るために init コマンドが使えます
結構長いので 直接 json 書いたほうが早いかもしれませんが パッケージ検索もできるので そのへんは npm より便利かもです

ダウンロードに時間が掛からないよう依存パッケージのないパッケージとして smarty を入れてみました

[user@fedora]/mnt/win/test/dir000/ctes% composer init


Welcome to the Composer config generator



This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [user/ctes]:
Description []:
Author [, n to skip]:
Invalid author string. Must be in the format: John Smith <john@example.com>
Author [, n to skip]: n
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]?
Search for a package: smarty

Found 15 packages matching smarty

[0] smarty/smarty
[1] slim/views
[2] michelf/php-smartypants
[3] ytake/laravel-smarty
[4] yiisoft/yii2-smarty
[5] noiselabs/smarty-bundle
[6] smarty-gettext/smarty-gettext
[7] bear/package
[8] dwoo/dwoo
[9] murganikolay/smarty-module
[10] mathmarques/smarty-view
[11] jaxon-php/jaxon-view-smarty
[12] jaxon-php/jaxon-view-smarty
[13] text/template
[14] smartystreets/phpsdk

Enter package # to add, or the complete package name if it is not listed: 0
Enter the version constraint to require (or leave blank to use the latest version):
Using version ^3.1 for smarty/smarty
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:

{
"name": "user/ctes",
"require": {
"smarty/smarty": "^3.1"
}
}

Do you confirm generation [yes]?

パッケージ名はデフォルトだと Linux にログイン中のユーザ名とそのフォルダ名になるようです
何も打たずにエンターしてると 「[]」 の中にあるデフォルト値が使われます

Author のところは空白はダメみたいで スキップするには 「n」 と入力する必要がありました

dependencies ではパッケージを検索してくれます
smarty と打ったら 15 個候補が出てきたので目的の 0 番を入力してます
バージョンは最新でいいので空欄です

正式名で入力していたら検索は出ずにバージョン指定になります

dev dependencies は特にいらないのでそのままエンターしました

最後に出力される内容が表示されて これで良いか確認してくれます
けっこう親切な作りですね

これは設定ファイルをつくるだけなのでインストールはされてないことに注意です

npm require

init をあれだけスキップして設定ファイルを作ってからインストールするのなら 最初からパッケージ指定でインストールだけしたほうが楽です

root@localhost ~/temp# echo "{}" > composer.json
root@localhost ~/temp# composer require smarty/smarty
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Using version ^3.1 for smarty/smarty
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing smarty/smarty (v3.1.32): Downloading (100%)
Writing lock file
Generating autoload files
root@localhost ~/temp# cat composer.json
{
"require": {
"smarty/smarty": "^3.1"
}
}

空の 「{}」 を composer.json に出力して require でインストールします
(ファイルがないなら勝手に作ってくれるのかも)
npm と違ってインストールするのは install じゃありません
install のあとにパッケージ名を打つと require 使ってね とメッセージがでます

require でインストールすると composer.json の require にパッケージが追加されます
--dev オプションをつけると require-dev にパッケージが追加されます

ここで 「smarty」 みたいな正式名じゃない名前を指定したらエラーになりますが 「もしかしてこれ?」 という感じで類似名のパッケージを候補に出してくれます

composer install/update

composer.json を直接書いたり init で作った後にインストールするなら

composer install

を実行します

composer.json や composer.lock から必要なパッケージがインストールされます
lock ファイルがあれば lock ファイルに指定されたバージョンがインストールされるので別環境でインストールしても同じバージョンになります

lock ファイルに合わせるのじゃなく最新版に更新したいなら

composer update

を使います
こっちだと lock ファイルも更新されます

help

ヘルプは

composer help

で表示できます

各コマンドのヘルプは

composer help init

など help のあとにコマンド名を入れると見れます
npm と同じです

ちょっと分かりづらかったのがコマンド一覧なのですが help では表示されず list を使います
インストールされたの一覧のような名前ですが コマンド一覧です

root@localhost ~/temp# composer list
Do not run Composer as root/super user! See https://getcomposer.org/root for details
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.6.5 2018-05-04 11:44:59

Usage:
command [options] [arguments]

Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
about Shows the short information about Composer.
archive Creates an archive of this composer package.
browse Opens the package's repository URL or homepage in your browser.
check-platform-reqs Check that platform requirements are satisfied.
clear-cache Clears composer's internal package cache.
clearcache Clears composer's internal package cache.
config Sets config options.
create-project Creates new project from a package into given directory.
depends Shows which packages cause the given package to be installed.
diagnose Diagnoses the system to identify common errors.
dump-autoload Dumps the autoloader.
dumpautoload Dumps the autoloader.
exec Executes a vendored binary/script.
global Allows running commands in the global composer dir ($COMPOSER_HOME).
help Displays help for a command
home Opens the package's repository URL or homepage in your browser.
info Shows information about packages.
init Creates a basic composer.json file in current directory.
install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
licenses Shows information about licenses of dependencies.
list Lists commands
outdated Shows a list of installed packages that have updates available, including their latest version.
prohibits Shows which packages prevent the given package from being installed.
remove Removes a package from the require or require-dev.
require Adds required packages to your composer.json and installs them.
run-script Runs the scripts defined in composer.json.
search Searches for packages.
show Shows information about packages.
status Shows a list of locally modified packages, for packages installed from source.
suggests Shows package suggestions.
update Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
upgrade Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
validate Validates a composer.json and composer.lock.
why Shows which packages cause the given package to be installed.
why-not Shows which packages prevent the given package from being installed.

紹介した以上にコマンドがいっぱいありますが とりあえずネットに上がってるパッケージをインストールして使うくらいならここまでの方法だけで大丈夫だと思います

ところで コマンド一覧を見ていて気になったのですが composer には publish 系がありません
npm だと npm コマンドで簡単にパッケージを公開できたのですが composer だともうちょっと面倒な手順が必要みたいです
github に公開して packagist に登録してリポジトリ連携させる必要があるみたいです

npm みたいに動くか怪しいレベルのパッケージがあまりないのは公開までのハードルの高さも関係ありそうですね



インストールしたパッケージを使う編にたぶん続きます