facebook twitter hatena line google mixi email

Php/composer/基本

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

パッケージ管理ツール node.jsのnpmみたいなもの。

ローカルプロジェクトにcomposerインストール

$ curl -s http://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer.phar
$ composer -V
Composer version e77435cd0c984e2031d915a6b42648e7b284dd5c 2014-07-02 15:44:54

使い方(例:monolog/monolog)

composer.json

{
    "require": {
        "monolog/monolog": ">=1.0.0"
    }
}
$ php composer.phar install

autoload

vender/autoload.phpでパッケージを読み込んでくれる。

composer.json編集した後の更新

composer update

vendorのdirの名前を変更

composer.json

{
    "require": {
        "monolog/monolog": ">=1.0.0"
    },
    "config": {
        "vendor-dir": "lib/"
    }
}

composer.json記述例

{
   "repositories": [
       {
           "type": "pear",
           "url": "http://pear.php.net/"
       }
   ],
   "require": {
       "pear-pear.php.net/PEAR": "1.9.*",
       "pear-pear.php.net/Mail": "1.2.*",
       "pear-pear.php.net/Mail_Mime": "1.8.*",
       "pear-pear.php.net/Net_SMTP": "1.6.*",
       "pear-pear.php.net/Net_UserAgent_Mobile": "1.0.*",
       "pear-pear.php.net/Image_Transform": "0.9.4"
   }
   "require-dev": {
       "phpunit/PHPUnit": "5.1.*"
   },
}

phpunitのinstall&実行例

{
   "require-dev": {
       "phpunit/phpunit": "5.1.*"
   }
}
$ vendor/bin/phpunit --bootstrap vendor/autoload.php tests/hoge.php

composerで入れたpearをpath登録

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../vendor/pear-pear.php.net/PEAR'),
    realpath(APPLICATION_PATH . '/../vender/pear-pear.php.net/Net_UserAgent_Mobile'),
    get_include_path(),
)));

composer自身のバージョンエラーが出た時

Warning: This development build of composer is over 30 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
$ sudo /usr/local/bin/composer self-update

以下vendor/autoload.phpエラーが出た場合

Failed to open stream: No such file or directory. The "vendor" folder does not exist. 
$ composer update --no-scripts

以下autoloadのエラーが出た場合

PHP Warning:  require(bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in bootstrap/autoload.php on line 17
$ composer update --no-scripts

参考

http://www.ryuzee.com/contents/blog/5681