Do You PHP?    
Search Engine Optimization  php5 powerd  Valid XHTML 1.0!  Valid CSS!  このサイトのはてなブックマーク数 



last updated
2005/08/26

counter hits
since 1999/11/06


Phing2 - PHP版Ant for PHP5

先日PHP4用のPhingネタを取り上げましたが、今回はPHP5用です。2005/08/25現在の現行系列Phing2.xから独立してPHP5専用になっています。最新バージョンver.2.1.0です。

今回はPHP4版と異なる

  • インストール手順
  • 動作確認
  • サンプルの実行と結果(PHPUnit2中心)

を中心にまとめてみました。

インストール手順

まずはインストールした環境ですが、PHPは以下のようなconfigureオプションを付けたPHP5.0.4です。

●PHP5.0.4のconfigureオプション
--with-apxs2=/usr/local/apache2/bin/apxs \
--prefix=/usr/local/lib/php5 \
--with-pear=/usr/local/lib/php5/pear \
--with-config-file-path=/usr/local/lib/php5/ini/5.0.4 \
--with-config-file-scan-dir=/usr/local/lib/php5/ini.d \
--enable-zend-multibyte \
--enable-mbstring \
--enable-mbregex \
--with-dom \
--with-gd=shared \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-ttf \
--with-freetype-dir \
--enable-gd-jis-conv \
--with-java=shared,/usr/local/jdk \
--with-xsl \
--with-oci8=/u01/app/oracle/product/10.1.0 \
--without-mysql \
--enable-soap

「--prefix」オプションを付けているため、phpコマンドのパスがデフォルトとは変わっていますが適宜読み替えてください。

Phing2はpearコマンドでのインストールが可能になっています。以下のようにPhingのサイトを直接指定することでインストール可能です。また、他の依存パッケージもインストールするため、「--alldeps」もしくは「-a」オプションを付けておきます。ただし、全ての機能を利用可能にするにはα版のパッケージもインストールされるため、PEARの設定preferred_stateをalphaに設定しておく必要があります。

●pearコマンドによるPhing2.xのインストール
$ /usr/local/lib/php5/bin/pear config-set preferred_state alpha
$ /usr/local/lib/php5/bin/pear install -a http://phing.info/pear/phing-current.tgz
downloading phing-current.tgz ...
Starting to download phing-current.tgz (370,301 bytes)
.................................................................
...........done: 370,301 bytes
downloading VersionControl_SVN-0.3.0alpha1.tgz ...
Starting to download VersionControl_SVN-0.3.0alpha1.tgz (33,829 bytes)
...done: 33,829 bytes
downloading PHPUnit2-2.3.0beta4.tgz ...
Starting to download PHPUnit2-2.3.0beta4.tgz (42,740 bytes)
...done: 42,740 bytes
downloading PhpDocumentor-1.3.0RC3.tgz ...
Starting to download PhpDocumentor-1.3.0RC3.tgz (2,711,672 bytes)
...done: 2,711,672 bytes
downloading xdebug-2.0.0beta3.tgz ...
Starting to download xdebug-2.0.0beta3.tgz (197,794 bytes)
...done: 197,794 bytes
Package 'Archive_Tar' already installed, skipping
downloading XML_Parser-1.2.6.tgz ...
Starting to download XML_Parser-1.2.6.tgz (12,944 bytes)
...done: 12,944 bytes
downloading Benchmark-1.2.3.tgz ...
Starting to download Benchmark-1.2.3.tgz (6,679 bytes)
...done: 6,679 bytes
downloading Log-1.8.7.tgz ...
Starting to download Log-1.8.7.tgz (32,693 bytes)
...done: 32,693 bytes
downloading XML_Beautifier-1.1.tgz ...
Starting to download XML_Beautifier-1.1.tgz (9,854 bytes)
...done: 9,854 bytes
Package 'PEAR' already installed, skipping
downloading DB-1.7.6.tgz ...
Starting to download DB-1.7.6.tgz (124,807 bytes)
...done: 124,807 bytes
downloading XML_Util-1.1.1.tgz ...
Starting to download XML_Util-1.1.1.tgz (8,358 bytes)
...done: 8,358 bytes
             :
