Skip to content

Commit

Permalink
Merge pull request #292 from hydephp/add-new-url-path-helpers
Browse files Browse the repository at this point in the history
Add new URL path helpers
  • Loading branch information
caendesilva authored Jul 30, 2022
2 parents 55b4f98 + 6570ef9 commit a3ca0b0
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 20 deletions.
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ This serves two purposes:
2. At release time, you can move the Unreleased section changes into a new release version section.

### Added
- for new features.
- Added `Hyde::url()` and `Hyde::hasSiteUrl()` helpers, replacing now deprecated `Hyde::uriPath()` helper

### Changed
- internal: DiscoveryService.php is no longer deprecated
- internal: CollectionService.php was merged into DiscoveryService

### Deprecated
- for soon-to-be removed features.
- Deprecated `Hyde::uriPath()`, use `Hyde::url()` or `Hyde::hasSiteUrl()` instead

### Removed
- internal: CollectionService.php has been removed, all its functionality has been moved to DiscoveryService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

<article class="mt-4 mb-8" itemscope itemtype="http://schema.org/Article">
<meta itemprop="identifier" content="{{ $post->slug }}">
@if(Hyde::uriPath())
<meta itemprop="url" content="{{ Hyde::uriPath('posts/' . $post->slug) }}">
@if(Hyde::hasSiteUrl())
<meta itemprop="url" content="{{ Hyde::url('posts/' . $post->slug) }}">
@endif

<header>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<article aria-label="Article" id="{{ Hyde::uriPath() ?? '' }}posts/{{ $page->slug }}" itemscope itemtype="http://schema.org/Article"
<article aria-label="Article" id="{{ Hyde::url("posts/$page->slug", '') }}" itemscope itemtype="http://schema.org/Article"
@class(['post-article mx-auto prose dark:prose-invert', 'torchlight-enabled' => Hyde\Framework\Helpers\Features::hasTorchlight()])>
<meta itemprop="identifier" content="{{ $page->slug }}">
@if(Hyde::uriPath())
<meta itemprop="url" content="{{ Hyde::uriPath('posts/' . $page->slug) }}">
@if(Hyde::hasSiteUrl())
<meta itemprop="url" content="{{ Hyde::url('posts/' . $page->slug) }}">
@endif

