[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cli/src/ -> lmbCliRunner.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/cli/src/lmbCliBaseCmd.class.php');
  10  
  11  @define('LIMB_CLI_INCLUDE_PATH', 'cli;limb/*/cli');
  12  
  13  /**

  14   * class lmbCliRunner.

  15   *

  16   * @package cli

  17   * @version $Id$

  18   */
  19  class lmbCliRunner
  20  {
  21    protected $input;
  22    protected $output;
  23    protected $return_on_exit = false;
  24    protected $use_exception = false;
  25    protected $search_path;
  26  
  27    function __construct($input, $output)
  28    {
  29      $this->input = $input;
  30      $this->output = $output;
  31      $this->search_path = LIMB_CLI_INCLUDE_PATH;
  32    }
  33  
  34    static function commandToClass($name)
  35    {
  36      return lmb_camel_case(self :: sanitizeName($name)) . 'CliCmd';
  37    }
  38  
  39    static function actionToMethod($name)
  40    {
  41      return lmb_camel_case(self :: sanitizeName($name));
  42    }
  43  
  44    static function sanitizeName($name)
  45    {
  46      $name = preg_replace('~\W~', '_', $name);
  47      return $name;
  48    }
  49  
  50    function setCommandSearchPath($path)
  51    {
  52      $this->search_path = $path;
  53    }
  54  
  55    function returnOnExit($flag = true)
  56    {
  57      $this->return_on_exit = $flag;
  58    }
  59  
  60    function throwOnError($flag = true)
  61    {
  62      $this->use_exception = $flag;
  63    }
  64  
  65    function execute()
  66    {
  67      if(!$command_name = $this->input->getArgument(0))
  68        $this->_error('You should specify command');
  69  
  70      if(!$command = $this->_mapCommandToObject($command_name))
  71        $this->_error("Command '$command_name' is invalid(could not map it to the command class)");
  72  
  73      $argv = $this->input->getArgv();
  74      array_shift($argv);
  75  
  76      $action = 'execute';
  77  
  78      if($arg = $this->input->getArgument(1))
  79      {
  80        $method = self :: actionToMethod($arg);
  81        if(method_exists($command, $method))
  82        {
  83          $action = $method;
  84          array_shift($argv);
  85        }
  86      }
  87      return $this->_exit((int)$command->$action($argv));
  88    }
  89  
  90    protected function _exit($code = 0)
  91    {
  92      if($this->return_on_exit)
  93        return $code;
  94      else
  95        exit($code);
  96    }
  97  
  98    protected function _error($message = '')
  99    {
 100      if($this->use_exception)
 101        throw new lmbException($message);
 102      else
 103        exit(1);
 104    }
 105  
 106    protected function _mapCommandToObject($command_name)
 107    {
 108      $items = explode(';', $this->search_path);
 109      foreach($items as $item)
 110      {
 111        $class = self :: commandToClass($command_name);
 112        $path = $item . '/' . $class . '.class.php';
 113  
 114        if($resolved = lmb_glob($path))
 115        {
 116          require_once($resolved[0]);
 117          return new $class($this->output);
 118        }
 119      }
 120    }
 121  }
 122  ?>


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7