Skip to content

Commit

Permalink
Begin sketching out the class
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 26, 2022
1 parent c3acf08 commit ed131bd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
54 changes: 53 additions & 1 deletion src/Actions/GeneratesDocumentationSearchIndexFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Hyde\Framework\Actions;

use Hyde\Framework\Contracts\ActionContract;
use Hyde\Framework\Hyde;
use Illuminate\Support\Collection;

/**
* Generate a JSON file that can be used as a search index for documentation pages.
Expand All @@ -11,8 +13,58 @@
*/
class GeneratesDocumentationSearchIndexFile implements ActionContract
{
public function execute()
public Collection $searchIndex;
public static string $filePath = '_site/docs/searchIndex.json';

public static function run(): void
{
(new static())->execute();
}

public function __construct()
{
$this->searchIndex = new Collection();
}

public function execute(): void
{
$this->generate();
$this->save();
}

public function generate(): void
{
foreach ($this->getSourceFileSlugs() as $page) {
$this->searchIndex->push(
$this->generatePageObject($page)
);
}
}

public function generatePageObject(string $slug): object
{
return (object) [
'slug' => $slug
];
}

public function getSourceFileSlugs(): array
{
return [];
}

public function getObject(): object
{
return (object) $this->searchIndex;
}

public function getJson(): string
{
return json_encode($this->getObject());
}

public function save(): void
{
file_put_contents(Hyde::path(static::$filePath), $this->getJson());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,38 @@
namespace Tests\Feature\Actions;

use Tests\TestCase;
use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile;
use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile as Action;

/**
* @covers \Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile
*/
class GeneratesDocumentationSearchIndexFileTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

unlinkIfExists(Action::$filePath);
}

protected function tearDown(): void
{
unlinkIfExists(Action::$filePath);

parent::tearDown();
}


// Test save method saves the file to the correct location.
public function test_save_method_saves_the_file_to_the_correct_location()
{
(new Action())->save();

$this->assertFileExists('_site/docs/searchIndex.json');
}


// Test it generates a JSON file with a search index

// Test it handles generation even when there are no pages
}

0 comments on commit ed131bd

Please sign in to comment.