PHP_Shell
[ class tree: PHP_Shell ] [ index: PHP_Shell ] [ all elements ]

Source for file Options.php

Documentation is available at Options.php

  1. <?php
  2.  
  3. require_once "PHP/Shell/Extensions.php"/* for the PHP_Shell_Interface */
  4.   
  5. /**
  6. *
  7. */
  8. class PHP_Shell_Options implements PHP_Shell_Extension {
  9.     /*
  10.     * instance of the current class
  11.     *
  12.     * @var PHP_Shell_Options
  13.     */
  14.     static protected $instance;
  15.  
  16.     /**
  17.     * known options and their setors
  18.     *
  19.     * @var array 
  20.     * @see registerOption
  21.     */
  22.     protected $options = array();
  23.  
  24.     /**
  25.     * known options and their setors
  26.     *
  27.     * @var array 
  28.     * @see registerOptionAlias
  29.     */
  30.     protected $option_aliases = array();
  31.  
  32.     public function register({
  33.         $cmd = PHP_Shell_Commands::getInstance();
  34.         $cmd->registerCommand('#^:set #'$this'cmdSet'':set <var>''set a shell variable');
  35.     }
  36.  
  37.     /**
  38.     * register a option
  39.     *
  40.     * @param string name of the option
  41.     * @param object object handle
  42.     * @param string method-name of the setor in the object
  43.     * @param string (unused)
  44.     */ 
  45.     public function registerOption($option$obj$setor$getor = null{
  46.         if (!method_exists($obj$setor)) {
  47.             throw new Exception(sprintf("setor %s doesn't exist on class %s"$setorget_class($obj)));
  48.         }
  49.  
  50.         $this->options[trim($option)= array("obj" => $obj"setor" => $setor);
  51.     }
  52.  
  53.     /**
  54.     * set a shell-var
  55.     *
  56.     * :set al to enable autoload
  57.     * :set bg=dark to enable highlighting with a dark backgroud
  58.     */
  59.     public function cmdSet($l{
  60.         if (!preg_match('#:set\s+([a-z]+)\s*(?:=\s*([a-z0-9]+)\s*)?$#i'$l$a)) {
  61.             print(':set failed: either :set <option> or :set <option> = <value>');
  62.             return;
  63.         }
  64.  
  65.         $this->execute($a[1]isset($a[2]$a[2: null);
  66.     }
  67.  
  68.     /**
  69.     * get all the option names
  70.     *
  71.     * @return array names of all options
  72.     */
  73.     public function getOptions({
  74.         return array_keys($this->options);
  75.     }
  76.  
  77.     /**
  78.     * map a option to another option
  79.     *
  80.     * e.g.: bg maps to background
  81.     *
  82.     * @param string alias
  83.     * @param string option
  84.     */
  85.     public function registerOptionAlias($alias$option{
  86.         if (!isset($this->options[$option])) {
  87.             throw new Exception(sprintf("Option %s is not known"$option));
  88.         }
  89.  
  90.         $this->option_aliases[trim($alias)trim($option);
  91.     
  92.     }
  93.  
  94.     /**
  95.     * execute a :set command
  96.     *
  97.     * calls the setor for the :set <option>
  98.     * 
  99.     * 
  100.     */
  101.     private function execute($key$value{
  102.         /* did we hit a alias (bg for backgroud) ? */
  103.         if (isset($this->option_aliases[$key])) {
  104.             $opt_key $this->option_aliases[$key];
  105.         else {
  106.             $opt_key $key;
  107.         }
  108.  
  109.         if (!isset($this->options[$opt_key])) {
  110.             print (':set '.$key.' failed: unknown key');
  111.             return;
  112.         }
  113.         
  114.         if (!isset($this->options[$opt_key]["setor"])) {
  115.             return;
  116.         }
  117.  
  118.         $setor $this->options[$opt_key]["setor"];
  119.         $obj $this->options[$opt_key]["obj"];
  120.         $obj->$setor($key$value);
  121.     }
  122.  
  123.     static function getInstance({
  124.         if (is_null(self::$instance)) {
  125.             $class = __CLASS__;
  126.             self::$instance = new $class();
  127.         }
  128.         return self::$instance;
  129.     }
  130. }

Documentation generated on Mon, 11 Mar 2019 14:40:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.