Skip to content

Commit e5ab940

Browse files
committed
Merge branch 'master' into decouple-publications-package
2 parents 968f770 + 554f2ea commit e5ab940

File tree

7 files changed

+244
-38
lines changed

7 files changed

+244
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Foundation\Concerns;
6+
7+
use Hyde\Foundation\FileCollection;
8+
use Hyde\Foundation\PageCollection;
9+
use Hyde\Foundation\RouteCollection;
10+
11+
/**
12+
* When creating a HydePHP extension, you should create a class that extends this one.
13+
*
14+
* After registering your implementation with the HydeKernel (usually in a service provider),
15+
* Hyde will be able to use the information within to integrate your plugin, and to allow you to
16+
* hook into various parts of the internal application lifecycle, and through that, all aspects of Hyde.
17+
*
18+
* Before creating your extension, it will certainly be helpful if you first become familiar
19+
* with the basic internal architecture of HydePHP, as well as how the auto-discovery system functions.
20+
*
21+
* @link https://hydephp.com/docs/master/architecture-concepts
22+
*
23+
* It's important that your class is registered before the HydeKernel boots.
24+
* An excellent place for this is the 'register' method of your extensions service provider,
25+
* where you call the 'registerExtension' method of the HydeKernel singleton instance,
26+
* which you can access via the Hyde\Hyde facade, or via the service container.
27+
*
28+
* @example ```php $this->app->make(HydeKernel::class)->registerExtension(MyExtension::class); ```
29+
*
30+
* @see \Hyde\Framework\Testing\Feature\HydeExtensionTest
31+
*/
32+
abstract class HydeExtension
33+
{
34+
/**
35+
* If your extension adds new page classes, you should register them here.
36+
*
37+
* Hyde will then automatically discover source files for the new page class,
38+
* generate routes, and compile the pages during the build process.
39+
*
40+
* @return array<class-string<\Hyde\Pages\Concerns\HydePage>>
41+
*/
42+
public static function getPageClasses(): array
43+
{
44+
return [];
45+
}
46+
47+
/**
48+
* If your extension needs to hook into the file discovery process,
49+
* you can configure the following handler method. It will be called
50+
* at the end of the file discovery process. The collection instance
51+
* will be injected, so that you can interact with it directly.
52+
*/
53+
public static function discoverFiles(FileCollection $collection): void
54+
{
55+
//
56+
}
57+
58+
/**
59+
* If your extension needs to hook into the page discovery process,
60+
* you can configure the following handler method. It will be called
61+
* at the end of the page discovery process. The collection instance
62+
* will be injected, so that you can interact with it directly.
63+
*/
64+
public static function discoverPages(PageCollection $collection): void
65+
{
66+
//
67+
}
68+
69+
/**
70+
* If your extension needs to hook into the route discovery process,
71+
* you can configure the following handler method. It will be called
72+
* at the end of the route discovery process. The collection instance
73+
* will be injected, so that you can interact with it directly.
74+
*/
75+
public static function discoverRoutes(RouteCollection $collection): void
76+
{
77+
//
78+
}
79+
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public function getSourceRoot(): string
5454
*
5555
* @experimental This feature is experimental and may change substantially before the 1.0.0 release.
5656
*
57+
* @deprecated This feature may be replaced by the {@see \Hyde\Foundation\Concerns\HydeExtension} system.
58+
*
5759
* If you are a package developer, and want a custom page class to be discovered,
5860
* you'll need to register it sometime before the boot process, before discovery is run.
5961
* Typically, you would do this by calling this method in the register method of a service provider.
@@ -90,15 +92,15 @@ public function getRegisteredPageClasses(): array
9092
return $this->pageClasses;
9193
}
9294

93-
/** @param class-string<\Hyde\Foundation\Extensions\HydeExtension> $extension */
95+
/** @param class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
9496
public function registerExtension(string $extension): void
9597
{
9698
if (! in_array($extension, $this->extensions, true)) {
9799
$this->extensions[] = $extension;
98100
}
99101
}
100102

101-
/** @return array<class-string<\Hyde\Foundation\Extensions\HydeExtension>> */
103+
/** @return array<class-string<\Hyde\Foundation\Concerns\HydeExtension>> */
102104
public function getRegisteredExtensions(): array
103105
{
104106
return $this->extensions;

packages/framework/src/Foundation/Extensions/HydeExtension.php

-33
This file was deleted.

packages/framework/src/Foundation/FileCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function runDiscovery(): self
8585
$this->discoverFilesFor($pageClass);
8686
}
8787

88-
/** @var class-string<\Hyde\Foundation\Extensions\HydeExtension> $extension */
88+
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
8989
foreach ($this->kernel->getRegisteredExtensions() as $extension) {
9090
$extension::discoverFiles($this);
9191
}

packages/framework/src/Foundation/PageCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function runDiscovery(): self
8888
$this->discoverPagesFor($pageClass);
8989
}
9090

91-
/** @var class-string<\Hyde\Foundation\Extensions\HydeExtension> $extension */
91+
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
9292
foreach ($this->kernel->getRegisteredExtensions() as $extension) {
9393
$extension::discoverPages($this);
9494
}

packages/framework/src/Foundation/RouteCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function runDiscovery(): self
7878
$this->discover($page);
7979
});
8080

