Source for file Handle.php

Documentation is available at Handle.php

  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.  
  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. {
  21.     /**#@+
  22.      *  @access     private
  23.      */
  24.  
  25.     /** @protected    object  Ethna_Controller    controllerオブジェクト */
  26.     protected $controller;
  27.  
  28.     /** @protected    object  Ethna_Controller    controllerオブジェクト($controllerの省略形) */
  29.     protected $ctl;
  30.  
  31.     /** @protected    object  Ethna_Pluguin       pluginオブジェクト */
  32.     protected $plugin;
  33.  
  34.     /**#@-*/
  35.  
  36.     // {{{ constructor
  37.     /**
  38.      *  Ethna_Handle constructor
  39.      *
  40.      *  @access public
  41.      */
  42.     public function __construct()
  43.     {
  44.         $this->controller = new Ethna_Controller(GATEWAY_CLI);
  45.         Ethna::clearErrorCallback();
  46.         Ethna::setErrorCallback(array('Ethna_Handle''handleError'));
  47.  
  48.         $this->ctl = $this->controller;
  49.         $this->plugin = $this->controller->getPlugin();
  50.     }
  51.     // }}}
  52.  
  53.     // {{{ getHandler
  54.     /**
  55.      *  get handler object
  56.      *
  57.      *  @access public
  58.      */
  59.     public function getHandler($id)
  60.     {
  61.         $name preg_replace('/\-(.)/e'"strtoupper('\$1')"ucfirst($id));
  62.         $handler $this->plugin->getPlugin('Handle'$name);
  63.         if (Ethna::isError($handler)) {
  64.             return $handler;
  65.         }
  66.  
  67.         return $handler;
  68.     }
  69.     // }}}
  70.  
  71.     // {{{ getHandlerList
  72.     /**
  73.      *  get an object list of all available handlers
  74.      *
  75.      *  @access public
  76.      */
  77.     public function getHandlerList()
  78.     {
  79.         $handler_list $this->plugin->getPluginList('Handle');
  80.         usort($handler_listarray($this"_handler_sort_callback"));
  81.  
  82.         return $handler_list;
  83.     }
  84.  
  85.     /**
  86.      *  sort callback method
  87.      */
  88.     public static function _handler_sort_callback($a$b)
  89.     {
  90.         return strcmp($a->getId()$b->getId());
  91.     }
  92.     // }}}
  93.  
  94.     // {{{ getEthnaController
  95.     /**
  96.      *  Ethna_Controllerのインスタンスを取得する
  97.      *  (Ethna_Handlerの文脈で呼び出されることが前提)
  98.      *
  99.      *  @access public
  100.      *  @static
  101.      */
  102.     public static function getEthnaController()
  103.     {
  104.         return Ethna_Controller::getInstance();
  105.     }
  106.     // }}}
  107.  
  108.     // {{{ getAppController
  109.     /**
  110.      *  アプリケーションのコントローラファイル/クラスを検索する
  111.      *
  112.      *  @access public
  113.      *  @static
  114.      */
  115.     public static function getAppController($app_dir null)
  116.     {
  117.         static $app_controller array();
  118.  
  119.         if (isset($app_controller[$app_dir])) {
  120.             return $app_controller[$app_dir];
  121.         else if ($app_dir === null{
  122.             return Ethna::raiseError('$app_dir not specified.');
  123.         }
  124.  
  125.         $ini_file null;
  126.         while (is_dir($app_dir)) {
  127.             if (is_file("$app_dir/.ethna")) {
  128.                 $ini_file "$app_dir/.ethna";
  129.                 break;
  130.             }
  131.             $app_dir dirname($app_dir);
  132.             if (Ethna_Util::isRootDir($app_dir)) {
  133.                 break;
  134.             }
  135.         }
  136.  
  137.         if ($ini_file === null{
  138.             return Ethna::raiseError('no .ethna file found');
  139.         }
  140.         
  141.         $macro parse_ini_file($ini_file);
  142.         if (isset($macro['controller_file']== false
  143.             || isset($macro['controller_class']== false{
  144.             return Ethna::raiseError('invalid .ethna file');
  145.         }
  146.         $file $macro['controller_file'];
  147.         $class $macro['controller_class'];
  148.  
  149.         $controller_file "$app_dir/$file";
  150.         if (is_file($controller_file== false{
  151.             return Ethna::raiseError("no such file $controller_file");
  152.         }
  153.  
  154.         include_once $controller_file;
  155.         if (class_exists($class== false{
  156.             return Ethna::raiseError("no such class $class");
  157.         }
  158.  
  159.         $global_controller $GLOBALS['_Ethna_controller'];
  160.         $app_controller[$app_dirnew $class(GATEWAY_CLI);
  161.         $GLOBALS['_Ethna_controller'$global_controller;
  162.         Ethna::clearErrorCallback();
  163.         Ethna::setErrorCallback(array('Ethna_Handle''handleError'));
  164.  
  165.         return $app_controller[$app_dir];
  166.     }
  167.     // }}}
  168.  
  169.     // {{{ getMasterSetting
  170.     /**
  171.      *  Ethna 本体の設定を取得する (ethnaコマンド用)
  172.      *
  173.      *  @param  $section    ini ファイルの section
  174.      *  @access public
  175.      */
  176.     public static function getMasterSetting($section null)
  177.     {
  178.         static $setting null;
  179.         if ($setting === null{
  180.             $ini_file ETHNA_BASE "/.ethna";
  181.             if (is_file($ini_file&& is_readable($ini_file)) {
  182.                 $setting parse_ini_file($ini_filetrue);
  183.             else {
  184.                 $setting array();
  185.             }
  186.         }
  187.  
  188.         if ($section === null{
  189.             return $setting;
  190.         else if (array_key_exists($section$setting)) {
  191.             return $setting[$section];
  192.         else {
  193.             $array array();
  194.             return $array;
  195.         }
  196.     }
  197.     // }}}
  198.  
  199.     // {{{ handleError
  200.     /**
  201.      *  Ethna コマンドでのエラーハンドリング
  202.      */
  203.     public static function handleError($eobj)
  204.     {
  205.         // do nothing.
  206.     }
  207.     // }}}
  208. }
  209. // }}}

Documentation generated on Fri, 11 Nov 2011 03:58:15 +0900 by phpDocumentor 1.4.3