いつかエンジニアになりたい

Nothing is too late to start

何となく利用しているComposerの利用方法を再入門する

概要

  • 最近のPHPのパッケージ依存管理で比較的デファクトスタンダードなComposer
  • なんとなく利用していたので利用目的、利用方法を整理する

環境

PHPのパッケージ依存管理ツール

  • Bundler,npmなど他の言語では当たり前のパッケージ依存管理ツールですがPHPには以下があります
  • 歴史的経緯なのかわからないが、PHPには意外にパッケージ管理ツールが少ない
  • Pearは昔から利用されているが、最近はComposerが主流
  • PearとComposerは大きくわけて以下の違いがある
    1. パッケージ管理される範囲 Pearではインストールしたパッケージをシステム全体に適用するのに対し、 Composerはプロジェクト単位に反映するため、root権限等も不要で、 プロジェクトごとにバージョンを変えたりもできる。
    2. クラスオートローディング機能を利用できる クラスオートローディング機能とはPHP5.3から本格的に利用され始めた機能で、 Pearではインストールしたパッケージをrequireやincludeなどで読み込む必要があったが、 Composerでは自動生成されるautoload.phpをrequireするだけで良い。

Composerの利用方法

インストール方法

PHP7をインストールする

vagrant@vagrant-ubuntu-trusty:~$ php --version
The program 'php' is currently not installed. You can install it by typing:
sudo apt-get install php5-cli
vagrant@vagrant-ubuntu-trusty:~$ sudo apt-get install -y software-properties-common
vagrant@vagrant-ubuntu-trusty:~$ sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
vagrant@vagrant-ubuntu-trusty:~$ sudo apt-get update -y
vagrant@vagrant-ubuntu-trusty:~$ sudo apt-get install -y php7.0
vagrant@vagrant-ubuntu-trusty:~$ php -v
PHP 7.0.2-4+deb.sury.org~vivid+1 (cli) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

Composerをインストールする

vagrant@vagrant-ubuntu-trusty:~$ curl -sS https://getcomposer.org/installer | php
vagrant@vagrant-ubuntu-trusty:~$ sudo mv composer.phar /usr/local/bin/composer
vagrant@vagrant-ubuntu-trusty:~$ composer --version
Composer version 1.0-dev (837fa805ec9f8dcb1e05e0fca4099f0dab4f1e04) 2016-01-22 19:09:44

Composerの利用方法

基本的な利用の流れ

  • PackagistよりComposerに対応したライブラリを探す
  • パッケージ依存関係を記述したcomposer.jsonを手動で作成するか、以下のコマンドで作成する
composer require twig/twig:~1.8
  • composer.jsonを利用し以下のコマンドでパッケージをインストールする
composer install
<?php
require 'vendor/autoload.php';

例)ComposerでGoutteをインストールしてみる

スクレイピングに利用するGoutteをインストールする

ComposerでGoutteをインストールする
vagrant@vagrant-ubuntu-trusty:~$ mkdir SampleProject1
vagrant@vagrant-ubuntu-trusty:~$ cd SampleProject1/
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ composer require fabpot/goutte
Using version ^3.1 for fabpot/goutte
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing guzzlehttp/promises (1.0.3)
    Downloading: 100%

  - Installing psr/http-message (1.0)
    Downloading: 100%

  - Installing guzzlehttp/psr7 (1.2.2)
    Downloading: 100%

  - Installing guzzlehttp/guzzle (6.1.1)
    Downloading: 100%

  - Installing symfony/polyfill-mbstring (v1.0.1)
    Downloading: 100%

  - Installing symfony/dom-crawler (v3.0.1)
    Downloading: 100%

  - Installing symfony/css-selector (v3.0.1)
    Downloading: 100%

  - Installing symfony/browser-kit (v3.0.1)
    Downloading: 100%

  - Installing fabpot/goutte (v3.1.2)
    Downloading: 100%

symfony/browser-kit suggests installing symfony/process ()
Writing lock file
Generating autoload files
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ ls -g
total 28
-rw-rw-r-- 1 vagrant    59 Jan 24 05:38 composer.json
-rw-rw-r-- 1 vagrant 17169 Jan 24 05:38 composer.lock
drwxrwxr-x 7 vagrant  4096 Jan 24 05:38 vendor
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
インストールしたパッケージを利用しスクレイピングしてみる
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ vim test.php
<?php
require 'vendor/autoload.php';

use Goutte\Client;

