Exemple #1
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $condition = $configuration->get('condition');
     $language = new ExpressionLanguage($this);
     $values = array('rateLimit' => new RateLimit($this->connection, $context), 'app' => $context->getApp(), 'routeId' => $context->getRouteId(), 'uriFragments' => $request->getUriFragments(), 'parameters' => $request->getParameters(), 'body' => new Accessor(new Validate(), $request->getBody()));
     if (!empty($condition) && $language->evaluate($condition, $values)) {
         return $this->processor->execute($configuration->get('true'), $request, $context);
     } else {
         return $this->processor->execute($configuration->get('false'), $request, $context);
     }
 }
Exemple #2
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     // parse json
     $parser = $this->templateFactory->newTextParser();
     $response = $parser->parse($request, $context, $configuration->get('patch'));
     // patch
     $patch = new Patch(Json::decode($response, false));
     $body = $patch->patch(Transformer::toStdClass($request->getBody()));
     $body = Transformer::toRecord($body);
     return $this->processor->execute($configuration->get('action'), $request->withBody($body), $context);
 }
Exemple #3
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $key = md5($configuration->get('action'));
     $item = $this->cache->getItem($key);
     if (!$item->isHit()) {
         $response = $this->processor->execute($configuration->get('action'), $request, $context);
         $item->set($response, $configuration->get('expire'));
         $this->cache->save($item);
     } else {
         $response = $item->get();
     }
     return $response;
 }
Exemple #4
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $yaml = new Parser();
     $rules = $yaml->parse($configuration->get('rules'));
     if (is_array($rules)) {
         $validator = $this->buildValidator($rules);
         // fragments
         $fragments = $request->getUriFragments();
         foreach ($fragments as $key => $value) {
             $validator->validateProperty('/~path/' . $key, $value);
         }
         // parameters
         $parameters = $request->getParameters();
         foreach ($parameters as $key => $value) {
             $validator->validateProperty('/~query/' . $key, $value);
         }
         // body
         $validator->validate($request->getBody());
         // check whether all required fields are available
         $fields = $validator->getRequiredNames();
         if (!empty($fields)) {
             throw new StatusCode\ClientErrorException('Missing required fields: ' . implode(', ', $fields));
         }
     }
     return $this->processor->execute($configuration->get('action'), $request, $context);
 }
 private function executeAction(RecordInterface $record, Version $version)
 {
     $actionId = $this->getActiveMethod()->getAction();
     if (is_int($actionId)) {
         try {
             $context = new FusioContext($this->context->get('fusio.routeId'), $this->app);
             $request = new Request($this->request, $this->uriFragments, $this->getParameters(), $record);
             $response = $this->processor->execute($actionId, $request, $context);
             $statusCode = $response->getStatusCode();
             $headers = $response->getHeaders();
             if (!empty($statusCode)) {
                 $this->setResponseCode($statusCode);
             }
             if (!empty($headers)) {
                 foreach ($headers as $name => $value) {
                     $this->setHeader($name, $value);
                 }
             }
             return $response->getBody();
         } catch (\Exception $e) {
             $this->apiLogger->appendError($this->logId, $e);
             throw $e;
         }
     } else {
         throw new StatusCode\ServiceUnavailableException('No action provided');
     }
 }
Exemple #6
0
 public function execute($actionId, $method, $uriFragments, $parameters, $headers, RecordInterface $body = null)
 {
     $action = $this->actionTable->get($actionId);
     if (!empty($action)) {
         if ($body === null) {
             $body = new Record();
         }
         $app = $this->appRepository->get(1);
         $user = $this->userRepository->get(1);
         $uriFragments = $this->parseQueryString($uriFragments);
         $parameters = $this->parseQueryString($parameters);
         $headers = $this->parseQueryString($headers);
         $context = new Context($actionId, $app, $user);
         $request = new Request(new HttpRequest(new Uri('/'), $method, $headers), $uriFragments, $parameters, $body);
         return $this->processor->execute($action->id, $request, $context);
     } else {
         return null;
     }
 }
Exemple #7
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $yaml = new Parser();
     $process = $yaml->parse($configuration->get('process'));
     $repository = new MemoryRepository();
     $id = 1;
     if (is_array($process)) {
         foreach ($process as $class => $config) {
             if (is_array($config)) {
                 $config = array_map('strval', $config);
                 if (isset($config['id'])) {
                     $name = $config['id'];
                     unset($config['id']);
                 } else {
                     $name = 'action-' . $id;
                 }
                 $action = new Action();
                 $action->setId($id);
                 $action->setName($name);
                 $action->setClass($class);
                 $action->setConfig($config);
                 $action->setDate(date('Y-m-d H:i:s'));
                 $repository->add($action);
                 $id++;
             }
         }
     }
     if ($id === 1) {
         throw new ConfigurationException('No process defined');
     }
     $this->processor->push($repository);
     try {
         $return = $this->processor->execute(1, $request, $context);
         $this->processor->pop();
         return $return;
     } catch (\Exception $e) {
         $this->processor->pop();
         throw $e;
     }
 }
Exemple #8
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $this->processor->execute($configuration->get('in'), $request, $context);
     return $this->processor->execute($configuration->get('out'), $request, $context);
 }
Exemple #9
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $response = $this->processor->execute($configuration->get('source'), $request, $context);
     $body = Transformer::toRecord($response->getBody());
     return $this->processor->execute($configuration->get('destination'), $request->withBody($body), $context);
 }