Skip to content

Commit 57e0d28

Browse files
authored
Merge pull request #1037 from hydephp/add-kernel-callbacks
Add HydeKernel booting callback API
2 parents 8298e25 + 5523291 commit 57e0d28

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

packages/framework/src/Foundation/Concerns/BootsHydeKernel.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ trait BootsHydeKernel
1818
private bool $readyToBoot = false;
1919
private bool $booting = false;
2020

21+
/** @var array<callable> */
22+
protected array $bootingCallbacks = [];
23+
24+
/** @var array<callable> */
25+
protected array $bootedCallbacks = [];
26+
27+
/**
28+
* Boot the Hyde Kernel and run the Auto-Discovery Process.
29+
*/
2130
public function boot(): void
2231
{
2332
if (! $this->readyToBoot || $this->booting) {
@@ -30,14 +39,50 @@ public function boot(): void
3039
$this->pages = PageCollection::init($this);
3140
$this->routes = RouteCollection::init($this);
3241

42+
foreach ($this->bootingCallbacks as $callback) {
43+
$callback($this);
44+
}
45+
3346
$this->files->boot();
3447
$this->pages->boot();
3548
$this->routes->boot();
3649

50+
foreach ($this->bootedCallbacks as $callback) {
51+
$callback($this);
52+
}
53+
3754
$this->booting = false;
3855
$this->booted = true;
3956
}
4057

58+
/**
59+
* Register a new boot listener.
60+
*
61+
* Your callback will be called before the kernel is booted.
62+
* You can use this to register your own routes, pages, etc.
63+
* The kernel instance will be passed to your callback.
64+
*
65+
* @param callable(\Hyde\Foundation\HydeKernel): void $callback
66+
*/
67+
public function booting(callable $callback): void
68+
{
69+
$this->bootingCallbacks[] = $callback;
70+
}
71+
72+
/**
73+
* Register a new "booted" listener.
74+
*
75+
* Your callback will be called after the kernel is booted.
76+
* You can use this to run any logic after discovery has completed.
77+
* The kernel instance will be passed to your callback.
78+
*
79+
* @param callable(\Hyde\Foundation\HydeKernel): void $callback
80+
*/
81+
public function booted(callable $callback): void
82+
{
83+
$this->bootedCallbacks[] = $callback;
84+
}
85+
4186
/** @internal */
4287
public function readyToBoot(): void
4388
{

packages/framework/tests/Feature/HydeKernelTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Hyde\Hyde;
1313
use Hyde\Pages\BladePage;
1414
use Hyde\Pages\DocumentationPage;
15+
use Hyde\Pages\InMemoryPage;
1516
use Hyde\Pages\MarkdownPage;
1617
use Hyde\Pages\MarkdownPost;
1718
use Hyde\Support\Facades\Render;
@@ -427,4 +428,103 @@ public function test_can_access_kernel_fluently_using_the_facade()
427428
$this->assertSame(HydeKernel::getInstance(), Hyde::kernel());
428429
$this->assertSame(HydeKernel::VERSION, Hyde::kernel()->version());
429430
}
431+
432+
public function test_can_register_booting_callback_closure()
433+
{
434+
$kernel = new HydeKernel();
435+
436+
$kernel->booting(function () {
437+
$this->assertTrue(true);
438+
});
439+
440+
$kernel->readyToBoot();
441+
$kernel->boot();
442+
}
443+
444+
public function test_can_register_booted_callback_closure()
445+
{
446+
$kernel = new HydeKernel();
447+
448+
$kernel->booted(function () {
449+
$this->assertTrue(true);
450+
});
451+
452+
$kernel->readyToBoot();
453+
$kernel->boot();
454+
}
455+
456+
public function test_can_register_booting_callback_callable()
457+
{
458+
$kernel = new HydeKernel();
459+
460+
$kernel->booting(new CallableClass($this));
461+
462+
$kernel->readyToBoot();
463+
$kernel->boot();
464+
}
465+
466+
public function test_can_register_booted_callback_callable()
467+
{
468+
$kernel = new HydeKernel();
469+
470+
$kernel->booted(new CallableClass($this));
471+
472+
$kernel->readyToBoot();
473+
$kernel->boot();
474+
}
475+
476+
public function test_booting_callback_receives_kernel_instance()
477+
{
478+
$kernel = new HydeKernel();
479+
480+
$kernel->booting(function ($_kernel) use ($kernel) {
481+
$this->assertSame($kernel, $_kernel);
482+
});
483+
484+
$kernel->readyToBoot();
485+
$kernel->boot();
486+
}
487+
488+
public function test_booted_callback_receives_kernel_instance()
489+
{
490+
$kernel = new HydeKernel();
491+
492+
$kernel->booted(function ($_kernel) use ($kernel) {
493+
$this->assertSame($kernel, $_kernel);
494+
});
495+
496+
$kernel->readyToBoot();
497+
$kernel->boot();
498+
}
499+
500+
public function test_can_use_booting_callbacks_to_inject_custom_pages()
501+
{
502+
$kernel = new HydeKernel();
503+
504+
$page = new InMemoryPage('foo');
505+
$kernel->booting(function (HydeKernel $kernel) use ($page): void {
506+
$kernel->pages()->addPage($page);
507+
});
508+
509+
$kernel->readyToBoot();
510+
$kernel->boot();
511+
512+
$this->assertSame($page, $kernel->pages()->getPage('foo'));
513+
$this->assertEquals($page->getRoute(), $kernel->routes()->getRoute('foo'));
514+
}
515+
}
516+
517+
class CallableClass
518+
{
519+
private TestCase $test;
520+
521+
public function __construct($test)
522+
{
523+
$this->test = $test;
524+
}
525+
526+
public function __invoke(): void
527+
{
528+
$this->test->assertTrue(true);
529+
}
430530
}

0 commit comments

Comments
 (0)