示例#1
0
文件: Clover.php 项目: sinri/Clover
 public static function startForCLI($root_path = null)
 {
     global $argv;
     global $argc;
     try {
         Clover::$current_task_uuid = uniqid();
         Clover::LogCommand();
         if ($root_path !== null) {
             Clover::setRootPath($root_path);
         }
         $controller_name = Clover::getController($sub_paths);
         if (empty($controller_name)) {
             $controller_name = Clover::$default_controller_name;
         }
         if (class_exists($controller_name)) {
             $controller = new $controller_name();
             if (!is_a($controller, 'CloverCommand')) {
                 throw new Exception($controller_name, -404);
             }
         } else {
             throw new Exception($controller_name, -404);
         }
         $parameter_list = array();
         if (!empty($sub_paths)) {
             $action = $sub_paths[0] . 'Action';
             if (method_exists($controller, $action)) {
                 unset($sub_paths[0]);
                 //some test
                 $reflect = new ReflectionMethod($controller_name, $action);
                 foreach ($reflect->getParameters() as $param) {
                     $param_name = $param->getName();
                     // set default values
                     if ($param->isDefaultValueAvailable() && $param->isDefaultValueConstant()) {
                         $parameter_list[$param_name] = $param->getDefaultValueConstantName();
                     } else {
                         $parameter_list[$param_name] = "";
                     }
                 }
                 foreach ($sub_paths as $item) {
                     $eq_index = strpos($item, '=');
                     if ($eq_index !== false) {
                         $p_name = substr($item, 2, $eq_index - 2);
                         $p_value = substr($item, $eq_index + 1);
                         if (isset($parameter_list[$p_name])) {
                             $parameter_list[$p_name] = $p_value;
                         }
                     }
                 }
             } else {
                 throw new Exception($controller_name . "->" . $action, -404);
             }
         } else {
             $action = 'defaultAction';
         }
         call_user_func_array(array($controller, 'beforeExecute'), array($controller_name, $action, $parameter_list));
         call_user_func_array(array($controller, $action), $parameter_list);
         call_user_func_array(array($controller, 'afterExecute'), array($controller_name, $action, $parameter_list));
     } catch (Exception $e) {
         if ($e->getCode() == -404) {
             //Command and action Not Found
             echo "Command and action missing: " . $e->getMessage() . PHP_EOL;
         } else {
             echo "Clover Command Error: " . $e->getMessage() . PHP_EOL;
         }
     }
 }