From 2e48eb886e0b67fc460d203d8bd1abeb7faa1bc6 Mon Sep 17 00:00:00 2001 From: Matthias von Bargen Date: Mon, 17 Jun 2024 18:54:22 +0200 Subject: [PATCH] add event handler configuration (#20) --- docker/caddy/Dockerfile | 3 +- src/Config/Apps/Events.php | 33 ++++++++++ src/Config/Apps/Events/Handlers/Exec.php | 65 +++++++++++++++++++ src/Config/Apps/Events/Subscription.php | 46 +++++++++++++ .../Apps/Events/HandlerInterface.php | 12 ++++ tests/Integration/CaddyTest.php | 29 +++++++++ tests/Unit/Apps/EventTest.php | 60 +++++++++++++++++ 7 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 src/Config/Apps/Events.php create mode 100644 src/Config/Apps/Events/Handlers/Exec.php create mode 100644 src/Config/Apps/Events/Subscription.php create mode 100644 src/Interfaces/Apps/Events/HandlerInterface.php create mode 100644 tests/Unit/Apps/EventTest.php diff --git a/docker/caddy/Dockerfile b/docker/caddy/Dockerfile index d1b2f86..2718bf7 100644 --- a/docker/caddy/Dockerfile +++ b/docker/caddy/Dockerfile @@ -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 diff --git a/src/Config/Apps/Events.php b/src/Config/Apps/Events.php new file mode 100644 index 0000000..438da3c --- /dev/null +++ b/src/Config/Apps/Events.php @@ -0,0 +1,33 @@ +subscriptions = $subscriptions; + } + + public function toArray(): array + { + return [ + 'subscriptions' => [ + ...array_map(function (Subscription $subscription): array { + return $subscription->toArray(); + }, $this->subscriptions), + ], + ]; + } +} diff --git a/src/Config/Apps/Events/Handlers/Exec.php b/src/Config/Apps/Events/Handlers/Exec.php new file mode 100644 index 0000000..1d3e356 --- /dev/null +++ b/src/Config/Apps/Events/Handlers/Exec.php @@ -0,0 +1,65 @@ +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'; + } +} diff --git a/src/Config/Apps/Events/Subscription.php b/src/Config/Apps/Events/Subscription.php new file mode 100644 index 0000000..cb480c7 --- /dev/null +++ b/src/Config/Apps/Events/Subscription.php @@ -0,0 +1,46 @@ +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), + ]; + } +} diff --git a/src/Interfaces/Apps/Events/HandlerInterface.php b/src/Interfaces/Apps/Events/HandlerInterface.php new file mode 100644 index 0000000..3391281 --- /dev/null +++ b/src/Interfaces/Apps/Events/HandlerInterface.php @@ -0,0 +1,12 @@ +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 */ diff --git a/tests/Unit/Apps/EventTest.php b/tests/Unit/Apps/EventTest.php new file mode 100644 index 0000000..e623aa1 --- /dev/null +++ b/tests/Unit/Apps/EventTest.php @@ -0,0 +1,60 @@ +assertEquals( + [ + 'subscriptions' => [ + [ + 'events' => [], + 'modules' => [], + 'handlers' => [ + [ + 'handler' => 'exec', + 'command' => 'datetime', + 'args' => [], + 'dir' => '/tmp', + 'timeout' => 0, + 'foreground' => false, + 'abort_codes' => [], + ], + ], + ], + ], + ], + $events->toArray() + ); + } + +} \ No newline at end of file