Skip to content

Commit a979101

Browse files
authored
Merge branch 'main' into fix/invalid-closing-tag-commented-out-code
2 parents dfa35c7 + 8d0d780 commit a979101

40 files changed

+299
-99
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/package-lock.json
44

55
# Top-level+Sub directories
6+
.tempest/
67
.cache/
78
.idea/
89
build/
@@ -27,4 +28,4 @@ dist
2728
profile/
2829
db-main.sqlite
2930
db-tenant-1.sqlite
30-
db-tenant-2.sqlite
31+
db-tenant-2.sqlite

packages/core/src/AppConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ final class AppConfig
1313
public string $baseUri;
1414

1515
public function __construct(
16+
public ?string $name = null,
17+
1618
?Environment $environment = null,
1719

1820
?string $baseUri = null,
@@ -26,7 +28,6 @@ public function __construct(
2628
public array $insightsProviders = [],
2729
) {
2830
$this->environment = $environment ?? Environment::fromEnv();
29-
3031
$this->baseUri = $baseUri ?? env('BASE_URI') ?? '';
3132
}
3233
}

packages/core/src/Config/app.config.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44

55
use Tempest\Core\AppConfig;
66

7-
return new AppConfig();
7+
use function Tempest\env;
8+
9+
return new AppConfig(
10+
name: env('APPLICATION_NAME'),
11+
baseUri: env('BASE_URI'),
12+
);

packages/core/src/FrameworkKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function loadConfig(): self
166166

167167
public function registerInternalStorage(): self
168168
{
169-
$path = $this->root . '/vendor/.tempest';
169+
$path = $this->root . '/.tempest';
170170

171171
if (! is_dir($path)) {
172172
mkdir($path, recursive: true);

packages/event-bus/src/EventBus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ interface EventBus
1010
{
1111
public function dispatch(string|object $event): void;
1212

13-
public function listen(string|object $event, Closure $handler): void;
13+
public function listen(Closure $handler, ?string $event = null): void;
1414
}

packages/event-bus/src/EventBusConfig.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Tempest\EventBus;
66

77
use Closure;
8+
use InvalidArgumentException;
89
use Tempest\Core\Middleware;
10+
use Tempest\Reflection\FunctionReflector;
911
use Tempest\Reflection\MethodReflector;
1012

1113
final class EventBusConfig
@@ -18,8 +20,17 @@ public function __construct(
1820
public Middleware $middleware = new Middleware(),
1921
) {}
2022

21-
public function addClosureHandler(string $event, Closure $handler): self
23+
public function addClosureHandler(Closure $handler, ?string $event = null): self
2224
{
25+
$event ??= new FunctionReflector($handler)
26+
->getParameter(key: 0)
27+
?->getType()
28+
->getName();
29+
30+
if (! $event) {
31+
throw new InvalidArgumentException('The first parameter of the closure must be the event name or type.');
32+
}
33+
2334
$handlerKey = spl_object_hash($handler);
2435

2536
$this->handlers[$event][$handlerKey] = new CallableEventHandler(

packages/event-bus/src/GenericEventBus.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function __construct(
1616
private EventBusConfig $eventBusConfig,
1717
) {}
1818

19-
public function listen(string|object $event, Closure $handler): void
19+
public function listen(Closure $handler, ?string $event = null): void
2020
{
21-
$this->eventBusConfig->addClosureHandler($event, $handler);
21+
$this->eventBusConfig->addClosureHandler($handler, $event);
2222
}
2323

2424
public function dispatch(string|object $event): void

packages/event-bus/src/Testing/FakeEventBus.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public function __construct(
1414
public EventBusConfig $eventBusConfig,
1515
) {}
1616

17-
public function listen(string|object $event, Closure $handler): void
17+
public function listen(Closure $handler, ?string $event = null): void
1818
{
19-
$this->eventBusConfig->addClosureHandler($event, $handler);
19+
$this->eventBusConfig->addClosureHandler($handler, $event);
2020
}
2121

2222
public function dispatch(string|object $event): void

packages/event-bus/src/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ function event(string|object $event): void
2020
/**
2121
* Registers a closure-based event listener for the given `$event`.
2222
*/
23-
function listen(string|object $event, Closure $handler): void
23+
function listen(Closure $handler, ?string $event = null): void
2424
{
2525
$config = get(EventBusConfig::class);
2626

27-
$config->addClosureHandler($event, $handler);
27+
$config->addClosureHandler($handler, $event);
2828
}
2929
}

packages/event-bus/tests/EventBusTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,32 @@ public function test_closure_based_handlers_using_listen_method(): void
126126
$hasHappened = false;
127127

128128
// @mago-expect best-practices/no-unused-parameter
129-
$eventBus->listen('my-event', function (string $event) use (&$hasHappened): void {
129+
$eventBus->listen(function (string $event) use (&$hasHappened): void {
130130
$hasHappened = true;
131-
});
131+
}, event: 'my-event');
132132

133133
$eventBus->dispatch('my-event');
134134

135135
$this->assertTrue($hasHappened);
136136
}
137137

138+
public function test_closure_based_handlers_using_listen_method_and_first_parameter(): void
139+
{
140+
$container = new GenericContainer();
141+
$config = new EventBusConfig();
142+
$eventBus = new GenericEventBus($container, $config);
143+
$hasHappened = false;
144+
145+
// @mago-expect best-practices/no-unused-parameter
146+
$eventBus->listen(function (ItHappened $event) use (&$hasHappened): void {
147+
$hasHappened = true;
148+
});
149+
150+
$eventBus->dispatch(new ItHappened());
151+
152+
$this->assertTrue($hasHappened);
153+
}
154+
138155
public function test_closure_based_handlers_using_function(): void
139156
{
140157
GenericContainer::setInstance($container = new GenericContainer());
@@ -145,9 +162,9 @@ public function test_closure_based_handlers_using_function(): void
145162
$hasHappened = false;
146163

147164
// @mago-expect best-practices/no-unused-parameter
148-
listen('my-event', function (string $event) use (&$hasHappened): void {
165+
listen(function (string $event) use (&$hasHappened): void {
149166
$hasHappened = true;
150-
});
167+
}, event: 'my-event');
151168

152169
get(EventBus::class)->dispatch('my-event');
153170

0 commit comments

Comments
 (0)