public static function run($task, $args)
 {
     // Make sure something is set
     if ($task === null or $task === 'help') {
         static::help();
         return;
     }
     // Just call and run() or did they have a specific method in mind?
     list($task, $method) = array_pad(explode(':', $task), 2, 'run');
     $task = ucfirst(strtolower($task));
     // Find the task
     if (!($file = \Fuel::find_file('tasks', $task))) {
         $files = \Fuel::list_files('tasks');
         $possibilities = array();
         foreach ($files as $file) {
             $possible_task = pathinfo($file, \PATHINFO_FILENAME);
             $difference = levenshtein($possible_task, $task);
             $possibilities[$difference] = $possible_task;
         }
         ksort($possibilities);
         if ($possibilities and current($possibilities) <= 5) {
             throw new Exception(sprintf('Task "%s" does not exist. Did you mean "%s"?', strtolower($task), current($possibilities)));
         } else {
             throw new Exception(sprintf('Task "%s" does not exist.', strtolower($task)));
         }
         return;
     }
     require $file;
     $task = '\\Fuel\\Tasks\\' . $task;
     $new_task = new $task();
     // The help option hs been called, so call help instead
     if (\Cli::option('help') && is_callable(array($new_task, 'help'))) {
         $method = 'help';
     }
     if ($return = call_user_func_array(array($new_task, $method), $args)) {
         \Cli::write($return);
     }
 }
Exemple #2
0
 /**
  * Find all of the task classes in the system and use reflection to discover the
  * commands we can call.
  *
  * @return array $taskname => array($taskmethods)
  **/
 protected static function _discover_tasks()
 {
     $result = array();
     $files = \Fuel::list_files('tasks');
     if (count($files) > 0) {
         foreach ($files as $file) {
             $task_name = str_replace('.php', '', basename($file));
             $class_name = '\\Fuel\\Tasks\\' . $task_name;
             require $file;
             $reflect = new \ReflectionClass($class_name);
             // Ensure we only pull out the public methods
             $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
             $result[$task_name] = array();
             if (count($methods) > 0) {
                 foreach ($methods as $method) {
                     $result[$task_name][] = $method->name;
                 }
             }
         }
     }
     return $result;
 }