Skip to content

Commit

Permalink
AutoExit configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mlebkowski committed Nov 25, 2024
1 parent 85a96d4 commit 9ddb6f6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/Cli/AutoExit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace WonderNetwork\SlimKernel\Cli;

use DI\Definition\Definition;
use function DI\value;

final class AutoExit {
private bool $value;

public static function yes(): Definition {
return value(new self(true));
}

public static function no(): Definition {
return value(new self(false));
}

private function __construct(bool $value) {
$this->value = $value;
}

public function value(): bool {
return $this->value;
}
}
4 changes: 3 additions & 1 deletion src/ServiceFactory/SymfonyConsoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DI\Definition\Helper\CreateDefinitionHelper;
use Symfony\Component\Console;
use WonderNetwork\SlimKernel\Cli\AutoExit;
use WonderNetwork\SlimKernel\ServiceFactory;
use WonderNetwork\SlimKernel\ServicesBuilder;
use function DI\autowire;
Expand All @@ -22,14 +23,15 @@ public function __construct(string $path = '/src/Cli/**/*Command.php', string $n

public function __invoke(ServicesBuilder $builder): iterable {
yield from $commands = $builder->autowire()->glob($this->path);
yield AutoExit::class => AutoExit::no();

yield Console\Application::class => collection($commands)
->keys()
->reduce(
static fn(CreateDefinitionHelper $def, string $command) => $def->method('add', get($command)),
autowire()
->constructor($this->name)
->method('setAutoExit', false),
->method('setAutoExit', static fn (AutoExit $autoExit) => $autoExit->value()),
);
}
}
2 changes: 1 addition & 1 deletion tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function test(): void {
$commands = $actual
->keys()
->reverse()
->drop(1)
->drop(count(['Application', 'AutoExit']))
->reverse()
->toArray();

Expand Down
5 changes: 3 additions & 2 deletions tests/StartupHook/ErrorHandlingHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

use Acme\ErrorHandlingSpy;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Psr7\Factory\ServerRequestFactory;
use Throwable;
use WonderNetwork\SlimKernel\KernelBuilder;

class ErrorHandlingHookTest extends TestCase {
public function testCustomMiddlewaresTakePrecedence() {
public function testCustomMiddlewaresTakePrecedence(): void {
$container = KernelBuilder::start(__DIR__.'/../Resources/ErrorMiddleware')
->glob('app/services/*.php')
->onStartup(
Expand All @@ -20,7 +21,7 @@ public function testCustomMiddlewaresTakePrecedence() {
)
->build();

/** @var App $app */
/** @var App<ContainerInterface> $app */
$app = $container->get(App::class);
/** @var ErrorHandlingSpy $spy */
$spy = $container->get(ErrorHandlingSpy::class);
Expand Down
3 changes: 2 additions & 1 deletion tests/StartupHook/RoutesStartupHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
namespace WonderNetwork\SlimKernel\StartupHook;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Psr7\Factory\ServerRequestFactory;
use WonderNetwork\SlimKernel\KernelBuilder;

class RoutesStartupHookTest extends TestCase {
public function test(): void {
/** @var App $app */
/** @var App<ContainerInterface> $app */
$app = KernelBuilder::start(__DIR__.'/../Resources/App')
->glob('app/services/*.php')
->onStartup(
Expand Down

0 comments on commit 9ddb6f6

Please sign in to comment.