$client = new Client();
$crawler = $client->request('GET', 'http://spotlight.pics/ja/');
$crawler->filter('article img')->each(function ($node) {
    print $node->attr('src')."\n";
});
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ php test.php
http://i4.pixiv.net/c/720x1200/img-master/img/2015/09/18/19/20/42/52586771_p0_master1200.jpg
http://i4.pixiv.net/c/720x1200/img-master/img/2014/08/06/13/59/56/45157527_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2014/10/14/21/29/40/46553081_p0_master1200.jpg
http://i1.pixiv.net/c/720x1200/img-master/img/2009/05/23/14/06/38/4391768_p0_master1200.jpg
http://i3.pixiv.net/c/720x1200/img-master/img/2014/06/13/17/47/32/44063774_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2008/11/14/00/26/04/2124805_p0_master1200.jpg
http://i1.pixiv.net/c/720x1200/img-master/img/2015/12/22/18/31/08/54165128_p0_master1200.jpg
http://i3.pixiv.net/c/720x1200/img-master/img/2014/11/03/23/14/42/46903258_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2013/12/27/09/58/56/40542565_p0_master1200.jpg
http://i4.pixiv.net/c/720x1200/img-master/img/2015/12/14/01/46/04/54032339_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2013/07/12/16/11/23/37005801_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2008/07/27/05/12/46/1248061_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2008/04/26/17/57/13/718657_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2010/10/03/01/53/38/13632409_p0_master1200.jpg
http://i1.pixiv.net/c/720x1200/img-master/img/2015/04/30/23/29/00/50115868_p0_master1200.jpg
http://i2.pixiv.net/c/720x1200/img-master/img/2011/01/31/23/45/29/16316625_p0_master1200.jpg
http://i3.pixiv.net/c/720x1200/img-master/img/2009/05/29/00/27/04/4476710_p0_master1200.jpg
http://i3.pixiv.net/c/720x1200/img-master/img/2009/10/30/22/38/52/6899990_p0_master1200.jpg
http://i1.pixiv.net/c/720x1200/img-master/img/2015/05/03/02/19/24/50162100_p0_master1200.jpg
http://i3.pixiv.net/c/720x1200/img-master/img/2014/08/09/00/16/26/45220066_p0_master1200.jpg

Composerのその他の利用方法

Composerでインストールしたパッケージリストを確認する

vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ composer show -i
fabpot/goutte             v3.1.2 A simple PHP Web Scraper
guzzlehttp/guzzle         6.1.1  Guzzle is a PHP HTTP client library
guzzlehttp/promises       1.0.3  Guzzle promises library
guzzlehttp/psr7           1.2.2  PSR-7 message implementation
psr/http-message          1.0    Common interface for HTTP messages
symfony/browser-kit       v3.0.1 Symfony BrowserKit Component
symfony/css-selector      v3.0.1 Symfony CssSelector Component
symfony/dom-crawler       v3.0.1 Symfony DomCrawler Component
symfony/polyfill-mbstring v1.0.1 Symfony polyfill for the Mbstring extension

ローカルのパッケージリストを最新化する

vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

パッケージリストからパッケージを探す

vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ composer search phpunit
phpunit/phpunit The PHP Unit Testing framework.
phpunit/phpunit-mock-objects Mock Object library for PHPUnit
phpunit/phpunit-selenium Selenium Server integration for PHPUnit
phpunit/phpunit-story Story extension for PHPUnit to facilitate Behaviour-Driven Development.
phpunit/phpunit-skeleton-generator Tool that can generate skeleton test classes from production code classes and vice versa
phpunit/dbunit DbUnit port for PHP/PHPUnit to support database interaction testing.
phpunit/phpunit-dom-assertions DOM assertions for PHPUnit
phpunit/phpcov CLI frontend for PHP_CodeCoverage
phpunit/php-code-coverage Library that provides collection, processing, and rendering functionality for PHP code coverage information.
phpunit/php-token-stream Wrapper around PHP's tokenizer extension.
phpunit/php-timer Utility class for timing
phpunit/php-file-iterator FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-text-template Simple template engine.
jbzoo/phpunit PHPUnit toolbox with short assert aliases and useful functions
eher/phpunit Unofficial version of PHPUnit to be handled with Composer.

composer.jsonCLIで更新する

vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ cat composer.json
{
    "require": {
        "fabpot/goutte": "^3.1"
    }
}
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ composer require phpunit/phpunit
Using version ^5.1 for phpunit/phpunit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing myclabs/deep-copy (1.5.0)
    Downloading: 100%

  - Installing sebastian/version (1.0.6)
    Downloading: 100%

  - Installing sebastian/resource-operations (1.0.0)
    Downloading: 100%

  - Installing sebastian/global-state (1.1.1)
    Downloading: 100%

  - Installing sebastian/recursion-context (1.0.2)
    Downloading: 100%

  - Installing sebastian/exporter (1.2.1)
    Downloading: 100%

  - Installing sebastian/environment (1.3.3)
    Downloading: 100%

  - Installing sebastian/diff (1.4.1)
    Downloading: 100%

  - Installing sebastian/comparator (1.2.0)
    Downloading: 100%

  - Installing symfony/yaml (v3.0.1)
    Downloading: 100%

  - Installing doctrine/instantiator (1.0.5)
    Downloading: 100%

  - Installing phpdocumentor/reflection-docblock (2.0.4)
    Downloading: 100%

  - Installing phpspec/prophecy (v1.5.0)
    Downloading: 100%

  - Installing phpunit/php-text-template (1.2.1)
    Downloading: 100%

  - Installing phpunit/phpunit-mock-objects (3.0.6)
    Downloading: 100%

  - Installing phpunit/php-timer (1.0.7)
    Downloading: 100%

  - Installing phpunit/php-token-stream (1.4.8)
    Downloading: 100%

  - Installing phpunit/php-file-iterator (1.4.1)
    Downloading: 100%

  - Installing phpunit/php-code-coverage (3.1.0)
    Downloading: 100%

  - Installing phpunit/phpunit (5.1.4)
    Downloading: 100%

sebastian/global-state suggests installing ext-uopz (*)
phpdocumentor/reflection-docblock suggests installing dflydev/markdown (~1.0)
phpdocumentor/reflection-docblock suggests installing erusev/parsedown (~1.0)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.2.1)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files
vagrant@vagrant-ubuntu-trusty:~/SampleProject1$ cat composer.json
{
    "require": {
        "fabpot/goutte": "^3.1",
        "phpunit/phpunit": "^5.1"
    }
}

参考