mirror of
https://github.com/flextype/flextype.git
synced 2025-08-06 13:16:45 +02:00
feat(query-api): Implement Flextype Query API #587
This commit is contained in:
89
src/flextype/core/Endpoints/Query.php
Normal file
89
src/flextype/core/Endpoints/Query.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
namespace Flextype\Endpoints;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
use function Glowy\Strings\strings;
|
||||
use function Flextype\parsers;
|
||||
use function count;
|
||||
|
||||
class Query extends Api
|
||||
{
|
||||
/**
|
||||
* Evaluate query.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function evaluate(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Request Parsed Body
|
||||
$requestParsedBody = $request->getParsedBody();
|
||||
|
||||
// Validate Api Request
|
||||
if (
|
||||
count($result = $this->validateApiRequest([
|
||||
'request' => $request,
|
||||
'api' => 'query',
|
||||
'params' => ['query', 'access_token', 'token'],
|
||||
])) > 0
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage($result['http_status_code']), $result['http_status_code']);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
// Evaluate the queries
|
||||
foreach ($requestParsedBody['query'] as $key => $value) {
|
||||
$evaluatedValue = parsers()->expressions()->eval($value);
|
||||
|
||||
if ($evaluatedValue instanceof \Glowy\Arrays\Arrays) {
|
||||
$evaluatedValue = $evaluatedValue->toArray();
|
||||
}
|
||||
|
||||
if ($evaluatedValue instanceof \Glowy\Strings\Strings) {
|
||||
$evaluatedValue = $evaluatedValue->toString();
|
||||
}
|
||||
|
||||
$data[$key] = $evaluatedValue;
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
// Clean up data
|
||||
foreach ($data as $key => $value) {
|
||||
|
||||
// Replace private fields
|
||||
if (strings($key)->startsWith('_')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$key] = $value;
|
||||
}
|
||||
|
||||
// Return response
|
||||
if (count($result) > 0) {
|
||||
return $this->getApiResponse($response, $result, 200);
|
||||
}
|
||||
|
||||
return $this->getApiResponse($response, [], 404);
|
||||
}
|
||||
}
|
34
src/flextype/routes/endpoints/query.php
Normal file
34
src/flextype/routes/endpoints/query.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Flextype\Endpoints\Query;
|
||||
|
||||
/**
|
||||
* Create entry
|
||||
*
|
||||
* endpoint: POST /api/v1/query
|
||||
*
|
||||
* Body:
|
||||
* token - [REQUIRED] - Valid public token.
|
||||
* access_token - [REQUIRED] - Valid private access token.
|
||||
* query - [REQUIRED] - Array of queries.
|
||||
*
|
||||
* Returns:
|
||||
* Query object.
|
||||
*/
|
||||
app()->post('/api/v1/query', [Query::class, 'evaluate'])->setName('query.evaluate');
|
@@ -23,6 +23,7 @@ require_once __DIR__ . '/endpoints/tokens.php';
|
||||
require_once __DIR__ . '/endpoints/cache.php';
|
||||
require_once __DIR__ . '/endpoints/entries.php';
|
||||
require_once __DIR__ . '/endpoints/registry.php';
|
||||
require_once __DIR__ . '/endpoints/query.php';
|
||||
|
||||
// Add project routes
|
||||
if (filesystem()->file(FLEXTYPE_PATH_PROJECT . '/routes/routes.php')->exists()) {
|
||||
|
@@ -858,4 +858,10 @@ api:
|
||||
cache:
|
||||
|
||||
# Set to true to enable Cache API
|
||||
enabled: true
|
||||
|
||||
# Query API
|
||||
query:
|
||||
|
||||
# Set to true to enable Query API
|
||||
enabled: true
|
Reference in New Issue
Block a user