Skip to content

Commit

Permalink
Merge pull request #1038 from hydephp/decouple-discovery-booting-from…
Browse files Browse the repository at this point in the history
…-foundation-collection-instantiation

Breaking: Decouple discovery booting from foundation collection instantiation
  • Loading branch information
caendesilva committed Feb 15, 2023
2 parents 58de4fd + 58c68ad commit 7fe075f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ abstract protected function runDiscovery(): self;

abstract protected function runExtensionCallbacks(): self;

public static function boot(HydeKernel $kernel): static
public static function init(HydeKernel $kernel): static
{
return (new static())->setKernel($kernel)->runDiscovery();
return (new static())->setKernel($kernel);
}

public function boot(): static
{
return $this->runDiscovery();
}

protected function __construct(array|Arrayable|null $items = [])
Expand Down
10 changes: 7 additions & 3 deletions packages/framework/src/Foundation/Concerns/BootsHydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public function boot(): void

$this->booting = true;

$this->files = FileCollection::boot($this);
$this->pages = PageCollection::boot($this);
$this->routes = RouteCollection::boot($this);
$this->files = FileCollection::init($this);
$this->pages = PageCollection::init($this);
$this->routes = RouteCollection::init($this);

$this->files->boot();
$this->pages->boot();
$this->routes->boot();

