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;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Utopia\Swoole\Response as SwooleResponse;
|
|
|
|
use Swoole\Http\Response as SwooleHTTPResponse;
|
2021-07-26 02:47:18 +12:00
|
|
|
use Utopia\Database\Document;
|
2020-12-23 04:24:30 +13:00
|
|
|
use Appwrite\Utopia\Response\Filter;
|
2020-06-24 03:01:20 +12:00
|
|
|
use Appwrite\Utopia\Response\Model;
|
2020-11-12 11:01:31 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\None;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Any;
|
2021-06-09 07:30:42 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Attribute;
|
2021-08-28 05:11:29 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\AttributeList;
|
2021-08-14 08:58:54 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\AttributeString;
|
|
|
|
use Appwrite\Utopia\Response\Model\AttributeInteger;
|
|
|
|
use Appwrite\Utopia\Response\Model\AttributeFloat;
|
2021-08-17 08:08:18 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\AttributeBoolean;
|
2021-08-17 07:54:25 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\AttributeEmail;
|
2021-09-11 08:14:12 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\AttributeEnum;
|
2021-08-17 07:54:25 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\AttributeIP;
|
|
|
|
use Appwrite\Utopia\Response\Model\AttributeURL;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\BaseList;
|
|
|
|
use Appwrite\Utopia\Response\Model\Collection;
|
2022-06-22 22:51:49 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Database;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Continent;
|
|
|
|
use Appwrite\Utopia\Response\Model\Country;
|
|
|
|
use Appwrite\Utopia\Response\Model\Currency;
|
2021-04-08 20:39:23 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Document as ModelDocument;
|
2020-10-30 02:07:56 +13:00
|
|
|
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;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Execution;
|
2021-12-07 03:12:41 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Build;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\File;
|
2021-06-15 17:24:39 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Bucket;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Func;
|
2021-06-09 07:30:42 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Index;
|
2020-12-29 05:41:38 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\JWT;
|
2020-10-30 02:07:56 +13:00
|
|
|
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;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Log;
|
2020-06-24 23:18:59 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Membership;
|
2021-09-01 07:04:01 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Metric;
|
2020-11-08 11:14:36 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Permissions;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Phone;
|
|
|
|
use Appwrite\Utopia\Response\Model\Platform;
|
|
|
|
use Appwrite\Utopia\Response\Model\Project;
|
|
|
|
use Appwrite\Utopia\Response\Model\Rule;
|
2022-01-25 11:54:13 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Deployment;
|
2020-11-13 00:54:16 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Token;
|
2020-10-30 02:07:56 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Webhook;
|
2021-04-22 01:37:51 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\Preferences;
|
2021-12-17 01:19:04 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\HealthAntivirus;
|
|
|
|
use Appwrite\Utopia\Response\Model\HealthQueue;
|
|
|
|
use Appwrite\Utopia\Response\Model\HealthStatus;
|
|
|
|
use Appwrite\Utopia\Response\Model\HealthTime;
|
|
|
|
use Appwrite\Utopia\Response\Model\HealthVersion;
|
2021-03-05 19:40:29 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Mock; // Keep last
|
2021-12-10 02:02:12 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Runtime;
|
2021-08-28 04:45:08 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\UsageBuckets;
|
|
|
|
use Appwrite\Utopia\Response\Model\UsageCollection;
|
|
|
|
use Appwrite\Utopia\Response\Model\UsageDatabase;
|
2022-06-22 22:51:49 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\UsageDatabases;
|
2021-08-28 04:45:08 +12:00
|
|
|
use Appwrite\Utopia\Response\Model\UsageFunctions;
|
|
|
|
use Appwrite\Utopia\Response\Model\UsageProject;
|
|
|
|
use Appwrite\Utopia\Response\Model\UsageStorage;
|
|
|
|
use Appwrite\Utopia\Response\Model\UsageUsers;
|
2020-05-16 23:28:26 +12:00
|
|
|
|
2020-11-01 00:06:09 +13:00
|
|
|
/**
|
2021-07-14 20:52:26 +12:00
|
|
|
* @method Response setStatusCode(int $code = 200)
|
2020-11-01 00:06:09 +13:00
|
|
|
*/
|
2020-10-30 02:07:56 +13:00
|
|
|
class Response extends SwooleResponse
|
2020-05-16 23:28:26 +12:00
|
|
|
{
|
2020-06-24 06:53:24 +12:00
|
|
|
// General
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_NONE = 'none';
|
|
|
|
public const MODEL_ANY = 'any';
|
|
|
|
public const MODEL_LOG = 'log';
|
|
|
|
public const MODEL_LOG_LIST = 'logList';
|
|
|
|
public const MODEL_ERROR = 'error';
|
|
|
|
public const MODEL_METRIC = 'metric';
|
|
|
|
public const MODEL_METRIC_LIST = 'metricList';
|
|
|
|
public const MODEL_ERROR_DEV = 'errorDev';
|
|
|
|
public const MODEL_BASE_LIST = 'baseList';
|
2022-06-22 22:51:49 +12:00
|
|
|
public const MODEL_USAGE_DATABASES = 'usageDatabases';
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_USAGE_DATABASE = 'usageDatabase';
|
|
|
|
public const MODEL_USAGE_COLLECTION = 'usageCollection';
|
|
|
|
public const MODEL_USAGE_USERS = 'usageUsers';
|
|
|
|
public const MODEL_USAGE_BUCKETS = 'usageBuckets';
|
|
|
|
public const MODEL_USAGE_STORAGE = 'usageStorage';
|
|
|
|
public const MODEL_USAGE_FUNCTIONS = 'usageFunctions';
|
|
|
|
public const MODEL_USAGE_PROJECT = 'usageProject';
|
2022-05-24 02:54:50 +12:00
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
// Database
|
2022-06-22 22:51:49 +12:00
|
|
|
public const MODEL_DATABASE = 'database';
|
|
|
|
public const MODEL_DATABASE_LIST = 'databaseList';
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_COLLECTION = 'collection';
|
|
|
|
public const MODEL_COLLECTION_LIST = 'collectionList';
|
|
|
|
public const MODEL_INDEX = 'index';
|
|
|
|
public const MODEL_INDEX_LIST = 'indexList';
|
|
|
|
public const MODEL_DOCUMENT = 'document';
|
|
|
|
public const MODEL_DOCUMENT_LIST = 'documentList';
|
2020-10-30 02:07:56 +13:00
|
|
|
|
2021-08-14 08:58:54 +12:00
|
|
|
// Database Attributes
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_ATTRIBUTE = 'attribute';
|
|
|
|
public const MODEL_ATTRIBUTE_LIST = 'attributeList';
|
|
|
|
public const MODEL_ATTRIBUTE_STRING = 'attributeString';
|
|
|
|
public const MODEL_ATTRIBUTE_INTEGER = 'attributeInteger';
|
|
|
|
public const MODEL_ATTRIBUTE_FLOAT = 'attributeFloat';
|
|
|
|
public const MODEL_ATTRIBUTE_BOOLEAN = 'attributeBoolean';
|
|
|
|
public const MODEL_ATTRIBUTE_EMAIL = 'attributeEmail';
|
|
|
|
public const MODEL_ATTRIBUTE_ENUM = 'attributeEnum';
|
|
|
|
public const MODEL_ATTRIBUTE_IP = 'attributeIp';
|
|
|
|
public const MODEL_ATTRIBUTE_URL = 'attributeUrl';
|
2021-08-14 08:58:54 +12:00
|
|
|
|
2020-06-24 06:53:24 +12:00
|
|
|
// Users
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_USER = 'user';
|
|
|
|
public const MODEL_USER_LIST = 'userList';
|
|
|
|
public const MODEL_SESSION = 'session';
|
|
|
|
public const MODEL_SESSION_LIST = 'sessionList';
|
|
|
|
public const MODEL_TOKEN = 'token';
|
|
|
|
public const MODEL_JWT = 'jwt';
|
|
|
|
public const MODEL_PREFERENCES = 'preferences';
|
2022-05-24 02:54:50 +12:00
|
|
|
|
2020-06-24 06:53:24 +12:00
|
|
|
// Storage
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_FILE = 'file';
|
|
|
|
public const MODEL_FILE_LIST = 'fileList';
|
|
|
|
public const MODEL_BUCKET = 'bucket';
|
|
|
|
public const MODEL_BUCKET_LIST = 'bucketList';
|
2020-06-24 06:53:24 +12:00
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
// Locale
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_LOCALE = 'locale';
|
|
|
|
public const MODEL_COUNTRY = 'country';
|
|
|
|
public const MODEL_COUNTRY_LIST = 'countryList';
|
|
|
|
public const MODEL_CONTINENT = 'continent';
|
|
|
|
public const MODEL_CONTINENT_LIST = 'continentList';
|
|
|
|
public const MODEL_CURRENCY = 'currency';
|
|
|
|
public const MODEL_CURRENCY_LIST = 'currencyList';
|
|
|
|
public const MODEL_LANGUAGE = 'language';
|
|
|
|
public const MODEL_LANGUAGE_LIST = 'languageList';
|
|
|
|
public const MODEL_PHONE = 'phone';
|
|
|
|
public const MODEL_PHONE_LIST = 'phoneList';
|
2020-10-30 02:07:56 +13:00
|
|
|
|
2020-06-24 06:53:24 +12:00
|
|
|
// Teams
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_TEAM = 'team';
|
|
|
|
public const MODEL_TEAM_LIST = 'teamList';
|
|
|
|
public const MODEL_MEMBERSHIP = 'membership';
|
|
|
|
public const MODEL_MEMBERSHIP_LIST = 'membershipList';
|
2020-06-05 21:53:06 +12:00
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
// Functions
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_FUNCTION = 'function';
|
|
|
|
public const MODEL_FUNCTION_LIST = 'functionList';
|
|
|
|
public const MODEL_RUNTIME = 'runtime';
|
|
|
|
public const MODEL_RUNTIME_LIST = 'runtimeList';
|
|
|
|
public const MODEL_DEPLOYMENT = 'deployment';
|
|
|
|
public const MODEL_DEPLOYMENT_LIST = 'deploymentList';
|
|
|
|
public const MODEL_EXECUTION = 'execution';
|
|
|
|
public const MODEL_EXECUTION_LIST = 'executionList';
|
|
|
|
public const MODEL_BUILD = 'build';
|
|
|
|
public const MODEL_BUILD_LIST = 'buildList'; // Not used anywhere yet
|
|
|
|
public const MODEL_FUNC_PERMISSIONS = 'funcPermissions';
|
2022-05-24 02:54:50 +12:00
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
// Project
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_PROJECT = 'project';
|
|
|
|
public const MODEL_PROJECT_LIST = 'projectList';
|
|
|
|
public const MODEL_WEBHOOK = 'webhook';
|
|
|
|
public const MODEL_WEBHOOK_LIST = 'webhookList';
|
|
|
|
public const MODEL_KEY = 'key';
|
|
|
|
public const MODEL_KEY_LIST = 'keyList';
|
|
|
|
public const MODEL_PLATFORM = 'platform';
|
|
|
|
public const MODEL_PLATFORM_LIST = 'platformList';
|
|
|
|
public const MODEL_DOMAIN = 'domain';
|
|
|
|
public const MODEL_DOMAIN_LIST = 'domainList';
|
2021-12-15 03:17:55 +13:00
|
|
|
|
|
|
|
// Health
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_HEALTH_STATUS = 'healthStatus';
|
|
|
|
public const MODEL_HEALTH_VERSION = 'healthVersion';
|
|
|
|
public const MODEL_HEALTH_QUEUE = 'healthQueue';
|
|
|
|
public const MODEL_HEALTH_TIME = 'healthTime';
|
|
|
|
public const MODEL_HEALTH_ANTIVIRUS = 'healthAntivirus';
|
2022-05-24 02:54:50 +12:00
|
|
|
|
2021-08-04 01:34:29 +12:00
|
|
|
// Deprecated
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_PERMISSIONS = 'permissions';
|
|
|
|
public const MODEL_RULE = 'rule';
|
|
|
|
public const MODEL_TASK = 'task';
|
2021-08-04 01:34:29 +12:00
|
|
|
|
2021-03-05 19:40:29 +13:00
|
|
|
// Tests (keep last)
|
2022-06-03 01:03:37 +12:00
|
|
|
public const MODEL_MOCK = 'mock';
|
2020-10-30 02:07:56 +13:00
|
|
|
|
2020-12-23 04:24:30 +13:00
|
|
|
/**
|
|
|
|
* @var Filter
|
|
|
|
*/
|
|
|
|
private static $filter = null;
|
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-08-01 22:22:04 +12:00
|
|
|
protected array $payload = [];
|
2020-10-30 02:07:56 +13:00
|
|
|
|
2020-06-27 00:27:58 +12:00
|
|
|
/**
|
|
|
|
* Response constructor.
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-10-27 13:08:29 +13:00
|
|
|
* @param float $time
|
2020-06-27 00:27:58 +12:00
|
|
|
*/
|
2020-10-30 02:07:56 +13:00
|
|
|
public function __construct(SwooleHTTPResponse $response)
|
2020-06-05 21:53:06 +12:00
|
|
|
{
|
|
|
|
$this
|
2020-10-30 02:07:56 +13:00
|
|
|
// General
|
2020-11-12 11:01:31 +13:00
|
|
|
->setModel(new None())
|
|
|
|
->setModel(new Any())
|
2020-06-24 03:01:20 +12:00
|
|
|
->setModel(new Error())
|
|
|
|
->setModel(new ErrorDev())
|
2020-10-30 02:07:56 +13:00
|
|
|
// Lists
|
2022-01-18 06:54:46 +13:00
|
|
|
->setModel(new BaseList('Documents List', self::MODEL_DOCUMENT_LIST, 'documents', self::MODEL_DOCUMENT))
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new BaseList('Collections List', self::MODEL_COLLECTION_LIST, 'collections', self::MODEL_COLLECTION))
|
2022-06-22 22:51:49 +12:00
|
|
|
->setModel(new BaseList('Databases List', self::MODEL_DATABASE_LIST, 'databases', self::MODEL_DATABASE))
|
2021-06-09 07:30:42 +12:00
|
|
|
->setModel(new BaseList('Indexes List', self::MODEL_INDEX_LIST, 'indexes', self::MODEL_INDEX))
|
2020-10-30 02:07:56 +13:00
|
|
|
->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))
|
2021-11-17 04:06:51 +13:00
|
|
|
->setModel(new BaseList('Logs List', self::MODEL_LOG_LIST, 'logs', self::MODEL_LOG))
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new BaseList('Files List', self::MODEL_FILE_LIST, 'files', self::MODEL_FILE))
|
2021-06-14 23:40:06 +12:00
|
|
|
->setModel(new BaseList('Buckets List', self::MODEL_BUCKET_LIST, 'buckets', self::MODEL_BUCKET))
|
2020-10-30 02:07:56 +13:00
|
|
|
->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))
|
2021-12-10 02:02:12 +13:00
|
|
|
->setModel(new BaseList('Runtimes List', self::MODEL_RUNTIME_LIST, 'runtimes', self::MODEL_RUNTIME))
|
2022-01-25 13:54:44 +13:00
|
|
|
->setModel(new BaseList('Deployments List', self::MODEL_DEPLOYMENT_LIST, 'deployments', self::MODEL_DEPLOYMENT))
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new BaseList('Executions List', self::MODEL_EXECUTION_LIST, 'executions', self::MODEL_EXECUTION))
|
2022-02-27 22:57:09 +13:00
|
|
|
->setModel(new BaseList('Builds List', self::MODEL_BUILD_LIST, 'builds', self::MODEL_BUILD)) // Not used anywhere yet
|
2020-11-12 18:12:14 +13:00
|
|
|
->setModel(new BaseList('Projects List', self::MODEL_PROJECT_LIST, 'projects', self::MODEL_PROJECT, true, false))
|
|
|
|
->setModel(new BaseList('Webhooks List', self::MODEL_WEBHOOK_LIST, 'webhooks', self::MODEL_WEBHOOK, true, false))
|
|
|
|
->setModel(new BaseList('API Keys List', self::MODEL_KEY_LIST, 'keys', self::MODEL_KEY, true, false))
|
|
|
|
->setModel(new BaseList('Platforms List', self::MODEL_PLATFORM_LIST, 'platforms', self::MODEL_PLATFORM, true, false))
|
|
|
|
->setModel(new BaseList('Domains List', self::MODEL_DOMAIN_LIST, 'domains', self::MODEL_DOMAIN, true, false))
|
2020-10-30 02:07:56 +13:00
|
|
|
->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))
|
2021-09-01 08:55:13 +12:00
|
|
|
->setModel(new BaseList('Metric List', self::MODEL_METRIC_LIST, 'metrics', self::MODEL_METRIC, true, false))
|
2020-10-30 02:07:56 +13:00
|
|
|
// Entities
|
2022-06-22 22:51:49 +12:00
|
|
|
->setModel(new Database())
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new Collection())
|
2021-06-12 06:07:05 +12:00
|
|
|
->setModel(new Attribute())
|
2021-08-28 05:11:29 +12:00
|
|
|
->setModel(new AttributeList())
|
2021-08-14 08:58:54 +12:00
|
|
|
->setModel(new AttributeString())
|
|
|
|
->setModel(new AttributeInteger())
|
|
|
|
->setModel(new AttributeFloat())
|
2021-08-17 08:08:18 +12:00
|
|
|
->setModel(new AttributeBoolean())
|
2021-08-17 07:54:25 +12:00
|
|
|
->setModel(new AttributeEmail())
|
2021-09-11 08:14:12 +12:00
|
|
|
->setModel(new AttributeEnum())
|
2021-08-17 07:54:25 +12:00
|
|
|
->setModel(new AttributeIP())
|
|
|
|
->setModel(new AttributeURL())
|
2021-06-12 06:07:05 +12:00
|
|
|
->setModel(new Index())
|
2021-04-08 20:39:23 +12:00
|
|
|
->setModel(new ModelDocument())
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new Log())
|
2020-06-24 03:01:20 +12:00
|
|
|
->setModel(new User())
|
2021-04-22 01:37:51 +12:00
|
|
|
->setModel(new Preferences())
|
2020-06-24 19:35:59 +12:00
|
|
|
->setModel(new Session())
|
2020-11-13 00:54:16 +13:00
|
|
|
->setModel(new Token())
|
2020-12-29 05:41:38 +13:00
|
|
|
->setModel(new JWT())
|
2020-06-24 03:01:20 +12:00
|
|
|
->setModel(new Locale())
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new File())
|
2021-06-15 17:24:39 +12:00
|
|
|
->setModel(new Bucket())
|
2020-06-24 17:14:42 +12:00
|
|
|
->setModel(new Team())
|
2020-06-24 23:18:59 +12:00
|
|
|
->setModel(new Membership())
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new Func())
|
2021-12-10 02:02:12 +13:00
|
|
|
->setModel(new Runtime())
|
2022-01-25 11:54:13 +13:00
|
|
|
->setModel(new Deployment())
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new Execution())
|
2021-12-07 03:12:41 +13:00
|
|
|
->setModel(new Build())
|
2020-10-30 02:07:56 +13:00
|
|
|
->setModel(new Project())
|
|
|
|
->setModel(new Webhook())
|
|
|
|
->setModel(new Key())
|
|
|
|
->setModel(new Domain())
|
|
|
|
->setModel(new Platform())
|
|
|
|
->setModel(new Country())
|
|
|
|
->setModel(new Continent())
|
|
|
|
->setModel(new Language())
|
|
|
|
->setModel(new Currency())
|
|
|
|
->setModel(new Phone())
|
2021-12-17 01:19:04 +13:00
|
|
|
->setModel(new HealthAntivirus())
|
|
|
|
->setModel(new HealthQueue())
|
|
|
|
->setModel(new HealthStatus())
|
|
|
|
->setModel(new HealthTime())
|
|
|
|
->setModel(new HealthVersion())
|
2021-09-01 08:55:13 +12:00
|
|
|
->setModel(new Metric())
|
2022-06-22 22:51:49 +12:00
|
|
|
->setModel(new UsageDatabases())
|
2021-08-28 04:45:08 +12:00
|
|
|
->setModel(new UsageDatabase())
|
|
|
|
->setModel(new UsageCollection())
|
|
|
|
->setModel(new UsageUsers())
|
|
|
|
->setModel(new UsageStorage())
|
|
|
|
->setModel(new UsageBuckets())
|
|
|
|
->setModel(new UsageFunctions())
|
|
|
|
->setModel(new UsageProject())
|
2020-10-30 02:07:56 +13:00
|
|
|
// Verification
|
|
|
|
// Recovery
|
2021-03-05 19:40:29 +13:00
|
|
|
// Tests (keep last)
|
2022-08-01 22:22:04 +12:00
|
|
|
->setModel(new Mock());
|
2020-07-05 06:21:42 +12:00
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
parent::__construct($response);
|
2020-06-05 21:53:06 +12:00
|
|
|
}
|
|
|
|
|
2020-05-16 23:28:26 +12:00
|
|
|
/**
|
|
|
|
* HTTP content types
|
|
|
|
*/
|
2022-06-03 01:03:37 +12:00
|
|
|
public const CONTENT_TYPE_YAML = 'application/x-yaml';
|
2020-05-16 23:28:26 +12:00
|
|
|
|
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
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-06-05 21:53:06 +12:00
|
|
|
* @return self
|
|
|
|
*/
|
2021-05-20 02:26:06 +12:00
|
|
|
public function setModel(Model $instance)
|
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
|
2021-10-07 03:22:38 +13: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])) {
|
2022-05-24 02:54:50 +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
|
|
|
}
|
|
|
|
|
2020-11-08 11:14:36 +13:00
|
|
|
/**
|
|
|
|
* Get Models List
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-11-08 11:14:36 +13:00
|
|
|
* @return Model[]
|
|
|
|
*/
|
|
|
|
public function getModels(): array
|
|
|
|
{
|
|
|
|
return $this->models;
|
|
|
|
}
|
|
|
|
|
2020-06-05 21:53:06 +12:00
|
|
|
/**
|
|
|
|
* Validate response objects and outputs
|
|
|
|
* the response according to given format type
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-10-31 21:42:41 +13:00
|
|
|
* @param Document $document
|
|
|
|
* @param string $model
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-10-31 21:42:41 +13:00
|
|
|
* 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
|
|
|
{
|
2021-01-04 07:12:11 +13:00
|
|
|
$output = $this->output($document, $model);
|
|
|
|
|
2021-01-12 02:50:54 +13:00
|
|
|
// If filter is set, parse the output
|
2021-12-31 05:17:01 +13:00
|
|
|
if (self::hasFilter()) {
|
2021-01-12 02:50:54 +13:00
|
|
|
$output = self::getFilter()->parse($output, $model);
|
2021-01-04 07:12:11 +13:00
|
|
|
}
|
|
|
|
|
2021-10-06 04:39:39 +13:00
|
|
|
$this->json(!empty($output) ? $output : new \stdClass());
|
2020-10-31 21:42:41 +13:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:05:43 +12:00
|
|
|
/**
|
|
|
|
* Generate valid response object from document data
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-10-31 21:42:41 +13:00
|
|
|
* @param Document $document
|
|
|
|
* @param string $model
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-10-31 21:42:41 +13:00
|
|
|
* return array
|
2020-06-24 18:05:43 +12:00
|
|
|
*/
|
2022-07-24 19:05:24 +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 = [];
|
|
|
|
|
2022-01-18 05:25:20 +13:00
|
|
|
$document = $model->filter($document);
|
|
|
|
|
2020-10-30 11:44:01 +13:00
|
|
|
if ($model->isAny()) {
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->payload = $document->getArrayCopy();
|
2022-08-01 22:22:04 +12:00
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
return $this->payload;
|
2020-10-30 02:07:56 +13:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:44:01 +13:00
|
|
|
foreach ($model->getRules() as $key => $rule) {
|
2022-08-01 22:22:04 +12:00
|
|
|
if (!$document->isSet($key) && $rule['required']) { // do not set attribute in response if not required
|
2022-08-08 22:35:28 +12:00
|
|
|
if (\array_key_exists('default', $rule)) {
|
2020-06-24 18:05:43 +12:00
|
|
|
$document->setAttribute($key, $rule['default']);
|
2020-10-30 11:44:01 +13:00
|
|
|
} else {
|
2022-05-24 02:54:50 +12:00
|
|
|
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])) {
|
2022-05-24 02:54:50 +12:00
|
|
|
throw new Exception($key . ' must be an array of type ' . $rule['type']);
|
2020-06-24 18:05:43 +12:00
|
|
|
}
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
foreach ($data[$key] as $index => $item) {
|
2020-10-30 11:44:01 +13:00
|
|
|
if ($item instanceof Document) {
|
2021-09-14 20:26:16 +12:00
|
|
|
if (\is_array($rule['type'])) {
|
|
|
|
foreach ($rule['type'] as $type) {
|
|
|
|
$condition = false;
|
|
|
|
foreach ($this->getModel($type)->conditions as $attribute => $val) {
|
|
|
|
$condition = $item->getAttribute($attribute) === $val;
|
2022-05-24 02:54:50 +12:00
|
|
|
if (!$condition) {
|
2021-09-14 20:26:16 +12:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($condition) {
|
|
|
|
$ruleType = $type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ruleType = $rule['type'];
|
2020-10-30 02:07:56 +13:00
|
|
|
}
|
|
|
|
|
2021-08-28 05:09:56 +12:00
|
|
|
if (!array_key_exists($ruleType, $this->models)) {
|
2022-05-24 02:54:50 +12:00
|
|
|
throw new Exception('Missing model for rule: ' . $ruleType);
|
2020-10-30 02:07:56 +13:00
|
|
|
}
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
$data[$key][$index] = $this->output($item, $ruleType);
|
2020-06-24 18:05:43 +12:00
|
|
|
}
|
|
|
|
}
|
2022-08-01 22:22:04 +12:00
|
|
|
} else {
|
|
|
|
if ($data[$key] instanceof Document) {
|
|
|
|
$data[$key] = $this->output($data[$key], $rule['type']);
|
|
|
|
}
|
2020-06-24 03:01:20 +12:00
|
|
|
}
|
2021-09-14 20:26:16 +12:00
|
|
|
|
2020-06-05 21:53:06 +12:00
|
|
|
$output[$key] = $data[$key];
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:07:56 +13:00
|
|
|
$this->payload = $output;
|
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
return $this->payload;
|
2020-05-16 23:28:26 +12:00
|
|
|
}
|
|
|
|
|
2022-07-24 21:49:51 +12:00
|
|
|
|
|
|
|
/**
|
2022-07-31 07:10:30 +12:00
|
|
|
* Output response
|
2022-07-24 21:49:51 +12:00
|
|
|
*
|
2022-07-31 07:10:30 +12:00
|
|
|
* Generate HTTP response output including the response header (+cookies) and body and prints them.
|
|
|
|
*
|
|
|
|
* @param string $body
|
2022-07-24 21:49:51 +12:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-07-31 07:10:30 +12:00
|
|
|
public function send(string $body = ''): void
|
2022-07-24 05:42:42 +12:00
|
|
|
{
|
|
|
|
$this->payload = [
|
2022-07-31 07:10:30 +12:00
|
|
|
'content-type' => $this->getContentType(),
|
|
|
|
'payload' => $body
|
2022-07-27 00:50:33 +12:00
|
|
|
];
|
2022-07-29 03:24:18 +12:00
|
|
|
|
2022-08-07 23:01:00 +12:00
|
|
|
parent::send($body);
|
2022-07-24 05:42:42 +12:00
|
|
|
}
|
|
|
|
|
2022-07-31 07:10:30 +12:00
|
|
|
|
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
|
2020-10-27 13:08:29 +13:00
|
|
|
*
|
|
|
|
* @return void
|
2020-05-16 23:28:26 +12:00
|
|
|
*/
|
2020-10-27 13:08:29 +13: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)
|
2022-08-01 22:22:04 +12:00
|
|
|
->send(yaml_emit($data, YAML_UTF8_ENCODING));
|
2020-05-16 23:28:26 +12:00
|
|
|
}
|
2020-10-30 02:07:56 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-07-27 00:50:33 +12:00
|
|
|
public function getPayload(): array
|
2020-10-30 02:07:56 +13:00
|
|
|
{
|
|
|
|
return $this->payload;
|
|
|
|
}
|
2020-12-23 04:24:30 +13:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to set a response filter
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-12-23 04:24:30 +13:00
|
|
|
* @param $filter the response filter to set
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-12-23 04:24:30 +13:00
|
|
|
* @return void
|
|
|
|
*/
|
2021-10-07 03:22:38 +13:00
|
|
|
public static function setFilter(?Filter $filter)
|
2020-12-23 04:24:30 +13:00
|
|
|
{
|
|
|
|
self::$filter = $filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the currently set filter
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-12-23 04:24:30 +13:00
|
|
|
* @return Filter
|
|
|
|
*/
|
2021-10-07 03:22:38 +13:00
|
|
|
public static function getFilter(): ?Filter
|
2020-12-23 04:24:30 +13:00
|
|
|
{
|
|
|
|
return self::$filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a filter has been set
|
2021-10-07 03:22:38 +13:00
|
|
|
*
|
2020-12-23 04:24:30 +13:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-12-31 05:17:01 +13:00
|
|
|
public static function hasFilter(): bool
|
2020-12-23 04:24:30 +13:00
|
|
|
{
|
|
|
|
return self::$filter != null;
|
|
|
|
}
|
2020-05-16 23:28:26 +12:00
|
|
|
}
|