<header aria-label="Header section" role="doc-pageheader">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function runPreflightCheck(): bool
if (! SitemapService::canGenerateSitemap()) {
$this->error('Cannot generate sitemap.xml, please check your configuration.');

if ((Hyde::uriPath() === false)) {
if (! Hyde::hasSiteUrl()) {
$this->warn('Hint: You don\'t have a site URL configured. Check config/hyde.php');
}
if (config('site.generate_sitemap', true) !== true) {
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Concerns/GeneratesPageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ protected function parseFrontMatterMetadata(): void
protected function makeOpenGraphPropertiesForArticle(): void
{
$this->properties['og:type'] = 'article';
if (Hyde::uriPath()) {
$this->properties['og:url'] = Hyde::uriPath(Hyde::pageLink('posts/'.$this->slug.'.html'));
if (Hyde::hasSiteUrl()) {
$this->properties['og:url'] = Hyde::url(Hyde::pageLink('posts/'.$this->slug.'.html'));
}

if (isset($this->matter['title'])) {
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/src/Concerns/HasPageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait HasPageMetadata
{
public function getCanonicalUrl(): string
{
return Hyde::uriPath(Hyde::pageLink($this->getCurrentPagePath().'.html'));
return Hyde::url(Hyde::pageLink($this->getCurrentPagePath().'.html'));
}

public function getDynamicMetadata(): array
Expand All @@ -29,14 +29,14 @@ public function getDynamicMetadata(): array
}

if ($this->canUseSitemapLink()) {
$array[] = '<link rel="sitemap" type="application/xml" title="Sitemap" href="'.Hyde::uriPath('sitemap.xml').'" />';
$array[] = '<link rel="sitemap" type="application/xml" title="Sitemap" href="'.Hyde::url('sitemap.xml').'" />';
}

if ($this->canUseRssFeedlink()) {
$array[] = '<link rel="alternate" type="application/rss+xml" title="'
.RssFeedService::getTitle()
.' RSS Feed" href="'
.Hyde::uriPath(RssFeedService::getDefaultOutputFilename())
.Hyde::url(RssFeedService::getDefaultOutputFilename())
.'" />';
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public function renderPageMetadata(): string

public function canUseCanonicalUrl(): bool
{
return Hyde::uriPath() && isset($this->slug);
return Hyde::hasSiteUrl() && isset($this->slug);
}

public function canUseSitemapLink(): bool
Expand Down
2 changes: 2 additions & 0 deletions packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* @method static RouteContract|null currentRoute()
* @method static string currentPage()
* @method static false|string uriPath(null|string $path = '')
* @method static bool hasSiteUrl()
* @method static string url(string $path = '', ?string $default = null)
* @method static void setBasePath($basePath)
* @method static void mixin(object $mixin, bool $replace = true)
* @method static string makeTitle(string $slug)
Expand Down
32 changes: 32 additions & 0 deletions packages/framework/src/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public function image(string $name): string
/**
* Return a qualified URI path, if SITE_URL is set in .env, else return false.
*
* @deprecated v0.53.0-beta - Use Hyde::url() or Hyde::hasSiteUrl() instead.
*
* @param string $path optional relative path suffix. Omit to return base url.
* @return string|false
*/
Expand All @@ -210,6 +212,36 @@ public function uriPath(string $path = ''): string|false
return false;
}

/**
* Check if a site base URL has been set in config (or .env).
*/
public function hasSiteUrl(): bool
{
return ! blank(config('site.url'));
}

/**
* Return a qualified URI path to the supplied path if a base URL is set.
*
* @param string $path optional relative path suffix. Omit to return base url.
* @param string|null $default optional default value to return if no site url is set.
* @return string
*
* @throws \Exception If no site URL is set and no default is provided
*/
public function url(string $path = '', ?string $default = null): string
{
if ($this->hasSiteUrl()) {
return rtrim(rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''), '/');
}

if ($default !== null) {
return $default.'/'.(trim($path, '/') ?? '');
}

throw new \Exception('No site URL has been set in config (or .env).');
}

/**
* Wrapper for the copy function, but allows choosing if files may be overwritten.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Models/Pages/MarkdownPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(array $matter = [], string $body = '', string $title

public function getCanonicalLink(): string
{
return Hyde::uriPath(Hyde::pageLink($this->getCurrentPagePath().'.html'));
return Hyde::url(Hyde::pageLink($this->getCurrentPagePath().'.html'));
}

public function getPostDescription(): string
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Models/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getLink(): string
/** @todo add to contract */
public function getPermalink(): string
{
return Hyde::uriPath(Hyde::pageLink($this->getOutputFilePath()));
return Hyde::url(Hyde::pageLink($this->getOutputFilePath()));
}

/** @inheritDoc */
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Services/RssFeedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function addAdditionalItemData(SimpleXMLElement $item, MarkdownPost $p

if (isset($post->image)) {
$image = $item->addChild('enclosure');
$image->addAttribute('url', isset($post->image->path) ? Hyde::uriPath('media/'.basename($post->image->path)) : $post->image->getSource());
$image->addAttribute('url', isset($post->image->path) ? Hyde::url('media/'.basename($post->image->path)) : $post->image->getSource());
$image->addAttribute('type', str_ends_with($post->image->getSource(), '.png') ? 'image/png' : 'image/jpeg');
$image->addAttribute('length', $post->image->getContentLength());
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public static function generateFeed(): string

public static function canGenerateFeed(): bool
{
return (Hyde::uriPath() !== false)
return Hyde::hasSiteUrl()
&& config('hyde.generate_rss_feed', true)
&& Features::hasBlogPosts()
&& extension_loaded('simplexml');
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Services/SitemapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static function generateSitemap(): string

public static function canGenerateSitemap(): bool
{
return (Hyde::uriPath() !== false)
return Hyde::hasSiteUrl()
&& config('site.generate_sitemap', true)
&& extension_loaded('simplexml');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Services/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function check_site_has_an_app_css_stylesheet(Result $result): Result

public function check_site_has_a_base_url_set(Result $result): Result
{
if ((bool) Hyde::uriPath() === true) {
if (Hyde::hasSiteUrl()) {
return $result->pass('Your site has a base URL set')
->withTip('This will allow Hyde to generate canonical URLs, sitemaps, RSS feeds, and more.');
}
Expand Down
3 changes: 3 additions & 0 deletions packages/framework/tests/Unit/HydeUriPathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use Hyde\Framework\Hyde;
use Hyde\Testing\TestCase;

/**
* @deprecated as method is deprecated
*/
class HydeUriPathHelperTest extends TestCase
{
public function test_helper_returns_false_when_no_site_url_is_set()
Expand Down
98 changes: 98 additions & 0 deletions packages/framework/tests/Unit/HydeUrlPathHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Hyde\Framework\Testing\Unit;

use Hyde\Framework\Hyde;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\HydeKernel::hasSiteUrl
* @covers \Hyde\Framework\HydeKernel::url
*/
class HydeUrlPathHelpersTest extends TestCase
{
public function test_has_site_url_returns_false_when_no_site_url_is_set()
{
config(['site.url' => null]);
$this->assertFalse(Hyde::hasSiteUrl());
}

public function test_has_site_url_returns_true_when_site_url_is_set()
{
config(['site.url' => 'https://example.com']);
$this->assertTrue(Hyde::hasSiteUrl());
}

// test that url returns the site url when no path is given
public function test_qualified_url_returns_site_url_when_no_path_is_given()
{
config(['site.url' => 'https://example.com']);
$this->assertEquals('https://example.com', Hyde::url());
}

// test that url returns the site url plus the given path
public function test_qualified_url_returns_site_url_plus_given_path()
{
config(['site.url' => 'https://example.com']);
$this->assertEquals('https://example.com/path', Hyde::url('path'));
}

// test that url returns the site url plus the given path with extension
public function test_qualified_url_returns_site_url_plus_given_path_with_extension()
{
config(['site.url' => 'https://example.com']);
$this->assertEquals('https://example.com/path.html', Hyde::url('path.html'));
}

// test that url returns the site url plus the given path with extension and query string
public function test_qualified_url_returns_site_url_plus_given_path_with_extension_and_query_string()
{
config(['site.url' => 'https://example.com']);
$this->assertEquals('https://example.com/path.html?query=string', Hyde::url('path.html?query=string'));
}

// test that url trims trailing slashes
public function test_qualified_url_trims_trailing_slashes()
{
config(['site.url' => 'https://example.com/']);
$this->assertEquals('https://example.com', Hyde::url());
$this->assertEquals('https://example.com', Hyde::url('/'));
$this->assertEquals('https://example.com/foo', Hyde::url('/foo/'));
}

// test that url accepts multiple schemes
public function test_qualified_url_accepts_multiple_schemes()
{
config(['site.url' => 'http://example.com']);
$this->assertEquals('http://example.com', Hyde::url());
}

// test that url throws an exception when no site url is set
public function test_qualified_url_throws_exception_when_no_site_url_is_set()
{
config(['site.url' => null]);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No site URL has been set in config (or .env).');
Hyde::url();
}

// test that url uses default parameter when supplied and no site url is set
public function test_qualified_url_uses_default_parameter_when_no_site_url_is_set()
{
config(['site.url' => null]);
$this->assertEquals('bar/foo', Hyde::url('foo', 'bar'));
}

// test that url does not use default parameter when supplied and a site url is set
public function test_qualified_url_does_not_use_default_parameter_when_site_url_is_set()
{
config(['site.url' => 'https://example.com']);
$this->assertEquals('https://example.com/foo', Hyde::url('foo', 'bar'));
}

public function test_helper_returns_expected_string_when_site_url_is_set()
{
config(['site.url' => 'https://example.com']);
$this->assertEquals('https://example.com/foo/bar.html', Hyde::url('foo/bar.html'));
}
}

0 comments on commit a3ca0b0

Please sign in to comment.