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: e3dfa2349bfed09e51fa3f2e09b2fbe3c8f3bebf $
  10.  */
  11.  
  12. require_once ETHNA_BASE '/class/Getopt.php';
  13.  
  14. // {{{ Ethna_Plugin_Handle
  15. /**
  16.  *  コマンドラインハンドラプラグインの基底クラス
  17.  *  
  18.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  19.  *  @access     public
  20.  *  @package    Ethna
  21.  */
  22. {
  23.     /** @protected    handler's id */
  24.     protected $id;
  25.  
  26.     /** @protected    command line arguments */
  27.     protected $arg_list;
  28.  
  29.     /**
  30.      *  Ethna_Handle constructor (stub for php4)
  31.      *
  32.      *  @access public
  33.      */
  34.     public function __construct($controller$type$name)
  35.     {
  36.         parent::__construct($controller$type$name);
  37.  
  38.         $id $name;
  39.         $id preg_replace('/^([A-Z])/e'"strtolower('\$1')"$id);
  40.         $id preg_replace('/([A-Z])/e'"'-' . strtolower('\$1')"$id);
  41.         $this->id = $id;
  42.     }
  43.  
  44.     /**
  45.      *  get handler-id
  46.      *
  47.      *  @access public
  48.      */
  49.     function getId()
  50.     {
  51.         return $this->id;
  52.     }
  53.  
  54.     /**
  55.      *  get handler's description
  56.      *
  57.      *  @access public
  58.      */
  59.     function getDescription()
  60.     {
  61.         return "description of " $this->id;
  62.     }
  63.  
  64.     /**
  65.      *  get handler's usage
  66.      *
  67.      *  @access public
  68.      */
  69.     function getUsage()
  70.     {
  71.         return "usage of " $this->id;
  72.     }
  73.  
  74.     /**
  75.      *  set arguments
  76.      *
  77.      *  @access public
  78.      */
  79.     function setArgList($arg_list)
  80.     {
  81.         $this->arg_list = $arg_list;
  82.     }
  83.  
  84.     /**
  85.      * easy getopt :)
  86.      *
  87.      * @param   array   $lopts  long options
  88.      * @return  array   list($opts, $args)
  89.      * @access  protected
  90.      */
  91.     function _getopt($lopts array())
  92.     {
  93.         // create opts
  94.         // ex: $lopts = array('foo', 'bar=');
  95.         $lopts to_array($lopts);
  96.         $sopts '';
  97.         $opt_def array();
  98.         foreach ($lopts as $lopt{
  99.             if ($lopt{strlen($lopt2=== '='{
  100.                 $opt_def[$lopt{0}substr($lopt0strlen($lopt2);
  101.                 $sopts .= $lopt{0'::';
  102.             else if ($lopt{strlen($lopt1=== '='{
  103.                 $opt_def[$lopt{0}substr($lopt0strlen($lopt1);
  104.                 $sopts .= $lopt{0':';
  105.             else {
  106.                 $opt_def[$lopt{0}$lopt;
  107.                 $sopts .= $lopt{0};
  108.             }
  109.         }
  110.  
  111.         // do getopt
  112.         // ex: $sopts = 'fb:';
  113.         $opt new Ethna_Getopt();
  114.         $opts_args $opt->getopt($this->arg_list$sopts$lopts);
  115.         if (Ethna::isError($opts_args)) {
  116.             return $opts_args;
  117.         }
  118.  
  119.         // parse opts
  120.         // ex: "-ff --bar=baz" gets
  121.         //      $opts = array('foo' => array(true, true),
  122.         //                    'bar' => array('baz'));
  123.         $opts array();
  124.         foreach ($opts_args[0as $opt{
  125.             $opt[0$opt[0]{0=== '-' $opt_def[$opt[0]{2}$opt_def[$opt[0]{0}];
  126.             $opt[1$opt[1=== null true $opt[1];
  127.             if (isset($opts[$opt[0]]=== false{
  128.                 $opts[$opt[0]] array($opt[1]);
  129.             else {
  130.                 $opts[$opt[0]][$opt[1];
  131.             }
  132.         }
  133.         $opts_args[0$opts;
  134.  
  135.         return $opts_args;
  136.     }
  137.  
  138.     /**
  139.      *  just perform
  140.      *
  141.      *  @access public
  142.      */
  143.     function perform()
  144.     {
  145.     }
  146.  
  147.     /**
  148.      *  show usage
  149.      *
  150.      *  @access public
  151.      */
  152.     function usage()
  153.     {
  154.         echo "usage:\n";
  155.         echo $this->getUsage("\n\n";
  156.     }
  157. }
  158. // }}}

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