Composerとは
PHPのパッケージ管理ツール
PHPではデフォルトインストールされているPEARがよく利用されていた
パッケージ管理ツールによって、ライブラリの依存関係を管理しやすくなり、依存しているライブラリのインストールも簡単になる
メリット
- プロジェクト(ディレクトリ)ごとにパッケージをインストール可能、そのためプロジェクトごとにあるライブラリのバージョンをわけることが可能。(PEARはOS全体に影響する)
- クラスオートローディング(クラスが使われているとそれに応じたファイルを自動的に読みこむ)機能が利用可能。requireやincludeなどを記述しなくてよくなる
インストール方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | root@hostname:/usr/local/sbin# curl -sS https://getcomposer.org/installer | php bash: php: コマンドが見つかりません bash: curl: コマンドが見つかりません root@hostname:/usr/local/sbin# aptitude install curl php5-cli 以下の新規パッケージがインストールされます: curl php5-cli ... root@hostname:/usr/local/sbin# curl -sS https://getcomposer.org/installer | php # -s:不要なプログレスメータを出力しない #!/usr/bin/env php All settings correct for using Composer Downloading... Composer successfully installed to: /usr/local/sbin/composer.phar Use it: php composer.phar root@hostname:/usr/local/sbin# ls | grep composer composer.phar root@hostname:/usr/local/sbin# cd /var/www/akat.info/test/composer ### パッケージの定義ファイル(composer.json)を作成 ### root@hostname:/var/www/akat.info/test/composer# vi composer.json ===== { "require": { "monolog/monolog": "1.2.*" } } ===== ### composer.jsonファイルを読み込み、依存パッケージなどのインストールを実施する ### oot@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar install Loading composer repositories with package information Installing dependencies (including require-dev) - Installing monolog/monolog (1.2.1) Downloading: 100% monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server) monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required)) monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server) Writing lock file Generating autoload files root@hostname:/var/www/akat.info/test/composer# tree -L 2 . ├── composer.json ├── composer.lock # インストールされたバージョンを記録する └── vendor # パッケージのインストール先 ├── autoload.php ├── composer └── monolog 3 directories, 3 files |
コマンド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | ### helpの表示 ### root@hostname:/var/www/akat.info/test# /usr/local/sbin/composer.phar -help ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version eba04dc211f794d6f3a5500cfcfb9f9c0efcefbc 2014-09-24 13:38:16 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. 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のバージョン確認 ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar -V Composer version eba04dc211f794d6f3a5500cfcfb9f9c0efcefbc 2014-09-24 13:38:16 ### composerが最新か確認する ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar self-update You are already using composer version eba04dc211f794d6f3a5500cfcfb9f9c0efcefbc. ### インストールされているパッケージ一覧を参照する ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar show -i monolog/monolog 1.2.1 Logging for PHP 5.3 ### パッケージを指定してバージョンアップする ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar update monolog/monolog Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Generating autoload files ### パッケージのバージョンアップ後、composer.lockを更新する ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar update Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Generating autoload files ### パッケージを検索する ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar search CakePHP cakephp/cakephp The CakePHP framework cakephp/cakephp-codesniffer CakePHP CodeSniffer Standards cakephp/debug_kit CakePHP Debug Kit cakephp/app CakePHP skeleton app cakephp/localized CakePHP Localized Plugin cakephp/migrations Database Migration plugin for CakePHP 3.0 based on Phinx cakephp/datasources CakePHP Datasources Plugin cakephp/monolog CakePHP Monolog Plugin cakephp/collection Work easily with arrays and iterators by having a battery of utility traversal methods cakephp/plugin-installer A composer installer for CakePHP 3.0+ plugins. friendsofcake/cakephp-test-utilities Package with support traits to ease unit testing. cakephp/acl Acl Plugin for CakePHP 3.x framework imsamurai/cakephp-advancedshell Advanced feutures for cakePHP shell imsamurai/cakephp-environment Provides improovement for tests, packagist, etc renan/cakephp-xhprof Plugin that quickly enables XHProf profiling for your CakePHP application. ### パッケージの詳細情報を表示する ### root@hostname:/var/www/akat.info/test/composer# /usr/local/sbin/composer.phar show cakephp/cakephp name : cakephp/cakephp descrip. : The CakePHP framework keywords : framework versions : dev-master, 3.0.x-dev, 3.0.0-beta1, 3.0.0-alpha2, 3.0.0-alpha1, 2.6.x-dev, 2.5.4, 2.5.3, 2.5.2, 2.5.1, 2.5.0, 2.5.0-RC2, 2.5.0-RC1, 2.5.0-beta, 2.4.x-dev, 2.4.10, 2.4.9, 2.4.8, 2.4.7, 2.4.6, 2.4.5, dev-3.0-psr-3, dev-3.0-buffered-iterator, dev-3.0-asset-filter, dev-3.0-sumby, dev-3.0-inflector, dev-3.0-use, dev-3.0-debug, dev-3.0-casing, dev-3.0-inflect-bug, dev-3.0-new-bake-templates, dev-3.0-translate-behavior-fix, dev-3.0-hhvm-take2, dev-3.0-method-rename, dev-3.0-windows-tests, dev-3.0-nest-label, dev-issue-4671, dev-issue-4664-webroot, dev-issue-4641, dev-issue-4616, dev-issue-3600, dev-master-email, dev-table-find-or-create type : library license : MIT source : [git] https://github.com/cakephp/cakephp.git eac4e3d6eeb6d90dfc237e745ba7298271280346 dist : [zip] https://api.github.com/repos/cakephp/cakephp/zipball/eac4e3d6eeb6d90dfc237e745ba7298271280346 eac4e3d6eeb6d90dfc237e745ba7298271280346 names : cakephp/cakephp requires php >=5.2.8 ext-mcrypt * requires (dev) phpunit/phpunit 3.7.* cakephp/debug_kit 2.2.* |
ブラウザから検索することも可能
https://packagist.org/
参考URL
公式サイト
https://getcomposer.org/
コマンド
https://getcomposer.org/doc/03-cli.md
概要やコマンド
http://codezine.jp/article/detail/7827
composer.jsonの概要
http://codezine.jp/article/detail/7887
Composerインストール
http://scrtree.github.io/blog/2013/11/17/howto-composer/
NetBeansとも連携可能
http://www.d-wood.com/blog/2013/06/20_4090.html