Skip to content

Commit

Permalink
Add Context class to increase the developer experience when adding cr…
Browse files Browse the repository at this point in the history
…on jobs
  • Loading branch information
loevgaard committed Sep 17, 2024
1 parent 3a0345a commit edd1a56
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ") }}');
}
};
Expand Down
81 changes: 81 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Setono\CronBuilder;

/**
* @implements \ArrayAccess<string, mixed>
* @implements \IteratorAggregate<string, mixed>
*/
final class Context implements \ArrayAccess, \IteratorAggregate, \Countable
{
public function __construct(
/** @var array<string, mixed> $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;
}
}
29 changes: 16 additions & 13 deletions src/CronBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ final class CronBuilder
*/
private array $files = [];

/** @var array<string, mixed> */
private array $context = [];
private readonly Context $context;

public function __construct(Environment $twig = null)
{
$this->twig = $twig ?? new Environment(new ArrayLoader());
$this->context = new Context();
}

/**
Expand Down Expand Up @@ -77,28 +77,31 @@ 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<string, mixed> $context
*/
public function setContext(array $context): self
{
/**
* @var mixed $value
*/
/** @var mixed $value */
foreach ($context as $key => $value) {
$this->addContext($key, $value);
}

return $this;
}

public function addContext(string $key, mixed $value): self
{
$this->context[$key] = $value;

return $this;
}

/**
* Returns a valid crontab string
*/
Expand Down Expand Up @@ -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());
}
}
5 changes: 3 additions & 2 deletions tests/cronjobs/jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ") }}');
}
};

0 comments on commit edd1a56

Please sign in to comment.