81-
/** @var class-string<\Hyde\Foundation\Extensions\HydeExtension> $extension */
81+
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
8282
foreach ($this->kernel->getRegisteredExtensions() as $extension) {
8383
$extension::discoverRoutes($this);
8484
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Testing\Feature;
6+
7+
use function func_get_args;
8+
use Hyde\Foundation\Concerns\HydeExtension;
9+
use Hyde\Foundation\FileCollection;
10+
use Hyde\Foundation\HydeKernel;
11+
use Hyde\Foundation\PageCollection;
12+
use Hyde\Foundation\RouteCollection;
13+
use Hyde\Hyde;
14+
use Hyde\Pages\Concerns\HydePage;
15+
use Hyde\Testing\TestCase;
16+
17+
/**
18+
* @covers \Hyde\Foundation\Concerns\HydeExtension
19+
* @covers \Hyde\Foundation\Concerns\ManagesHydeKernel
20+
* @covers \Hyde\Foundation\HydeKernel
21+
* @covers \Hyde\Foundation\FileCollection
22+
* @covers \Hyde\Foundation\PageCollection
23+
* @covers \Hyde\Foundation\RouteCollection
24+
*/
25+
class HydeExtensionFeatureTest extends TestCase
26+
{
27+
protected HydeKernel $kernel;
28+
29+
protected function setUp(): void
30+
{
31+
parent::setUp();
32+
33+
$this->kernel = HydeKernel::getInstance();
34+
}
35+
36+
public function testBaseClassGetPageClasses()
37+
{
38+
$this->assertSame([], HydeExtension::getPageClasses());
39+
}
40+
41+
public function testBaseClassDiscoveryHandlers()
42+
{
43+
HydeExtension::discoverFiles(Hyde::files());
44+
HydeExtension::discoverPages(Hyde::pages());
45+
HydeExtension::discoverRoutes(Hyde::routes());
46+
47+
$this->markTestSuccessful();
48+
}
49+
50+
public function testCanRegisterNewExtension()
51+
{
52+
$this->kernel->registerExtension(HydeTestExtension::class);
53+
$this->assertSame([HydeTestExtension::class], $this->kernel->getRegisteredExtensions());
54+
}
55+
56+
public function testHandlerMethodsAreCalledByDiscovery()
57+
{
58+
$this->kernel->registerExtension(HydeTestExtension::class);
59+
60+
$this->assertSame([], HydeTestExtension::$callCache);
61+
62+
$this->kernel->boot();
63+
64+
$this->assertSame(['files', 'pages', 'routes'], HydeTestExtension::$callCache);
65+
66+
HydeTestExtension::$callCache = [];
67+
}
68+
69+
public function testFileHandlerDependencyInjection()
70+
{
71+
$this->kernel->registerExtension(SpyableTestExtension::class);
72+
$this->kernel->boot();
73+
74+
$this->assertInstanceOf(FileCollection::class, ...SpyableTestExtension::getCalled('files'));
75+
}
76+
77+
public function testPageHandlerDependencyInjection()
78+
{
79+
$this->kernel->registerExtension(SpyableTestExtension::class);
80+
$this->kernel->boot();
81+
82+
$this->assertInstanceOf(PageCollection::class, ...SpyableTestExtension::getCalled('pages'));
83+
}
84+
85+
public function testRouteHandlerDependencyInjection()
86+
{
87+
$this->kernel->registerExtension(SpyableTestExtension::class);
88+
$this->kernel->boot();
89+
90+
$this->assertInstanceOf(RouteCollection::class, ...SpyableTestExtension::getCalled('routes'));
91+
}
92+
93+
protected function markTestSuccessful(): void
94+
{
95+
$this->assertTrue(true);
96+
}
97+
}
98+
99+
class HydeTestExtension extends HydeExtension
100+
{
101+
// An easy way to assert the handlers are called.
102+
public static array $callCache = [];
103+
104+
public static function getPageClasses(): array
105+
{
106+
return [
107+
HydeExtensionTestPage::class,
108+
];
109+
}
110+
111+
public static function discoverFiles(FileCollection $collection): void
112+
{
113+
static::$callCache[] = 'files';
114+
}
115+
116+
public static function discoverPages(PageCollection $collection): void
117+
{
118+
static::$callCache[] = 'pages';
119+
}
120+
121+
public static function discoverRoutes(RouteCollection $collection): void
122+
{
123+
static::$callCache[] = 'routes';
124+
}
125+
}
126+
127+
class SpyableTestExtension extends HydeExtension
128+
{
129+
private static array $callCache = [];
130+
131+
public static function discoverFiles(FileCollection $collection): void
132+
{
133+
self::$callCache['files'] = func_get_args();
134+
}
135+
136+
public static function discoverPages(PageCollection $collection): void
137+
{
138+
self::$callCache['pages'] = func_get_args();
139+
}
140+
141+
public static function discoverRoutes(RouteCollection $collection): void
142+
{
143+
self::$callCache['routes'] = func_get_args();
144+
}
145+
146+
public static function getCalled(string $method): array
147+
{
148+
return self::$callCache[$method];
149+
}
150+
}
151+
152+
class HydeExtensionTestPage extends HydePage
153+
{
154+
public function compile(): string
155+
{
156+
return '';
157+
}
158+
}

0 commit comments

Comments
 (0)