Optional dependencies:
'bcmath' PHP extension is recommended to utilize some features
install ok: Benchmark 1.2.3
install ok: DB 1.7.6
install ok: XML_Util 1.1.1
install ok: Log 1.8.7
install ok: XML_Parser 1.2.6
install ok: VersionControl_SVN 0.3.0alpha1
PHP version >= 5.1.0b1 is required
PHPUnit2: Dependencies failed
$
 

なお、PHPUnit2についてはバージョン2.3.0β1からPHP5.1.0β1以降が必要になっていますので、別途PEARのページから一つ前のバージョンである2.2.1をダウンロード・インストールしました。また、phpDocumentorについてもPHPUnit2のエラーのため正しくインストールできていませんでしたので、こちらも別途インストールしました。

●PHPUnit2、phpDocumentorのインストール
$ /usr/local/lib/php5/bin/pear install --alldeps PHPUnit2-2.2.1.tgz
$ /usr/local/lib/php5/bin/pear install --alldeps phpdocumentor
$ 

設定が終わったら、正しくコマンドがインストールされているか確認しておきます。

●PATHの確認
$ ls /usr/local/lib/php5/bin/
pear*   php*         phpdoc*      phpize*   scripts/
phing*  php-config*  phpextdist*  phpunit*
$ 

Phingのドキュメントは、Phingのサイトもしくは/usr/local/lib/php5/pear/doc/phing/phing_guideディレクトリにあります。

サンプルの実行

Phing2にはサンプルが用意されています。今回は適当なディレクトリ下にサンプルディレクトリをコピーしてを使ってみました。ここではベースとなるディレクトリを「sample」としています。

●サンプルのコピー
$ mkdir sample
$ cd sample/
$ cp -rp /usr/local/lib/php5/pear/doc/phing/example phing_example
$ cd phing_sample/
$ 

phing_sample/build.xmlを見ていただくと、PHP4用のbinarycloud版にはなかったタスク(タグ)があれこれあります。例えば、

  • phpdoc(binarycloud版ではドキュメント化されていませんでしたがタスク本体は用意されています)
  • phpunit2
  • coverage-setup
  • phpunit2report
  • coverage-report

で、標準でPHPUnit2やphpDocumentorが用意されています。

なお、PHPUnit2のカバレッジを利用するためには、PHPUnit2ネタのページにもある通り、Xdebugをロードしておく必要があります。今回はzend_extensionとして設定しました。

●php.ini
extension_dir = "/usr/local/lib/php5/lib/php/extensions/no-debug-non-zts-20041030/"
zend_extension=xdebug.so

ここまで来たら、いよいよphingコマンドを実行します。LD_LIBRARY_PATHは、私の環境ではxdebug.soをロードするために必要でした。

●phingの実行
$ LD_LIBRARY_PATH=/usr/local/lib/php5/lib/php/extensions/no-debug-non-zts-20041030/ \
> /usr/local/lib/php5/bin/phing
Buildfile: /path/to/sample/phing_example/build.xml

Hello World Example > test:

Hello World Example > prepare:
    [mkdir] Created dir: /path/to/sample/phing_example/reports
    [mkdir] Created dir: /path/to/sample/phing_example/reports/tests
    [mkdir] Created dir: /path/to/sample/phing_example/docs

Hello World Example > reports:
Execution of target "reports" failed for the following reason:
 /path/to/sample/phing_example/build.xml:26:325:
 Could not find file ../../etc/phpunit2-frames.xsl

BUILD FAILED
/path/to/sample/phing_example/build.xml:26:325:
              Could not find file ../../etc/phpunit2-frames.xsl
Total time: 0.4470 seconds
$ 

う。。。楽しみにしていたカバレッジのレポート作成でエラー。。。( ̄Д ̄;)

