OSDN Git Service

- fixed bug Ethna_Session#isAnonymous returns invalid result (thanks:longkey1)
[ethna/ethna.git] / class / Ethna_Handle.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_Handle.php
5  *
6  *  @author     Masaki Fujimoto <fujimoto@php.net>
7  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
8  *  @package    Ethna
9  *  @version    $Id$
10  */
11
12 // {{{ Ethna_Handle
13 /**
14  *  Manager class of Ethna (Command Line) Handlers
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_Handle
21 {
22     /**#@+
23      *  @access     private
24      */
25
26     /** @var    object  Ethna_Controller    controllerオブジェクト */
27     var $controller;
28
29     /** @var    object  Ethna_Controller    controllerオブジェクト($controllerの省略形) */
30     var $ctl;
31
32     /** @var    object  Ethna_Pluguin       pluginオブジェクト */
33     var $plugin;
34
35     /**#@-*/
36
37     // {{{ constructor
38     /**
39      *  Ethna_Handle constructor (stub for php4)
40      *
41      *  @access public
42      */
43     function Ethna_Handle()
44     {
45         $this->controller =& new Ethna_Controller(GATEWAY_CLI);
46         Ethna::clearErrorCallback();
47         Ethna::setErrorCallback(array('Ethna_Handle', 'handleError'));
48
49         $this->ctl =& $this->controller;
50         $this->plugin =& $this->controller->getPlugin();
51     }
52     // }}}
53
54     // {{{ getHandler
55     /**
56      *  get handler object
57      *
58      *  @access public
59      */
60     function &getHandler($id)
61     {
62         $name = preg_replace('/\-(.)/e', "strtoupper('\$1')", ucfirst($id));
63         $handler =& $this->plugin->getPlugin('Handle', $name);
64         if (Ethna::isError($handler)) {
65             return $handler;
66         }
67
68         return $handler;
69     }
70     // }}}
71
72     // {{{ getHandlerList
73     /**
74      *  get an object list of all available handlers
75      *
76      *  @access public
77      */
78     function getHandlerList()
79     {
80         $handler_list = $this->plugin->getPluginList('Handle');
81         usort($handler_list, array($this, "_handler_sort_callback"));
82
83         return $handler_list;
84     }
85
86     /**
87      *  sort callback method
88      */
89     function _handler_sort_callback($a, $b)
90     {
91         return strcmp($a->getId(), $b->getId());
92     }
93     // }}}
94
95     // {{{ getEthnaController
96     /**
97      *  Ethna_Controllerのインスタンスを取得する
98      *  (Ethna_Handlerの文脈で呼び出されることが前提)
99      *
100      *  @access public
101      *  @static
102      */
103     function &getEthnaController()
104     {
105         return Ethna_Controller::getInstance();
106     }
107     // }}}
108
109     // {{{ getAppController
110     /**
111      *  アプリケーションのコントローラファイル/クラスを検索する
112      *
113      *  @access public
114      *  @static
115      */
116     function &getAppController($app_dir = null)
117     {
118         static $app_controller = array();
119
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.');
124         }
125
126         $ini_file = null;
127         while (is_dir($app_dir)) {
128             if (is_file("$app_dir/.ethna")) {
129                 $ini_file = "$app_dir/.ethna";
130                 break;
131             }
132             $app_dir = dirname($app_dir);
133             if (Ethna_Util::isRootDir($app_dir)) {
134                 break;
135             }
136         }
137
138         if ($ini_file === null) {
139             return Ethna::raiseError('no .ethna file found');
140         }
141         
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');
146         }
147         $file = $macro['controller_file'];
148         $class = $macro['controller_class'];
149
150         $controller_file = "$app_dir/$file";
151         if (is_file($controller_file) == false) {
152             return Ethna::raiseError("no such file $controller_file");
153         }
154
155         include_once $controller_file;
156         if (class_exists($class) == false) {
157             return Ethna::raiseError("no such class $class");
158         }
159
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'));
165
166         return $app_controller[$app_dir];
167     }
168     // }}}
169
170     // {{{ getMasterSetting
171     /**
172      *  Ethna 本体の設定を取得する (ethnaコマンド用)
173      *
174      *  @param  $section    ini ファイルの section
175      *  @access public
176      */
177     function &getMasterSetting($section = null)
178     {
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);
184             } else {
185                 $setting = array();
186             }
187         }
188
189         if ($section === null) {
190             return $setting;
191         } else if (array_key_exists($section, $setting)) {
192             return $setting[$section];
193         } else {
194             $array = array();
195             return $array;
196         }
197     }
198     // }}}
199
200     // {{{ handleError
201     /**
202      *  Ethna コマンドでのエラーハンドリング
203      */
204     function handleError(&$eobj)
205     {
206         // do nothing.
207     }
208     // }}}
209 }
210 // }}}
211 ?>