$this->booting = false;
$this->booted = true;
Expand Down
18 changes: 9 additions & 9 deletions packages/framework/tests/Feature/FileCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FileCollectionTest extends TestCase
{
public function test_boot_method_creates_new_page_collection_and_discovers_pages_automatically()
{
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();
$this->assertInstanceOf(FileCollection::class, $collection);
$this->assertInstanceOf(Collection::class, $collection);

Expand All @@ -36,7 +36,7 @@ public function test_boot_method_creates_new_page_collection_and_discovers_pages

public function test_get_source_files_returns_all_discovered_source_files_when_no_parameter_is_supplied()
{
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();

$this->assertEquals([
'_pages/404.blade.php' => new SourceFile('_pages/404.blade.php', BladePage::class),
Expand All @@ -49,15 +49,15 @@ public function test_get_source_files_does_not_include_non_page_source_files()
$this->withoutDefaultPages();
$this->file('_pages/foo.txt');

$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();
$this->assertEquals([], $collection->getSourceFiles()->all());

$this->restoreDefaultPages();
}

public function test_get_media_files_returns_all_discovered_media_files()
{
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();
$this->assertEquals([
'_media/app.css' => new MediaFile('_media/app.css'),
], $collection->getMediaFiles()->all());
Expand All @@ -66,7 +66,7 @@ public function test_get_media_files_returns_all_discovered_media_files()
public function test_get_media_files_does_not_include_non_media_files()
{
$this->file('_media/foo.blade.php');
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();
$this->assertEquals([
'_media/app.css' => new MediaFile('_media/app.css'),
], $collection->getMediaFiles()->all());
Expand All @@ -75,7 +75,7 @@ public function test_get_media_files_does_not_include_non_media_files()
public function test_blade_pages_are_discovered()
{
$this->file('_pages/foo.blade.php');
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();

$this->assertArrayHasKey('_pages/foo.blade.php', $collection->toArray());
$this->assertEquals(new SourceFile('_pages/foo.blade.php', BladePage::class), $collection->get('_pages/foo.blade.php'));
Expand All @@ -84,7 +84,7 @@ public function test_blade_pages_are_discovered()
public function test_markdown_pages_are_discovered()
{
$this->file('_pages/foo.md');
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();

$this->assertArrayHasKey('_pages/foo.md', $collection->toArray());
$this->assertEquals(new SourceFile('_pages/foo.md', MarkdownPage::class), $collection->get('_pages/foo.md'));
Expand All @@ -93,7 +93,7 @@ public function test_markdown_pages_are_discovered()
public function test_markdown_posts_are_discovered()
{
$this->file('_posts/foo.md');
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();

$this->assertArrayHasKey('_posts/foo.md', $collection->toArray());
$this->assertEquals(new SourceFile('_posts/foo.md', MarkdownPost::class), $collection->get('_posts/foo.md'));
Expand All @@ -102,7 +102,7 @@ public function test_markdown_posts_are_discovered()
public function test_documentation_pages_are_discovered()
{
$this->file('_docs/foo.md');
$collection = FileCollection::boot(Hyde::getInstance());
$collection = FileCollection::init(Hyde::getInstance())->boot();
$this->assertArrayHasKey('_docs/foo.md', $collection->toArray());
$this->assertEquals(new SourceFile('_docs/foo.md', DocumentationPage::class), $collection->get('_docs/foo.md'));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/tests/Feature/HydeExtensionFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function test_register_extension_method_does_not_register_already_registe
public function test_custom_registered_pages_are_discovered_by_the_file_collection_class()
{
app(HydeKernel::class)->registerExtension(TestPageExtension::class);
FileCollection::boot(app(HydeKernel::class));
FileCollection::init(app(HydeKernel::class))->boot();

$this->directory('foo');
$this->file('foo/bar.txt');
Expand All @@ -189,7 +189,7 @@ public function test_custom_registered_pages_are_discovered_by_the_page_collecti
$this->file('foo/bar.txt');

app(HydeKernel::class)->registerExtension(TestPageExtension::class);
PageCollection::boot(app(HydeKernel::class));
PageCollection::init(app(HydeKernel::class))->boot();

$this->assertArrayHasKey('foo/bar.txt', Pages::all());
$this->assertEquals(new TestPageClass('bar'), Pages::get('foo/bar.txt'));
Expand All @@ -201,7 +201,7 @@ public function test_custom_registered_pages_are_discovered_by_the_route_collect
$this->file('foo/bar.txt');

app(HydeKernel::class)->registerExtension(TestPageExtension::class);
RouteCollection::boot(app(HydeKernel::class));
RouteCollection::init(app(HydeKernel::class))->boot();

$this->assertArrayHasKey('foo/bar', Routes::all());
$this->assertEquals(new Route(new TestPageClass('bar')), Routes::get('foo/bar'));
Expand Down
22 changes: 11 additions & 11 deletions packages/framework/tests/Feature/PageCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PageCollectionTest extends TestCase
{
public function test_boot_method_creates_new_page_collection_and_discovers_pages_automatically()
{
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();
$this->assertInstanceOf(PageCollection::class, $collection);
$this->assertInstanceOf(Collection::class, $collection);

Expand All @@ -37,7 +37,7 @@ public function test_boot_method_creates_new_page_collection_and_discovers_pages
public function test_blade_pages_are_discovered()
{
$this->file('_pages/foo.blade.php');
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();

$this->assertArrayHasKey('_pages/foo.blade.php', $collection->toArray());
$this->assertEquals(new BladePage('foo'), $collection->get('_pages/foo.blade.php'));
Expand All @@ -46,7 +46,7 @@ public function test_blade_pages_are_discovered()
public function test_markdown_pages_are_discovered()
{
$this->file('_pages/foo.md');
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();

$this->assertArrayHasKey('_pages/foo.md', $collection->toArray());
$this->assertEquals(new MarkdownPage('foo'), $collection->get('_pages/foo.md'));
Expand All @@ -55,7 +55,7 @@ public function test_markdown_pages_are_discovered()
public function test_markdown_posts_are_discovered()
{
$this->file('_posts/foo.md');
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();

$this->assertArrayHasKey('_posts/foo.md', $collection->toArray());
$this->assertEquals(new MarkdownPost('foo'), $collection->get('_posts/foo.md'));
Expand All @@ -64,15 +64,15 @@ public function test_markdown_posts_are_discovered()
public function test_documentation_pages_are_discovered()
{
$this->file('_docs/foo.md');
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();
$this->assertArrayHasKey('_docs/foo.md', $collection->toArray());
$this->assertEquals(new DocumentationPage('foo'), $collection->get('_docs/foo.md'));
}

public function test_get_page_returns_parsed_page_object_for_given_source_path()
{
$this->file('_pages/foo.blade.php');
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();
$this->assertEquals(new BladePage('foo'), $collection->getPage('_pages/foo.blade.php'));
}

Expand All @@ -86,7 +86,7 @@ public function test_get_pages_returns_collection_of_pages_of_given_class()
$this->file('_docs/foo.md');
$this->file('_pages/foo.html');

$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();
$this->assertCount(5, $collection);

$this->assertContainsOnlyInstancesOf(BladePage::class, $collection->getPages(BladePage::class));
Expand Down Expand Up @@ -114,7 +114,7 @@ public function test_get_pages_returns_all_pages_when_not_supplied_with_class_st
$this->file('_docs/foo.md');
$this->file('_pages/foo.html');

$collection = PageCollection::boot(Hyde::getInstance())->getPages();
$collection = PageCollection::init(Hyde::getInstance())->boot()->getPages();
$this->assertCount(5, $collection);

$this->assertEquals(new BladePage('foo'), $collection->get('_pages/foo.blade.php'));
Expand All @@ -129,7 +129,7 @@ public function test_get_pages_returns_all_pages_when_not_supplied_with_class_st
public function test_get_pages_returns_empty_collection_when_no_pages_are_discovered()
{
$this->withoutDefaultPages();
$collection = PageCollection::boot(Hyde::getInstance());
$collection = PageCollection::init(Hyde::getInstance())->boot();
$this->assertEmpty($collection->getPages());
$this->restoreDefaultPages();
}
Expand All @@ -145,7 +145,7 @@ public function test_pages_are_not_discovered_for_disabled_features()
touch('_posts/post.md');
touch('_docs/doc.md');

$this->assertEmpty(PageCollection::boot(Hyde::getInstance()));
$this->assertEmpty(PageCollection::init(Hyde::getInstance())->boot());

unlink('_pages/blade.blade.php');
unlink('_pages/markdown.md');
Expand All @@ -170,7 +170,7 @@ public function test_pages_with_custom_source_directories_are_discovered_properl
touch(Hyde::path('.source/posts/foo.md'));
touch(Hyde::path('.source/docs/foo.md'));

$collection = PageCollection::boot(Hyde::getInstance())->getPages();
$collection = PageCollection::init(Hyde::getInstance())->boot()->getPages();
$this->assertCount(4, $collection);

$this->assertEquals(new BladePage('foo'), $collection->get('.source/pages/foo.blade.php'));
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Feature/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RouteCollectionTest extends TestCase
{
public function test_boot_method_discovers_all_pages()
{
$collection = RouteCollection::boot(Hyde::getInstance());
$collection = RouteCollection::init(Hyde::getInstance())->boot();

$this->assertInstanceOf(RouteCollection::class, $collection);
$this->assertInstanceOf(Collection::class, $collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
class BaseFoundationCollectionTest extends TestCase
{
public function test_boot()
public function test_init()
{
$booted = BaseFoundationCollectionTestClass::boot(HydeKernel::getInstance());
$booted = BaseFoundationCollectionTestClass::init(HydeKernel::getInstance())->boot();

$this->assertInstanceOf(BaseFoundationCollection::class, $booted);
$this->assertInstanceOf(BaseFoundationCollectionTestClass::class, $booted);
Expand All @@ -26,7 +26,7 @@ public function test_boot()

public function test_get_instance()
{
$booted = BaseFoundationCollectionTestClass::boot(HydeKernel::getInstance());
$booted = BaseFoundationCollectionTestClass::init(HydeKernel::getInstance())->boot();

$this->assertSame($booted, $booted->getInstance());
}
Expand Down

0 comments on commit 7fe075f

Please sign in to comment.