Skip to content

Commit edf6a54

Browse files
authored
Merge pull request #431 from hydephp/organize-and-restructure-code
Organize and restructure Foundation and Kernel code
2 parents 9bbf4f4 + fece307 commit edf6a54

10 files changed

+102
-66
lines changed

packages/framework/src/Foundation/BaseSystemCollection.php packages/framework/src/Foundation/Concerns/BaseFoundationCollection.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

3-
namespace Hyde\Framework\Foundation;
3+
namespace Hyde\Framework\Foundation\Concerns;
44

55
use Hyde\Framework\HydeKernel;
66
use Illuminate\Support\Collection;
77

88
/**
9-
* @internal Base class for the system collections.
9+
* @internal Base class for the kernel auto-discovery collections.
1010
*
1111
* @see \Hyde\Framework\Foundation\FileCollection
1212
* @see \Hyde\Framework\Foundation\PageCollection
1313
* @see \Hyde\Framework\Foundation\RouteCollection
1414
*/
15-
abstract class BaseSystemCollection extends Collection
15+
abstract class BaseFoundationCollection extends Collection
1616
{
1717
protected HydeKernel $kernel;
1818

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Foundation\Concerns;
4+
5+
use Hyde\Framework\Foundation\FileCollection;
6+
use Hyde\Framework\Foundation\PageCollection;
7+
use Hyde\Framework\Foundation\RouteCollection;
8+
9+
/**
10+
* @internal Single-use trait for the HydeKernel class.
11+
*
12+
* @see \Hyde\Framework\HydeKernel
13+
*/
14+
trait HandlesFoundationCollections
15+
{
16+
public function files(): FileCollection
17+
{
18+
$this->needsToBeBooted();
19+
20+
return $this->files;
21+
}
22+
23+
public function pages(): PageCollection
24+
{
25+
$this->needsToBeBooted();
26+
27+
return $this->pages;
28+
}
29+
30+
public function routes(): RouteCollection
31+
{
32+
$this->needsToBeBooted();
33+
34+
return $this->routes;
35+
}
36+
37+
protected function needsToBeBooted(): void
38+
{
39+
if (! $this->booted) {
40+
$this->boot();
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Foundation\Concerns;
4+
5+
use Hyde\Framework\Foundation\FileCollection;
6+
use Hyde\Framework\Foundation\PageCollection;
7+
use Hyde\Framework\Foundation\RouteCollection;
8+
use Hyde\Framework\HydeKernel;
9+
10+
/**
11+
* @internal Single-use trait for the HydeKernel class.
12+
*
13+
* @see \Hyde\Framework\HydeKernel
14+
*/
15+
trait ManagesHydeKernel
16+
{
17+
public function boot(): void
18+
{
19+
$this->booted = true;
20+
21+
$this->files = FileCollection::boot($this);
22+
$this->pages = PageCollection::boot($this);
23+
$this->routes = RouteCollection::boot($this);
24+
}
25+
26+
public static function getInstance(): HydeKernel
27+
{
28+
return static::$instance;
29+
}
30+
31+
public static function setInstance(HydeKernel $instance): void
32+
{
33+
static::$instance = $instance;
34+
}
35+
36+
public function setBasePath(string $basePath): void
37+
{
38+
$this->basePath = rtrim($basePath, '/\\');
39+
}
40+
41+
public function getBasePath(): string
42+
{
43+
return $this->basePath;
44+
}
45+
}

packages/framework/src/Foundation/FileCollection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hyde\Framework\Foundation;
44

55
use Hyde\Framework\Contracts\AbstractPage;
6+
use Hyde\Framework\Foundation\Concerns\BaseFoundationCollection;
67
use Hyde\Framework\Helpers\Features;
78
use Hyde\Framework\Models\File;
89
use Hyde\Framework\Models\Pages\BladePage;
@@ -14,7 +15,7 @@
1415
/**
1516
* @see \Hyde\Framework\Foundation\FileCollection
1617
*/
17-
final class FileCollection extends BaseSystemCollection
18+
final class FileCollection extends BaseFoundationCollection
1819
{
1920
public function getSourceFiles(?string $pageClass = null): self
2021
{

packages/framework/src/Foundation/PageCollection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Hyde\Framework\Contracts\PageContract;
66
use Hyde\Framework\Exceptions\FileNotFoundException;
7+
use Hyde\Framework\Foundation\Concerns\BaseFoundationCollection;
78
use Hyde\Framework\Helpers\Features;
89
use Hyde\Framework\Models\Pages\BladePage;
910
use Hyde\Framework\Models\Pages\DocumentationPage;
@@ -15,7 +16,7 @@
1516
* @see \Hyde\Framework\Foundation\RouteCollection
1617
* @see \Hyde\Framework\Testing\Feature\PageCollectionTest
1718
*/
18-
final class PageCollection extends BaseSystemCollection
19+
final class PageCollection extends BaseFoundationCollection
1920
{
2021
public function getPage(string $sourcePath): PageContract
2122
{

packages/framework/src/Foundation/RouteCollection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Hyde\Framework\Contracts\PageContract;
66
use Hyde\Framework\Contracts\RouteContract;
7+
use Hyde\Framework\Foundation\Concerns\BaseFoundationCollection;
78
use Hyde\Framework\Models\Route;
89

910
/**
@@ -29,7 +30,7 @@
2930
* determine where a source file will be compiled to, and where a compiled
3031
* file was generated from.
3132
*/
32-
final class RouteCollection extends BaseSystemCollection
33+
final class RouteCollection extends BaseFoundationCollection
3334
{
3435
public function getRoutes(?string $pageClass = null): self
3536
{

packages/framework/src/HydeKernel.php

+2-57
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
*/
3030
class HydeKernel implements HydeKernelContract, Arrayable, \JsonSerializable
3131
{
32+
use Foundation\Concerns\HandlesFoundationCollections;
3233
use Foundation\Concerns\ImplementsStringHelpers;
3334
use Foundation\Concerns\ForwardsHyperlinks;
3435
use Foundation\Concerns\ForwardsFilesystem;
36+
use Foundation\Concerns\ManagesHydeKernel;
3537

3638
use JsonSerializesArrayable;
3739
use Macroable;
@@ -56,40 +58,11 @@ public function __construct(?string $basePath = null)
5658
$this->hyperlinks = new Hyperlinks($this);
5759
}
5860

59-
public function boot(): void
60-
{
61-
$this->booted = true;
62-
63-
$this->files = FileCollection::boot($this);
64-
$this->pages = PageCollection::boot($this);
65-
$this->routes = RouteCollection::boot($this);
66-
}
67-
68-
public static function setInstance(HydeKernel $instance): void
69-
{
70-
static::$instance = $instance;
71-
}
72-
73-
public static function getInstance(): HydeKernel
74-
{
75-
return static::$instance;
76-
}
77-
7861
public static function version(): string
7962
{
8063
return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased';
8164
}
8265

83-
public function getBasePath(): string
84-
{
85-
return $this->basePath;
86-
}
87-
88-
public function setBasePath(string $basePath): void
89-
{
90-
$this->basePath = rtrim($basePath, '/\\');
91-
}
92-
9366
public function features(): Features
9467
{
9568
return new Features;
@@ -110,27 +83,6 @@ public function currentRoute(): ?RouteContract
11083
return View::shared('currentRoute');
11184
}
11285

113-
public function files(): FileCollection
114-
{
115-
$this->needsToBeBooted();
116-
117-
return $this->files;
118-
}
119-
120-
public function pages(): PageCollection
121-
{
122-
$this->needsToBeBooted();
123-
124-
return $this->pages;
125-
}
126-
127-
public function routes(): RouteCollection
128-
{
129-
$this->needsToBeBooted();
130-
131-
return $this->routes;
132-
}
133-
13486
/**
13587
* @inheritDoc
13688
*
@@ -146,11 +98,4 @@ public function toArray(): array
14698
'routes' => $this->routes(),
14799
];
148100
}
149-
150-
protected function needsToBeBooted(): void
151-
{
152-
if (! $this->booted) {
153-
$this->boot();
154-
}
155-
}
156101
}

packages/framework/tests/Feature/FileCollectionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* @covers \Hyde\Framework\Foundation\FileCollection
17-
* @covers \Hyde\Framework\Foundation\BaseSystemCollection
17+
* @covers \Hyde\Framework\Foundation\Concerns\BaseFoundationCollection
1818
*/
1919
class FileCollectionTest extends TestCase
2020
{

packages/framework/tests/Feature/PageCollectionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
/**
1515
* @covers \Hyde\Framework\Foundation\PageCollection
16-
* @covers \Hyde\Framework\Foundation\BaseSystemCollection
16+
* @covers \Hyde\Framework\Foundation\Concerns\BaseFoundationCollection
1717
*/
1818
class PageCollectionTest extends TestCase
1919
{

packages/framework/tests/Feature/RouteCollectionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* @covers \Hyde\Framework\Foundation\RouteCollection
17-
* @covers \Hyde\Framework\Foundation\BaseSystemCollection
17+
* @covers \Hyde\Framework\Foundation\Concerns\BaseFoundationCollection
1818
*/
1919
class RouteCollectionTest extends TestCase
2020
{

0 commit comments

Comments
 (0)