示例#1
0
 /** @param $task Task */
 public function processTask($task)
 {
     parent::processTask($task);
     $data = $task->request->post->get('commands');
     if (!is_string($data)) {
         $this->postResults(array(ServiceResult::createWithError('error', 'No data', '::GLOBAL::')));
         return;
     }
     if (strlen($data) > $this->maxPackageSize) {
         $this->postResults(array(ServiceResult::createWithError('error', 'Data too large', '::GLOBAL::')));
         return;
     }
     $data = json_decode($data, true);
     if (json_last_error() != JSON_ERROR_NONE || !is_array($data)) {
         $this->postResults(array(ServiceResult::createWithError('error', 'Invalid data', '::GLOBAL::')));
         return;
     }
     $results = array();
     $f = new ArrayFilter();
     foreach ($data as $command) {
         if (!is_array($command)) {
             $this->postResults(array(ServiceResult::createWithError('error', 'Invalid data', '::GLOBAL::')));
             return;
         }
         $f->setData($command);
         $id = $f->get('id');
         $command = $f->get('command');
         $data = $f->asFilter('data');
         $results[] = $this->_executeCommand($command, $data, $id);
     }
     $this->postResults($results);
 }
示例#2
0
 protected function _executeCommand($command, $data = null, $id = null)
 {
     try {
         if (!preg_match('!^[a-zA-Z0-9_/]+$!', $command)) {
             return ServiceResult::createWithError('error', 'Invalid command', $id);
         } else {
             $command = str_replace('/', '\\', $command);
             $found = false;
             $className = null;
             foreach ($this->commandNamespaces as $commandNamespace) {
                 $className = $commandNamespace . $command . 'Command';
                 if (class_exists($className)) {
                     $found = true;
                     break;
                 }
             }
             if ($found) {
                 /** @var ServiceCommand $c */
                 $c = new $className();
                 $c->task = $this->_task;
                 $c->app = $this->_task->app;
                 $c->data = $data;
                 $c->result = new ServiceResult($id);
                 $c->execute();
                 return $c->result;
             } else {
                 return ServiceResult::createWithError('error', 'Invalid command', $id);
             }
         }
     } catch (PhpWarningException $e) {
         $this->_task->app->events->trigger('logException', $e);
         return ServiceResult::createWithError('error', 'An unknown error has occurred', $id);
     } catch (PhpErrorException $e) {
         $this->_task->app->events->trigger('logException', $e);
         return ServiceResult::createWithError('error', 'An unknown error has occurred', $id);
     }
 }