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 Exception;
|
|
|
|
use InvalidArgumentException;
|
2019-05-09 18:54:39 +12:00
|
|
|
use Resque;
|
|
|
|
|
|
|
|
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 USAGE_QUEUE_NAME = 'v1-usage';
|
|
|
|
const USAGE_CLASS_NAME = 'UsageV1';
|
|
|
|
|
|
|
|
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 = '';
|
|
|
|
protected array $params = [];
|
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;
|
|
|
|
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-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-03-29 21:30:57 +13:00
|
|
|
* @return Event
|
2020-12-07 21:51:12 +13:00
|
|
|
*/
|
|
|
|
public function setClass(string $class): self
|
|
|
|
{
|
|
|
|
$this->class = $class;
|
|
|
|
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
|
|
|
|
* @return Event
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Execute Event.
|
2022-03-29 21:30:57 +13:00
|
|
|
*
|
|
|
|
* @return Event
|
|
|
|
* @throws InvalidArgumentException
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2022-03-29 21:30:57 +13:00
|
|
|
public function trigger(): self
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
|
|
|
Resque::enqueue($this->queue, $this->class, $this->params);
|
2020-07-05 01:06:23 +12:00
|
|
|
|
2022-03-29 21:30:57 +13:00
|
|
|
return $this->reset();
|
2020-07-05 01:06:23 +12:00
|
|
|
}
|
|
|
|
|
2022-03-29 21:30:57 +13:00
|
|
|
/**
|
|
|
|
* Resets event.
|
|
|
|
*
|
|
|
|
* @return Event
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
static function generateEvents(string $pattern, array $params = []): array
|
|
|
|
{
|
|
|
|
$parts = \explode('.', $pattern);
|
|
|
|
$count = \count($parts);
|
|
|
|
|
|
|
|
if ($count < 2 || $count > 6) {
|
|
|
|
throw new Exception("Patten incorrect.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identify all sestions of the pattern.
|
|
|
|
*/
|
|
|
|
$type = $parts[0];
|
|
|
|
$action = match ($count) {
|
|
|
|
2 => $parts[1],
|
|
|
|
3, 4 => $parts[2],
|
|
|
|
5, 6 => $parts[4]
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($count > 4) {
|
|
|
|
$subType = $parts[2];
|
|
|
|
$subResource = $parts[3];
|
|
|
|
if ($count === 6) {
|
|
|
|
$attribute = $parts[5];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($count > 2) {
|
|
|
|
$resource = $parts[1];
|
|
|
|
if ($count === 4) {
|
|
|
|
$attribute = $parts[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$paramKeys = \array_keys($params);
|
|
|
|
$paramValues = \array_values($params);
|
|
|
|
|
|
|
|
$patterns = [];
|
|
|
|
$resource ??= false;
|
|
|
|
$subResource ??= false;
|
|
|
|
$attribute ??= false;
|
|
|
|
|
|
|
|
if (empty($params) && ($type ?? false) && !$resource) {
|
|
|
|
return [$pattern];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($resource && !\in_array(\trim($resource, '[]'), $paramKeys)) {
|
|
|
|
throw new InvalidArgumentException("{$resource} is missing from the params.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($subResource && !\in_array(\trim($subResource, '[]'), $paramKeys)) {
|
|
|
|
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 {
|
|
|
|
if ($attribute) {
|
|
|
|
$patterns[] = \implode('.', [$type, $resource, $action, $attribute]);
|
|
|
|
}
|
|
|
|
$patterns[] = \implode('.', [$type, $resource, $action]);
|
|
|
|
$patterns[] = \implode('.', [$type, $resource]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($subResource) {
|
|
|
|
$patterns[] = \implode('.', [$type, $resource, $subType, $subResource]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
return $events;
|
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|