2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
14 * Manager class of Ethna (Command Line) Handlers
16 * @author Masaki Fujimoto <fujimoto@php.net>
26 /** @var object Ethna_Controller controllerオブジェクト */
29 /** @var object Ethna_Controller controllerオブジェクト($controllerの省略形) */
32 /** @var object Ethna_Pluguin pluginオブジェクト */
39 * Ethna_Handle constructor (stub for php4)
43 function Ethna_Handle()
45 $this->controller =& new Ethna_Controller(GATEWAY_CLI);
46 Ethna::clearErrorCallback();
47 Ethna::setErrorCallback(array('Ethna_Handle', 'handleError'));
49 $this->ctl =& $this->controller;
50 $this->plugin =& $this->controller->getPlugin();
60 function &getHandler($id)
62 $name = preg_replace('/\-(.)/e', "strtoupper('\$1')", ucfirst($id));
63 $handler =& $this->plugin->getPlugin('Handle', $name);
64 if (Ethna::isError($handler)) {
74 * get an object list of all available handlers
78 function getHandlerList()
80 $handler_list = $this->plugin->getPluginList('Handle');
81 usort($handler_list, array($this, "_handler_sort_callback"));
87 * sort callback method
89 function _handler_sort_callback($a, $b)
91 return strcmp($a->getId(), $b->getId());
95 // {{{ getEthnaController
97 * Ethna_Controllerのインスタンスを取得する
98 * (Ethna_Handlerの文脈で呼び出されることが前提)
103 function &getEthnaController()
105 return Ethna_Controller::getInstance();
109 // {{{ getAppController
111 * アプリケーションのコントローラファイル/クラスを検索する
116 function &getAppController($app_dir = null)
118 static $app_controller = array();
120 if (isset($app_controller[$app_dir])) {
121 return $app_controller[$app_dir];
122 } else if ($app_dir === null) {
123 return Ethna::raiseError('$app_dir not specified.');
127 while (is_dir($app_dir)) {
128 if (is_file("$app_dir/.ethna")) {
129 $ini_file = "$app_dir/.ethna";
132 $app_dir = dirname($app_dir);
133 if (Ethna_Util::isRootDir($app_dir)) {
138 if ($ini_file === null) {
139 return Ethna::raiseError('no .ethna file found');
142 $macro = parse_ini_file($ini_file);
143 if (isset($macro['controller_file']) == false
144 || isset($macro['controller_class']) == false) {
145 return Ethna::raiseError('invalid .ethna file');
147 $file = $macro['controller_file'];
148 $class = $macro['controller_class'];
150 $controller_file = "$app_dir/$file";
151 if (is_file($controller_file) == false) {
152 return Ethna::raiseError("no such file $controller_file");
155 include_once $controller_file;
156 if (class_exists($class) == false) {
157 return Ethna::raiseError("no such class $class");
160 $global_controller =& $GLOBALS['_Ethna_controller'];
161 $app_controller[$app_dir] =& new $class(GATEWAY_CLI);
162 $GLOBALS['_Ethna_controller'] =& $global_controller;
163 Ethna::clearErrorCallback();
164 Ethna::setErrorCallback(array('Ethna_Handle', 'handleError'));
166 return $app_controller[$app_dir];
170 // {{{ getMasterSetting
172 * Ethna 本体の設定を取得する (ethnaコマンド用)
174 * @param $section ini ファイルの section
177 function &getMasterSetting($section = null)
179 static $setting = null;
180 if ($setting === null) {
181 $ini_file = ETHNA_BASE . "/.ethna";
182 if (is_file($ini_file) && is_readable($ini_file)) {
183 $setting = parse_ini_file($ini_file, true);
189 if ($section === null) {
191 } else if (array_key_exists($section, $setting)) {
192 return $setting[$section];
202 * Ethna コマンドでのエラーハンドリング
204 function handleError(&$eobj)