diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
commit | 4ce65d59ca91871cfd126497158200a818720bce (patch) | |
tree | e277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/scheduler/src/Common | |
parent | Initial commit. (diff) | |
download | icinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.tar.xz icinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.zip |
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/scheduler/src/Common')
-rw-r--r-- | vendor/ipl/scheduler/src/Common/Promises.php | 108 | ||||
-rw-r--r-- | vendor/ipl/scheduler/src/Common/TaskProperties.php | 83 | ||||
-rw-r--r-- | vendor/ipl/scheduler/src/Common/Timers.php | 60 |
3 files changed, 251 insertions, 0 deletions
diff --git a/vendor/ipl/scheduler/src/Common/Promises.php b/vendor/ipl/scheduler/src/Common/Promises.php new file mode 100644 index 0000000..b896627 --- /dev/null +++ b/vendor/ipl/scheduler/src/Common/Promises.php @@ -0,0 +1,108 @@ +<?php + +namespace ipl\Scheduler\Common; + +use ArrayObject; +use InvalidArgumentException; +use Ramsey\Uuid\UuidInterface; +use React\Promise\PromiseInterface; +use SplObjectStorage; + +trait Promises +{ + /** @var SplObjectStorage<UuidInterface, ArrayObject<int, PromiseInterface>> */ + protected $promises; + + /** + * Add the given promise for the specified UUID + * + * **Example Usage:** + * + * ```php + * $promise = work(); + * $promises->addPromise($uuid, $promise); + * ``` + * + * @param UuidInterface $uuid + * @param PromiseInterface $promise + * + * @return $this + */ + protected function addPromise(UuidInterface $uuid, PromiseInterface $promise): self + { + if (! $this->promises->contains($uuid)) { + $this->promises->attach($uuid, new ArrayObject()); + } + + $this->promises[$uuid][] = $promise; + + return $this; + } + + /** + * Remove the given promise for the specified UUID + * + * **Example Usage:** + * + * ```php + * $promise->always(function () use ($uuid, $promise) { + * $promises->removePromise($uuid, $promise); + * }) + * ``` + * + * @param UuidInterface $uuid + * @param PromiseInterface $promise + * + * @return $this + * + * @throws InvalidArgumentException If the given UUID doesn't have any registered promises or when the specified + * UUID promises doesn't contain the provided promise + */ + protected function removePromise(UuidInterface $uuid, PromiseInterface $promise): self + { + if (! $this->promises->contains($uuid)) { + throw new InvalidArgumentException( + sprintf('There are no registered promises for UUID %s', $uuid->toString()) + ); + } + + foreach ($this->promises[$uuid] as $k => $v) { + if ($v === $promise) { + unset($this->promises[$uuid][$k]); + + return $this; + } + } + + throw new InvalidArgumentException( + sprintf('There is no such promise for UUID %s', $uuid->toString()) + ); + } + + /** + * Detach and return promises for the given UUID, if any + * + * **Example Usage:** + * + * ```php + * foreach ($promises->detachPromises($uuid) as $promise) { + * $promise->cancel(); + * } + * ``` + * + * @param UuidInterface $uuid + * + * @return PromiseInterface[] + */ + protected function detachPromises(UuidInterface $uuid): array + { + if (! $this->promises->contains($uuid)) { + return []; + } + + $promises = $this->promises[$uuid]; + $this->promises->detach($uuid); + + return $promises->getArrayCopy(); + } +} diff --git a/vendor/ipl/scheduler/src/Common/TaskProperties.php b/vendor/ipl/scheduler/src/Common/TaskProperties.php new file mode 100644 index 0000000..4ab65e2 --- /dev/null +++ b/vendor/ipl/scheduler/src/Common/TaskProperties.php @@ -0,0 +1,83 @@ +<?php + +namespace ipl\Scheduler\Common; + +use LogicException; +use Ramsey\Uuid\UuidInterface; + +trait TaskProperties +{ + /** @var string */ + protected $description; + + /** @var string Name of this task */ + protected $name; + + /** @var UuidInterface Unique identifier of this task */ + protected $uuid; + + /** + * Set the description of this task + * + * @param ?string $desc + * + * @return $this + */ + public function setDescription(?string $desc): self + { + $this->description = $desc; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function getName(): string + { + if (! $this->name) { + throw new LogicException('Task name must not be null'); + } + + return $this->name; + } + + /** + * Set the name of this Task + * + * @param string $name + * + * @return $this + */ + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getUuid(): UuidInterface + { + if (! $this->uuid) { + throw new LogicException('Task UUID must not be null'); + } + + return $this->uuid; + } + + /** + * Set the UUID of this task + * + * @param UuidInterface $uuid + * + * @return $this + */ + public function setUuid(UuidInterface $uuid): self + { + $this->uuid = $uuid; + + return $this; + } +} diff --git a/vendor/ipl/scheduler/src/Common/Timers.php b/vendor/ipl/scheduler/src/Common/Timers.php new file mode 100644 index 0000000..2d0641f --- /dev/null +++ b/vendor/ipl/scheduler/src/Common/Timers.php @@ -0,0 +1,60 @@ +<?php + +namespace ipl\Scheduler\Common; + +use Ramsey\Uuid\UuidInterface; +use React\EventLoop\TimerInterface; +use SplObjectStorage; + +trait Timers +{ + /** @var SplObjectStorage<UuidInterface, TimerInterface> */ + protected $timers; + + /** + * Set a timer for the given UUID + * + * **Example Usage:** + * + * ```php + * $timers->attachTimer($uuid, Loop::addTimer($interval, $callback)); + * ``` + * + * @param UuidInterface $uuid + * @param TimerInterface $timer + * + * @return $this + */ + protected function attachTimer(UuidInterface $uuid, TimerInterface $timer): self + { + $this->timers->attach($uuid, $timer); + + return $this; + } + + /** + * Detach and return the timer for the given UUID, if any + * + * **Example Usage:** + * + * ```php + * Loop::cancelTimer($timers->detachTimer($uuid)); + * ``` + * + * @param UuidInterface $uuid + * + * @return ?TimerInterface + */ + protected function detachTimer(UuidInterface $uuid): ?TimerInterface + { + if (! $this->timers->contains($uuid)) { + return null; + } + + $timer = $this->timers->offsetGet($uuid); + + $this->timers->detach($uuid); + + return $timer; + } +} |