Skip to content

Commit

Permalink
graceful setup and start
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Aug 13, 2024
1 parent 309bbc8 commit f3751b3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 40 deletions.
16 changes: 9 additions & 7 deletions src/Xssless.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ public function clean(string $html, ?ConfigInterface $tempConfig = null): string
};
}

public function start(?ConfigInterface $tempConfig = null): ServiceInterface
public function start(?ConfigInterface $tempConfig = null): ?ServiceInterface
{
$service = $this->makeCleaner($tempConfig);

if (! $service instanceof ServiceInterface) {
throw new XsslessException("'".$service::class."' must implement: '".ServiceInterface::class."'.");
return null;
}

return $service->start();
}

public function setup(?ConfigInterface $tempConfig = null): void
public function setup(?ConfigInterface $tempConfig = null): bool
{
$service = $this->makeCleaner($tempConfig);
$cleaner = $this->makeCleaner($tempConfig);

if (! $service instanceof HasSetupInterface) {
throw new XsslessException("'".$service::class."' must implement: '".HasSetupInterface::class."'.");
if (! $cleaner instanceof HasSetupInterface) {
return false;
}

$service->setup();
$cleaner->setup();

return true;
}

public function usingLaravelConfig(): static
Expand Down
4 changes: 3 additions & 1 deletion src/laravel/Commands/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class SetupCommand extends Command
public function handle(): void
{
// TODO: non Laravel command
Xssless::usingLaravelConfig()->setup();
Xssless::usingLaravelConfig()->setup() ?
$this->info('Setup done.') :
$this->info('The current driver has no setup.');
}
}
31 changes: 23 additions & 8 deletions src/laravel/Commands/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Medilies\Xssless\Laravel\Commands;

use Illuminate\Console\Command;
use Medilies\Xssless\Interfaces\ServiceInterface;
use Medilies\Xssless\Laravel\Facades\Xssless;

class StartCommand extends Command
Expand All @@ -18,15 +19,13 @@ public function handle(): void
// TODO: non Laravel command
$service = Xssless::usingLaravelConfig()->start();

$terminate = function ($signal) use ($service) {
$this->warn("Terminating...\n");
$service->stop();
exit;
};
if (is_null($service)) {
$this->info('The current driver is not a service to start.');

// ? Is this necessary
pcntl_signal(SIGTERM, $terminate);
pcntl_signal(SIGINT, $terminate);
return;
}

$this->onTermination($service);

while ($service->isRunning()) {
$output = $service->getIncrementalOutput();
Expand All @@ -44,4 +43,20 @@ public function handle(): void
usleep(100_000);
}
}

private function onTermination(ServiceInterface $service): void
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || ! extension_loaded('pcntl')) {
return;
}

$terminate = function ($signal) use ($service) {
$this->warn("Terminating...\n");
$service->stop();
exit;
};

pcntl_signal(SIGTERM, $terminate);
pcntl_signal(SIGINT, $terminate);
}
}
4 changes: 2 additions & 2 deletions src/laravel/Facades/Xssless.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

/**
* @method static string clean(string $html, ?ConfigInterface $config = null)
* @method static ServiceInterface start(?ConfigInterface $config = null)
* @method static void setup(?ConfigInterface $config = null)
* @method static ?ServiceInterface start(?ConfigInterface $config = null)
* @method static bool setup(?ConfigInterface $config = null)
* @method static static usingLaravelConfig()
* @method static static using(ConfigInterface $config)
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Dompurify/DompurifyCliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
test('setup()', function () {
$cleaner = (new Xssless)->using(new DompurifyCliConfig);

expect(fn () => $cleaner->setup())->not->toThrow(Exception::class);
expect($cleaner->setup())->toBeTrue();
});

test('exec()', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/Dompurify/DompurifyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
// ----------------------------------------------------------------------------

test('setup()', function () {
$cleaner = (new DompurifyService)->configure(new DompurifyServiceConfig);
$cleaner = (new Xssless)->using(new DompurifyServiceConfig);

expect(fn () => $cleaner->setup())->not->toThrow(Exception::class);
expect($cleaner->setup())->toBeTrue();
});

test('send()', function () {
Expand Down
46 changes: 27 additions & 19 deletions tests/XsslessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
use Medilies\Xssless\Interfaces\ConfigInterface;
use Medilies\Xssless\Xssless;

it('throws when makeCleaner() with no config', function () {
// ----------------------------------------------------------------------------
// makeCleaner()
// ----------------------------------------------------------------------------

it('throws when makeCleaner() with no interface', function () {
$cleaner = new Xssless;

$cleaner->using(new class implements ConfigInterface
Expand All @@ -20,22 +24,39 @@ public function getClass(): string
expect(fn () => $cleaner->clean('foo'))->toThrow(XsslessException::class);
});

it('throws when makeCleaner() with no interface', function () {
it('throws when makeCleaner() with no config', function () {
$cleaner = new Xssless;

expect(fn () => $cleaner->clean('foo'))->toThrow(XsslessException::class);
});

it('throws when start() with CliInterface', function () {
// ----------------------------------------------------------------------------
// return gracefully when interface not implemented
// ----------------------------------------------------------------------------

it('returns null when start() without ServiceInterface', function () {
$cleaner = new Xssless;
$cleaner->using(new DompurifyCliConfig);

expect(fn () => $cleaner->start())->toThrow(XsslessException::class);
expect($cleaner->start())->toBeNull();
});

it('throws when setup() without HasSetupInterface', function () {
it('returns false when setup() without HasSetupInterface', function () {
$cleaner = new Xssless;

class NoSetupDriver implements CliInterface
{
public function configure(ConfigInterface $config): static
{
return $this;
}

public function exec(string $html): string
{
return '';
}
}

$cleaner->using(new class implements ConfigInterface
{
public function getClass(): string
Expand All @@ -44,18 +65,5 @@ public function getClass(): string
}
});

expect(fn () => $cleaner->setup())->toThrow(XsslessException::class);
expect($cleaner->setup())->toBeFalse();
});

class NoSetupDriver implements CliInterface
{
public function configure(ConfigInterface $config): static
{
return $this;
}

public function exec(string $html): string
{
return '';
}
}

0 comments on commit f3751b3

Please sign in to comment.