-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add event handler configuration (#20)
- Loading branch information
Showing
7 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
|
||
} |