Esempio n. 1
0
 /**
  * @return bool|mixed
  * @throws \DreamFactory\Core\Events\Exceptions\ScriptException
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  */
 protected function processRequest()
 {
     //	Now all actions must be HTTP verbs
     if (!Verbs::contains($this->action)) {
         throw new BadRequestException('The action "' . $this->action . '" is not supported.');
     }
     $logOutput = $this->request->getParameterAsBool('log_output', true);
     $data = ['request' => $this->request->toArray()];
     $output = null;
     $result = ScriptEngineManager::runScript($this->content, 'script.' . $this->name, $this->engineConfig, $this->scriptConfig, $data, $output);
     if (!empty($output) && $logOutput) {
         \Log::info("Script '{$this->name}' output:" . PHP_EOL . $output . PHP_EOL);
     }
     //  Bail on errors...
     if (is_array($result) && isset($result['script_result'], $result['script_result']['error'])) {
         throw new InternalServerErrorException($result['script_result']['error']);
     }
     if (is_array($result) && isset($result['exception'])) {
         throw new InternalServerErrorException(ArrayUtils::get($result, 'exception', ''));
     }
     //  The script runner should return an array
     if (is_array($result) && isset($result['__tag__'])) {
         $scriptResult = ArrayUtils::get($result, 'script_result', []);
         return $scriptResult;
     } else {
         Log::error('  * Script did not return an array: ' . print_r($result, true));
     }
     return $output;
 }
Esempio n. 2
0
 /**
  * @param $verb
  *
  * @return $this
  * @throws \Exception
  */
 public function setMethod($verb)
 {
     if (!Verbs::contains($verb)) {
         throw new \Exception("Invalid method '{$verb}'");
     }
     $this->method = $verb;
     return $this;
 }
Esempio n. 3
0
 /**
  * Given a verb, check if this host is allowed to use it
  *
  * @param string $verb
  *
  * @return bool
  */
 public function isVerbAllowed($verb)
 {
     $verb = strtoupper($verb);
     if (!Verbs::contains($verb)) {
         throw new \InvalidArgumentException('The verb "' . $verb . '" is not valid.');
     }
     return in_array($verb, $this->_allowedVerbs);
 }
Esempio n. 4
0
 /**
  * Handles the POST requests
  *
  * @param null|string $version
  * @param null|string $service
  * @param null|string $resource
  *
  * @return null|ServiceResponseInterface
  */
 public function handlePOST($version = null, $service = null, $resource = null)
 {
     // Check for the various method override headers or query params
     $xMethod = Request::header('X-HTTP-Method', Request::header('X-HTTP-Method-Override', Request::header('X-Method-Override', Request::query('method'))));
     if (!empty($xMethod)) {
         if (!in_array($xMethod, Verbs::getDefinedConstants())) {
             throw new MethodNotAllowedHttpException("Invalid verb tunneling with " . $xMethod);
         }
         Request::setMethod($xMethod);
     }
     return $this->handleService($version, $service, $resource);
 }
Esempio n. 5
0
 /**
  * Sets the HTTP Action verb
  *
  * @param $action string
  *
  * @return $this
  */
 protected function setAction($action)
 {
     $this->action = trim(strtoupper($action));
     //	Check verb aliases, set correct action allowing for closures
     if (null !== ($alias = ArrayUtils::get($this->verbAliases, $this->action))) {
         //	A closure?
         if (in_array($alias, Verbs::getDefinedConstants()) || !is_callable($alias)) {
             //	Set original and work with alias
             $this->originalAction = $this->action;
             $this->action = $alias;
         }
     }
     return $this;
 }