Skip to content

Commit 3492142

Browse files
authored
Merge pull request #1206 from hydephp/finalize-foundation-collection-facades
Finalize foundation collection facades
2 parents 32ab900 + 3f34aa9 commit 3492142

File tree

8 files changed

+63
-124
lines changed

8 files changed

+63
-124
lines changed

packages/framework/src/Foundation/Facades/Files.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,14 @@
66

77
use Hyde\Foundation\HydeKernel;
88
use Hyde\Foundation\Kernel\FileCollection;
9-
use Hyde\Framework\Exceptions\FileNotFoundException;
10-
use Hyde\Support\Filesystem\ProjectFile;
11-
use Hyde\Support\Filesystem\SourceFile;
129
use Illuminate\Support\Facades\Facade;
1310

1411
/**
1512
* @mixin \Hyde\Foundation\Kernel\FileCollection
1613
*/
1714
class Files extends Facade
1815
{
19-
public static function getFile(string $filePath): ProjectFile
20-
{
21-
return static::getFacadeRoot()->get($filePath) ?? throw new FileNotFoundException(message: "File [$filePath] not found in file collection");
22-
}
23-
24-
/**
25-
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass
26-
* @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\SourceFile>
27-
*/
28-
public static function getSourceFiles(?string $pageClass = null): FileCollection
29-
{
30-
return $pageClass ? static::getSourceFilesFor($pageClass) : static::getAllSourceFiles();
31-
}
32-
33-
/**
34-
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass
35-
* @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\SourceFile>
36-
*/
37-
public static function getSourceFilesFor(string $pageClass): FileCollection
38-
{
39-
return static::getAllSourceFiles()->where(fn (SourceFile $file): bool => $file->model === $pageClass);
40-
}
41-
42-
/** @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\SourceFile> */
43-
public static function getAllSourceFiles(): FileCollection
44-
{
45-
return static::getFacadeRoot()->where(fn (ProjectFile $file): bool => $file instanceof SourceFile);
46-
}
47-
48-
/** @return \Hyde\Foundation\Kernel\FileCollection<string, \Hyde\Support\Filesystem\ProjectFile> */
16+
/** @return \Hyde\Foundation\Kernel\FileCollection<string, \Hyde\Support\Filesystem\SourceFile> */
4917
public static function getFacadeRoot(): FileCollection
5018
{
5119
return HydeKernel::getInstance()->files();

packages/framework/src/Foundation/Facades/Pages.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@
66

77
use Hyde\Foundation\HydeKernel;
88
use Hyde\Foundation\Kernel\PageCollection;
9-
use Hyde\Framework\Exceptions\FileNotFoundException;
10-
use Hyde\Pages\Concerns\HydePage;
119
use Illuminate\Support\Facades\Facade;
1210

1311
/**
1412
* @mixin \Hyde\Foundation\Kernel\PageCollection
1513
*/
1614
class Pages extends Facade
1715
{
18-
public static function getPage(string $sourcePath): HydePage
19-
{
20-
return static::getFacadeRoot()->get($sourcePath) ?? throw new FileNotFoundException(message: "Page [$sourcePath] not found in page collection");
21-
}
22-
23-
public static function getPages(?string $pageClass = null): PageCollection
24-
{
25-
return $pageClass ? static::getFacadeRoot()->filter(function (HydePage $page) use ($pageClass): bool {
26-
return $page instanceof $pageClass;
27-
}) : static::getFacadeRoot();
28-
}
29-
3016
/** @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage> */
3117
public static function getFacadeRoot(): PageCollection
3218
{

packages/framework/src/Foundation/Facades/Routes.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@
66

77
use Hyde\Foundation\HydeKernel;
88
use Hyde\Foundation\Kernel\RouteCollection;
9-
use Hyde\Framework\Exceptions\RouteNotFoundException;
10-
use Hyde\Support\Models\Route;
119
use Illuminate\Support\Facades\Facade;
1210

1311
/**
1412
* @mixin \Hyde\Foundation\Kernel\RouteCollection
1513
*/
1614
class Routes extends Facade
1715
{
18-
public static function getRoute(string $routeKey): Route
19-
{
20-
return static::getFacadeRoot()->get($routeKey) ?? throw new RouteNotFoundException(message: "Route [$routeKey] not found in route collection");
21-
}
22-
23-
public static function getRoutes(?string $pageClass = null): RouteCollection
24-
{
25-
return $pageClass ? static::getFacadeRoot()->filter(function (Route $route) use ($pageClass): bool {
26-
return $route->getPage() instanceof $pageClass;
27-
}) : static::getFacadeRoot();
28-
}
29-
3016
/** @return \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route> */
3117
public static function getFacadeRoot(): RouteCollection
3218
{

packages/framework/src/Foundation/Kernel/FileCollection.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,26 @@
55
namespace Hyde\Foundation\Kernel;
66

77
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8+
use Hyde\Framework\Exceptions\FileNotFoundException;
89
use Hyde\Pages\Concerns\HydePage;
910
use Hyde\Support\Filesystem\SourceFile;
1011

1112
/**
12-
* The FileCollection contains all the discovered source and media files,
13-
* and thus has an integral role in the Hyde Auto Discovery process.
13+
* The FileCollection contains all the discovered source files.
1414
*
1515
* @template T of \Hyde\Support\Filesystem\SourceFile
1616
* @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
1717
*
1818
* @property array<string, SourceFile> $items The files in the collection.
1919
*
2020
* This class is stored as a singleton in the HydeKernel.
21-
* You would commonly access it via one of the facades:
21+
* You would commonly access it via the facade or Hyde helper:
2222
*
2323
* @see \Hyde\Foundation\Facades\Files
2424
* @see \Hyde\Hyde::files()
2525
*/
2626
final class FileCollection extends BaseFoundationCollection
2727
{
28-
/**
29-
* This method adds the specified file to the file collection.
30-
* It can be used by package developers to add a file that can be discovered.
31-
*
32-
* In order for your file to be further processed you must call this method during the boot process,
33-
* either using a Kernel bootingCallback, or by using a HydeExtension's discovery handler callback.
34-
*/
3528
public function addFile(SourceFile $file): void
3629
{
3730
$this->put($file->getPath(), $file);
@@ -65,4 +58,20 @@ protected function discoverFilesFor(string $pageClass): void
6558
}
6659
}
6760
}
61+
62+
public function getFile(string $filePath): SourceFile
63+
{
64+
return $this->get($filePath) ?? throw new FileNotFoundException(message: "File [$filePath] not found in file collection");
65+
}
66+
67+
/**
68+
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass
69+
* @return \Hyde\Foundation\Kernel\FileCollection<string, \Hyde\Support\Filesystem\SourceFile>
70+
*/
71+
public function getFiles(?string $pageClass = null): FileCollection
72+
{
73+
return $pageClass ? $this->filter(function (SourceFile $file) use ($pageClass): bool {
74+
return $file->model === $pageClass;
75+
}) : $this;
76+
}
6877
}

packages/framework/src/Foundation/Kernel/PageCollection.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace Hyde\Foundation\Kernel;
66

77
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8+
use Hyde\Framework\Exceptions\FileNotFoundException;
89
use Hyde\Framework\Services\DiscoveryService;
910
use Hyde\Pages\Concerns\HydePage;
10-
use Hyde\Support\Filesystem\ProjectFile;
1111
use Hyde\Support\Filesystem\SourceFile;
1212

1313
/**
@@ -19,37 +19,24 @@
1919
* @property array<string, HydePage> $items The pages in the collection.
2020
*
2121
* This class is stored as a singleton in the HydeKernel.
22-
* You would commonly access it via one of the facades:
22+
* You would commonly access it via the facade or Hyde helper:
2323
*
2424
* @see \Hyde\Foundation\Facades\PageCollection
2525
* @see \Hyde\Hyde::pages()
2626
*/
2727
final class PageCollection extends BaseFoundationCollection
2828
{
29-
/**
30-
* This method adds the specified page to the page collection.
31-
* It can be used by package developers to add a page that will be compiled.
32-
*
33-
* Note that this method when used outside of this class is only intended to be used for adding on-off pages;
34-
* If you are registering multiple pages, you may instead want to register an entire custom page class,
35-
* as that will allow you to utilize the full power of the HydePHP autodiscovery.
36-
*
37-
* In order for your page to be routable and compilable you must call this method during the boot process,
38-
* either using a Kernel bootingCallback, or by using a HydeExtension's discovery handler callback.
39-
*/
4029
public function addPage(HydePage $page): void
4130
{
4231
$this->put($page->getSourcePath(), $page);
4332
}
4433

4534
protected function runDiscovery(): void
4635
{
47-
$this->kernel->files()->each(function (ProjectFile $file): void {
48-
if ($file instanceof SourceFile) {
49-
$this->addPage($file->model::parse(
50-
DiscoveryService::pathToIdentifier($file->model, $file->getPath())
51-
));
52-
}
36+
$this->kernel->files()->each(function (SourceFile $file): void {
37+
$this->addPage($file->model::parse(
38+
DiscoveryService::pathToIdentifier($file->model, $file->getPath())
39+
));
5340
});
5441
}
5542

@@ -60,4 +47,20 @@ protected function runExtensionCallbacks(): void
6047
$extension->discoverPages($this);
6148
}
6249
}
50+
51+
public function getPage(string $sourcePath): HydePage
52+
{
53+
return $this->get($sourcePath) ?? throw new FileNotFoundException(message: "Page [$sourcePath] not found in page collection");
54+
}
55+
56+
/**
57+
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass
58+
* @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage>
59+
*/
60+
public function getPages(?string $pageClass = null): PageCollection
61+
{
62+
return $pageClass ? $this->filter(function (HydePage $page) use ($pageClass): bool {
63+
return $page instanceof $pageClass;
64+
}) : $this;
65+
}
6366
}

packages/framework/src/Foundation/Kernel/RouteCollection.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Hyde\Foundation\Kernel;
66

77
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8-
use Hyde\Foundation\Facades\Routes;
8+
use Hyde\Framework\Exceptions\RouteNotFoundException;
99
use Hyde\Pages\Concerns\HydePage;
1010
use Hyde\Support\Models\Route;
1111

@@ -18,42 +18,13 @@
1818
* @property array<string, Route> $items The routes in the collection.
1919
*
2020
* This class is stored as a singleton in the HydeKernel.
21-
* You would commonly access it via one of the facades:
21+
* You would commonly access it via the facade or Hyde helper:
2222
*
2323
* @see \Hyde\Foundation\Facades\Router
2424
* @see \Hyde\Hyde::routes()
25-
*
26-
* This is not a router in the traditional sense that it decides where to go.
27-
* Instead, it creates a pre-generated object encapsulating the Hyde autodiscovery.
28-
*
29-
* This not only let us emulate Laravel route helpers, but also serve as the
30-
* canonical source of truth for the vital HydePHP autodiscovery process.
31-
*
32-
* The routes defined can then also be used to power the RealtimeCompiler without
33-
* having to reverse-engineer the source file mapping.
34-
*
35-
* Routes are not intended to be added manually, instead the route index is created using
36-
* the exact same rules as the current autodiscovery process and compiled file output.
37-
* However, extensions can add routes using the discovery handler callbacks.
38-
*
39-
* The route index serves as a multidimensional mapping allowing you to
40-
* determine where a source file will be compiled to, and where a compiled
41-
* file was generated from. This bridges the gaps between the source and
42-
* the compiled web accessible URI routes the static site generator creates.
4325
*/
4426
final class RouteCollection extends BaseFoundationCollection
4527
{
46-
/**
47-
* This method adds the specified route to the route index.
48-
* It can be used by package developers to hook into the routing system.
49-
*
50-
* Note that this method when used outside of this class is only intended to be used for adding on-off routes;
51-
* If you are registering multiple routes, you may instead want to register an entire custom page class,
52-
* as that will allow you to utilize the full power of the HydePHP autodiscovery.
53-
*
54-
* In addition, you might actually rather want to use the PageCollection's addPage method
55-
* instead as all pages there are automatically also added as routes here as well.
56-
*/
5728
public function addRoute(Route $route): void
5829
{
5930
$this->put($route->getRouteKey(), $route);
@@ -72,4 +43,20 @@ protected function runExtensionCallbacks(): void
7243
$extension->discoverRoutes($this);
7344
}
7445
}
46+
47+
public function getRoute(string $routeKey): Route
48+
{
49+
return $this->get($routeKey) ?? throw new RouteNotFoundException(message: "Route [$routeKey] not found in route collection");
50+
}
51+
52+
/**
53+
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass
54+
* @return \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route>
55+
*/
56+
public function getRoutes(?string $pageClass = null): RouteCollection
57+
{
58+
return $pageClass ? $this->filter(function (Route $route) use ($pageClass): bool {
59+
return $route->getPage() instanceof $pageClass;
60+
}) : $this;
61+
}
7562
}

packages/framework/src/Pages/Concerns/HydePage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static function parse(string $identifier): HydePage
118118
*/
119119
public static function files(): array
120120
{
121-
return Files::getSourceFiles(static::class)->map(function (SourceFile $file): string {
121+
return Files::getFiles(static::class)->map(function (SourceFile $file): string {
122122
return DiscoveryService::pathToIdentifier(static::class, $file->getPath());
123123
})->values()->toArray();
124124
}

packages/framework/tests/Feature/FileCollectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public function test_get_source_files_returns_all_discovered_source_files_when_n
5555
$this->assertEquals([
5656
'_pages/404.blade.php' => new SourceFile('_pages/404.blade.php', BladePage::class),
5757
'_pages/index.blade.php' => new SourceFile('_pages/index.blade.php', BladePage::class),
58-
], Files::getSourceFiles()->all());
58+
], Files::getFiles()->all());
5959
}
6060

6161
public function test_get_source_files_does_not_include_non_page_source_files()
6262
{
6363
$this->withoutDefaultPages();
6464
$this->file('_pages/foo.txt');
6565

66-
$this->assertEquals([], Files::getSourceFiles()->all());
66+
$this->assertEquals([], Files::getFiles()->all());
6767

6868
$this->restoreDefaultPages();
6969
}

0 commit comments

Comments
 (0)