OSDN Git Service

Remove PHP closing tag
[ethna/ethna.git] / class / Ethna_Plugin.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_Plugin.php
5  *
6  *  @author     ICHII Takashi <ichii386@schweetheart.jp>
7  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
8  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
9  *  @package    Ethna
10  *  @version    $Id$
11  */
12
13 // {{{ Ethna_Plugin
14 /**
15  *  プラグインクラス
16  *  
17  *  @author     ICHII Takashi <ichii386@schweetheart.jp>
18  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
19  *  @access     public
20  *  @package    Ethna
21  */
22 class Ethna_Plugin
23 {
24     /**#@+
25      *  @access private
26      */
27
28     /** @var    object  Ethna_Controller    コントローラオブジェクト */
29     var $controller;
30
31     /** @var    object  Ethna_Controller    コントローラオブジェクト($controllerの省略形) */
32     var $ctl;
33
34     /** @var    object  Ethna_Logger        ログオブジェクト */
35     var $logger;
36
37     /** @var    array   プラグインのオブジェクト(インスタンス)を保存する配列 */
38     var $obj_registry = array();
39
40     /** @var    array   プラグインのクラス名、ソースファイル名を保存する配列 */
41     var $src_registry = array();
42
43     /** @var    array   検索対象ディレクトリを,プラグインの優先順に保存する配列 */
44     var $_dirlist = array();
45
46     /**#@-*/
47
48     // {{{ コンストラクタ
49     /**
50      *  Ethna_Pluginのコンストラクタ
51      *
52      *  @access public
53      *  @param  object  Ethna_Controller    $controller コントローラオブジェクト
54      */
55     function Ethna_Plugin(&$controller)
56     {
57         $this->controller =& $controller;
58         $this->ctl =& $this->controller;
59         $this->logger = null;
60
61         // load dir_registry
62         $this->_loadPluginDirList();
63
64     }
65
66     /**
67      *  loggerをsetする。
68      *
69      *  LogWriterはpluginなので、pluginインスタンス作成時点では
70      *  loggerに依存しないようにする。
71      *
72      *  @access public
73      *  @param  object  Ethna_Logger    $logger ログオブジェクト
74      */
75     function setLogger(&$logger)
76     {
77         if ($this->logger === null && is_object($logger)) {
78             $this->logger =& $logger;
79         }
80     }
81     // }}}
82
83     // {{{ プラグイン呼び出しインタフェース
84     /**
85      *  プラグインのインスタンスを取得
86      *
87      *  @access public
88      *  @param  string  $type   プラグインの種類
89      *  @param  string  $name   プラグインの名前
90      *  @return object  プラグインのインスタンス
91      */
92     function &getPlugin($type, $name)
93     {
94         return $this->_getPlugin($type, $name);
95     }
96
97     /**
98      *  ある種類 ($type) のプラグイン ($name) の全リストを取得
99      *
100      *  @access public
101      *  @param  string  $type   プラグインの種類
102      *  @return array   プラグインオブジェクトの配列
103      */
104     function getPluginList($type)
105     {
106         $plugin_list = array();
107
108         $this->searchAllPluginSrc($type);
109         if (isset($this->src_registry[$type]) == false) {
110             return $plugin_list;
111         }
112         foreach ($this->src_registry[$type] as $name => $value) {
113             if (is_null($value)) {
114                 continue;
115             }
116             $plugin_list[$name] =& $this->getPlugin($type, $name);
117         }
118         return $plugin_list;
119     }
120     // }}}
121
122     // {{{ obj_registry のアクセサ
123     /**
124      *  プラグインのインスタンスをレジストリから取得
125      *
126      *  @access private
127      *  @param  string  $type   プラグインの種類
128      *  @param  string  $name   プラグインの名前
129      *  @return object  プラグインのインスタンス
130      */
131     function &_getPlugin($type, $name)
132     {
133         if (isset($this->obj_registry[$type]) == false) {
134             $this->obj_registry[$type] = array();
135
136             // プラグインの親クラスを(存在すれば)読み込み
137             list($class, $file) = $this->getPluginNaming($type, null);
138             $dir = $this->_searchPluginSrcDir($type, null);
139             if (!Ethna::isError($dir)) {
140                 $this->_includePluginSrc($class, $dir, $file, true);
141             }
142         }
143
144         // key がないときはプラグインをロードする
145         if (array_key_exists($name, $this->obj_registry[$type]) == false) {
146             $this->_loadPlugin($type, $name);
147         }
148
149         // null のときはロードに失敗している
150         if (is_null($this->obj_registry[$type][$name])) {
151             return Ethna::raiseWarning('plugin [type=%s, name=%s] is not found',
152                 E_PLUGIN_NOTFOUND, $type, $name);
153         }
154
155         // プラグインのインスタンスを返す
156         return $this->obj_registry[$type][$name];
157     }
158
159     /**
160      *  プラグインをincludeしてnewし,レジストリに登録
161      *
162      *  @access private
163      *  @param  string  $type   プラグインの種類
164      *  @param  string  $name   プラグインの名前
165      */
166     function _loadPlugin($type, $name)
167     {
168         // プラグインのファイル名を取得
169         $plugin_src_registry = $this->_getPluginSrc($type, $name);
170         if (is_null($plugin_src_registry)) {
171             $this->obj_registry[$type][$name] = null;
172             return;
173         }
174         list($plugin_class, $plugin_dir, $plugin_file) = $plugin_src_registry;
175
176         // プラグインのファイルを読み込み
177         $r =& $this->_includePluginSrc($plugin_class, $plugin_dir, $plugin_file);
178         if (Ethna::isError($r)) {
179             $this->obj_registry[$type][$name] = null;
180             return;
181         }
182
183         // プラグイン作成
184         $instance =& new $plugin_class($this->controller, $type, $name);
185         if (is_object($instance) == false
186             || strcasecmp(get_class($instance), $plugin_class) != 0) {
187
188             if ($this->logger !== null) {
189                 $this->logger->log(LOG_WARNING, 'plugin [%s::%s] instantiation failed', $type, $name);
190             }
191
192             $this->obj_registry[$type][$name] = null;
193             return;
194         }
195         $this->obj_registry[$type][$name] =& $instance;
196     }
197
198     /**
199      *  プラグインのインスタンスをレジストリから消す
200      *
201      *  @access private
202      *  @param  string  $type   プラグインの種類
203      *  @param  string  $name   プラグインの名前
204      */
205     function _unloadPlugin($type, $name)
206     {
207         unset($this->obj_registry[$type][$name]);
208     }
209     // }}}
210
211     /**
212      *  プラグインのインスタンスをレジストリから消す
213      *
214      *  @access private
215      *  @param  string  $type   プラグインの種類
216      *  @param  string  $name   プラグインの名前
217      */
218     function _loadPluginDirList()
219     {
220         $this->_dirlist[] = $this->controller->getDirectory('plugin');
221
222         // include_path から検索
223         $include_path_list = explode(PATH_SEPARATOR, get_include_path());
224
225         // Communiy based libraries
226         $extlib_dir = implode(DIRECTORY_SEPARATOR, array('Ethna', 'extlib', 'Plugin'));
227         // Ethna bandle
228         $class_dir = implode(DIRECTORY_SEPARATOR, array('Ethna', 'class', 'Plugin'));
229         foreach ($include_path_list as $include_path) {
230             if (is_dir($include_path . DIRECTORY_SEPARATOR . $extlib_dir)) {
231                 $this->_dirlist[] = $include_path . DIRECTORY_SEPARATOR . $extlib_dir;
232             }
233             if (is_dir($include_path . DIRECTORY_SEPARATOR . $class_dir)) {
234                 $this->_dirlist[] = $include_path . DIRECTORY_SEPARATOR . $class_dir;
235             }
236         }
237     }
238
239     // {{{ src_registry のアクセサ
240     /**
241      *  プラグインのソースファイル名とクラス名をレジストリから取得
242      *
243      *  @access private
244      *  @param  string  $type   プラグインの種類
245      *  @param  string  $name   プラグインの名前
246      *  @return array   ソースファイル名とクラス名からなる配列
247      */
248     function _getPluginSrc($type, $name)
249     {
250         if (isset($this->src_registry[$type]) == false) {
251             $this->src_registry[$type] = array();
252         }
253
254         // key がないときはプラグインの検索をする
255         if (array_key_exists($name, $this->src_registry[$type]) == false) {
256             $this->_searchPluginSrc($type, $name);
257         }
258
259         // プラグインのソースを返す
260         return $this->src_registry[$type][$name];
261     }
262     // }}}
263
264     // {{{ プラグインファイル検索部分
265     /**
266      *  プラグインのクラス名、ディレクトリ、ファイル名を決定
267      *
268      *  @access public
269      *  @param  string  $type   プラグインの種類
270      *  @param  string  $name   プラグインの名前 (nullのときは親クラス)
271      *  @param  string  $appid  アプリケーションID (廃止予定)
272      *  @return array   プラグインのクラス名、ファイル名の配列
273      */
274     function getPluginNaming($type, $name = null, $appid = 'Ethna')
275     {
276         $ext = $this->ctl->getExt('php');
277
278         $plugin_class_name = array(
279             $appid,
280             'Plugin',
281             $type,
282         );
283
284         if ($name !== null) {
285             $plugin_class_name[] = $name;
286         }
287         else {
288             $name = $type;
289         }
290
291         $class = implode('_', $plugin_class_name);
292         $file  = "{$name}.{$ext}";
293
294         return array($class, $file);
295     }
296
297     /**
298      *  プラグインのソースを include する
299      *
300      *  @access private
301      *  @param  string  $class  クラス名
302      *  @param  string  $dir    ディレクトリ名
303      *  @param  string  $file   ファイル名
304      *  @param  bool    $parent 親クラスかどうかのフラグ
305      *  @return true|Ethna_Error
306      */
307     function &_includePluginSrc($class, $dir, $file, $parent = false)
308     {
309         $true = true;
310         if (class_exists($class)) {
311             return $true;
312         }
313
314         $file = $dir . '/' . $file;
315         if (file_exists_ex($file) === false) {
316             if ($parent === false) {
317                 return Ethna::raiseWarning('plugin file is not found: [%s]',
318                                            E_PLUGIN_NOTFOUND, $file);
319             } else {
320                 return $true;
321             }
322         }
323
324         include_once $file;
325
326         if (class_exists($class) === false) {
327             if ($parent === false) {
328                 return Ethna::raiseWarning('plugin class [%s] is not defined',
329                     E_PLUGIN_NOTFOUND, $class);
330             } else {
331                 return $true;
332             }
333         }
334
335         if ($parent === false) {
336             if ($this->logger !== null) {
337                 $this->logger->log(LOG_DEBUG, 'plugin class [%s] is defined', $class);
338             }
339         }
340         return $true;
341     }
342
343     /**
344      *  プラグインのソースディレクトリを決定する
345      *
346      *  @param  string  $type   プラグインの種類
347      *  @param  string  $name   プラグインの名前 (nullのときは親クラス)
348      *  @retur  string  directory
349      */
350     function _searchPluginSrcDir($type, $name)
351     {
352         list(, $file) = $this->getPluginNaming($type, $name);
353
354         $dir_prefix = "";
355         if ($name !== null) {
356             $dir_prefix = DIRECTORY_SEPARATOR . $type;
357         }
358
359         // dirlist にしたがって検索
360         foreach ($this->_dirlist as $dir) {
361             $dir .= $dir_prefix;
362
363             if (file_exists($dir . DIRECTORY_SEPARATOR . $file)) {
364                 return $dir;
365             }
366         }
367
368         return Ethna::raiseWarning('plugin file is not found in search directories: [%s]',
369                                    E_PLUGIN_NOTFOUND, $file);
370     }
371
372     /**
373      *  アプリケーション, extlib, Ethna の順でプラグインのソースを検索する
374      *
375      *  @access private
376      *  @param  string  $type   プラグインの種類
377      *  @param  string  $name   プラグインの名前
378      *  @return array   class, dir, file
379      */
380     function _searchPluginSrc($type, $name)
381     {
382         list($class, $file) = $this->getPluginNaming($type, $name);
383
384         // 古いバージョンのプラグインの命名規則にしたがったファイルは無視
385         if (strpos($name, "_") !== false) {
386             return;
387         }
388
389         if (class_exists($class)) {
390             // すでにクラスが存在する場合は特別にスキップ
391             if (isset($this->src_registry[$type]) == false) {
392                 $this->src_registry[$type] = array();
393             }
394         }
395
396         $dir = $this->_searchPluginSrcDir($type, $name);
397
398         if (Ethna::isError($dir)) {
399             $this->src_registry[$type][$name] = null;
400             return ;
401         }
402
403         if (file_exists("{$dir}/{$file}")) {
404             $this->logger->log(LOG_DEBUG, 'plugin file is found in search: [%s/%s]',
405                                $dir, $file);
406             if (isset($this->src_registry[$type]) == false) {
407                 $this->src_registry[$type] = array();
408             }
409             $this->src_registry[$type][$name] = array($class, $dir, $file);
410             return;
411         }
412
413         // 見つからなかった場合 (nullで記憶しておく)
414         $this->logger->log(LOG_WARNING, 'plugin file for [type=%s, name=%s] is not found in search', $type, $name);
415         $this->src_registry[$type][$name] = null;
416     }
417
418     /**
419      *  プラグインの種類 ($type) をすべて検索する
420      *
421      *  @access public
422      *  @return array
423      */
424     function searchAllPluginType()
425     {
426         $type_list = array();
427         foreach($this->_dirlist as $dir) {
428             $type_dir= glob($dir . DIRECTORY_SEPARATOR . "*", GLOB_ONLYDIR);
429             if (!$type_dir) {
430                 continue;
431             }
432             foreach ($type_dir as $dir) {
433                 if ($type_dir{0} != '.') {
434                     $type_list[basename($dir)] = 0;
435                 }
436             }
437         }
438         return array_keys($type_list);
439     }
440
441     /**
442      *  指定された $type のプラグイン (のソース) をすべて検索する
443      *
444      *  @access public
445      *  @param  string  $type   プラグインの種類
446      */
447     function searchAllPluginSrc($type)
448     {
449         // 後で見付かったもので上書きするので $this->appid_list の逆順とする
450         $name_list = array();
451         $ext = $this->ctl->getExt('php');
452
453         foreach($this->_dirlist as $dir) {
454             $files = glob($dir . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . "/*." . $ext);
455             if (!$files) {
456                 $this->logger->log(LOG_DEBUG, 'cannot open plugin directory: [%s/%s]', $dir, $type);
457                 continue;
458             }
459             $this->logger->log(LOG_DEBUG, 'plugin directory opened: [%s]', $dir);
460             foreach ($files as $plugin_file) {
461                 $plugin_name = substr(basename($plugin_file), 0, - strlen($ext) - 1);
462                 $name_list[$plugin_name] = 0;
463             }
464         }
465
466         foreach (array_keys($name_list) as $name) {
467             // 冗長だがもう一度探しなおす
468             $this->_searchPluginSrc($type, $name);
469         }
470     }
471     // }}}
472
473     // {{{ static な include メソッド
474     /**
475      *  Ethna 本体付属のプラグインのソースを include する
476      *  (B.C.) Ethna 2.5.0 perview 5 以降,このメソッドには意味がありません.Ethna_Plugin::import を使ってください
477      *
478      *  @access public
479      *  @param  string  $type   プラグインの種類
480      *  @param  string  $name   プラグインの名前
481      *  @static
482      */
483     function includeEthnaPlugin($type, $name)
484     {
485         Ethna_Plugin::import($type, $name);
486     }
487
488     /**
489      *  プラグインのソースを include する
490      *
491      *  @access public
492      *  @param  string  $type   プラグインの種類
493      *  @param  string  $name   プラグインの名前
494      *  @param  string  $appid  アプリケーションID
495      */
496     function includePlugin($type, $name = null)
497     {
498         list($class, $file) = $this->getPluginNaming($type, $name);
499         $dir = $this->_searchPluginSrcDir($type, $name);
500         $this->_includePluginSrc($class, $dir, $file);
501     }
502     // }}}
503
504     /**
505      *  プラグインのソースを include する
506      *
507      *  @access public
508      *  @param  string  $type   プラグインの種類
509      *  @param  string  $name   プラグインの名前
510      *  @param  string  $appid  アプリケーションID
511      *  @static
512      */
513     // static function import($type, $name)
514     function import($type, $name = null)
515     {
516         $ctl =& Ethna_Controller::getInstance();
517         $plugin =& $ctl->getPlugin();
518
519         if ($appid === null) {
520             $appid = $ctl->getAppId();
521         }
522
523         $plugin->includePlugin($type, $name);
524     }
525     // }}}
526 }
527 // }}}