이제 컴포저를 사용하여 프로젝트내 패키지의 의존성을 관리해 보겠습니다.


Homestead 에는 이미 설치되어 있으나 윈도나 OS X, 리눅스에 직접 컴포저를 설치할 독자들은 http://goo.gl/Mr9cgi 를 참고해서 설치하면 됩니다.

이제 윈도나 OS X 에 컴포저를 설치한 독자들은 cmd.exe 와 터미널을 열고 Homestead 사용자는 vagrant ssh 로  명령어로 연결해 봅시다.


연결후 콘솔에 composer 명령어를 옵션없이 입력하면 사용 가능한 명령어 목록을 출력합니다.

composer
 
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev (825b4b9c63a29c586d005de8dbbcfdfeb86bbf6f) 2015-01-20 16:39:06
Usage:
 [options] command [arguments]
Options:
 --help (-h)           Display this help message.
 --quiet (-q)          Do not output any message.
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
 --version (-V)        Display this application version.
 --ansi                Force ANSI output.
 --no-ansi             Disable ANSI output.
 --no-interaction (-n) Do not ask any interactive question.
 --profile             Display timing and memory usage information
 --working-dir (-d)    If specified, use the given directory as working directory.
Available commands:
 about            Short information about Composer
 archive          Create an archive of this composer package
 browse           Opens the package's repository URL or homepage in your browser.
 clear-cache      Clears composer's internal package cache.
 clearcache       Clears composer's internal package cache.
 config           Set config options
 create-project   Create new project from a package into given directory.
 depends          Shows which packages depend on the given package
 diagnose         Diagnoses the system to identify common errors.
 dump-autoload    Dumps the autoloader
 dumpautoload     Dumps the autoloader
 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             Show 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         Show information about licenses of dependencies
 list             Lists commands
 remove           Removes a package from the require or require-dev
 require          Adds required packages to your composer.json and installs them
 run-script       Run the scripts defined in composer.json.
 search           Search for packages
 self-update      Updates composer.phar to the latest version.
 selfupdate       Updates composer.phar to the latest version.
 show             Show information about packages
 status           Show a list of locally modified packages
 update           Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
 validate         Validates a composer.json
BASH


먼저 컴포저를 사용하는 my-proj 라는 프로젝트를 만들겠습니다. 다음 명령어를 쉘 상에서 실행합니다.

$ mkdir my-proj
$ cd my-proj
BASH

composer의 모든 설정은  composer.json 이라는 파일을 통해 관리합니다. 이 파일은 json 으로 기술하며 가장 중요한 의존성은 require 항목으로 선언하며 "패키지 이름": "버전" 형식으로 기술합니다.


패키지 이름은 다른 패키지와 충돌을 방지하기 위해 "vendor/project"  와 같이 지정하며 벤더는 회사명이나 자신의 id 를 많이 사용하며 project 는 PHP 프로젝트명을 기술합니다.


다음은 로그 라이브러리인 monolog 를 지정하는 composer.json 예제로 monolog 는 벤더와 프로젝트 명을 동일하게 사용하고 있습니다.

{
    "require": {
        "monolog/monolog": "1.13.*"
    }
}
JS

또는 다음 커맨드 라인 명령을 사용하면 자동으로 composer.json 의 require 항목에 의존성을 추가합니다.

composer require "monolog/monolog": "1.13.*"
JS


require 항목은  json 의 object 형식이므로 다수의 의존성을 선언할 수 있습니다. 기존 의존성에 PHP 의 DateTime library 인 carbon 을 추가해 보겠습니다. 카본 패키지의 벤더 이름은 "nesbot" 이고 프로젝트 이름은 "carbon" 입니다.

{
    "require": {
        "monolog/monolog": "1.13.*",
        "nesbot/carbon": "~1.14"
    }
}
JS

이제 composer install 명령어로 의존성 있는 라이브러리를 설치할 수 있습니다.

$ composer install
 
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing psr/log (1.0.0)
    Downloading: 100%         
  - Installing monolog/monolog (1.13.1)
    Downloading: 100%         
  - Installing symfony/translation (v2.6.6)
    Loading from cache
  - Installing nesbot/carbon (1.18.0)
    Loading from cache
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
...
Writing lock file
Generating autoload files
BASH
composer 는 의존성 있는 라이브러리를 위와 같이 자동으로 vendor 폴더 밑에 설치합니다. ls -l vendor 명령어로 설치된 라이브러리를 확인할 수 있습니다.

$ ls -l vendor/

-rw-rw-r-- 1 vagrant vagrant  183 May  3 15:36 autoload.php
drwxrwxr-x 2 vagrant vagrant 4096 May  3 15:36 composer
drwxrwxr-x 3 vagrant vagrant 4096 May  3 15:36 monolog
drwxrwxr-x 3 vagrant vagrant 4096 May  3 15:36 nesbot
drwxrwxr-x 3 vagrant vagrant 4096 May  3 15:36 psr
drwxrwxr-x 3 vagrant vagrant 4096 May  3 15:36 symfony
BASH

"monolog/monolog suggests installing ..." 항목은 이 라이브러리를 사용할 때 같이 사용하면 좋은 라이브러리를 제안하는 항목입니다.
여기서는 monolog 의 logger 를 특정 장치로 보낼때 필요한 외부 라이브러리를 추천하고 있으며 라이브러리를 추천할 때 composer.json 에 suggest 항목으로 기술하면 됩니다. 

다음은 monolog 의 composer.json 의 일부 항목으로 PHP 버전은 5.3 이상을 요구하며 PHP 용 로그 표준인 psr/log 를 사용하는 것을 알 수 있습니다.
suggest 로 지정한 항목은 monolog 를 사용하는 프로젝트에서 composer 로 의존성 설정시 출력하게 됩니다.
{
    "require": {
        "php": ">=5.3.0",
        "psr/log": "~1.0"
    },
    "suggest": {
        "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
        "raven/raven": "Allow sending log messages to a Sentry server"
    }
}
JS