OpenShift で Composer を使う


[PHP 5.4 の Web アプリを OpenShift で動かす - Qiita](http://qiita.com/takatama/items/8b91136d78b79a0cf569) の続き。
MySQL を使うには [OpenShift で PHP から MySQL 5.5 を使う - Qiita](http://qiita.com/takatama/items/1b0f0fd8f6807b526d3e)

Mac を使って開発する。

```
$ sw_vers -productVersion 
10.10.2
```

## 事前準備

Composer を Mac にインストールしておく。
Homebrew の [homebrew-php](https://github.com/Homebrew/homebrew-php) を使って PHP5.4 と Composer をインストールする (PHPを先に、Composerを後に)。

```
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew install php54
$ brew install composer
```

## ローカルで動作を確認する

例題として、Guzzle を使ってお天気情報を取得する。

参考: [Guzzle - 1週間分の天気予報をコンソールに表示するPHPスクリプト - Qiita](http://qiita.com/takatama/items/eef575cf1b245256299c)

### (1) Composer でインストールするパッケージは Git で管理しない

Composer でインストールしたファイルは vendor/ 以下に入るが、Git では管理させない。

参考: [Should I commit the dependencies in my vendor directory?](https://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md#should-i-commit-the-dependencies-in-my-vendor-directory-)

```
$ cd <OpenShiftのアプリケーションディレクトリ>
$ vi .gitignore
```

```.gitignore
vendor/
```

### (2) composer.json を作る

Guzzle を指定する。

```composer.json
{
   "require": {
      "guzzlehttp/guzzle": "~5.0"
   }
}
```

### (3) 関連するパッケージをインストールする

```
$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing react/promise (v2.2.0)
    Downloading: 100%         

  - Installing guzzlehttp/streams (3.0.0)
    Loading from cache

  - Installing guzzlehttp/ringphp (1.0.5)
    Loading from cache

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

Writing lock file
Generating autoload files
```

### (4) autoload.php を有効にする

autoload.php を index.php で読み込む。
Guzzle を使ってお天気情報を取得してみる。

```
$ vi index.php
```

```index.php
<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$city_and_country = 'kashiwa,jp';
$url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=' . $city_and_country . '&cnt=1';
$client = new Client();
echo $client->get($url)->getBody();
```

### (5) ローカルで実行してみる

```
php index.php 
{"cod":"200","message":0.2581,"city":{"id":1859924,"name":"Kashiwa","coord":{"lon":139.968887,"lat":35.854439},"country":"JP","population":0,"sys":{"population":0}},"cnt":1,"list":[{"dt":1424570400,"temp":{"day":283.93,"min":283.93,"max":285.43,"night":285.43,"eve":283.96,"morn":283.93},"pressure":1032.72,"humidity":97,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":5.47,"deg":184,"clouds":92,"rain":3}]}
```

## OpenShift で動かす
OpenShift で Composer を有効にした後で、Git に add/commit/push する。

### (1) OpenShift で Composer を有効にする

[PHP Markers | OpenShift Developers](https://developers.openshift.com/en/php-markers.html) の use_composer を使う。

```
$ touch .openshift/markers/use_composer
```

### (2) 公開する

```
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   index.php

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore
	.openshift/markers/use_composer
	composer.json
	composer.lock	

$ git add .

$ git commit -m 'enable Composer and use Guzzle to fetch weather data'

$ git push
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 2.10 KiB | 0 bytes/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Stopping PHP 5.4 cartridge (Apache+mod_php)
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Building git ref 'master', commit e815d11
remote: Checking .openshift/pear.txt for PEAR dependency...
remote: Checking composer.json for Composer dependency... 
remote: Loading composer repositories with package information
remote: Installing dependencies (including require-dev) from lock file
remote:   - Installing react/promise (v2.2.0)
remote:     Downloading: 100%
remote: 
remote:   - Installing guzzlehttp/streams (3.0.0)
remote:     Downloading: 100%
remote: 
remote:   - Installing guzzlehttp/ringphp (1.0.5)
remote:     Downloading: 100%
remote: 
remote:   - Installing guzzlehttp/guzzle (5.2.0)
remote:     Downloading: 100%
remote: 
remote: Generating autoload files
remote: Preparing build for deployment
remote: Deployment id is bf319162
remote: Activating deployment
remote: Starting PHP 5.4 cartridge (Apache+mod_php)
remote: Application directory "/" selected as DocumentRoot
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://xxxxxxxxxxxxxxxxxx@foo-bar.rhcloud.com/~/git/bar.git/
   c2ee0b4..e815d11  master -> master
```

Web ブラウザーからアクセスして動作を確認する。http://foo-bar.rhcloud.com にアクセスすれば、お天気データを取得できているはず。