Source for file Ethna_Plugin_Handle.php

Documentation is available at Ethna_Plugin_Handle.php

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

Documentation generated on Fri, 11 Nov 2011 04:00:20 +0900 by phpDocumentor 1.4.3