Beispiel #1
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof MongoDB) {
         $collection = $configuration->get('collection');
         $collection = $connection->{$collection};
         if ($collection instanceof MongoCollection) {
             // parse json
             $parser = $this->templateFactory->newTextParser();
             $query = $parser->parse($request, $context, $configuration->get('criteria'));
             $query = !empty($query) ? json_decode($query) : array();
             $fields = $configuration->get('projection');
             $fields = !empty($fields) ? json_decode($fields) : array();
             $result = $collection->findOne($query, $fields);
             if (empty($result)) {
                 throw new NotFoundException('Entry not available');
             }
             return $this->response->build(200, [], $result);
         } else {
             throw new ConfigurationException('Invalid collection');
         }
     } else {
         throw new ConfigurationException('Given connection must be an MongoDB connection');
     }
 }
Beispiel #2
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);
 }
 public function getConnection(ParametersInterface $config)
 {
     $callback = $config->get('callback');
     if (!is_callable($callback)) {
         throw new ConfigurationException('Invalid callback provided');
     }
     return call_user_func_array($callback, []);
 }
Beispiel #4
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $callback = $configuration->get('callback');
     if (!is_callable($callback)) {
         throw new ConfigurationException('Invalid callback provided');
     }
     return call_user_func_array($callback, [$this->response, $request, $configuration, $context]);
 }
Beispiel #5
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);
     }
 }
Beispiel #6
0
 /**
  * @param \Fusio\Engine\ParametersInterface $config
  * @return \MongoDB
  */
 public function getConnection(ParametersInterface $config)
 {
     $rawOptions = $config->get('options');
     if (!empty($rawOptions)) {
         parse_str($rawOptions, $options);
         $client = new MongoClient($config->get('url'), $options);
     } else {
         $client = new MongoClient($config->get('url'));
     }
     return $client->selectDB($config->get('database'));
 }
Beispiel #7
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);
 }
Beispiel #8
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     // parse json
     $parser = $this->templateFactory->newTextParser();
     $response = $parser->parse($request, $context, $configuration->get('response'));
     if (!empty($response)) {
         $statusCode = $configuration->get('statusCode') ?: 200;
         return $this->response->build($statusCode, [], Json::decode($response, false));
     } else {
         throw new ConfigurationException('No response defined');
     }
 }
Beispiel #9
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof Pheanstalk) {
         $writer = new Writer\Json();
         $body = $writer->write($request->getBody());
         $connection->useTube($configuration->get('queue'))->put($body);
         return $this->response->build(200, [], ['success' => true, 'message' => 'Push was successful']);
     } else {
         throw new ConfigurationException('Given connection must be an Beanstalk connection');
     }
 }
Beispiel #10
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof Connection) {
         // parse sql
         $parser = $this->templateFactory->newSqlParser();
         $sql = $parser->parse($request, $context->withConnection($connection), $configuration->get('sql'));
         $connection->executeUpdate($sql, $parser->getSqlParameters());
         return $this->response->build(200, [], array('success' => true, 'message' => 'Execution was successful'));
     } else {
         throw new ConfigurationException('Given connection must be an DBAL connection');
     }
 }
Beispiel #11
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;
 }
Beispiel #12
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof AMQPStreamConnection) {
         $writer = new Writer\Json();
         $body = $writer->write($request->getBody());
         $message = new AMQPMessage($body, array('content_type' => $writer->getContentType(), 'delivery_mode' => 2));
         $channel = $connection->channel();
         $channel->basic_publish($message, '', $configuration->get('queue'));
         return $this->response->build(200, [], array('success' => true, 'message' => 'Push was successful'));
     } else {
         throw new ConfigurationException('Given connection must be an AMQP connection');
     }
 }
Beispiel #13
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof Connection) {
         // parse sql
         $parser = $this->templateFactory->newSqlParser();
         $sql = $parser->parse($request, $context->withConnection($connection), $configuration->get('sql'));
         $result = $connection->fetchAll($sql, $parser->getSqlParameters());
         $key = $configuration->get('propertyName') ?: 'entry';
         return $this->response->build(200, [], [$key => CurveArray::nest($result)]);
     } else {
         throw new ConfigurationException('Given connection must be a DBAL connection');
     }
 }