このphpunit2-frames.xsl、pearコマンドでインストールしたphing-current.tgzには含まれていますがpackage.xmlに記述がないことから、正しくインストールされないようです。ということで、改めてphing-current.tgzをダウンロードし、アーカイブ内のphing-2.1.0/etcディレクトリをsample/etcとして展開しました。

●etcディレクトリの展開
$ tar zxf phing-current.tgz phing-2.1.0/etc
$ mv phing-2.1.0/etc ./
$ rmdir phing-2.1.0/
$ ls
build.xml  docs/  etc/  reports/  src/
$ 

また、build.xml内のetcディレクトリへの相対パス「../../etc」を「etc」に修正しておきます。

●build.xmlの修正
<?xml version="1.0"?>
               :
   <target name="reports">
               :
    <phpunit2report infile="reports/testsuites.xml"
                    format="frames" todir="reports/tests" styledir="etc"/>
    <coverage-report outfile="reports/coverage.xml">
      <report todir="reports/coverage" styledir="etc"/>
    </coverage-report>
  </target>
               :

で、よくよく見てみると、coverage-reportタスクのレポート出力先(reportタグのtodir属性)が「reports/coverage」となっていますが、prepareターゲットで作成されていませんのでエラーとなってしまいます。こちらも追記しておきます。

●build.xmlの修正(2)
<?xml version="1.0"?>

  <target name="prepare">
    <mkdir dir="reports"/>
    <mkdir dir="reports/tests"/>
    <mkdir dir="reports/coverage"/>
    <mkdir dir="docs"/>
  </target>
               :

ここまで来たら、再度phingコマンドを実行します。以下の例は、prepareターゲットで作成されたディレクトリは削除しています。

●phingの実行
$ rm -r docs/ reports/
$ LD_LIBRARY_PATH=/usr/local/lib/php5/lib/php/extensions/no-debug-non-zts-20041030/ \
> /usr/local/lib/php5/bin/phing
Buildfile: /path/to/sample/phing_example/build.xml

Hello World Example > test:

Hello World Example > prepare:
    [mkdir] Created dir: /path/to/sample/phing_example/reports
    [mkdir] Created dir: /path/to/sample/phing_example/reports/tests
    [mkdir] Created dir: /path/to/sample/phing_example/reports/coverage
    [mkdir] Created dir: /path/to/sample/phing_example/docs

Hello World Example > reports:

Hello World Example > docs:

Hello World Example > build:
      [zip] Building zip: /path/to/sample/phing_example/helloworld.zip
      [tar] Building tar: /path/to/sample/phing_example/helloworld.tar

BUILD FINISHED

Total time: 1.3040 second
$ 

やっと動作しました(^-^; ユニットテストのレポートカバレッジレポートAPIドキュメントもちゃんと作成されています。

ここまでの結果はEXPERIENCEのページから参照することができます。

まとめ

etcディレクトリにはやられました。。。

ではなくて、PhingやPHPUnit、phpDocumentorなどが出そろってきたことで、Javaの開発環境でいう「Ant+JUnit+JCoverage」な環境がPHPでも可能になったと思います。また、Phingのexecタスクを使うことで外部コマンドを利用できますので、「cvs+Ant+JUnit+JCoverage+scp/ftp/rsync」のようなことも可能でしょう。個人的には、PHP言語自身だけではなく、その周辺ツール(特に開発向け)が揃ってきていることは非常に嬉しく思います。いろいろ楽できますし(^-^;

PHP5になりオブジェクト指向言語色を強めたことで、これまでJavaの世界で培われてきた開発方法論やフレームワーク、開発ツールに対する「ノウハウ」がPHPにポートされることが多くなるでしょう。そういった意味でも、PHPだけではなくJavaの動向もチェックしておくことが重要だなぁ、と思う今日この頃です。



About This Site |  Privacy Policy |  Contact
Copyright © 1999 - 2005 by Hideyuki SHIMOOKA all rights reserved.