Skip to content

Commit

Permalink
Simplified the cron builder even further
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Mar 3, 2025
1 parent 76d53d8 commit 023faa7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
"require": {
"php": ">=8.1",
"dragonmantank/cron-expression": "^3.0",
"symfony/finder": "^6.4 || ^7.0",
"twig/twig": "^2.0 || ^3.0"
},
"require-dev": {
"infection/infection": "^0.27.11",
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.19.0",
"setono/code-quality-pack": "^2.8.2",
"shipmonk/composer-dependency-analyser": "^1.8.2",
"symfony/finder": "^6.4 || ^7.0"
"shipmonk/composer-dependency-analyser": "^1.8.2"

},
"prefer-stable": true,
"autoload": {
Expand Down
8 changes: 6 additions & 2 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ public function get(string $key, mixed $default = null): mixed
return $this->context[$key];
}

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

return $this;
}

public function remove(string $key): void
public function remove(string $key): self
{
unset($this->context[$key]);

return $this;
}

public function offsetExists($offset): bool
Expand Down
60 changes: 23 additions & 37 deletions src/CronBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace Setono\CronBuilder;

use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

final class CronBuilder
{
private readonly Environment $twig;

private string $delimiter = 'Automatically generated by Setono Cron Builder - DO NOT EDIT';
public string $delimiter = 'Automatically generated by Setono Cron Builder - DO NOT EDIT';

/**
* List of file names with cron job definitions
Expand All @@ -20,22 +21,28 @@ final class CronBuilder
*/
private array $files = [];

private readonly Context $context;

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

/**
* @param non-empty-string $delimiter
* @param string|list<string> $directories
*/
public function setDelimiter(string $delimiter): self
public function __construct(string|array $directories = [])
{
$this->delimiter = $delimiter;
$this->twig = new Environment(new ArrayLoader());
$this->context = new Context();

return $this;
if (is_string($directories)) {
$directories = [$directories];
}

if ([] !== $directories) {
$this->addFiles(
(new Finder())
->files()
->in($directories)
->name('*.php'),
);
}
}

public function addFile(string|\SplFileInfo $file): self
Expand All @@ -48,6 +55,10 @@ public function addFile(string|\SplFileInfo $file): self
throw new \InvalidArgumentException(sprintf('The file %s does not exist', $file));
}

if (in_array($file, $this->files, true)) {
return $this;
}

$this->files[] = $file;

return $this;
Expand All @@ -65,31 +76,6 @@ 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 */
foreach ($context as $key => $value) {
$this->addContext($key, $value);
}

return $this;
}

/**
* Returns a valid crontab string
*/
Expand Down
29 changes: 17 additions & 12 deletions tests/CronBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Setono\CronBuilder;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;

final class CronBuilderTest extends TestCase
{
Expand All @@ -31,10 +30,7 @@ public function it_builds(): void
*/
public function it_builds_with_prod_env_set(): void
{
$crontab = self::getCronBuilder()
->addContext('env', 'prod')
->build()
;
$crontab = self::getCronBuilder(['env' => 'prod'])->build();

$expected = <<<CRON
###> Generated by PHPUnit ###
Expand Down Expand Up @@ -104,16 +100,25 @@ public function invalidJobsFileProvider(): \Generator
yield ['jobs4.php'];
}

private static function getCronBuilder(): CronBuilder
/**
* @param array<string, mixed> $context
*/
private static function getCronBuilder(array $context = []): CronBuilder
{
$cronBuilder = new CronBuilder();
$cronBuilder->setDelimiter('Generated by PHPUnit')
->addFiles((new Finder())->files()->in(__DIR__ . '/cronjobs'))
->addContext('release_path', '/home/johndoe/public_html')
->setContext(['args' => ['--verbose']])
$cronBuilder = new CronBuilder(__DIR__ . '/cronjobs');
$cronBuilder->delimiter = 'Generated by PHPUnit';

$cronBuilder
->context
->set('release_path', '/home/johndoe/public_html')
->set('args', '--verbose')
->set('release_number', 12)
;

$cronBuilder->context()->set('release_number', 12);
/** @var mixed $value */
foreach ($context as $key => $value) {
$cronBuilder->context->set($key, $value);
}

return $cronBuilder;
}
Expand Down

0 comments on commit 023faa7

Please sign in to comment.