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
 /**
  * @return bool|mixed
  * @throws BadRequestException
  */
 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.');
     }
     $methodToCall = false;
     //	Check verb aliases as closures
     if (true === $this->autoDispatch && null !== ($alias = ArrayUtils::get($this->verbAliases, $this->action))) {
         //	A closure?
         if (!in_array($alias, Verbs::getDefinedConstants()) && is_callable($alias)) {
             $methodToCall = $alias;
         }
     }
     //  Not an alias, build a dispatch method if needed
     if (!$methodToCall) {
         //	If we have a dedicated handler method, call it
         $method = str_ireplace(static::ACTION_TOKEN, $this->action, $this->autoDispatchPattern);
         if ($this->autoDispatch && method_exists($this, $method)) {
             $methodToCall = [$this, $method];
         }
     }
     if ($methodToCall) {
         $result = call_user_func($methodToCall);
         //  Only GETs trigger after the call
         if (Verbs::GET == $this->action) {
             $this->triggerActionEvent($result, null, null, true);
         }
         return $result;
     }
     //	Otherwise just return false
     return false;
 }