1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/src/Appwrite/Utopia/Response.php

315 lines
11 KiB
PHP
Raw Normal View History

2020-05-16 23:28:26 +12:00
<?php
2020-06-23 00:17:14 +12:00
namespace Appwrite\Utopia;
2020-05-16 23:28:26 +12:00
use Exception;
use Utopia\Swoole\Response as SwooleResponse;
use Swoole\Http\Response as SwooleHTTPResponse;
2020-06-05 21:53:06 +12:00
use Appwrite\Database\Document;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response\Model\Any;
use Appwrite\Utopia\Response\Model\BaseList;
use Appwrite\Utopia\Response\Model\Collection;
use Appwrite\Utopia\Response\Model\Continent;
use Appwrite\Utopia\Response\Model\Country;
use Appwrite\Utopia\Response\Model\Currency;
use Appwrite\Utopia\Response\Model\Domain;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\Error;
use Appwrite\Utopia\Response\Model\ErrorDev;
use Appwrite\Utopia\Response\Model\Execution;
use Appwrite\Utopia\Response\Model\File;
use Appwrite\Utopia\Response\Model\Func;
use Appwrite\Utopia\Response\Model\Key;
use Appwrite\Utopia\Response\Model\Language;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\User;
2020-06-24 19:35:59 +12:00
use Appwrite\Utopia\Response\Model\Session;
2020-06-24 17:14:42 +12:00
use Appwrite\Utopia\Response\Model\Team;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\Locale;
use Appwrite\Utopia\Response\Model\Log;
use Appwrite\Utopia\Response\Model\Membership;
use Appwrite\Utopia\Response\Model\Phone;
use Appwrite\Utopia\Response\Model\Platform;
use Appwrite\Utopia\Response\Model\Project;
use Appwrite\Utopia\Response\Model\Rule;
use Appwrite\Utopia\Response\Model\Tag;
use Appwrite\Utopia\Response\Model\Task;
use Appwrite\Utopia\Response\Model\Webhook;
2020-05-16 23:28:26 +12:00
2020-11-01 00:06:09 +13:00
/**
* @method public function setStatusCode(int $code = 200): Response
*/
class Response extends SwooleResponse
2020-05-16 23:28:26 +12:00
{
2020-06-24 06:53:24 +12:00
// General
const MODEL_ANY = 'any';
const MODEL_LOG = 'log';
const MODEL_LOG_LIST = 'logList';
2020-06-24 03:01:20 +12:00
const MODEL_ERROR = 'error';
const MODEL_ERROR_DEV = 'errorDev';
2020-06-24 17:14:42 +12:00
const MODEL_BASE_LIST = 'baseList';
const MODEL_PERMISSIONS = 'permissions';
2020-06-24 06:53:24 +12:00
// Database
const MODEL_COLLECTION = 'collection';
const MODEL_COLLECTION_LIST = 'collectionList';
const MODEL_RULE = 'rule';
const MODEL_DOCUMENT_LIST = 'documentList';
2020-06-24 06:53:24 +12:00
// Users
2020-06-24 03:01:20 +12:00
const MODEL_USER = 'user';
const MODEL_USER_LIST = 'userList';
2020-06-24 06:53:24 +12:00
const MODEL_SESSION = 'session';
const MODEL_SESSION_LIST = 'sessionList';
const MODEL_TOKEN = 'token'; // - Missing
2020-06-24 06:53:24 +12:00
// Storage
const MODEL_FILE = 'file';
const MODEL_FILE_LIST = 'fileList';
const MODEL_BUCKET = 'bucket'; // - Missing
2020-06-24 06:53:24 +12:00
// Locale
const MODEL_LOCALE = 'locale';
const MODEL_COUNTRY = 'country';
const MODEL_COUNTRY_LIST = 'countryList';
const MODEL_CONTINENT = 'continent';
const MODEL_CONTINENT_LIST = 'continentList';
const MODEL_CURRENCY = 'currency';
const MODEL_CURRENCY_LIST = 'currencyList';
const MODEL_LANGUAGE = 'langauge';
const MODEL_LANGUAGE_LIST = 'langaugeList';
const MODEL_PHONE = 'phone';
const MODEL_PHONE_LIST = 'phoneList';
2020-06-24 06:53:24 +12:00
// Teams
const MODEL_TEAM = 'team';
2020-06-24 17:14:42 +12:00
const MODEL_TEAM_LIST = 'teamList';
2020-06-26 08:26:02 +12:00
const MODEL_MEMBERSHIP = 'membership';
const MODEL_MEMBERSHIP_LIST = 'membershipList';
2020-06-05 21:53:06 +12:00
// Functions
const MODEL_FUNCTION = 'function';
const MODEL_FUNCTION_LIST = 'functionList';
const MODEL_TAG = 'tag';
const MODEL_TAG_LIST = 'tagList';
const MODEL_EXECUTION = 'execution';
const MODEL_EXECUTION_LIST = 'executionList';
// Project
const MODEL_PROJECT = 'project';
const MODEL_PROJECT_LIST = 'projectsList';
const MODEL_WEBHOOK = 'webhook';
const MODEL_WEBHOOK_LIST = 'webhookList';
const MODEL_KEY = 'key';
const MODEL_KEY_LIST = 'keyList';
const MODEL_TASK = 'task';
const MODEL_TASK_LIST = 'taskList';
const MODEL_PLATFORM = 'platform';
const MODEL_PLATFORM_LIST = 'platformList';
const MODEL_DOMAIN = 'domain';
const MODEL_DOMAIN_LIST = 'domainList';
/**
* @var array
*/
protected $payload = [];
2020-06-27 00:27:58 +12:00
/**
* Response constructor.
*
* @param float $time
2020-06-27 00:27:58 +12:00
*/
public function __construct(SwooleHTTPResponse $response)
2020-06-05 21:53:06 +12:00
{
$this
// General
2020-06-24 03:01:20 +12:00
->setModel(new Error())
->setModel(new ErrorDev())
// Lists
->setModel(new BaseList('Collections List', self::MODEL_COLLECTION_LIST, 'collections', self::MODEL_COLLECTION))
->setModel(new BaseList('Documents List', self::MODEL_DOCUMENT_LIST, 'documents', self::MODEL_ANY))
->setModel(new BaseList('Users List', self::MODEL_USER_LIST, 'users', self::MODEL_USER))
->setModel(new BaseList('Sessions List', self::MODEL_SESSION_LIST, 'sessions', self::MODEL_SESSION))
->setModel(new BaseList('Logs List', self::MODEL_LOG_LIST, 'logs', self::MODEL_LOG, false))
->setModel(new BaseList('Files List', self::MODEL_FILE_LIST, 'files', self::MODEL_FILE))
->setModel(new BaseList('Teams List', self::MODEL_TEAM_LIST, 'teams', self::MODEL_TEAM))
->setModel(new BaseList('Memberships List', self::MODEL_MEMBERSHIP_LIST, 'memberships', self::MODEL_MEMBERSHIP))
->setModel(new BaseList('Functions List', self::MODEL_FUNCTION_LIST, 'functions', self::MODEL_FUNCTION))
->setModel(new BaseList('Tags List', self::MODEL_TAG_LIST, 'tags', self::MODEL_TAG))
->setModel(new BaseList('Executions List', self::MODEL_EXECUTION_LIST, 'executions', self::MODEL_EXECUTION))
->setModel(new BaseList('Projects List', self::MODEL_PROJECT_LIST, 'projects', self::MODEL_PROJECT))
->setModel(new BaseList('Webhooks List', self::MODEL_WEBHOOK_LIST, 'webhooks', self::MODEL_WEBHOOK))
->setModel(new BaseList('API Keys List', self::MODEL_KEY_LIST, 'keys', self::MODEL_KEY))
->setModel(new BaseList('Tasks List', self::MODEL_TASK_LIST, 'tasks', self::MODEL_TASK))
->setModel(new BaseList('Platforms List', self::MODEL_PLATFORM_LIST, 'platforms', self::MODEL_PLATFORM))
->setModel(new BaseList('Domains List', self::MODEL_DOMAIN_LIST, 'domains', self::MODEL_DOMAIN))
->setModel(new BaseList('Countries List', self::MODEL_COUNTRY_LIST, 'countries', self::MODEL_COUNTRY))
->setModel(new BaseList('Continents List', self::MODEL_CONTINENT_LIST, 'continents', self::MODEL_CONTINENT))
->setModel(new BaseList('Languages List', self::MODEL_LANGUAGE_LIST, 'languages', self::MODEL_LANGUAGE))
->setModel(new BaseList('Currencies List', self::MODEL_CURRENCY_LIST, 'currencies', self::MODEL_CURRENCY))
->setModel(new BaseList('Phones List', self::MODEL_PHONE_LIST, 'phones', self::MODEL_PHONE))
// Entities
->setModel(new Any())
->setModel(new Collection())
->setModel(new Rule())
->setModel(new Log())
2020-06-24 03:01:20 +12:00
->setModel(new User())
2020-06-24 19:35:59 +12:00
->setModel(new Session())
2020-06-24 03:01:20 +12:00
->setModel(new Locale())
->setModel(new File())
2020-06-24 17:14:42 +12:00
->setModel(new Team())
->setModel(new Membership())
->setModel(new Func())
->setModel(new Tag())
->setModel(new Execution())
->setModel(new Project())
->setModel(new Webhook())
->setModel(new Key())
->setModel(new Task())
->setModel(new Domain())
->setModel(new Platform())
->setModel(new Country())
->setModel(new Continent())
->setModel(new Language())
->setModel(new Currency())
->setModel(new Phone())
// Verification
// Recovery
2020-06-05 21:53:06 +12:00
;
2020-07-05 06:21:42 +12:00
parent::__construct($response);
2020-06-05 21:53:06 +12:00
}
2020-05-16 23:28:26 +12:00
/**
* HTTP content types
*/
const CONTENT_TYPE_YAML = 'application/x-yaml';
2020-06-05 21:53:06 +12:00
/**
* List of defined output objects
*/
2020-06-24 03:01:20 +12:00
protected $models = [];
2020-06-05 21:53:06 +12:00
/**
2020-06-24 03:01:20 +12:00
* Set Model Object
2020-06-05 21:53:06 +12:00
*
* @return self
*/
2020-06-24 03:01:20 +12:00
public function setModel(Model $instance): self
2020-05-16 23:28:26 +12:00
{
2020-06-24 03:01:20 +12:00
$this->models[$instance->getType()] = $instance;
2020-06-05 21:53:06 +12:00
return $this;
}
/**
2020-06-24 03:01:20 +12:00
* Get Model Object
2020-06-05 21:53:06 +12:00
*
2020-06-24 03:01:20 +12:00
* @return Model
2020-06-05 21:53:06 +12:00
*/
2020-06-24 03:01:20 +12:00
public function getModel(string $key): Model
2020-06-05 21:53:06 +12:00
{
2020-10-28 08:44:15 +13:00
if (!isset($this->models[$key])) {
2020-06-24 03:01:20 +12:00
throw new Exception('Undefined model: '.$key);
2020-06-05 21:53:06 +12:00
}
2020-06-24 03:01:20 +12:00
return $this->models[$key];
2020-06-05 21:53:06 +12:00
}
/**
* Validate response objects and outputs
* the response according to given format type
2020-10-31 21:42:41 +13:00
*
* @param Document $document
* @param string $model
*
* return void
2020-06-05 21:53:06 +12:00
*/
2020-11-01 00:06:09 +13:00
public function dynamic(Document $document, string $model): void
2020-06-05 21:53:06 +12:00
{
2020-11-01 00:06:09 +13:00
$this->json($this->output($document, $model));
2020-10-31 21:42:41 +13:00
}
2020-06-24 18:05:43 +12:00
/**
* Generate valid response object from document data
2020-10-31 21:42:41 +13:00
*
* @param Document $document
* @param string $model
*
* return array
2020-06-24 18:05:43 +12:00
*/
public function output(Document $document, string $model): array
2020-06-24 18:05:43 +12:00
{
$data = $document;
2020-06-24 03:01:20 +12:00
$model = $this->getModel($model);
2020-06-05 21:53:06 +12:00
$output = [];
2020-10-30 11:44:01 +13:00
if ($model->isAny()) {
return $document->getArrayCopy();
}
2020-10-30 11:44:01 +13:00
foreach ($model->getRules() as $key => $rule) {
if (!$document->isSet($key)) {
if (!is_null($rule['default'])) {
2020-06-24 18:05:43 +12:00
$document->setAttribute($key, $rule['default']);
2020-10-30 11:44:01 +13:00
} else {
throw new Exception('Model '.$model->getName().' is missing response key: '.$key);
2020-06-23 02:33:37 +12:00
}
2020-06-05 21:53:06 +12:00
}
2020-10-30 11:44:01 +13:00
if ($rule['array']) {
if (!is_array($data[$key])) {
throw new Exception($key.' must be an array of type '.$rule['type']);
2020-06-24 18:05:43 +12:00
}
foreach ($data[$key] as &$item) {
2020-10-30 11:44:01 +13:00
if ($item instanceof Document) {
if (!array_key_exists($rule['type'], $this->models)) {
throw new Exception('Missing model for rule: '. $rule['type']);
}
2020-06-24 18:05:43 +12:00
$item = $this->output($item, $rule['type']);
}
}
2020-06-24 03:01:20 +12:00
}
2020-06-05 21:53:06 +12:00
$output[$key] = $data[$key];
}
$this->payload = $output;
2020-06-24 18:05:43 +12:00
return $output;
2020-05-16 23:28:26 +12:00
}
/**
* YAML
*
* This helper is for sending YAML HTTP response.
* It sets relevant content type header ('application/x-yaml') and convert a PHP array ($data) to valid YAML using native yaml_parse
*
* @see https://en.wikipedia.org/wiki/YAML
*
* @param array $data
*
* @return void
2020-05-16 23:28:26 +12:00
*/
public function yaml(array $data): void
2020-05-16 23:28:26 +12:00
{
2020-10-28 08:44:15 +13:00
if (!extension_loaded('yaml')) {
2020-05-16 23:28:26 +12:00
throw new Exception('Missing yaml extension. Learn more at: https://www.php.net/manual/en/book.yaml.php');
}
$this
->setContentType(Response::CONTENT_TYPE_YAML)
->send(yaml_emit($data, YAML_UTF8_ENCODING))
;
}
/**
* @return array
*/
public function getPayload():array
{
return $this->payload;
}
2020-05-16 23:28:26 +12:00
}