Skip to content

Commit

Permalink
Merge pull request #582 from hydephp/code-refactors
Browse files Browse the repository at this point in the history
Code refactors
  • Loading branch information
caendesilva authored Oct 25, 2022
2 parents eb0dbe5 + dd40cb2 commit 5399eee
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 19 deletions.
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ If you however have written custom code that explicitly references the old names
- for soon-to-be removed features.

### Removed
- Removed MetadataItemContract.php (use new abstract class BaseMetadataElement)
- Removed single usage trait AsksToRebuildSite.php (inlined into HydePublishHomepageCommand.php)
- Removed interface ActionContract.php

Expand All @@ -68,4 +67,5 @@ The internal metadata handling has been refactored to make it more flexible and

- The MetadataBag class's namespace has been changed from `Hyde\Framework\Models\Metadata\MetadataBag` to `Hyde\Framework\Modules\Metadata\MetadataBag;`
- All metadata models have been moved to the new namespace `Hyde\Framework\Modules\Metadata\Models`
- All metadata models have been renamed, changing the suffix `Item` to `Model`
- All metadata models have been renamed, changing the suffix `Item` to `Element`
- Renamed MetadataItemContract.php to MetadataElementContract.php in the new namespace `Hyde\Framework\Modules\Metadata`
3 changes: 1 addition & 2 deletions packages/framework/src/Actions/StaticPageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public function __invoke(): string
{
Hyde::shareViewData($this->page);

$this->needsDirectory(Hyde::sitePath());
$this->needsDirectory(dirname(Hyde::sitePath($this->page->getOutputPath())));
$this->needsParentDirectory(Hyde::sitePath($this->page->getOutputPath()));

return $this->save($this->page->compile());
}
Expand Down
12 changes: 12 additions & 0 deletions packages/framework/src/Concerns/InteractsWithDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public static function needsDirectories(array $directories): void
static::needsDirectory($directory);
}
}

/**
* Ensure the supplied file's parent directory exists by creating it if it does not.
*
* @param string $file
* @param int $levels
* @return void
*/
public static function needsParentDirectory(string $file, int $levels = 1): void
{
static::needsDirectory(dirname($file, $levels));
}
}
3 changes: 1 addition & 2 deletions packages/framework/src/Modules/Metadata/MetadataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Hyde\Framework\Concerns\HydePage;
use Hyde\Framework\Helpers\Meta;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Modules\Metadata\Models\BaseMetadataElement;
use Illuminate\Contracts\Support\Htmlable;

/**
Expand Down Expand Up @@ -50,7 +49,7 @@ public function get(): array
);
}

public function add(BaseMetadataElement|string $item): static
public function add(MetadataElementContract|string $item): static
{
if ($item instanceof Models\LinkElement) {
$this->links[$item->uniqueKey()] = $item;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Hyde\Framework\Modules\Metadata;

interface MetadataElementContract extends \Stringable
{
public function __toString(): string;

public function uniqueKey(): string;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Hyde\Framework\Modules\Metadata\Models;

class LinkElement extends BaseMetadataElement
use Hyde\Framework\Modules\Metadata\MetadataElementContract;

class LinkElement implements MetadataElementContract
{
protected string $rel;
protected string $href;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Hyde\Framework\Modules\Metadata\Models;

class MetadataElement extends BaseMetadataElement
use Hyde\Framework\Modules\Metadata\MetadataElementContract;

class MetadataElement implements MetadataElementContract
{
protected string $name;
protected string $content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Hyde\Framework\Modules\Metadata\Models;

class OpenGraphElement extends BaseMetadataElement
use Hyde\Framework\Modules\Metadata\MetadataElementContract;

class OpenGraphElement implements MetadataElementContract
{
protected string $property;
protected string $content;
Expand Down
26 changes: 26 additions & 0 deletions packages/framework/tests/Feature/StaticPageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Hyde\Framework\Models\Pages\HtmlPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Models\Support\Site;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;

/**
* Feature tests for the StaticPageBuilder class.
Expand Down Expand Up @@ -137,4 +139,28 @@ public function test_creates_custom_documentation_directory()
$this->validateBasicHtml(file_get_contents(Hyde::path('_site/docs/foo/foo.html')));
unlink(Hyde::path('_site/docs/foo/foo.html'));
}

public function test_site_directory_can_be_customized()
{
Site::$outputPath = 'foo';

new StaticPageBuilder(MarkdownPage::make('foo'), true);

$this->assertFileExists(Hyde::path('foo/foo.html'));
$this->validateBasicHtml(file_get_contents(Hyde::path('foo/foo.html')));

File::deleteDirectory(Hyde::path('foo'));
}

public function test_site_directory_can_be_customized_with_nested_pages()
{
Site::$outputPath = 'foo';

new StaticPageBuilder(MarkdownPost::make('foo'), true);

$this->assertFileExists(Hyde::path('foo/posts/foo.html'));
$this->validateBasicHtml(file_get_contents(Hyde::path('foo/posts/foo.html')));

File::deleteDirectory(Hyde::path('foo'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ public function test_needs_directories_creates_multiple_directories()

File::deleteDirectory(Hyde::path('bar'));
}

public function test_needs_parent_directory_creates_directory_for_the_parent_file()
{
$this->needsParentDirectory(Hyde::path('foo/bar/baz.txt'));
$this->assertDirectoryExists(Hyde::path('foo/bar'));
}
}

0 comments on commit 5399eee

Please sign in to comment.