PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/class/Handle.php

http://github.com/ethna/ethna
PHP | 210 lines | 105 code | 25 blank | 80 comment | 24 complexity | 9c32ce5a412c94f6a3058d8a55563cd7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4. * 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: f3d32974043c7fbc97d1dec586714c59b834c285 $
  10. */
  11. // {{{ Ethna_Handle
  12. /**
  13. * Manager class of Ethna (Command Line) Handlers
  14. *
  15. * @author Masaki Fujimoto <fujimoto@php.net>
  16. * @access public
  17. * @package Ethna
  18. */
  19. class Ethna_Handle
  20. {
  21. /**#@+
  22. * @access private
  23. */
  24. /** @protected object Ethna_Controller controller?????? */
  25. protected $controller;
  26. /** @protected object Ethna_Controller controller??????($controller????) */
  27. protected $ctl;
  28. /** @protected object Ethna_Pluguin plugin?????? */
  29. protected $plugin;
  30. /**#@-*/
  31. // {{{ constructor
  32. /**
  33. * Ethna_Handle constructor
  34. *
  35. * @access public
  36. */
  37. public function __construct()
  38. {
  39. $this->controller = new Ethna_Controller(GATEWAY_CLI);
  40. Ethna::clearErrorCallback();
  41. Ethna::setErrorCallback(array('Ethna_Handle', 'handleError'));
  42. $this->ctl = $this->controller;
  43. $this->plugin = $this->controller->getPlugin();
  44. }
  45. // }}}
  46. // {{{ getHandler
  47. /**
  48. * get handler object
  49. *
  50. * @access public
  51. */
  52. public function getHandler($id)
  53. {
  54. $name = preg_replace('/\-(.)/e', "strtoupper('\$1')", ucfirst($id));
  55. $handler = $this->plugin->getPlugin('Handle', $name);
  56. if (Ethna::isError($handler)) {
  57. return $handler;
  58. }
  59. return $handler;
  60. }
  61. // }}}
  62. // {{{ getHandlerList
  63. /**
  64. * get an object list of all available handlers
  65. *
  66. * @access public
  67. */
  68. public function getHandlerList()
  69. {
  70. $handler_list = $this->plugin->getPluginList('Handle');
  71. usort($handler_list, array($this, "_handler_sort_callback"));
  72. return $handler_list;
  73. }
  74. /**
  75. * sort callback method
  76. */
  77. public static function _handler_sort_callback($a, $b)
  78. {
  79. return strcmp($a->getId(), $b->getId());
  80. }
  81. // }}}
  82. // {{{ getEthnaController
  83. /**
  84. * Ethna_Controller????????????
  85. * (Ethna_Handler???????????????)
  86. *
  87. * @access public
  88. * @static
  89. */
  90. public static function getEthnaController()
  91. {
  92. return Ethna_Controller::getInstance();
  93. }
  94. // }}}
  95. // {{{ getAppController
  96. /**
  97. * ???????????????????/????????
  98. *
  99. * @access public
  100. * @static
  101. */
  102. public static function getAppController($app_dir = null)
  103. {
  104. static $app_controller = array();
  105. if (isset($app_controller[$app_dir])) {
  106. return $app_controller[$app_dir];
  107. } else if ($app_dir === null) {
  108. return Ethna::raiseError('$app_dir not specified.');
  109. }
  110. $ini_file = null;
  111. while (is_dir($app_dir)) {
  112. if (is_file("$app_dir/.ethna")) {
  113. $ini_file = "$app_dir/.ethna";
  114. break;
  115. }
  116. $app_dir = dirname($app_dir);
  117. if (Ethna_Util::isRootDir($app_dir)) {
  118. break;
  119. }
  120. }
  121. if ($ini_file === null) {
  122. return Ethna::raiseError('no .ethna file found');
  123. }
  124. $macro = parse_ini_file($ini_file);
  125. if (isset($macro['controller_file']) == false
  126. || isset($macro['controller_class']) == false) {
  127. return Ethna::raiseError('invalid .ethna file');
  128. }
  129. $file = $macro['controller_file'];
  130. $class = $macro['controller_class'];
  131. $controller_file = "$app_dir/$file";
  132. if (is_file($controller_file) == false) {
  133. return Ethna::raiseError("no such file $controller_file");
  134. }
  135. include_once $controller_file;
  136. if (class_exists($class) == false) {
  137. return Ethna::raiseError("no such class $class");
  138. }
  139. $global_controller = $GLOBALS['_Ethna_controller'];
  140. $app_controller[$app_dir] = new $class(GATEWAY_CLI);
  141. $GLOBALS['_Ethna_controller'] = $global_controller;
  142. Ethna::clearErrorCallback();
  143. Ethna::setErrorCallback(array('Ethna_Handle', 'handleError'));
  144. return $app_controller[$app_dir];
  145. }
  146. // }}}
  147. // {{{ getMasterSetting
  148. /**
  149. * Ethna ?????????? (ethna?????)
  150. *
  151. * @param $section ini ????? section
  152. * @access public
  153. */
  154. public static function getMasterSetting($section = null)
  155. {
  156. static $setting = null;
  157. if ($setting === null) {
  158. $ini_file = ETHNA_BASE . "/.ethna";
  159. if (is_file($ini_file) && is_readable($ini_file)) {
  160. $setting = parse_ini_file($ini_file, true);
  161. } else {
  162. $setting = array();
  163. }
  164. }
  165. if ($section === null) {
  166. return $setting;
  167. } else if (array_key_exists($section, $setting)) {
  168. return $setting[$section];
  169. } else {
  170. $array = array();
  171. return $array;
  172. }
  173. }
  174. // }}}
  175. // {{{ handleError
  176. /**
  177. * Ethna ???????????????
  178. */
  179. public static function handleError($eobj)
  180. {
  181. // do nothing.
  182. }
  183. // }}}
  184. }
  185. // }}}