appwrite/src/Appwrite/Utopia/Response.php

864 lines
34 KiB
PHP
Raw Normal View History

2020-05-16 11:28:26 +00:00
<?php
2020-06-22 12:17:14 +00:00
namespace Appwrite\Utopia;
2020-05-16 11:28:26 +00:00
2024-08-07 13:15:53 +00:00
use Appwrite\Utopia\Fetch\BodyMultipart;
use Appwrite\Utopia\Response\Filter;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response\Model\Account;
use Appwrite\Utopia\Response\Model\AlgoArgon2;
use Appwrite\Utopia\Response\Model\AlgoBcrypt;
use Appwrite\Utopia\Response\Model\AlgoMd5;
use Appwrite\Utopia\Response\Model\AlgoPhpass;
use Appwrite\Utopia\Response\Model\AlgoScrypt;
use Appwrite\Utopia\Response\Model\AlgoScryptModified;
use Appwrite\Utopia\Response\Model\AlgoSha;
use Appwrite\Utopia\Response\Model\Any;
use Appwrite\Utopia\Response\Model\Attribute;
use Appwrite\Utopia\Response\Model\AttributeBoolean;
use Appwrite\Utopia\Response\Model\AttributeDatetime;
use Appwrite\Utopia\Response\Model\AttributeEmail;
use Appwrite\Utopia\Response\Model\AttributeEnum;
use Appwrite\Utopia\Response\Model\AttributeFloat;
use Appwrite\Utopia\Response\Model\AttributeInteger;
use Appwrite\Utopia\Response\Model\AttributeIP;
use Appwrite\Utopia\Response\Model\AttributeList;
use Appwrite\Utopia\Response\Model\AttributeRelationship;
use Appwrite\Utopia\Response\Model\AttributeString;
use Appwrite\Utopia\Response\Model\AttributeURL;
use Appwrite\Utopia\Response\Model\AuthProvider;
use Appwrite\Utopia\Response\Model\BaseList;
use Appwrite\Utopia\Response\Model\Branch;
use Appwrite\Utopia\Response\Model\Bucket;
use Appwrite\Utopia\Response\Model\Build;
use Appwrite\Utopia\Response\Model\Collection;
use Appwrite\Utopia\Response\Model\ConsoleVariables;
use Appwrite\Utopia\Response\Model\Continent;
use Appwrite\Utopia\Response\Model\Country;
use Appwrite\Utopia\Response\Model\Currency;
use Appwrite\Utopia\Response\Model\Database;
use Appwrite\Utopia\Response\Model\Deployment;
use Appwrite\Utopia\Response\Model\Document as ModelDocument;
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\Framework;
2024-12-02 12:20:54 +00:00
use Appwrite\Utopia\Response\Model\FrameworkAdapter;
2025-02-06 18:29:43 +00:00
use Appwrite\Utopia\Response\Model\FrameworkDetection;
use Appwrite\Utopia\Response\Model\FrameworkProviderRepository;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model\Func;
use Appwrite\Utopia\Response\Model\Headers;
use Appwrite\Utopia\Response\Model\HealthAntivirus;
use Appwrite\Utopia\Response\Model\HealthCertificate;
use Appwrite\Utopia\Response\Model\HealthQueue;
use Appwrite\Utopia\Response\Model\HealthStatus;
use Appwrite\Utopia\Response\Model\HealthTime;
use Appwrite\Utopia\Response\Model\HealthVersion;
use Appwrite\Utopia\Response\Model\Identity;
use Appwrite\Utopia\Response\Model\Index;
use Appwrite\Utopia\Response\Model\Installation;
use Appwrite\Utopia\Response\Model\JWT;
use Appwrite\Utopia\Response\Model\Key;
use Appwrite\Utopia\Response\Model\Language;
use Appwrite\Utopia\Response\Model\Locale;
use Appwrite\Utopia\Response\Model\LocaleCode;
use Appwrite\Utopia\Response\Model\Log;
use Appwrite\Utopia\Response\Model\Membership;
use Appwrite\Utopia\Response\Model\Message;
use Appwrite\Utopia\Response\Model\Metric;
use Appwrite\Utopia\Response\Model\MetricBreakdown;
use Appwrite\Utopia\Response\Model\MFAChallenge;
use Appwrite\Utopia\Response\Model\MFAFactors;
use Appwrite\Utopia\Response\Model\MFARecoveryCodes;
use Appwrite\Utopia\Response\Model\MFAType;
use Appwrite\Utopia\Response\Model\Migration;
use Appwrite\Utopia\Response\Model\MigrationFirebaseProject;
use Appwrite\Utopia\Response\Model\MigrationReport;
use Appwrite\Utopia\Response\Model\Mock;
use Appwrite\Utopia\Response\Model\MockNumber;
use Appwrite\Utopia\Response\Model\None;
use Appwrite\Utopia\Response\Model\Phone;
use Appwrite\Utopia\Response\Model\Platform;
use Appwrite\Utopia\Response\Model\Preferences;
use Appwrite\Utopia\Response\Model\Project;
use Appwrite\Utopia\Response\Model\Provider;
use Appwrite\Utopia\Response\Model\Rule;
use Appwrite\Utopia\Response\Model\Runtime;
2025-02-06 18:29:43 +00:00
use Appwrite\Utopia\Response\Model\RuntimeDetection;
use Appwrite\Utopia\Response\Model\RuntimeProviderRepository;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model\Session;
2024-10-22 15:01:38 +00:00
use Appwrite\Utopia\Response\Model\Site;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model\Specification;
use Appwrite\Utopia\Response\Model\Subscriber;
use Appwrite\Utopia\Response\Model\Target;
use Appwrite\Utopia\Response\Model\Team;
use Appwrite\Utopia\Response\Model\TemplateEmail;
use Appwrite\Utopia\Response\Model\TemplateFramework;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model\TemplateFunction;
use Appwrite\Utopia\Response\Model\TemplateRuntime;
use Appwrite\Utopia\Response\Model\TemplateSite;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model\TemplateSMS;
use Appwrite\Utopia\Response\Model\TemplateVariable;
use Appwrite\Utopia\Response\Model\Token;
use Appwrite\Utopia\Response\Model\Topic;
use Appwrite\Utopia\Response\Model\UsageBuckets;
use Appwrite\Utopia\Response\Model\UsageCollection;
use Appwrite\Utopia\Response\Model\UsageDatabase;
use Appwrite\Utopia\Response\Model\UsageDatabases;
use Appwrite\Utopia\Response\Model\UsageFunction;
use Appwrite\Utopia\Response\Model\UsageFunctions;
use Appwrite\Utopia\Response\Model\UsageProject;
2024-10-24 13:21:40 +00:00
use Appwrite\Utopia\Response\Model\UsageSite;
use Appwrite\Utopia\Response\Model\UsageSites;
2024-10-08 07:54:40 +00:00
use Appwrite\Utopia\Response\Model\UsageStorage;
use Appwrite\Utopia\Response\Model\UsageUsers;
use Appwrite\Utopia\Response\Model\User;
use Appwrite\Utopia\Response\Model\Variable;
use Appwrite\Utopia\Response\Model\VcsContent;
use Appwrite\Utopia\Response\Model\Webhook;
2024-03-06 17:34:21 +00:00
use Exception;
2024-08-07 13:15:53 +00:00
use JsonException;
2024-10-08 07:54:40 +00:00
use Swoole\Http\Response as SwooleHTTPResponse;
2023-08-04 08:56:46 +00:00
// Keep last
2024-03-06 17:34:21 +00:00
use Utopia\Database\Document;
2024-10-08 07:54:40 +00:00
use Utopia\Swoole\Response as SwooleResponse;
2020-05-16 11:28:26 +00:00
2020-10-31 11:06:09 +00:00
/**
2022-05-04 09:17:19 +00:00
* @method int getStatusCode()
* @method Response setStatusCode(int $code = 200)
2020-10-31 11:06:09 +00:00
*/
2024-10-08 07:54:40 +00:00
class Response extends SwooleResponse
2020-05-16 11:28:26 +00:00
{
2020-06-23 18:53:24 +00:00
// General
2022-06-02 13:03:37 +00: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';
2023-12-11 15:19:08 +00:00
public const MODEL_METRIC_BREAKDOWN = 'metricBreakdown';
2022-06-02 13:03:37 +00:00
public const MODEL_ERROR_DEV = 'errorDev';
public const MODEL_BASE_LIST = 'baseList';
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 10:51:49 +00:00
public const MODEL_USAGE_DATABASES = 'usageDatabases';
2022-06-02 13:03:37 +00: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';
2022-07-17 10:30:58 +00:00
public const MODEL_USAGE_FUNCTION = 'usageFunction';
2024-10-24 13:21:40 +00:00
public const MODEL_USAGE_SITES = 'usageSites';
public const MODEL_USAGE_SITE = 'usageSite';
2022-06-02 13:03:37 +00:00
public const MODEL_USAGE_PROJECT = 'usageProject';
2022-05-23 14:54:50 +00:00
// Database
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 10:51:49 +00:00
public const MODEL_DATABASE = 'database';
public const MODEL_DATABASE_LIST = 'databaseList';
2022-06-02 13:03:37 +00: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';
// Database Attributes
2022-06-02 13:03:37 +00: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';
2022-07-25 08:53:41 +00:00
public const MODEL_ATTRIBUTE_DATETIME = 'attributeDatetime';
2023-03-14 08:24:53 +00:00
public const MODEL_ATTRIBUTE_RELATIONSHIP = 'attributeRelationship';
2020-06-23 18:53:24 +00:00
// Users
public const MODEL_ACCOUNT = 'account';
2022-06-02 13:03:37 +00: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_IDENTITY = 'identity';
public const MODEL_IDENTITY_LIST = 'identityList';
2022-06-02 13:03:37 +00:00
public const MODEL_TOKEN = 'token';
public const MODEL_JWT = 'jwt';
public const MODEL_PREFERENCES = 'preferences';
2022-05-23 14:54:50 +00:00
2023-06-22 13:35:49 +00:00
// MFA
2024-02-15 11:26:34 +00:00
public const MODEL_MFA_TYPE = 'mfaType';
public const MODEL_MFA_FACTORS = 'mfaFactors';
2023-06-22 13:35:49 +00:00
public const MODEL_MFA_OTP = 'mfaTotp';
public const MODEL_MFA_CHALLENGE = 'mfaChallenge';
2024-03-01 16:22:51 +00:00
public const MODEL_MFA_RECOVERY_CODES = 'mfaRecoveryCodes';
2023-06-22 13:35:49 +00:00
2022-06-15 08:11:48 +00:00
// Users password algos
public const MODEL_ALGO_MD5 = 'algoMd5';
public const MODEL_ALGO_SHA = 'algoSha';
public const MODEL_ALGO_SCRYPT = 'algoScrypt';
public const MODEL_ALGO_SCRYPT_MODIFIED = 'algoScryptModified';
public const MODEL_ALGO_BCRYPT = 'algoBcrypt';
public const MODEL_ALGO_ARGON2 = 'algoArgon2';
public const MODEL_ALGO_PHPASS = 'algoPhpass';
2020-06-23 18:53:24 +00:00
// Storage
2022-06-02 13:03:37 +00: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-23 18:53:24 +00:00
// Locale
2022-06-02 13:03:37 +00:00
public const MODEL_LOCALE = 'locale';
2023-04-17 02:10:17 +00:00
public const MODEL_LOCALE_CODE = 'localeCode';
public const MODEL_LOCALE_CODE_LIST = 'localeCodeList';
2022-06-02 13:03:37 +00:00
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';
// Messaging
public const MODEL_PROVIDER = 'provider';
public const MODEL_PROVIDER_LIST = 'providerList';
public const MODEL_MESSAGE = 'message';
public const MODEL_MESSAGE_LIST = 'messageList';
public const MODEL_TOPIC = 'topic';
public const MODEL_TOPIC_LIST = 'topicList';
public const MODEL_SUBSCRIBER = 'subscriber';
public const MODEL_SUBSCRIBER_LIST = 'subscriberList';
public const MODEL_TARGET = 'target';
public const MODEL_TARGET_LIST = 'targetList';
2020-06-23 18:53:24 +00:00
// Teams
2022-06-02 13:03:37 +00: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 09:53:06 +00:00
// VCS
public const MODEL_INSTALLATION = 'installation';
public const MODEL_INSTALLATION_LIST = 'installationList';
public const MODEL_FRAMEWORK_PROVIDER_REPOSITORY = 'frameworkProviderRepository';
public const MODEL_FRAMEWORK_PROVIDER_REPOSITORY_LIST = 'frameworkProviderRepositoryList';
public const MODEL_RUNTIME_PROVIDER_REPOSITORY = 'runtimeProviderRepository';
public const MODEL_RUNTIME_PROVIDER_REPOSITORY_LIST = 'runtimeProviderRepositoryList';
public const MODEL_BRANCH = 'branch';
public const MODEL_BRANCH_LIST = 'branchList';
2025-02-06 18:29:43 +00:00
public const MODEL_FRAMEWORK_DETECTION = 'frameworkDetection';
public const MODEL_RUNTIME_DETECTION = 'runtimeDetection';
public const MODEL_VCS_CONTENT = 'vcsContent';
public const MODEL_VCS_CONTENT_LIST = 'vcsContentList';
2024-10-22 15:01:38 +00:00
// Sites
public const MODEL_SITE = 'site';
public const MODEL_SITE_LIST = 'siteList';
public const MODEL_FRAMEWORK = 'framework';
public const MODEL_FRAMEWORK_LIST = 'frameworkList';
2024-12-02 12:20:54 +00:00
public const MODEL_FRAMEWORK_ADAPTER = 'frameworkAdapter';
public const MODEL_TEMPLATE_SITE = 'templateSite';
public const MODEL_TEMPLATE_SITE_LIST = 'templateSiteList';
public const MODEL_TEMPLATE_FRAMEWORK = 'templateFramework';
2024-10-22 15:01:38 +00:00
// Functions
2022-06-02 13:03:37 +00: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';
2023-02-14 11:01:38 +00:00
public const MODEL_HEADERS = 'headers';
public const MODEL_SPECIFICATION = 'specification';
public const MODEL_SPECIFICATION_LIST = 'specificationList';
2024-07-26 12:17:03 +00:00
public const MODEL_TEMPLATE_FUNCTION = 'templateFunction';
public const MODEL_TEMPLATE_FUNCTION_LIST = 'templateFunctionList';
public const MODEL_TEMPLATE_RUNTIME = 'templateRuntime';
public const MODEL_TEMPLATE_VARIABLE = 'templateVariable';
2022-05-23 14:54:50 +00:00
2023-03-08 18:30:01 +00:00
// Proxy
public const MODEL_PROXY_RULE = 'proxyRule';
public const MODEL_PROXY_RULE_LIST = 'proxyRuleList';
2023-08-04 16:21:41 +00:00
// Migrations
public const MODEL_MIGRATION = 'migration';
public const MODEL_MIGRATION_LIST = 'migrationList';
public const MODEL_MIGRATION_REPORT = 'migrationReport';
public const MODEL_MIGRATION_FIREBASE_PROJECT = 'firebaseProject';
public const MODEL_MIGRATION_FIREBASE_PROJECT_LIST = 'firebaseProjectList';
2022-05-23 14:54:50 +00:00
// Project
2022-06-02 13:03:37 +00: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';
2024-02-11 14:51:19 +00:00
public const MODEL_MOCK_NUMBER = 'mockNumber';
2023-08-07 10:49:55 +00:00
public const MODEL_AUTH_PROVIDER = 'authProvider';
public const MODEL_AUTH_PROVIDER_LIST = 'authProviderList';
2022-06-02 13:03:37 +00:00
public const MODEL_PLATFORM = 'platform';
public const MODEL_PLATFORM_LIST = 'platformList';
2022-07-20 07:18:49 +00:00
public const MODEL_VARIABLE = 'variable';
public const MODEL_VARIABLE_LIST = 'variableList';
public const MODEL_VCS = 'vcs';
2023-03-13 09:33:11 +00:00
public const MODEL_SMS_TEMPLATE = 'smsTemplate';
2023-03-10 11:04:11 +00:00
public const MODEL_EMAIL_TEMPLATE = 'emailTemplate';
2021-12-14 14:17:55 +00:00
// Health
2022-06-02 13:03:37 +00: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';
2024-02-12 01:18:19 +00:00
public const MODEL_HEALTH_CERTIFICATE = 'healthCertificate';
public const MODEL_HEALTH_STATUS_LIST = 'healthStatusList';
2022-05-23 14:54:50 +00:00
2023-03-31 15:14:59 +00:00
// Console
public const MODEL_CONSOLE_VARIABLES = 'consoleVariables';
2021-08-03 13:34:29 +00:00
// Deprecated
2022-06-02 13:03:37 +00:00
public const MODEL_PERMISSIONS = 'permissions';
public const MODEL_RULE = 'rule';
public const MODEL_TASK = 'task';
2023-08-19 18:26:47 +00:00
public const MODEL_DOMAIN = 'domain';
public const MODEL_DOMAIN_LIST = 'domainList';
2021-08-03 13:34:29 +00:00
2021-03-05 06:40:29 +00:00
// Tests (keep last)
2022-06-02 13:03:37 +00:00
public const MODEL_MOCK = 'mock';
/**
2024-03-07 13:48:36 +00:00
* @var array<Filter>
*/
2024-03-07 14:41:20 +00:00
protected array $filters = [];
/**
* @var array
*/
2022-08-01 10:22:04 +00:00
protected array $payload = [];
2020-06-26 12:27:58 +00:00
/**
* Response constructor.
*
* @param float $time
2020-06-26 12:27:58 +00:00
*/
2024-10-08 07:54:40 +00:00
public function __construct(SwooleHTTPResponse $response)
2020-06-05 09:53:06 +00:00
{
2024-10-08 07:54:40 +00:00
$this
// General
->setModel(new None())
->setModel(new Any())
->setModel(new Error())
->setModel(new ErrorDev())
// Lists
->setModel(new BaseList('Documents List', self::MODEL_DOCUMENT_LIST, 'documents', self::MODEL_DOCUMENT))
->setModel(new BaseList('Collections List', self::MODEL_COLLECTION_LIST, 'collections', self::MODEL_COLLECTION))
->setModel(new BaseList('Databases List', self::MODEL_DATABASE_LIST, 'databases', self::MODEL_DATABASE))
->setModel(new BaseList('Indexes List', self::MODEL_INDEX_LIST, 'indexes', self::MODEL_INDEX))
->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('Identities List', self::MODEL_IDENTITY_LIST, 'identities', self::MODEL_IDENTITY))
->setModel(new BaseList('Logs List', self::MODEL_LOG_LIST, 'logs', self::MODEL_LOG))
->setModel(new BaseList('Files List', self::MODEL_FILE_LIST, 'files', self::MODEL_FILE))
->setModel(new BaseList('Buckets List', self::MODEL_BUCKET_LIST, 'buckets', self::MODEL_BUCKET))
->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))
2024-10-22 15:01:38 +00:00
->setModel(new BaseList('Sites List', self::MODEL_SITE_LIST, 'sites', self::MODEL_SITE))
->setModel(new BaseList('Site Templates List', self::MODEL_TEMPLATE_SITE_LIST, 'templates', self::MODEL_TEMPLATE_SITE))
2024-10-08 07:54:40 +00:00
->setModel(new BaseList('Functions List', self::MODEL_FUNCTION_LIST, 'functions', self::MODEL_FUNCTION))
->setModel(new BaseList('Function Templates List', self::MODEL_TEMPLATE_FUNCTION_LIST, 'templates', self::MODEL_TEMPLATE_FUNCTION))
->setModel(new BaseList('Installations List', self::MODEL_INSTALLATION_LIST, 'installations', self::MODEL_INSTALLATION))
->setModel(new BaseList('Framework Provider Repositories List', self::MODEL_FRAMEWORK_PROVIDER_REPOSITORY_LIST, 'frameworkProviderRepositories', self::MODEL_FRAMEWORK_PROVIDER_REPOSITORY))
->setModel(new BaseList('Runtime Provider Repositories List', self::MODEL_RUNTIME_PROVIDER_REPOSITORY_LIST, 'runtimeProviderRepositories', self::MODEL_RUNTIME_PROVIDER_REPOSITORY))
2024-10-08 07:54:40 +00:00
->setModel(new BaseList('Branches List', self::MODEL_BRANCH_LIST, 'branches', self::MODEL_BRANCH))
->setModel(new BaseList('Frameworks List', self::MODEL_FRAMEWORK_LIST, 'frameworks', self::MODEL_FRAMEWORK))
2024-10-08 07:54:40 +00:00
->setModel(new BaseList('Runtimes List', self::MODEL_RUNTIME_LIST, 'runtimes', self::MODEL_RUNTIME))
->setModel(new BaseList('Deployments List', self::MODEL_DEPLOYMENT_LIST, 'deployments', self::MODEL_DEPLOYMENT))
->setModel(new BaseList('Executions List', self::MODEL_EXECUTION_LIST, 'executions', self::MODEL_EXECUTION))
->setModel(new BaseList('Builds List', self::MODEL_BUILD_LIST, 'builds', self::MODEL_BUILD)) // Not used anywhere yet
->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('Auth Providers List', self::MODEL_AUTH_PROVIDER_LIST, 'platforms', self::MODEL_AUTH_PROVIDER, true, false))
->setModel(new BaseList('Platforms List', self::MODEL_PLATFORM_LIST, 'platforms', self::MODEL_PLATFORM, true, false))
->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))
->setModel(new BaseList('Metric List', self::MODEL_METRIC_LIST, 'metrics', self::MODEL_METRIC, true, false))
->setModel(new BaseList('Variables List', self::MODEL_VARIABLE_LIST, 'variables', self::MODEL_VARIABLE))
->setModel(new BaseList('Status List', self::MODEL_HEALTH_STATUS_LIST, 'statuses', self::MODEL_HEALTH_STATUS))
->setModel(new BaseList('Rule List', self::MODEL_PROXY_RULE_LIST, 'rules', self::MODEL_PROXY_RULE))
->setModel(new BaseList('Locale codes list', self::MODEL_LOCALE_CODE_LIST, 'localeCodes', self::MODEL_LOCALE_CODE))
->setModel(new BaseList('Provider list', self::MODEL_PROVIDER_LIST, 'providers', self::MODEL_PROVIDER))
->setModel(new BaseList('Message list', self::MODEL_MESSAGE_LIST, 'messages', self::MODEL_MESSAGE))
->setModel(new BaseList('Topic list', self::MODEL_TOPIC_LIST, 'topics', self::MODEL_TOPIC))
->setModel(new BaseList('Subscriber list', self::MODEL_SUBSCRIBER_LIST, 'subscribers', self::MODEL_SUBSCRIBER))
->setModel(new BaseList('Target list', self::MODEL_TARGET_LIST, 'targets', self::MODEL_TARGET))
->setModel(new BaseList('Migrations List', self::MODEL_MIGRATION_LIST, 'migrations', self::MODEL_MIGRATION))
->setModel(new BaseList('Migrations Firebase Projects List', self::MODEL_MIGRATION_FIREBASE_PROJECT_LIST, 'projects', self::MODEL_MIGRATION_FIREBASE_PROJECT))
->setModel(new BaseList('Specifications List', self::MODEL_SPECIFICATION_LIST, 'specifications', self::MODEL_SPECIFICATION))
->setModel(new BaseList('VCS Content List', self::MODEL_VCS_CONTENT_LIST, 'contents', self::MODEL_VCS_CONTENT))
// Entities
->setModel(new Database())
->setModel(new Collection())
->setModel(new Attribute())
->setModel(new AttributeList())
->setModel(new AttributeString())
->setModel(new AttributeInteger())
->setModel(new AttributeFloat())
->setModel(new AttributeBoolean())
->setModel(new AttributeEmail())
->setModel(new AttributeEnum())
->setModel(new AttributeIP())
->setModel(new AttributeURL())
->setModel(new AttributeDatetime())
->setModel(new AttributeRelationship())
->setModel(new Index())
->setModel(new ModelDocument())
->setModel(new Log())
->setModel(new User())
->setModel(new AlgoMd5())
->setModel(new AlgoSha())
->setModel(new AlgoPhpass())
->setModel(new AlgoBcrypt())
->setModel(new AlgoScrypt())
->setModel(new AlgoScryptModified())
->setModel(new AlgoArgon2())
->setModel(new Account())
->setModel(new Preferences())
->setModel(new Session())
->setModel(new Identity())
->setModel(new Token())
->setModel(new JWT())
->setModel(new Locale())
->setModel(new LocaleCode())
->setModel(new File())
->setModel(new Bucket())
->setModel(new Team())
->setModel(new Membership())
2024-10-22 15:01:38 +00:00
->setModel(new Site())
->setModel(new TemplateSite())
->setModel(new TemplateFramework())
2024-10-08 07:54:40 +00:00
->setModel(new Func())
->setModel(new TemplateFunction())
->setModel(new TemplateRuntime())
->setModel(new TemplateVariable())
->setModel(new Installation())
->setModel(new FrameworkProviderRepository())
->setModel(new RuntimeProviderRepository())
2025-02-06 18:29:43 +00:00
->setModel(new FrameworkDetection())
->setModel(new RuntimeDetection())
2024-10-08 07:54:40 +00:00
->setModel(new VcsContent())
->setModel(new Branch())
->setModel(new Runtime())
->setModel(new Framework())
2024-12-02 12:20:54 +00:00
->setModel(new FrameworkAdapter())
2024-10-08 07:54:40 +00:00
->setModel(new Deployment())
->setModel(new Execution())
->setModel(new Build())
->setModel(new Project())
->setModel(new Webhook())
->setModel(new Key())
->setModel(new MockNumber())
->setModel(new AuthProvider())
->setModel(new Platform())
->setModel(new Variable())
->setModel(new Country())
->setModel(new Continent())
->setModel(new Language())
->setModel(new Currency())
->setModel(new Phone())
->setModel(new HealthAntivirus())
->setModel(new HealthQueue())
->setModel(new HealthStatus())
->setModel(new HealthCertificate())
->setModel(new HealthTime())
->setModel(new HealthVersion())
->setModel(new Metric())
->setModel(new MetricBreakdown())
->setModel(new UsageDatabases())
->setModel(new UsageDatabase())
->setModel(new UsageCollection())
->setModel(new UsageUsers())
->setModel(new UsageStorage())
->setModel(new UsageBuckets())
->setModel(new UsageFunctions())
->setModel(new UsageFunction())
2024-10-24 13:21:40 +00:00
->setModel(new UsageSites())
->setModel(new UsageSite())
2024-10-08 07:54:40 +00:00
->setModel(new UsageProject())
->setModel(new Headers())
->setModel(new Specification())
->setModel(new Rule())
->setModel(new TemplateSMS())
->setModel(new TemplateEmail())
->setModel(new ConsoleVariables())
->setModel(new MFAChallenge())
->setModel(new MFARecoveryCodes())
->setModel(new MFAType())
->setModel(new MFAFactors())
->setModel(new Provider())
->setModel(new Message())
->setModel(new Topic())
->setModel(new Subscriber())
->setModel(new Target())
->setModel(new Migration())
->setModel(new MigrationReport())
->setModel(new MigrationFirebaseProject())
// Tests (keep last)
->setModel(new Mock());
parent::__construct($response);
2020-06-05 09:53:06 +00:00
}
2020-05-16 11:28:26 +00:00
/**
* HTTP content types
*/
2022-06-02 13:03:37 +00:00
public const CONTENT_TYPE_YAML = 'application/x-yaml';
2022-07-11 21:52:00 +00:00
public const CONTENT_TYPE_NULL = 'null';
2024-08-07 13:15:53 +00:00
public const CONTENT_TYPE_MULTIPART = 'multipart/form-data';
2020-05-16 11:28:26 +00:00
2024-10-08 07:54:40 +00:00
/**
* List of defined output objects
*/
protected $models = [];
/**
* Set Model Object
*
* @return self
*/
public function setModel(Model $instance)
{
$this->models[$instance->getType()] = $instance;
return $this;
}
/**
* Get Model Object
*
* @param string $key
* @return Model
* @throws Exception
*/
public function getModel(string $key): Model
{
if (!isset($this->models[$key])) {
throw new Exception('Undefined model: ' . $key);
}
return $this->models[$key];
}
/**
* Get Models List
*
* @return Model[]
*/
public function getModels(): array
{
return $this->models;
}
public function applyFilters(array $data, string $model): array
{
foreach ($this->filters as $filter) {
$data = $filter->parse($data, $model);
}
return $data;
}
2020-06-05 09:53:06 +00:00
/**
* Validate response objects and outputs
* the response according to given format type
*
2020-10-31 08:42:41 +00:00
* @param Document $document
* @param string $model
*
2020-10-31 08:42:41 +00:00
* return void
* @throws Exception
2020-06-05 09:53:06 +00:00
*/
2020-10-31 11:06:09 +00:00
public function dynamic(Document $document, string $model): void
2020-06-05 09:53:06 +00:00
{
2023-08-25 15:13:25 +00:00
$output = $this->output(clone $document, $model);
$output = $this->applyFilters($output, $model);
2021-01-03 18:12:11 +00:00
2022-07-11 21:52:00 +00:00
switch ($this->getContentType()) {
case self::CONTENT_TYPE_JSON:
2024-08-07 13:15:53 +00:00
try {
$this->json(!empty($output) ? $output : new \stdClass());
} catch (JsonException $e) {
2024-08-08 08:19:33 +00:00
throw new Exception('Failed to parse response: ' . $e->getMessage(), 400);
2024-08-07 13:15:53 +00:00
}
break;
2022-07-11 21:52:00 +00:00
case self::CONTENT_TYPE_YAML:
$this->yaml(!empty($output) ? $output : new \stdClass());
break;
2022-07-11 21:52:00 +00:00
2021-03-18 18:55:43 +00:00
case self::CONTENT_TYPE_NULL:
break;
2024-08-07 13:15:53 +00:00
case self::CONTENT_TYPE_MULTIPART:
$this->multipart(!empty($output) ? $output : new \stdClass());
break;
2022-07-11 21:52:00 +00:00
default:
2021-03-18 18:55:43 +00:00
if ($model === self::MODEL_NONE) {
$this->noContent();
} else {
$this->json(!empty($output) ? $output : new \stdClass());
2021-03-18 18:55:43 +00:00
}
break;
}
2020-10-31 08:42:41 +00:00
}
2020-06-24 06:05:43 +00:00
/**
* Generate valid response object from document data
*
2020-10-31 08:42:41 +00:00
* @param Document $document
* @param string $model
*
2020-10-31 08:42:41 +00:00
* return array
* @return array
* @throws Exception
2020-06-24 06:05:43 +00:00
*/
public function output(Document $document, string $model): array
2020-06-24 06:05:43 +00:00
{
2023-08-25 15:13:25 +00:00
$data = clone $document;
2024-10-08 07:54:40 +00:00
$model = $this->getModel($model);
2020-06-05 09:53:06 +00:00
$output = [];
2023-08-25 15:13:25 +00:00
$data = $model->filter($data);
2021-03-17 20:45:47 +00:00
2020-10-29 22:44:01 +00:00
if ($model->isAny()) {
$this->payload = $data->getArrayCopy();
2022-08-01 10:22:04 +00:00
2020-11-30 21:41:58 +00:00
return $this->payload;
}
2020-10-29 22:44:01 +00:00
foreach ($model->getRules() as $key => $rule) {
2023-08-25 15:13:25 +00:00
if (!$data->isSet($key) && $rule['required']) { // do not set attribute in response if not required
if (\array_key_exists('default', $rule)) {
$data->setAttribute($key, $rule['default']);
2020-10-29 22:44:01 +00:00
} else {
2022-05-23 14:54:50 +00:00
throw new Exception('Model ' . $model->getName() . ' is missing response key: ' . $key);
2020-06-22 14:33:37 +00:00
}
2020-06-05 09:53:06 +00:00
}
2024-09-22 02:43:29 +00:00
if (!$data->isSet($key) && !$rule['required']) { // set output key null if data key is not set and required is false
$output[$key] = null;
continue;
}
2024-07-14 06:05:51 +00:00
2020-10-29 22:44:01 +00:00
if ($rule['array']) {
if (!is_array($data[$key])) {
2022-05-23 14:54:50 +00:00
throw new Exception($key . ' must be an array of type ' . $rule['type']);
2020-06-24 06:05:43 +00:00
}
2022-08-01 10:22:04 +00:00
foreach ($data[$key] as $index => $item) {
2020-10-29 22:44:01 +00:00
if ($item instanceof Document) {
2021-09-14 08:26:16 +00:00
if (\is_array($rule['type'])) {
foreach ($rule['type'] as $type) {
$condition = false;
2024-10-08 07:54:40 +00:00
foreach ($this->getModel($type)->conditions as $attribute => $val) {
2021-09-14 08:26:16 +00:00
$condition = $item->getAttribute($attribute) === $val;
2022-05-23 14:54:50 +00:00
if (!$condition) {
2021-09-14 08:26:16 +00:00
break;
}
}
if ($condition) {
$ruleType = $type;
break;
}
}
} else {
$ruleType = $rule['type'];
}
2024-10-08 07:54:40 +00:00
if (!array_key_exists($ruleType, $this->models)) {
2022-05-23 14:54:50 +00:00
throw new Exception('Missing model for rule: ' . $ruleType);
}
2022-08-01 10:22:04 +00:00
$data[$key][$index] = $this->output($item, $ruleType);
2020-06-24 06:05:43 +00:00
}
}
2022-08-01 10:22:04 +00:00
} else {
if ($data[$key] instanceof Document) {
$data[$key] = $this->output($data[$key], $rule['type']);
}
2020-06-23 15:01:20 +00:00
}
2021-09-14 08:26:16 +00:00
2020-06-05 09:53:06 +00:00
$output[$key] = $data[$key];
}
$this->payload = $output;
2020-11-30 21:41:58 +00:00
return $this->payload;
2020-05-16 11:28:26 +00:00
}
2022-07-24 09:49:51 +00:00
/**
2022-07-30 19:10:30 +00:00
* Output response
2022-07-24 09:49:51 +00:00
*
2022-07-30 19:10:30 +00:00
* Generate HTTP response output including the response header (+cookies) and body and prints them.
*
* @param string $body
2022-07-24 09:49:51 +00:00
*
* @return void
*/
2022-08-14 15:01:34 +00:00
public function file(string $body = ''): void
{
$this->payload = [
2022-08-14 15:01:34 +00:00
'payload' => $body
2022-07-26 12:50:33 +00:00
];
2022-08-14 15:01:34 +00:00
$this->send($body);
}
2020-05-16 11:28:26 +00: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
* @throws Exception
2020-05-16 11:28:26 +00:00
*/
public function yaml(array $data): void
2020-05-16 11:28:26 +00:00
{
2020-10-27 19:44:15 +00:00
if (!extension_loaded('yaml')) {
2020-05-16 11:28:26 +00: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)
2023-06-22 13:35:49 +00:00
->send(\yaml_emit($data, YAML_UTF8_ENCODING));
2020-05-16 11:28:26 +00:00
}
2024-08-07 13:15:53 +00:00
/**
* Multipart
*
* This helper is for sending multipart/form-data HTTP response.
* It sets relevant content type header ('multipart/form-data') and convert a PHP array ($data) to valid Multipart using BodyMultipart
*
* @param array $data
*
* @return void
*/
public function multipart(array $data): void
{
$multipart = new BodyMultipart();
foreach ($data as $key => $value) {
$multipart->setPart($key, $value);
}
$this
2024-08-14 09:39:55 +00:00
->setContentType($multipart->exportHeader())
2024-08-07 13:15:53 +00:00
->send($multipart->exportBody());
}
/**
* JSON
*
* This helper is for sending JSON HTTP response.
* It sets relevant content type header ('application/json') and convert a PHP array ($data) to valid JSON using native json_encode
*
* @see http://en.wikipedia.org/wiki/JSON
*
* @param mixed $data
* @return void
*/
public function json($data): void
{
if (!is_array($data) && !$data instanceof \stdClass) {
2024-08-10 20:55:03 +00:00
throw new \Exception('Response body is not a valid JSON object.');
2024-08-07 13:15:53 +00:00
}
$this
->setContentType(Response::CONTENT_TYPE_JSON, self::CHARSET_UTF8)
->send(\json_encode($data, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
}
/**
* @return array
*/
2022-05-23 14:54:50 +00:00
public function getPayload(): array
{
return $this->payload;
}
/**
2024-03-07 13:48:36 +00:00
* Function to add a response filter, the order of filters are first in - first out.
*
* @param $filter the response filter to set
*
* @return void
*/
2024-03-07 14:41:20 +00:00
public function addFilter(Filter $filter): void
{
2024-03-07 14:41:20 +00:00
$this->filters[] = $filter;
}
/**
* Return the currently set filter
*
* @return Filter
*/
2024-03-07 14:41:20 +00:00
public function getFilters(): array
{
2024-03-07 14:41:20 +00:00
return $this->filters;
2024-03-07 13:48:36 +00:00
}
/**
* Reset filters
*
* @return void
*/
2024-03-07 14:41:20 +00:00
public function resetFilters(): void
2024-03-07 13:48:36 +00:00
{
2024-03-07 14:41:20 +00:00
$this->filters = [];
}
/**
* Check if a filter has been set
*
* @return bool
*/
2024-03-07 14:41:20 +00:00
public function hasFilters(): bool
{
2024-03-07 14:41:20 +00:00
return !empty($this->filters);
}
2023-02-14 13:58:13 +00:00
/**
* Set Header
*
* @param string $key
* @param string $value
* @return void
*/
public function setHeader(string $key, string $value): void
{
$this->sendHeader($key, $value);
}
2020-05-16 11:28:26 +00:00
}