LoginSignup
16
16

More than 5 years have passed since last update.

Composer.jsonを書いてみよう

Posted at

はじめに

4ヶ月ほど前に初めてComposerを触る機会があり、それ以来お世話になりっぱなし。
よくわからないまま使うのも良くないので、テスト駆動開発を購入して、TDDを実践してみるときにどうせならcomposer.jsonを自分で書いてみようと思ったので備忘録として残します。

Composerとは

公式ドキュメントによると「Composerとは依存管理ツールです。」とのことです。
昔の私にはこの日本語がよくワカラナカッタ。

依存管理、というのはあるライブラリAで別のライブラリBを使っているため、ライブラリAをインストールしただけでは実行できず、ライブラリBもインストールする必要がある。というような関係性のことです。
これらを手動で1つずつ入れていくのは非常に面倒なので、それらの関係をツールを使って管理しましょう。ということ。
Composerを使ってライブラリAをインストールするとライブラリBも同時にインストールされます。便利。

composer.jsonの作成

適当なプロジェクトのディレクトリで

composer init

を実行。

設定を色々聞かれますが、今回は全部デフォルト。

Welcome to the Composer config generator


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

Package name (<vendor>/<name>) [package-name]:
Description []:
Author [user-name <email>, n to skip]:
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:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:

実行したディレクトリの配下にcomposer.jsonが作成されていることを確認。

composer.json

{
    "name": "tabtt/test",
    "authors": [
        {
            "name": "user-name",
            "email": "email"
        }
    ],
    "require": {}
}

Composerを使ってPHPUnitをインストール

composer.jsonを以下のように変更する

composer.json

{
    "name": "tabtt/test",
    "authors": [
        {
            "name": "user-name",
            "email": "email"
        }
    ],
    "require": {
        "phpunit/phpunit": "3.7.*"
    }
}

保存したら

composer install

PHPUnitのモジュールがインストールされた。

Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 8 installs, 0 updates, 0 removals
  - Installing symfony/yaml (v2.8.28): Loading from cache
  - Installing phpunit/php-text-template (1.2.1): Loading from cache
  - Installing phpunit/phpunit-mock-objects (1.2.3): Loading from cache
  - Installing phpunit/php-timer (1.0.9): Loading from cache
  - Installing phpunit/php-file-iterator (1.4.2): Loading from cache
  - Installing phpunit/php-token-stream (1.2.2): Loading from cache
  - Installing phpunit/php-code-coverage (1.2.18): Loading from cache
  - Installing phpunit/phpunit (3.7.38): Loading from cache
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.0.5)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files

ディレクトリ配下にモジュールがインストールされたのが確認できれば完了

vendor
├── autoload.php
├── bin
├── composer
├── phpunit
└── symfony

以上です。

16
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16