2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
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-03-29 21:30:57 +13:00
|
|
|
const DATABASE_QUEUE_NAME = 'v1-database';
|
2021-06-18 06:22:06 +12:00
|
|
|
const DATABASE_CLASS_NAME = 'DatabaseV1';
|
|
|
|
|
2020-12-23 01:51:46 +13:00
|
|
|
const DELETE_QUEUE_NAME = 'v1-deletes';
|
|
|
|
const DELETE_CLASS_NAME = 'DeletesV1';
|
|
|
|
|
|
|
|
const AUDITS_QUEUE_NAME = 'v1-audits';
|
|
|
|
const AUDITS_CLASS_NAME = 'AuditsV1';
|
|
|
|
|
|
|
|
const MAILS_QUEUE_NAME = 'v1-mails';
|
|
|
|
const MAILS_CLASS_NAME = 'MailsV1';
|
2020-12-28 06:57:35 +13:00
|
|
|
|
|
|
|
const FUNCTIONS_QUEUE_NAME = 'v1-functions';
|
|
|
|
const FUNCTIONS_CLASS_NAME = 'FunctionsV1';
|
|
|
|
|
|
|
|
const WEBHOOK_QUEUE_NAME = 'v1-webhooks';
|
|
|
|
const WEBHOOK_CLASS_NAME = 'WebhooksV1';
|
|
|
|
|
|
|
|
const CERTIFICATES_QUEUE_NAME = 'v1-certificates';
|
|
|
|
const CERTIFICATES_CLASS_NAME = 'CertificatesV1';
|
2022-01-31 22:46:24 +13:00
|
|
|
|
|
|
|
const BUILDS_QUEUE_NAME = 'v1-builds';
|
|
|
|
const BUILDS_CLASS_NAME = 'BuildsV1';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
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 = [];
|
|
|
|
protected ?Document $project = null;
|
|
|
|
protected ?Document $user = null;
|
2022-04-19 04:21:45 +12:00
|
|
|
protected ?Document $context = 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;
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
/**
|
2022-03-29 21:30:57 +13:00
|
|
|
* Set queue used for this event.
|
|
|
|
*
|
2020-12-07 21:51:12 +13:00
|
|
|
* @param string $queue
|
2022-03-29 21:30:57 +13:00
|
|
|
* @return Event
|
2020-12-07 21:51:12 +13:00
|
|
|
*/
|
|
|
|
public function setQueue(string $queue): self
|
|
|
|
{
|
|
|
|
$this->queue = $queue;
|
2022-04-04 18:30:07 +12:00
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-29 21:30:57 +13:00
|
|
|
* Get queue used for this event.
|
|
|
|
*
|
2020-12-07 21:51:12 +13:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-03-29 21:30:57 +13:00
|
|
|
public function getQueue(): string
|
2020-12-07 21:51:12 +13:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2022-04-14 00:39:31 +12:00
|
|
|
$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
|
|
|
|
*/
|
2022-04-14 00:39:31 +12:00
|
|
|
public function getProject(): Document
|
2022-04-04 18:30:07 +12:00
|
|
|
{
|
2022-04-14 00:39:31 +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
|
|
|
|
{
|
2022-04-14 00:39:31 +12:00
|
|
|
$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
|
|
|
|
*/
|
2022-04-14 00:39:31 +12:00
|
|
|
public function getUser(): Document
|
2022-04-04 18:30:07 +12:00
|
|
|
{
|
2022-04-14 00:39:31 +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.
|
|
|
|
*
|
|
|
|
* @param Document $payload
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @return Document
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param Document $context
|
|
|
|
* @return self
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
public function setContext(Document $context): self
|
2022-04-04 18:30:07 +12:00
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->context = $context;
|
2022-04-04 18:30:07 +12:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-05-11 01:03:34 +12:00
|
|
|
/**
|
|
|
|
* Get context for this event.
|
|
|
|
*
|
|
|
|
* @return Document
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
public function getContext(): ?Document
|
2022-04-04 18:30:07 +12:00
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
return $this->context;
|
2022-04-04 18:30:07 +12:00
|
|
|
}
|
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
/**
|
2022-03-29 21:30:57 +13:00
|
|
|
* Set class used for this event.
|
2020-12-07 21:51:12 +13:00
|
|
|
* @param string $class
|
2022-05-11 01:03:34 +12:00
|
|
|
* @return self
|
2020-12-07 21:51:12 +13:00
|
|
|
*/
|
|
|
|
public function setClass(string $class): self
|
|
|
|
{
|
|
|
|
$this->class = $class;
|
2022-04-04 18:30:07 +12:00
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-29 21:30:57 +13:00
|
|
|
* Get class used for this event.
|
|
|
|
*
|
2020-12-07 21:51:12 +13:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-03-29 21:30:57 +13:00
|
|
|
public function getClass(): string
|
2020-12-07 21:51:12 +13:00
|
|
|
{
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
2022-03-29 21:30:57 +13:00
|
|
|
* Set param of event.
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
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-09-07 05:04:26 +12:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-29 21:30:57 +13:00
|
|
|
* Get param of event.
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
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
|
|
|
/**
|
2019-09-07 05:04:26 +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, [
|
2022-04-14 00:39:31 +12:00
|
|
|
'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], '[');
|
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];
|
|
|
|
if ($count === 6) {
|
|
|
|
$attribute = $parts[5];
|
|
|
|
}
|
2022-04-04 18:30:07 +12:00
|
|
|
} else {
|
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;
|
|
|
|
$attribute ??= false;
|
|
|
|
$action = match (true) {
|
|
|
|
!$hasSubResource && $count > 2 => $parts[2],
|
|
|
|
$hasSubResource && $count > 4 => $parts[4],
|
|
|
|
default => false
|
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
'type' => $type,
|
|
|
|
'resource' => $resource,
|
|
|
|
'subType' => $subType,
|
|
|
|
'subResource' => $subResource,
|
|
|
|
'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-04-04 18:30:07 +12:00
|
|
|
static function generateEvents(string $pattern, array $params = []): array
|
|
|
|
{
|
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'];
|
|
|
|
$action = $parsed['action'];
|
|
|
|
$attribute = $parsed['attribute'];
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2022-04-14 00:39:31 +12: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.");
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:39:31 +12:00
|
|
|
if ($subResource && !\in_array(\trim($subResource, "\[\]"), $paramKeys)) {
|
2022-03-29 21:30:57 +13:00
|
|
|
throw new InvalidArgumentException("{$subResource} is missing from the params.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create all possible patterns including placeholders.
|
|
|
|
*/
|
|
|
|
if ($action) {
|
|
|
|
if ($subResource) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if ($current === $key) continue;
|
|
|
|
|
|
|
|
$filtered = \array_filter($paramKeys, fn(string $k) => $k === $current);
|
|
|
|
$events[] = \str_replace($paramKeys, $paramValues, \str_replace($filtered, '*', $eventPattern));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|