1
0
Fork 0
mirror of synced 2024-05-18 19:52:39 +12:00
appwrite/src/Appwrite/Event/Event.php

468 lines
12 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
namespace Appwrite\Event;
2019-05-09 18:54:39 +12:00
2022-03-29 21:30:57 +13:00
use InvalidArgumentException;
2019-05-09 18:54:39 +12:00
use Resque;
2022-04-04 18:30:07 +12:00
use Utopia\Database\Document;
2019-05-09 18:54:39 +12:00
class Event
{
2022-06-03 01:03:37 +12:00
public const DATABASE_QUEUE_NAME = 'v1-database';
public const DATABASE_CLASS_NAME = 'DatabaseV1';
2021-06-18 06:22:06 +12:00
2022-06-03 01:03:37 +12:00
public const DELETE_QUEUE_NAME = 'v1-deletes';
public const DELETE_CLASS_NAME = 'DeletesV1';
2022-06-03 01:03:37 +12:00
public const AUDITS_QUEUE_NAME = 'v1-audits';
public const AUDITS_CLASS_NAME = 'AuditsV1';
2022-06-03 01:03:37 +12:00
public const MAILS_QUEUE_NAME = 'v1-mails';
public const MAILS_CLASS_NAME = 'MailsV1';
2020-12-28 06:57:35 +13:00
2022-06-03 01:03:37 +12:00
public const FUNCTIONS_QUEUE_NAME = 'v1-functions';
public const FUNCTIONS_CLASS_NAME = 'FunctionsV1';
2020-12-28 06:57:35 +13:00
2022-06-03 01:03:37 +12:00
public const WEBHOOK_QUEUE_NAME = 'v1-webhooks';
public const WEBHOOK_CLASS_NAME = 'WebhooksV1';
2020-12-28 06:57:35 +13:00
2022-06-03 01:03:37 +12:00
public const CERTIFICATES_QUEUE_NAME = 'v1-certificates';
public const CERTIFICATES_CLASS_NAME = 'CertificatesV1';
2022-01-31 22:46:24 +13:00
2022-06-03 01:03:37 +12:00
public const BUILDS_QUEUE_NAME = 'v1-builds';
public const BUILDS_CLASS_NAME = 'BuildsV1';
2019-05-09 18:54:39 +12:00
2022-06-09 01:57:34 +12:00
public const MESSAGING_QUEUE_NAME = 'v1-messaging';
public const MESSAGING_CLASS_NAME = 'MessagingV1';
2022-03-29 21:30:57 +13:00
protected string $queue = '';
protected string $class = '';
2022-04-04 18:30:07 +12:00
protected string $event = '';
2022-03-29 21:30:57 +13:00
protected array $params = [];
2022-04-04 18:30:07 +12:00
protected array $payload = [];
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 22:51:49 +12:00
protected array $context = [];
2022-04-04 18:30:07 +12:00
protected ?Document $project = null;
protected ?Document $user = null;
2019-05-09 18:54:39 +12:00
/**
* @param string $queue
* @param string $class
2022-03-29 21:30:57 +13:00
* @return void
2019-05-09 18:54:39 +12:00
*/
2020-07-05 01:06:23 +12:00
public function __construct(string $queue, string $class)
2019-05-09 18:54:39 +12:00
{
$this->queue = $queue;
$this->class = $class;
}
/**
2022-03-29 21:30:57 +13:00
* Set queue used for this event.
*
* @param string $queue
2022-03-29 21:30:57 +13:00
* @return Event
*/
public function setQueue(string $queue): self
{
$this->queue = $queue;
2022-04-04 18:30:07 +12:00
return $this;
}
/**
2022-03-29 21:30:57 +13:00
* Get queue used for this event.
*
* @return string
*/
2022-03-29 21:30:57 +13:00
public function getQueue(): string
{
return $this->queue;
}
2022-04-04 18:30:07 +12:00
/**
* Set event name used for this event.
* @param string $event
* @return Event
*/
public function setEvent(string $event): self
{
$this->event = $event;
return $this;
}
/**
* Get event name used for this event.
*
* @return string
*/
public function getEvent(): string
{
return $this->event;
}
2022-05-11 01:03:34 +12:00
/**
* Set project for this event.
*
* @param Document $project
* @return self
*/
2022-04-04 18:30:07 +12:00
public function setProject(Document $project): self
{
$this->project = $project;
2022-04-04 18:30:07 +12:00
return $this;
}
2022-05-11 01:03:34 +12:00
/**
* Get project for this event.
*
* @return Document
*/
public function getProject(): Document
2022-04-04 18:30:07 +12:00
{
return $this->project;
2022-04-04 18:30:07 +12:00
}
2022-05-11 01:03:34 +12:00
/**
* Set user for this event.
*
* @param Document $user
* @return self
*/
2022-04-04 18:30:07 +12:00
public function setUser(Document $user): self
{
$this->user = $user;
2022-04-04 18:30:07 +12:00
return $this;
}
2022-05-11 01:03:34 +12:00
/**
* Get project for this event.
*
* @return Document
*/
public function getUser(): Document
2022-04-04 18:30:07 +12:00
{
return $this->user;
2022-04-04 18:30:07 +12:00
}
2022-05-11 01:03:34 +12:00
/**
* Set payload for this event.
*
2022-05-17 05:15:38 +12:00
* @param array $payload
2022-05-11 01:03:34 +12:00
* @return self
*/
2022-04-04 18:30:07 +12:00
public function setPayload(array $payload): self
{
$this->payload = $payload;
return $this;
}
2022-05-11 01:03:34 +12:00
/**
* Get payload for this event.
*
2022-05-17 05:15:38 +12:00
* @return array
2022-05-11 01:03:34 +12:00
*/
2022-04-04 18:30:07 +12:00
public function getPayload(): array
{
return $this->payload;
}
2022-05-11 01:03:34 +12:00
/**
* Set context for this event.
*
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 22:51:49 +12:00
* @param string $key
2022-05-11 01:03:34 +12:00
* @param Document $context
* @return self
*/
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 22:51:49 +12:00
public function setContext(string $key, Document $context): self
2022-04-04 18:30:07 +12:00
{
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 22:51:49 +12:00
$this->context[$key] = $context;
2022-04-04 18:30:07 +12:00
return $this;
}
2022-05-11 01:03:34 +12:00
/**
* Get context for this event.
*
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 22:51:49 +12:00
* @param string $key
*
2022-05-17 05:16:08 +12:00
* @return null|Document
2022-05-11 01:03:34 +12:00
*/
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 22:51:49 +12:00
public function getContext(string $key): ?Document
2022-04-04 18:30:07 +12:00
{
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 22:51:49 +12:00
return $this->context[$key] ?? null;
2022-04-04 18:30:07 +12:00
}
/**
2022-03-29 21:30:57 +13:00
* Set class used for this event.
* @param string $class
2022-05-11 01:03:34 +12:00
* @return self
*/
public function setClass(string $class): self
{
$this->class = $class;
2022-04-04 18:30:07 +12:00
return $this;
}
/**
2022-03-29 21:30:57 +13:00
* Get class used for this event.
*
* @return string
*/
2022-03-29 21:30:57 +13:00
public function getClass(): string
{
return $this->class;
}
2019-05-09 18:54:39 +12:00
/**
2022-03-29 21:30:57 +13:00
* Set param of event.
*
2022-03-29 21:30:57 +13:00
* @param string $key
* @param mixed $value
2022-05-11 01:03:34 +12:00
* @return self
2019-05-09 18:54:39 +12:00
*/
2022-03-29 21:30:57 +13:00
public function setParam(string $key, mixed $value): self
2019-05-09 18:54:39 +12:00
{
$this->params[$key] = $value;
2019-05-09 18:54:39 +12:00
return $this;
}
/**
2022-03-29 21:30:57 +13:00
* Get param of event.
*
2022-03-29 21:30:57 +13:00
* @param string $key
* @return mixed
2019-05-09 18:54:39 +12:00
*/
2022-03-29 21:30:57 +13:00
public function getParam(string $key): mixed
2019-05-09 18:54:39 +12:00
{
2022-03-29 21:30:57 +13:00
return $this->params[$key] ?? null;
2019-05-09 18:54:39 +12:00
}
2022-04-04 18:30:07 +12:00
/**
* Get all params of the event.
*
* @return array
*/
public function getParams(): array
{
return $this->params;
}
2019-05-09 18:54:39 +12:00
/**
* Execute Event.
2022-03-29 21:30:57 +13:00
*
2022-04-04 18:30:07 +12:00
* @return string|bool
2022-03-29 21:30:57 +13:00
* @throws InvalidArgumentException
2019-05-09 18:54:39 +12:00
*/
2022-04-04 18:30:07 +12:00
public function trigger(): string|bool
2019-05-09 18:54:39 +12:00
{
2022-04-04 18:30:07 +12:00
return Resque::enqueue($this->queue, $this->class, [
'project' => $this->project,
'user' => $this->user,
2022-04-04 18:30:07 +12:00
'payload' => $this->payload,
2022-04-19 04:21:45 +12:00
'context' => $this->context,
2022-04-04 18:30:07 +12:00
'events' => Event::generateEvents($this->getEvent(), $this->getParams())
]);
2020-07-05 01:06:23 +12:00
}
2022-03-29 21:30:57 +13:00
/**
* Resets event.
*
2022-05-11 01:03:34 +12:00
* @return self
2022-03-29 21:30:57 +13:00
*/
2020-07-05 01:06:23 +12:00
public function reset(): self
{
$this->params = [];
return $this;
2019-05-09 18:54:39 +12:00
}
2022-03-29 21:30:57 +13:00
2022-04-19 04:21:45 +12:00
/**
* Parses event pattern and returns the parts in their respective section.
*
* @param string $pattern
* @return array
*/
2022-04-04 18:30:07 +12:00
public static function parseEventPattern(string $pattern): array
2022-03-29 21:30:57 +13:00
{
$parts = \explode('.', $pattern);
$count = \count($parts);
/**
2022-05-11 01:03:34 +12:00
* Identify all sections of the pattern.
2022-03-29 21:30:57 +13:00
*/
2022-04-04 18:30:07 +12:00
$type = $parts[0] ?? false;
$resource = $parts[1] ?? false;
$hasSubResource = $count > 3 && \str_starts_with($parts[3], '[');
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 22:51:49 +12:00
$hasSubSubResource = $count > 5 && \str_starts_with($parts[5], '[') && $hasSubResource;
2022-03-29 21:30:57 +13:00
2022-04-04 18:30:07 +12:00
if ($hasSubResource) {
2022-03-29 21:30:57 +13:00
$subType = $parts[2];
$subResource = $parts[3];
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 22:51:49 +12:00
}
if ($hasSubSubResource) {
$subSubType = $parts[4];
$subSubResource = $parts[5];
if ($count == 8) {
$attribute = $parts[7];
}
}
if ($hasSubResource && !$hasSubSubResource) {
2022-03-29 21:30:57 +13:00
if ($count === 6) {
$attribute = $parts[5];
}
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 22:51:49 +12:00
}
if (!$hasSubResource) {
2022-03-29 21:30:57 +13:00
if ($count === 4) {
$attribute = $parts[3];
}
}
2022-04-04 18:30:07 +12:00
$subType ??= false;
$subResource ??= false;
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 22:51:49 +12:00
$subSubType ??= false;
$subSubResource ??= false;
2022-04-04 18:30:07 +12:00
$attribute ??= false;
$action = match (true) {
!$hasSubResource && $count > 2 => $parts[2],
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 22:51:49 +12:00
$hasSubSubResource => $parts[6] ?? false,
2022-04-04 18:30:07 +12:00
$hasSubResource && $count > 4 => $parts[4],
default => false
};
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 22:51:49 +12:00
2022-04-04 18:30:07 +12:00
return [
'type' => $type,
'resource' => $resource,
'subType' => $subType,
'subResource' => $subResource,
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 22:51:49 +12:00
'subSubType' => $subSubType,
'subSubResource' => $subSubResource,
2022-04-04 18:30:07 +12:00
'action' => $action,
'attribute' => $attribute,
];
}
2022-04-19 04:21:45 +12:00
/**
* Generates all possible events from a pattern.
*
* @param string $pattern
* @param array $params
* @return array
* @throws \InvalidArgumentException
*/
2022-06-03 01:09:04 +12:00
public static function generateEvents(string $pattern, array $params = []): array
2022-04-04 18:30:07 +12:00
{
2022-05-11 01:03:34 +12:00
// $params = \array_filter($params, fn($param) => !\is_array($param));
2022-03-29 21:30:57 +13:00
$paramKeys = \array_keys($params);
$paramValues = \array_values($params);
$patterns = [];
2022-04-04 18:30:07 +12:00
$parsed = self::parseEventPattern($pattern);
$type = $parsed['type'];
$resource = $parsed['resource'];
$subType = $parsed['subType'];
$subResource = $parsed['subResource'];
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 22:51:49 +12:00
$subSubType = $parsed['subSubType'];
$subSubResource = $parsed['subSubResource'];
2022-04-04 18:30:07 +12:00
$action = $parsed['action'];
$attribute = $parsed['attribute'];
2022-03-29 21:30:57 +13:00
if ($resource && !\in_array(\trim($resource, "\[\]"), $paramKeys)) {
2022-03-29 21:30:57 +13:00
throw new InvalidArgumentException("{$resource} is missing from the params.");
}
if ($subResource && !\in_array(\trim($subResource, "\[\]"), $paramKeys)) {
2022-03-29 21:30:57 +13:00
throw new InvalidArgumentException("{$subResource} is missing from the params.");
}
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 22:51:49 +12:00
if ($subSubResource && !\in_array(\trim($subSubResource, "\[\]"), $paramKeys)) {
throw new InvalidArgumentException("{$subSubResource} is missing from the params.");
}
2022-03-29 21:30:57 +13:00
/**
* Create all possible patterns including placeholders.
*/
if ($action) {
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 22:51:49 +12:00
if ($subSubResource) {
if ($attribute) {
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource, $subSubType, $subSubResource, $action, $attribute]);
}
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource, $subSubType, $subSubResource, $action]);
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource, $subSubType, $subSubResource]);
} elseif ($subResource) {
2022-03-29 21:30:57 +13:00
if ($attribute) {
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource, $action, $attribute]);
}
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource, $action]);
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource]);
} else {
$patterns[] = \implode('.', [$type, $resource, $action]);
2022-04-19 04:21:45 +12:00
}
if ($attribute) {
$patterns[] = \implode('.', [$type, $resource, $action, $attribute]);
2022-03-29 21:30:57 +13:00
}
}
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 22:51:49 +12:00
if ($subSubResource) {
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource, $subSubType, $subSubResource]);
}
2022-03-29 21:30:57 +13:00
if ($subResource) {
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource]);
}
2022-04-19 04:21:45 +12:00
$patterns[] = \implode('.', [$type, $resource]);
2022-03-29 21:30:57 +13:00
/**
* Removes all duplicates.
*/
$patterns = \array_unique($patterns);
/**
* Set all possible values of the patterns and replace placeholders.
*/
$events = [];
foreach ($patterns as $eventPattern) {
$events[] = \str_replace($paramKeys, $paramValues, $eventPattern);
$events[] = \str_replace($paramKeys, '*', $eventPattern);
foreach ($paramKeys as $key) {
foreach ($paramKeys as $current) {
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 22:51:49 +12:00
if ($subSubResource) {
foreach ($paramKeys as $subCurrent) {
if ($subCurrent === $current || $subCurrent === $key) {
continue;
}
$filtered1 = \array_filter($paramKeys, fn(string $k) => $k === $subCurrent);
$events[] = \str_replace($paramKeys, $paramValues, \str_replace($filtered1, '*', $eventPattern));
$filtered2 = \array_filter($paramKeys, fn(string $k) => $k === $current);
$events[] = \str_replace($paramKeys, $paramValues, \str_replace($filtered2, '*', \str_replace($filtered1, '*', $eventPattern)));
$events[] = \str_replace($paramKeys, $paramValues, \str_replace($filtered2, '*', $eventPattern));
}
} else {
if ($current === $key) {
continue;
}
$filtered = \array_filter($paramKeys, fn(string $k) => $k === $current);
$events[] = \str_replace($paramKeys, $paramValues, \str_replace($filtered, '*', $eventPattern));
2022-05-24 02:54:50 +12:00
}
2022-03-29 21:30:57 +13:00
}
}
}
/**
* Remove [] from the events.
*/
$events = \array_map(fn (string $event) => \str_replace(['[', ']'], '', $event), $events);
2022-04-04 18:30:07 +12:00
$events = \array_unique($events);
2022-03-29 21:30:57 +13:00
return $events;
}
}