mult1mate\crontab\TaskManager::parseCommand PHP Method

parseCommand() public static method

Parses command and returns an array which contains class, method and arguments of the command
public static parseCommand ( string $command ) : array
$command string
return array
    public static function parseCommand($command)
    {
        if (preg_match('/([\\w\\\\]+)::(\\w+)\\((.*)\\)/', $command, $match)) {
            $params = explode(',', $match[3]);
            if (1 == count($params) && '' == $params[0]) {
                //prevents to pass an empty string
                $params[0] = null;
            }
            return array($match[1], $match[2], $params);
        }
        throw new TaskManagerException('Command not recognized');
    }

Usage Example

Beispiel #1
0
 /**
  * Parses given command, creates new class object and calls its method via call_user_func_array
  * @param string $command
  * @return mixed
  */
 public static function parseAndRunCommand($command)
 {
     try {
         list($class, $method, $args) = TaskManager::parseCommand($command);
         if (!class_exists($class)) {
             TaskLoader::loadController($class);
         }
         $obj = new $class();
         if (!method_exists($obj, $method)) {
             throw new TaskManagerException('method ' . $method . ' not found in class ' . $class);
         }
         return call_user_func_array(array($obj, $method), $args);
     } catch (\Exception $e) {
         echo 'Caught an exception: ' . get_class($e) . ': ' . PHP_EOL . $e->getMessage() . PHP_EOL;
         return false;
     }
 }