diff --git a/README.md b/README.md index 0a38151..8eb1245 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,13 @@ The following two code snippets outlines the simplest usage of the cron builder. declare(strict_types=1); +use Setono\CronBuilder\Context; use Setono\CronBuilder\CronJob; -return static function (array $context): iterable { +return static function (Context $context): iterable { yield new CronJob('0 0 * * *', '/usr/bin/php {{ release_path }}/send-report.php {{ args|join(" ") }}', 'Run every day at midnight'); - if ($context['env'] ?? '' === 'prod') { + if ($context->get('env') === 'prod') { yield new CronJob('0 0 * * *', '/usr/bin/php {{ release_path }}/process.php {{ args|join(" ") }}'); } }; diff --git a/src/Context.php b/src/Context.php new file mode 100644 index 0000000..fda96c9 --- /dev/null +++ b/src/Context.php @@ -0,0 +1,81 @@ + + * @implements \IteratorAggregate + */ +final class Context implements \ArrayAccess, \IteratorAggregate, \Countable +{ + public function __construct( + /** @var array $context */ + private array $context = [], + ) { + } + + public function has(string $key): bool + { + return array_key_exists($key, $this->context); + } + + public function get(string $key, mixed $default = null): mixed + { + if (!$this->has($key)) { + return $default; + } + + return $this->context[$key]; + } + + public function set(string $key, mixed $value): void + { + $this->context[$key] = $value; + } + + public function offsetExists($offset): bool + { + return $this->has($offset); + } + + public function offsetGet($offset): mixed + { + return $this->get($offset); + } + + public function offsetSet($offset, $value): void + { + if (null === $offset) { + throw new \InvalidArgumentException('The offset cannot be null'); + } + + $this->set($offset, $value); + } + + public function offsetUnset($offset): void + { + unset($this->context[$offset]); + } + + public function getIterator(): \ArrayIterator + { + return new \ArrayIterator($this->context); + } + + public function count(): int + { + return count($this->context); + } + + public function toArray(): array + { + return $this->context; + } + + public function isEmpty(): bool + { + return [] === $this->context; + } +} diff --git a/src/CronBuilder.php b/src/CronBuilder.php index bf4dd13..dd70c6d 100644 --- a/src/CronBuilder.php +++ b/src/CronBuilder.php @@ -20,12 +20,12 @@ final class CronBuilder */ private array $files = []; - /** @var array */ - private array $context = []; + private readonly Context $context; public function __construct(Environment $twig = null) { $this->twig = $twig ?? new Environment(new ArrayLoader()); + $this->context = new Context(); } /** @@ -77,14 +77,24 @@ public function addFiles(iterable $files): self return $this; } + public function context(): Context + { + return $this->context; + } + + public function addContext(string $key, mixed $value): self + { + $this->context->set($key, $value); + + return $this; + } + /** * @param array $context */ public function setContext(array $context): self { - /** - * @var mixed $value - */ + /** @var mixed $value */ foreach ($context as $key => $value) { $this->addContext($key, $value); } @@ -92,13 +102,6 @@ public function setContext(array $context): self return $this; } - public function addContext(string $key, mixed $value): self - { - $this->context[$key] = $value; - - return $this; - } - /** * Returns a valid crontab string */ @@ -176,6 +179,6 @@ public static function merge(string $existingCron, self $cronBuilder): string private function parse(string $value): string { - return $this->twig->createTemplate($value)->render($this->context); + return $this->twig->createTemplate($value)->render($this->context->toArray()); } } diff --git a/tests/cronjobs/jobs.php b/tests/cronjobs/jobs.php index 9acd338..8f449e7 100644 --- a/tests/cronjobs/jobs.php +++ b/tests/cronjobs/jobs.php @@ -2,12 +2,13 @@ declare(strict_types=1); +use Setono\CronBuilder\Context; use Setono\CronBuilder\CronJob; -return static function (array $context): iterable { +return static function (Context $context): iterable { yield new CronJob('0 0 * * *', '/usr/bin/php {{ release_path }}/send-report.php {{ args|join(" ") }}', 'Run every day at midnight'); - if ($context['env'] ?? '' === 'prod') { + if ($context->get('env') === 'prod') { yield new CronJob('0 0 * * *', '/usr/bin/php {{ release_path }}/process.php {{ args|join(" ") }}'); } };