Skip to content

Commit

Permalink
Change internal search generator method to return JSON directly
Browse files Browse the repository at this point in the history
Since saving to disk is handled by the build process, this class now just needs to be responsible for generating the JSON. See #1498
  • Loading branch information
caendesilva committed Dec 18, 2023
1 parent 02ed17d commit f3a480d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,17 +27,16 @@ 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.
*/
public static function handle(): string
{
$service = new static();
$service->run();
$service->save();

return $service->path;
return $service->index->toJson();
}

protected function __construct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct()

public function compile(): string
{
return GeneratesDocumentationSearchIndex::generate();
return GeneratesDocumentationSearchIndex::handle();
}

public static function outputPath(string $identifier = ''): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f3a480d

Please sign in to comment.