PHP Composer 설치 및 사용법
composer 는 ruby 의 bundle 처럼 패키지간의 의존성을 관리해 주는 유틸리티입니다.
composer v2 기준입니다.
설치
- openssl extension 이 활성화되어야 한다.
Linux/Unix/Mac OS X
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin/
또는 curl 이 없을 경우 php 로 바로 설치. (https 관련 에러가 날 경우 php.ini 의 openssl extension 이 주석해제 되었는지 확인)
php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin/
사용하기 쉽게 symbolic link 를 건다.
sudo ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
Windows
자동 설치
- 설치 프로그램을 다운로드 받아 실행 https://getcomposer.org/Composer-Setup.exe
수동 설치
- cmd.exe 를 연다.
- PATH 가 걸린 디렉터리로 이동한다.
다음 명령어를 실행하면 composer.phar 이 설치된다.
php -r "readfile('https://getcomposer.org/installer');" | php
CODEcomposer.phar 이 있는 디렉터리에서 composer.bat 이름으로 배치 파일을 작성한다.
@ECHO OFF php "%~dp0composer.phar" %*
CODE
Composer version 1 설치
사용
명령어 없이 실행하면 사용 가능한 명령어 목록을 출력한다.
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
composer.json
프로젝트 설정은 composer.json 파일에 기술하며 의존성 항목은 require 항목으로 기술한다. require 는 "owner/packagename" : "version" 형식으로 기술한다.
{
"name": "lesstif/my-project",
"require": {
"laravel/framework": "4.2.*",
"kmd/logviewer": "1.2.*",
"monolog/monolog": "1.11.*"
"netresearch/jsonmapper" : "*"
},
}
버전은 기본적으로 프로젝트의 tags 디렉터리에서 찾으며 -dev 가 붙을 경우 branches 에서 가져 오게 된다. (버전 명명법 보기)
더 자세한 내용은 의존성 지옥(dependency hell), 유의적 버전(Semantic Versioning)을 참고
require 구문은 composer.json 에 직접 기술해도 되지만 require 명령을 사용하는게 더 간편하다.
composer require monolog/monolog 1.11.\*
패키지 설치
composer.json 에 필요한 패키지를 다 기술했으면 install 명령어로 설치할 수 있다. 설치된 패키지는 vendor 폴더밑에 위치한다.
composer install
패키지 갱신
composer.json 파일이 변경되었으면 update 명령어로 반영할 수 있다.
composer update
Autoloading
composer 로 설치한 library 들은 autoload.php 에 등록되므로 PHP 개발시에 필요한 라이브러리를 일일이 require 구문을 사용하지 않고 다음 문장 하나만 적어 주면 된다.
require 'vendor/autoload.php';
개발 브랜치 사용하기
버전 항목을 "dev-master" 로 설정하면 릴리스 되지 않은 master 브랜치의 마지막 버전을 가져 옴.
{
"require": {
"lesstif/php-jira-rest-client": "dev-master"
}
}
develop 브랜치를 사용할 경우 "dev-master" 대신 "dev-develop" 로 지정
{
"require": {
"lesstif/php-jira-rest-client": "dev-develop"
}
}
composer config
config 옵션으로 composer 의 기본 동작을 제어하는 옵션을 설정할 수 있음.
설정 보기
composer.json 이 있는 폴더에서 아래 명령을 실행하여 현재 설정을 출력
composer config --list
전역 설정을 보려면 global 옵션을 추가
composer global config --list
설정
config 명령뒤에 설정할 key value 를 입력
composer config repositories.foo vcs https://github.com/foo/bar
전역적으로 설정할 경우 config 앞에 global 키워드 추가
composer global config minimum-stability dev
composer로 신규 프로젝트 만들기
기존 프로젝트 복제하여 생성
옵션에 프로젝트 명을 주면 기존 프로젝트를 설치하면서 프로젝트 생성. 마지막 옵션(2.2.*) 은 사용할 버전이며 생략시 최신 버전이 사용됨.
php composer.phar create-project doctrine/orm path/to/my-cloned-project 2.2.*
새로 생성
프로젝트 폴더 생성
$ mkdir monolog-eloquent $ cd monolog-eloquent
CODE프로젝트 초기화
$ composer init
CODE프로젝트 설정 입력
Click here to expand...
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.Package name (<vendor>/<name>) [root/monolog-eloquent]: lesstif/monolog-eloquent
Description []: Eloquent ORM driver for Laravel Monolog.
Author [KwangSeob Jeong <lesstif@gmail.com>]:
Minimum Stability []:
Package Type []:
License []: MITDefine your dependencies.
Would you like to define your dependencies (require) interactively [yes]?
Search for a package: laravelFound 15 packages matching laravel
[0] laravel/laravel
[1] laravel/framework
[2] artdarek/oauth-4-laravel
[3] jelovac/bitly4laravel
[4] laravel/installerEnter package # to add, or the complete package name if it is not listed: 1
Enter the version constraint to require (or leave blank to use the latest version): ~5.0
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package: phpunitFound 15 packages matching phpunit
[0] phpunit/phpunit
[1] phpunit/phpunit-mock-objects
[2] phpunit/phpunit-selenium
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): ~4.0Search for a package:
{
"name": "lesstif/monolog-eloquent",
"description": "Eloquent ORM driver for Laravel Monolog.",
"require": {
"laravel/framework": "~5.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"license": "MIT",
"authors": [
{
"name": "KwangSeob Jeong",
"email": "lesstif@gmail.com"
}
]
}Do you confirm generation [yes]?
- composer.json 이 생성됨.
.gitignore 파일 생성(https://www.gitignore.io/api/vim,laravel,composer)
# Created by https://www.gitignore.io ### Vim ### [._]*.s[a-w][a-z] [._]s[a-w][a-z] *.un~ Session.vim .netrwhist *~ ### Laravel ### /bootstrap/compiled.php .env.*.php .env.php .env ### Composer ### composer.phar vendor/ # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file # composer.lock
CODEcomposer install 로 의존성 패키지 설치
$ composer install
CODE
같이 보기
- 컴포저(Composer) - PHP용 패키지 관리자 - "쉽게 배우는 라라벨5 프로그래밍" 에서 발췌
- 내가 만든 PHP Composer 패키지를 등록해 보자. PHP 용 중앙 저장소 Packagist.org
- 모노로그(monolog) - PHP 용 로깅 프레임워크(logging framework)
- PHP Composer 로 워드프레스와 플러그인, 테마 관리하기
- Laravel 4 설치 및 설정
Ref
- 유의적 버전(Semantic Versioning) 2.0.0
- https://getcomposer.org/doc/01-basic-usage.md#package-versions
- https://getcomposer.org/doc/02-libraries.md