Ethnaの環境構築
基本方針
PEARを利用したパッケージ管理は行いません。最新版が落ちてくるのはいいのですが、環境によってバージョンが変わると好ましくない場合もあるので、必要な時に手動で差し替えるようにします。
一般的には公式サイトと同じ様にPearを利用した方が簡単だと思います。
プロジェクト名は「ethna_test」とします。
PEARの取得
http://pear.php.net/package/PEAR/download
PEAR本体
上記から取得して解凍しPEAR.phpのみ取得。
他のライブラリから依存されている可能性が高いので必要です。他にもいろいろファイルがありますが自動的にパッケージを取得するものですので、今回は使いません。
基本的にバージョン違いによる依存性はあまりないと思います。
/ethna_test/libs/pear/PEAR.php
に設置。
Smartyの取得
http://www.smarty.net/download.php
上記から取得。結構こまごま変更されているので、最新版を持ってこよう。あまりバージョンで依存性は少ないと思います。
にlibsの中身のみ設置。/ethna_test/libs/Smarty/Smarty.class.php があるようにする。
Pear::DBの取得
http://pear.php.net/package/DB/download
上記から取得。もう古いライブラリなので、個人的には他のDBアクセスエンジンに変更した方がいいと思う。。。
昔に利用した場合にはMDB2に入れ替えていました。あとで他のに変更するとして、今回は標準環境で作ってみたいと思います。
に設置。
Ethnaの取得
上記から取得。今回はバージョン 2.5.0 preview1を利用。
これはこまめに新しいのに入れ替えた方がいいですが、マイナーバージョンがあがっていると動かなくなる場合があるので、テスト環境で確認後に更新しましょう!
に設置
プロジェクトの作成準備
本来Ethnaはコマンドラインから実行することが前提となっています。今回はコマンドライン禁止でいきます!
まず正規の作成方法ですが
http://ethna.jp/ethna-document-tutorial-practice1.html
ここを見ると
ethna add-project -b=/tmp sample
こんな感じみたいですね。実行している中身を見てみるとWindiws環境の場合 pear/Ethna/bin/ethna_handle.php を呼んでいるみたいです。
ethna_handle.php の依存関係調査
中身で読み込んでいるライブラリは
require_once 'PEAR.php'; require_once 'Console/Getopt.php'; require_once 'Ethna/Ethna.php';
この3つです。Getopt以外はすでにありますね。Getoptはコマンドライン系の取得ライブラリみたいです。
Getopt の取得
http://pear.php.net/package/Console_Getopt
上記から取得。
これはコマンドラインでしか使わないので、通常のpearと隔離して設置したいと思います。
/ethna_test/libs/pear-cli/Console/Getopt.php
に設置。
プロジェクトスケルトン作成用プログラム作成
<?php //ライブラリパス(Ethna,PEAR,ETC...) ini_set('include_path', dirname(__FILE__) . '/libs/pear' . PATH_SEPARATOR . dirname(__FILE__) . '/libs/pear-cli' ); // 引数セット $argv = array("add-project.php", "add-project", "codes"); // プロジェクトのスケルトン作成! echo "<pre>"; require_once 'Ethna/bin/ethna_handle.php';
Getoptは$argvの中身を見て処理していますので、引数をプログラムから与えてあげます。本当は完全なWebからのヘルパー作った方がいいですね。今度作成してみたいと思います。
引数の内容ですが1番目がスクリプトの名前。ここは利用しないので空白でも大丈夫です。2番目以降が実際のコマンドラインの引数となります。
今回は /ethna_test/codes にプロジェクトのファイルを出力することにしました。あらかじめ /ethna_test/codes/ フォルダを作成しておく必要があります。書き込み権限も与えてあげてくださいね。
実行
(ローカル環境)http://localhost/ethna_test/add-project.php
project sub directory created [C:\xampp\htdocs\ethna_test/codes/app] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/action] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/action_cli] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/action_xmlrpc] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/filter] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/plugin] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/plugin/Filter] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/plugin/Validator] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/view] project sub directory created [C:\xampp\htdocs\ethna_test/codes/app/test] project sub directory created [C:\xampp\htdocs\ethna_test/codes/bin] project sub directory created [C:\xampp\htdocs\ethna_test/codes/etc] project sub directory created [C:\xampp\htdocs\ethna_test/codes/lib] project sub directory created [C:\xampp\htdocs\ethna_test/codes/locale] project sub directory created [C:\xampp\htdocs\ethna_test/codes/locale/ja_JP] project sub directory created [C:\xampp\htdocs\ethna_test/codes/locale/ja_JP/LC_MESSAGES] project sub directory created [C:\xampp\htdocs\ethna_test/codes/log] project sub directory created [C:\xampp\htdocs\ethna_test/codes/schema] project sub directory created [C:\xampp\htdocs\ethna_test/codes/skel] project sub directory created [C:\xampp\htdocs\ethna_test/codes/template] project sub directory created [C:\xampp\htdocs\ethna_test/codes/template/ja_JP] project sub directory created [C:\xampp\htdocs\ethna_test/codes/tmp] project sub directory created [C:\xampp\htdocs\ethna_test/codes/www] project sub directory created [C:\xampp\htdocs\ethna_test/codes/www/css] project sub directory created [C:\xampp\htdocs\ethna_test/codes/www/js] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/locale/ja_JP/ethna_sysmsg.ini -> C:\xampp\htdocs\ethna_test/codes/locale/ja_JP/LC_MESSAGES/ethna_sysmsg.ini] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/www.index.php -> C:\xampp\htdocs\ethna_test/codes/www/index.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/www.info.php -> C:\xampp\htdocs\ethna_test/codes/www/info.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/www.unittest.php -> C:\xampp\htdocs\ethna_test/codes/www/unittest.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/www.xmlrpc.php -> C:\xampp\htdocs\ethna_test/codes/www/xmlrpc.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/www.css.ethna.css -> C:\xampp\htdocs\ethna_test/codes/www/css/ethna.css] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/dot.ethna -> C:\xampp\htdocs\ethna_test/codes/.ethna] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.controller.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_Controller.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.error.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_Error.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.actionclass.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_ActionClass.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.actionform.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_ActionForm.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.viewclass.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_ViewClass.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.action.default.php -> C:\xampp\htdocs\ethna_test/codes/app/action/Index.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.plugin.filter.default.php -> C:\xampp\htdocs\ethna_test/codes/app/plugin/Filter/Codes_Plugin_Filter_ExecutionTime.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.view.default.php -> C:\xampp\htdocs\ethna_test/codes/app/view/Index.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.unittest.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_UnitTestManager.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/app.url_handler.php -> C:\xampp\htdocs\ethna_test/codes/app/Codes_UrlHandler.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/etc.ini.php -> C:\xampp\htdocs\ethna_test/codes/etc/codes-ini.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/template.index.tpl -> C:\xampp\htdocs\ethna_test/codes/template/ja_JP/index.tpl] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.action.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.action.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.action_cli.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.action_cli.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.action_test.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.action_test.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.app_object.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.app_object.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.entry_www.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.entry_www.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.entry_cli.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.entry_cli.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.view.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.view.php] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.template.tpl -> C:\xampp\htdocs\ethna_test/codes/skel/skel.template.tpl] file generated [C:\xampp\htdocs\ethna_test\libs\pear\Ethna/skel/skel.view_test.php -> C:\xampp\htdocs\ethna_test/codes/skel/skel.view_test.php] project skelton for [codes] is successfully generated at [C:\xampp\htdocs\ethna_test]
私の環境はxamppだったので上記のような感じになりました。
プロジェクトの動作確認
(ローカル環境)http://localhost/ethna_test/codes/www/
プロジェクトのURLにアクセスすると、エラーが出るはずです。
Warning: require_once(Ethna/Ethna.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\ethna_test\codes\app\Codes_Controller.php on line 20 Fatal error: require_once() [function.require]: Failed opening required 'Ethna/Ethna.php' (include_path='C:\xampp\htdocs\ethna_test\codes/app;C:\xampp\htdocs\ethna_test\codes/lib;.;C:\xampp\php\pear\') in C:\xampp\htdocs\ethna_test\codes\app\Codes_Controller.php on line 20
Pearが読み込めませんのでエラーが発生します。このエラーが発生しない場合には利用している環境のEthnaが利用されていますので、バージョンが違うので動いていたりします。。。
プロジェクトの初期設定
最低でもPearとSmartyの場所を指定する必要があります。
/ethna_test/codes/app/Codes_Controller.php
/** Application base directory */ define('BASE', dirname(dirname(__FILE__))); // Pear and Smarty Path ini_set('include_path', dirname(BASE) . '/libs/pear' . PATH_SEPARATOR . dirname(BASE) . '/libs' ); /** include_path setting (adding "/app" and "/lib" directory to include_path) */ $app = BASE . "/app"; $lib = BASE . "/lib"; ini_set('include_path', implode(PATH_SEPARATOR, array($app, $lib)) . PATH_SEPARATOR . ini_get('include_path'));
動作検証
(ローカル環境)http://localhost/ethna_test/codes/www/
hello, world! が表示されたら成功です!
ここまででコマンドラインがなくても実行できることがわかりましたので、Webからのインターフェースを今度作成してみたいと思います。
また今回は開発環境なのでプロジェクト全体がWebからアクセスできるように設置していますが、本番環境の場合には /ethna_test/codes/www が公開されるように設定します。それ以外のファイルにアクセスされると危ないですからね。
作成物
http://akira.info/labs/ethna/ethna_test.zip
ここにアップしておきました。
Ethnaのバージョンは利用する時の最新にして、codesの中身を消して作り直すことでベースとして利用できるかな?