Skip to content

Commit

Permalink
add event handler configuration (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvb91 authored Jun 17, 2024
1 parent 016ec89 commit 2e48eb8
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docker/caddy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM caddy:2.8.4-builder-alpine

RUN xcaddy build --with github.com/darkweak/souin/plugins/caddy@a2e88383a2bced983914cc12bde413ad8887b56d \
--with github.com/darkweak/souin@a2e88383a2bced983914cc12bde413ad8887b56d
--with github.com/darkweak/souin@a2e88383a2bced983914cc12bde413ad8887b56d \
--with github.com/mholt/caddy-events-exec

# See https://caddyserver.com/docs/conventions#file-locations for details
ENV XDG_CONFIG_HOME /config
Expand Down
33 changes: 33 additions & 0 deletions src/Config/Apps/Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps;

use mattvb91\CaddyPhp\Config\Apps\Events\Subscription;
use mattvb91\CaddyPhp\Interfaces\App;

class Events implements App
{
/** @var Subscription[] */
private array $subscriptions;

/**
* @param Subscription[] $subscriptions
*/
public function __construct(array $subscriptions = [])
{
$this->subscriptions = $subscriptions;
}

public function toArray(): array
{
return [
'subscriptions' => [
...array_map(function (Subscription $subscription): array {
return $subscription->toArray();
}, $this->subscriptions),
],
];
}
}
65 changes: 65 additions & 0 deletions src/Config/Apps/Events/Handlers/Exec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Events\Handlers;

use mattvb91\CaddyPhp\Interfaces\Apps\Events\HandlerInterface;

/**
* https://caddyserver.com/docs/json/apps/events/subscriptions/handlers/exec/
*/
class Exec implements HandlerInterface
{
private string $command;

/** @var string[] */
private array $args;

private string $dir;

private int $timeout;

private bool $foreground;

/** @var int[] */
private array $abort_codes;

/**
* @param string[] $args
* @param int[] $abort_codes
*/
public function __construct(
string $command,
array $args,
string $dir,
int $timeout = 0,
bool $foreground = false,
array $abort_codes = [0]
) {
$this->command = $command;
$this->args = $args;
$this->dir = $dir;
$this->timeout = $timeout;
$this->foreground = $foreground;
$this->abort_codes = $abort_codes;
}

public function toArray(): array
{
return [
'handler' => $this->getHandler(),
"command" => $this->command,
"args" => $this->args,
"dir" => $this->dir,
"timeout" => $this->timeout,
"foreground" => $this->foreground,
"abort_codes" => $this->abort_codes,
];
}

public function getHandler(): string
{
return 'exec';
}
}
46 changes: 46 additions & 0 deletions src/Config/Apps/Events/Subscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Apps\Events;

use mattvb91\CaddyPhp\Interfaces\Apps\Events\HandlerInterface;
use mattvb91\CaddyPhp\Interfaces\Arrayable;

class Subscription implements Arrayable
{
/** @var string[] */
private array $events;

/** @var string [] */
private array $modules;

/** @var HandlerInterface[] */
private array $handlers;

/**
* @param string[] $events
* @param string[] $modules
* @param HandlerInterface[] $handlers
*/
public function __construct(
array $events = [],
array $modules = [],
array $handlers = []
) {
$this->events = $events;
$this->modules = $modules;
$this->handlers = $handlers;
}

public function toArray(): array
{
return [
'events' => $this->events,
'modules' => $this->modules,
'handlers' => array_map(function (HandlerInterface $handler): array {
return $handler->toArray();
}, $this->handlers),
];
}
}
12 changes: 12 additions & 0 deletions src/Interfaces/Apps/Events/HandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Interfaces\Apps\Events;

use mattvb91\CaddyPhp\Interfaces\Arrayable;

interface HandlerInterface extends Arrayable
{
public function getHandler(): string;
}
29 changes: 29 additions & 0 deletions tests/Integration/CaddyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use mattvb91\CaddyPhp\Caddy;
use mattvb91\CaddyPhp\Config\Apps\Events;
use mattvb91\CaddyPhp\Config\Apps\Http;
use mattvb91\CaddyPhp\Config\Apps\Http\Server\Route;
use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Handle\Authentication;
Expand Down Expand Up @@ -61,6 +62,34 @@ public function testCanLoadWithHttpApp(): void
$this->assertTrue($caddy->load());
}

/**
* @coversNothing
*/
public function testCanLoadWithEventsApp()
{
$caddy = new Caddy();
$caddy->addApp(
(new Events([
(new Events\Subscription(
handlers: [
new Events\Handlers\Exec(
"datetime",
[],
"/tmp",
0,
false,
[]
),
]
)),
]))
);

$this->assertTrue(
$caddy->load()
);
}

/**
* @coversNothing
*/
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/Apps/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tests\Unit\Apps;

use mattvb91\CaddyPhp\Config\Apps\Events;
use Tests\TestCase;

class EventTest extends TestCase
{

/**
* @covers \mattvb91\CaddyPhp\Config\Apps\Events::toArray
* @covers \mattvb91\CaddyPhp\Config\Apps\Events::__construct
* @covers \mattvb91\CaddyPhp\Config\Apps\Events\Subscription::toArray
* @covers \mattvb91\CaddyPhp\Config\Apps\Events\Subscription::__construct
* @covers \mattvb91\CaddyPhp\Config\Apps\Events\Handlers\Exec::toArray
* @covers \mattvb91\CaddyPhp\Config\Apps\Events\Handlers\Exec::__construct
*/
public function testAddingExecEvent()
{
$events = (new Events([
(new Events\Subscription(
handlers: [
new Events\Handlers\Exec(
"datetime",
[],
"/tmp",
0,
false,
[]
),
]
)),
]));

$this->assertEquals(
[
'subscriptions' => [
[
'events' => [],
'modules' => [],
'handlers' => [
[
'handler' => 'exec',
'command' => 'datetime',
'args' => [],
'dir' => '/tmp',
'timeout' => 0,
'foreground' => false,
'abort_codes' => [],
],
],
],
],
],
$events->toArray()
);
}

}

0 comments on commit 2e48eb8

Please sign in to comment.