Skip to content

Code refactors #650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
561d639
Add helper to add a child element and automatically escape it
caendesilva Nov 6, 2022
6c347e6
Move helper to base class
caendesilva Nov 6, 2022
cd7f680
Use the custom helper in more places
caendesilva Nov 6, 2022
0daf9c7
Merge branch 'refactor-xml-services'
caendesilva Nov 6, 2022
a5843c1
Merge branch 'refactor-xml-services' into code-refactors
caendesilva Nov 6, 2022
29bf618
Move unit test to the unit test suite
caendesilva Nov 6, 2022
fd878b2
Remove already resolved todo
caendesilva Nov 6, 2022
44efad6
Revert "Remove already resolved todo"
caendesilva Nov 6, 2022
5b3ac72
Update todo to what I think it meant to say
caendesilva Nov 6, 2022
0a5965d
Test site build command respects custom output path
caendesilva Nov 6, 2022
d7b2875
Add additional assertions
caendesilva Nov 6, 2022
ab81311
Shorten local variable name
caendesilva Nov 6, 2022
57157bd
Add todo
caendesilva Nov 6, 2022
750d07a
Strongly type the closure types
caendesilva Nov 6, 2022
0c7553b
Update local variable name to better match type
caendesilva Nov 6, 2022
6e6d19f
Strongly type closure return values
caendesilva Nov 6, 2022
d5bba8c
Add todo
caendesilva Nov 6, 2022
87473cd
Apply fixes from StyleCI
StyleCIBot Nov 7, 2022
8843ea6
Merge branch 'master' into code-refactors
caendesilva Nov 7, 2022
1378ded
Type hint against the contract
caendesilva Nov 7, 2022
c9af683
Apply fixes from StyleCI
StyleCIBot Nov 7, 2022
0445797
Import namespace as alias
caendesilva Nov 7, 2022
2bb888d
Apply fixes from StyleCI
StyleCIBot Nov 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/**
* @internal Single-use trait for the HydeKernel class.
*
* @todo Consider if this logic is better suited for a "Render" class solely for handling data related to the current render.
*
* @see \Hyde\Foundation\HydeKernel
*/
trait ManagesViewData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Hyde\Facades\Features;
use Hyde\Facades\Meta;
use Hyde\Framework\Features\Metadata\MetadataElementContract as Element;
use Hyde\Framework\Features\XmlGenerators\RssFeedGenerator;
use Hyde\Hyde;
use Hyde\Pages\Concerns\HydePage;
Expand All @@ -18,38 +19,38 @@ class GlobalMetadataBag extends MetadataBag
{
public static function make(): static
{
$metadataBag = new self();
$metadata = new self();

foreach (config('hyde.meta', []) as $item) {
$metadataBag->add($item);
$metadata->add($item);
}

if (Features::sitemap()) {
$metadataBag->add(Meta::link('sitemap', Hyde::url('sitemap.xml'), [
$metadata->add(Meta::link('sitemap', Hyde::url('sitemap.xml'), [
'type' => 'application/xml', 'title' => 'Sitemap',
]));
}

if (Features::rss()) {
$metadataBag->add(Meta::link('alternate', Hyde::url(RssFeedGenerator::getFilename()), [
$metadata->add(Meta::link('alternate', Hyde::url(RssFeedGenerator::getFilename()), [
'type' => 'application/rss+xml', 'title' => RssFeedGenerator::getDescription(),
]));
}

if (Hyde::currentPage() !== null) {
static::filterDuplicateMetadata($metadataBag, View::shared('page'));
static::filterDuplicateMetadata($metadata, View::shared('page'));
}

return $metadataBag;
return $metadata;
}

protected static function filterDuplicateMetadata(GlobalMetadataBag $global, HydePage $page): void
{
// Reject any metadata from the global metadata bag that is already present in the page metadata bag.

foreach (['links', 'metadata', 'properties', 'generics'] as $type) {
$global->$type = array_filter($global->$type, fn ($meta) => ! in_array($meta->uniqueKey(),
array_map(fn ($meta) => $meta->uniqueKey(), $page->metadata->$type)
$global->$type = array_filter($global->$type, fn (Element $element): bool => ! in_array($element->uniqueKey(),
array_map(fn (Element $element): string => $element->uniqueKey(), $page->metadata->$type)
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
/**
* Holds the metadata tags for a page or the site model.
*
* @todo These properties should probably not be public
*
* @see \Hyde\Framework\Testing\Feature\MetadataTest
* @see \Hyde\Framework\Features\Metadata\PageMetadataBag
* @see \Hyde\Framework\Features\Metadata\GlobalMetadataBag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ protected function escape(string $string): string
{
return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, 'UTF-8');
}

protected function addChild(SimpleXMLElement $element, string $name, string $value): SimpleXMLElement
{
return $element->addChild($name, $this->escape($value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ protected function constructBaseElement(): void
protected function addItem(MarkdownPost $post): void
{
$item = $this->xmlElement->channel->addChild('item');
$item->addChild('title', $post->title);
$item->addChild('description', $post->description);
$this->addChild($item, 'title', $post->title);
$this->addChild($item, 'description', $post->description);

$this->addDynamicItemData($item, $post);
}

protected function addDynamicItemData(SimpleXMLElement $item, MarkdownPost $post): void
{
if (isset($post->canonicalUrl)) {
$item->addChild('link', $post->canonicalUrl);
$item->addChild('guid', $post->canonicalUrl);
$this->addChild($item, 'link', $post->canonicalUrl);
$this->addChild($item, 'guid', $post->canonicalUrl);
}

if (isset($post->date)) {
$item->addChild('pubDate', $post->date->dateTimeObject->format(DATE_RSS));
$this->addChild($item, 'pubDate', $post->date->dateTimeObject->format(DATE_RSS));
}

if (isset($post->author)) {
$item->addChild('dc:creator', $post->author->getName(), 'http://purl.org/dc/elements/1.1/');
}

if (isset($post->category)) {
$item->addChild('category', $post->category);
$this->addChild($item, 'category', $post->category);
}

if (isset($post->image)) {
Expand All @@ -79,12 +79,12 @@ protected function addBaseChannelItems(): void
{
$channel = $this->xmlElement->channel;

$channel->addChild('title', $this->escape(Site::name()));
$channel->addChild('link', $this->escape(Site::url()));
$channel->addChild('description', $this->escape($this->getDescription()));
$channel->addChild('language', config('site.language', 'en'));
$channel->addChild('generator', 'HydePHP '.Hyde::version());
$channel->addChild('lastBuildDate', date(DATE_RSS));
$this->addChild($channel, 'title', Site::name());
$this->addChild($channel, 'link', Site::url());
$this->addChild($channel, 'description', $this->getDescription());
$this->addChild($channel, 'language', config('site.language', 'en'));
$this->addChild($channel, 'generator', 'HydePHP '.Hyde::version());
$this->addChild($channel, 'lastBuildDate', date(DATE_RSS));
}

protected function addAtomLinkItem(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ protected function addRoute(Route $route): void
{
$urlItem = $this->xmlElement->addChild('url');

$urlItem->addChild('loc', $this->escape(Hyde::url($route->getOutputPath())));
$urlItem->addChild('lastmod', $this->escape($this->getLastModDate($route->getSourcePath())));
$urlItem->addChild('changefreq', 'daily');
$this->addChild($urlItem, 'loc', Hyde::url($route->getOutputPath()));
$this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath()));
$this->addChild($urlItem, 'changefreq', 'daily');

if (config('hyde.sitemap.dynamic_priority', true)) {
$urlItem->addChild('priority', $this->getPriority(
$this->addChild($urlItem, 'priority', $this->getPriority(
$route->getPageClass(), $route->getPage()->getIdentifier()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Hyde\Framework\Testing\Feature;
namespace Hyde\Framework\Testing\Unit;

use Hyde\Facades\Site;
use Hyde\Framework\HydeServiceProvider;
Expand All @@ -13,12 +13,24 @@

/**
* Class BuildOutputDirectoryCanBeChangedTest.
*
* @todo add test for the Rebuild Service
* @todo this unit test should be moved to the unit test suite
*/
class BuildOutputDirectoryCanBeChangedTest extends TestCase
{
public function test_site_output_directory_can_be_changed_for_site_builds()
{
$this->file('_posts/test-post.md');

Site::$outputPath = ('_site/build');

$this->artisan('build');

$this->assertFileExists(Hyde::path('_site/build/posts/test-post.html'));
$this->assertFileExists(Hyde::path('_site/build/media/app.css'));
$this->assertFileExists(Hyde::path('_site/build/index.html'));

File::deleteDirectory(Hyde::path('_site/build'));
}

public function test_site_output_directory_can_be_changed_in_static_page_builder()
{
$this->file('_posts/test-post.md');
Expand Down