Beispiel #14
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof Connection) {
         // parse sql
         $parser = $this->templateFactory->newSqlParser();
         $sql = $parser->parse($request, $context->withConnection($connection), $configuration->get('sql'));
         $result = $connection->fetchAssoc($sql, $parser->getSqlParameters());
         if (empty($result)) {
             throw new StatusCode\NotFoundException('Entry not available');
         }
         return $this->response->build(200, [], CurveArray::nest($result));
     } else {
         throw new ConfigurationException('Given connection must be an DBAL connection');
     }
 }
Beispiel #15
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof MongoDB) {
         $collection = $connection->selectCollection($configuration->get('collection'));
         if ($collection instanceof MongoCollection) {
             // parse json
             $parser = $this->templateFactory->newTextParser();
             $document = $parser->parse($request, $context, $configuration->get('document'));
             $document = !empty($document) ? json_decode($document) : array();
             $collection->insert($document);
             return $this->response->build(200, [], ['success' => true, 'message' => 'Execution was successful']);
         } else {
             throw new ConfigurationException('Invalid collection');
         }
     } else {
         throw new ConfigurationException('Given connection must be an MongoDB connection');
     }
 }
Beispiel #16
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;
     }
 }
Beispiel #17
0
 public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     $connection = $this->connector->getConnection($configuration->get('connection'));
     if ($connection instanceof MongoDB) {
         $collection = $connection->selectCollection($configuration->get('collection'));
         if ($collection instanceof MongoCollection) {
             // parse json
             $parser = $this->templateFactory->newTextParser();
             $query = $parser->parse($request, $context, $configuration->get('criteria'));
             $query = !empty($query) ? json_decode($query) : array();
             $fields = $configuration->get('projection');
             $fields = !empty($fields) ? json_decode($fields) : array();
             $cursor = $collection->find($query, $fields);
             $key = $configuration->get('propertyName') ?: 'entry';
             $sort = $configuration->get('sort');
             if (!empty($sort)) {
                 parse_str($sort, $sortParameters);
                 $sortParameters = array_map(function ($value) {
                     $value = (int) $value;
                     return $value == 1 || $value == -1 ? $value : 1;
                 }, $sortParameters);
                 $cursor->sort($sortParameters);
             }
             $limit = (int) $configuration->get('limit');
             if ($limit > 0) {
                 $cursor->limit($limit);
             }
             $data = array();
             foreach ($cursor as $row) {
                 $data[] = $row;
             }
             return $this->response->build(200, [], [$key => $data]);
         } else {
             throw new ConfigurationException('Invalid collection');
         }
     } else {
         throw new ConfigurationException('Given connection must be an MongoDB connection');
     }
 }
Beispiel #18
0
 /**
  * @param \Fusio\Engine\ParametersInterface $config
  * @return \MongoDB
  */
 public function getConnection(ParametersInterface $config)
 {
     return new AMQPStreamConnection($config->get('host'), $config->get('port'), $config->get('user'), $config->get('password'), $config->get('vhost'));
 }
Beispiel #19
0
 protected function executeRequest(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context)
 {
     // parse body
     $parser = $this->templateFactory->newTextParser();
     $body = $parser->parse($request, $context, $configuration->get('body'));
     // build request
     $method = $configuration->get('method') ?: 'POST';
     $headers = $this->parserHeaders($configuration->get('headers'));
     $request = new Request(new Url($configuration->get('url')), $method, $headers, $body);
     return $this->http->request($request);
 }
Beispiel #20
0
 /**
  * @param \Fusio\Engine\ParametersInterface $config
  * @return \Doctrine\DBAL\Connection
  */
 public function getConnection(ParametersInterface $config)
 {
     $params = array('dbname' => $config->get('database'), 'user' => $config->get('username'), 'password' => $config->get('password'), 'host' => $config->get('host'), 'driver' => $config->get('type'));
     return DriverManager::getConnection($params);
 }
Beispiel #21
0
 /**
  * @param \Fusio\Engine\ParametersInterface $config
  * @return \MongoDB
  */
 public function getConnection(ParametersInterface $config)
 {
     return new Pheanstalk($config->get('host'));
 }
Beispiel #22
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);
 }
Beispiel #23
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);
 }
Beispiel #24
0
 /**
  * @param \Fusio\Engine\ParametersInterface $config
  * @return \Doctrine\DBAL\Connection
  */
 public function getConnection(ParametersInterface $config)
 {
     return DriverManager::getConnection(array('url' => $config->get('url')));
 }