From f3a480d31e55f0b7f693623eb9134300033a49b9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 18 Dec 2023 22:35:19 +0100 Subject: [PATCH] Change internal search generator method to return JSON directly Since saving to disk is handled by the build process, this class now just needs to be responsible for generating the JSON. See https://github.com/hydephp/develop/pull/1498 --- .../GeneratesDocumentationSearchIndex.php | 7 +++--- .../DocumentationSearchIndex.php | 2 +- .../DocumentationSearchServiceTest.php | 25 +++---------------- 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/packages/framework/src/Framework/Actions/GeneratesDocumentationSearchIndex.php b/packages/framework/src/Framework/Actions/GeneratesDocumentationSearchIndex.php index 30aeecc0d7f..b4e8eb230ed 100644 --- a/packages/framework/src/Framework/Actions/GeneratesDocumentationSearchIndex.php +++ b/packages/framework/src/Framework/Actions/GeneratesDocumentationSearchIndex.php @@ -17,7 +17,7 @@ use function trim; /** - * @internal Generate a JSON file that can be used as a search index for documentation pages. + * @internal Generate a JSON string that can be used as a search index for documentation pages. */ class GeneratesDocumentationSearchIndex { @@ -27,7 +27,7 @@ class GeneratesDocumentationSearchIndex protected string $path; /** - * Generate the search index and save it to disk. + * @since v2.x This method returns the JSON string instead of saving it to disk and returning the path. * * @return string The path to the generated file. */ @@ -35,9 +35,8 @@ public static function handle(): string { $service = new static(); $service->run(); - $service->save(); - return $service->path; + return $service->index->toJson(); } protected function __construct() diff --git a/packages/framework/src/Framework/Features/Documentation/DocumentationSearchIndex.php b/packages/framework/src/Framework/Features/Documentation/DocumentationSearchIndex.php index 5ec5b32db00..ac14115b422 100644 --- a/packages/framework/src/Framework/Features/Documentation/DocumentationSearchIndex.php +++ b/packages/framework/src/Framework/Features/Documentation/DocumentationSearchIndex.php @@ -26,7 +26,7 @@ public function __construct() public function compile(): string { - return GeneratesDocumentationSearchIndex::generate(); + return GeneratesDocumentationSearchIndex::handle(); } public static function outputPath(string $identifier = ''): string diff --git a/packages/framework/tests/Feature/Services/DocumentationSearchServiceTest.php b/packages/framework/tests/Feature/Services/DocumentationSearchServiceTest.php index 0689b4665bb..a67b05bffe8 100644 --- a/packages/framework/tests/Feature/Services/DocumentationSearchServiceTest.php +++ b/packages/framework/tests/Feature/Services/DocumentationSearchServiceTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Feature\Services; -use Hyde\Facades\Filesystem; use Hyde\Framework\Actions\GeneratesDocumentationSearchIndex; use Hyde\Hyde; use Hyde\Testing\CreatesTemporaryFiles; @@ -32,14 +31,13 @@ public function test_it_generates_a_json_file_with_a_search_index() { $this->file('_docs/foo.md'); - GeneratesDocumentationSearchIndex::handle(); $this->assertSame(json_encode([[ 'slug' => 'foo', 'title' => 'Foo', 'content' => '', 'destination' => 'foo.html', - ]]), file_get_contents('_site/docs/search.json')); + ]]), GeneratesDocumentationSearchIndex::handle()); } public function test_it_adds_all_files_to_search_index() @@ -53,20 +51,7 @@ public function test_it_adds_all_files_to_search_index() public function test_it_handles_generation_even_when_there_are_no_pages() { - GeneratesDocumentationSearchIndex::handle(); - - $this->assertSame('[]', file_get_contents('_site/docs/search.json')); - - Filesystem::unlink('_site/docs/search.json'); - } - - public function test_save_method_saves_the_file_to_the_correct_location() - { - GeneratesDocumentationSearchIndex::handle(); - - $this->assertFileExists('_site/docs/search.json'); - - Filesystem::unlink('_site/docs/search.json'); + $this->assertSame('[]', GeneratesDocumentationSearchIndex::handle()); } public function test_it_generates_a_valid_JSON() @@ -134,10 +119,6 @@ public function test_nested_source_files_do_not_retain_directory_name_in_search_ protected function getArray(): array { - GeneratesDocumentationSearchIndex::handle(); - $array = json_decode(file_get_contents('_site/docs/search.json'), true); - Filesystem::unlink('_site/docs/search.json'); - - return $array; + return json_decode(GeneratesDocumentationSearchIndex::handle(), true); } }