PHP Composer の導入
installer のダウンロード
curl -sS https://getcomposer.org/installer | php
コマンドの確認するなら、
php composer.phar
local/bin/compoer に移動しておく。
mv composer.phar /usr/local/bin/composer
~/composer/composer.phar とかにしている人もいるので、ここじゃなきゃいけない、みたいなのはないのかな。分からなくなったら、
which composer
とかすればいい。
プロジェクトを作る
新しくプロジェクトを作るなら、
composer init
で、対話形式に composer.json が作れる。最初に、
Package name (<vendor>/<name>)
と ベンダー名/パッケージ名
が聞かれるので、このスラッシュ形式で指定。
Description
プロジェクトの説明を記入。
Author
name <mail address>
の形式で指定。
Minimum Stability
最小限の「安定度合い」的なものを聞かれるので、dev
, alpha
, beta
, RC
, and stable
から適時選択。デフォルトは、stable
。
Package Type
プロジェクトなら project
としてれば良さそう。デフォルトは library
。
License
オプショナルだけど、ドキュメントは、
Optional, but it is highly recommended to supply this.
と言っている。まぁ、そうだな。
Search for a package:
いよいよパッケージ。例えば、phpunit
など。
[0] phpunit/phpunit [1] eher/phpunit [2] jbzoo/phpunit [3] sugared-rim/phpunit [4] hiqdev/php-units [5] task/phpunit [6] phpunit/phpunit-mock-objects [7] phpunit/phpunit-selenium [8] phpunit/phpunit-story [9] phpunit/phpunit-skeleton-generator [10] phpunit/dbunit [11] phpunit/phpunit-dom-assertions [12] phpunit/phpcov [13] phpunit/phpunit-mink-trait [14] phpunit/php-timer
ばーっと候補がでるので、0
を入力。
Enter the version constraint to require (or leave blank to use the latest version)
最新版を使うなら、そのまま return で進む。
Using version ^6.1 for phpunit/phpunit Search for a package:
うっす。もういらないので、return で進む。
{ "name": "hoge/composer-test", "description": "this is a test.", "type": "project", "require-dev": { "phpunit/phpunit": "^6.1" }, "authors": [ { "name": "hoge", "email": "hoge@huga.com" } ], "require": {} } Do you confirm generation [yes]?
こんな json ができれば、OK。yes
で終了。ls
で確認すれば、composer.json がディレクトリに作成されている。
パッケージのインストール
composer install
と、手元の環境の PHP が 5.5.36
だったので、
Problem 1 - phpunit/phpunit 6.1.4 requires php ^7.0 -> your PHP version (5.5.36) does not satisfy that requirement. - phpunit/phpunit 6.1.3 requires php ^7.0 -> your PHP version (5.5.36) does not satisfy that requirement. - phpunit/phpunit 6.1.2 requires php ^7.0 -> your PHP version (5.5.36) does not satisfy that requirement. - phpunit/phpunit 6.1.1 requires php ^7.0 -> your PHP version (5.5.36) does not satisfy that requirement. - phpunit/phpunit 6.1.0 requires php ^7.0 -> your PHP version (5.5.36) does not satisfy that requirement. - Installation request for phpunit/phpunit ^6.1 -> satisfiable by phpunit/phpunit[6.1.0, 6.1.1, 6.1.2, 6.1.3, 6.1.4].
composer.json を編集して、phpunit のバージョンを phpunit-4.8.35.phar
とする。
"require-dev": { "phpunit/phpunit": "4.8.35" },
Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 21 installs, 0 updates, 0 removals - Installing symfony/yaml (v3.2.8): Downloading (100%) - Installing sebastian/version (1.0.6): Downloading (100%) - Installing sebastian/global-state (1.1.1): Downloading (100%) - Installing sebastian/recursion-context (1.0.5): Downloading (100%) - Installing sebastian/exporter (1.2.2): Downloading (100%) - Installing sebastian/environment (1.3.8): Downloading (100%) - Installing sebastian/diff (1.4.3): Downloading (100%) - Installing sebastian/comparator (1.2.4): Downloading (100%) - Installing doctrine/instantiator (1.0.5): Downloading (100%) - Installing phpunit/php-text-template (1.2.1): Downloading (100%) - Installing phpunit/phpunit-mock-objects (2.3.8):Downloading (100%) ) - Installing phpunit/php-timer (1.0.9): Downloading (100%) - Installing phpunit/php-file-iterator (1.4.2): Downloading (100%) - Installing phpunit/php-token-stream (1.4.11): Downloading (100%) - Installing phpunit/php-code-coverage (2.2.4): Downloading (100%) - Installing webmozart/assert (1.2.0): Downloading (100%) - Installing phpdocumentor/reflection-common (1.0): Downloading (100%) - Installing phpdocumentor/type-resolver (0.2.1): Downloading (100%) - Installing phpdocumentor/reflection-docblock (3.1.1): Downloading (100%) - Installing phpspec/prophecy (v1.7.0): Downloading (100%) - Installing phpunit/phpunit (4.8.35): Downloading (100%) symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command) sebastian/global-state suggests installing ext-uopz (*) phpunit/php-code-coverage suggests installing ext-xdebug (>=2.2.1) phpunit/phpunit suggests installing phpunit/php-invoker (~1.1) Writing lock file Generating autoload files
依存モジュールもまとめて、いっぱい。 ls
すると、
composer.json composer.lock vendor
lock ファイル と vendor ディレクトリができている。vendor という名前のディレクトリに外部の依存モジュールが格納されるので、.gitignore にこのディレクトリを指定しておけば OK。
を参考。
パッケージの追加
composer require cebe/markdown
require
で追加できる。composer.json と lock ファイルも更新される。バージョン指定する場合は、
composer require hoge/huge:1.1.11
みたいにする。
参考