diff --git a/packages/framework/src/Concerns/Internal/FileHelpers.php b/packages/framework/src/Concerns/Internal/FileHelpers.php deleted file mode 100644 index faf0fecbe81..00000000000 --- a/packages/framework/src/Concerns/Internal/FileHelpers.php +++ /dev/null @@ -1,174 +0,0 @@ - 0) { - $route .= str_repeat('../', $nestCount); - } - $route .= static::pageLink($destination); - - return str_replace('//', '/', $route); - } - - /** - * Get the current page path, or fall back to the root path. - */ - public static function currentPage(): string - { - return View::shared('currentPage', ''); - } - - /** - * Get the current page route, or fall back to null. - */ - public static function currentRoute(): ?RouteContract - { - return View::shared('currentRoute'); - } - - /** - * Gets a relative web link to the given image stored in the _site/media folder. - * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. - */ - public static function image(string $name, string $current = null): string - { - if ($current === null) { - $current = static::currentPage(); - } - - if (str_starts_with($name, 'http')) { - return $name; - } - - return static::relativeLink('media/'.basename($name), $current); - } - - /** - * Return a qualified URI path, if SITE_URL is set in .env, else return false. - * - * @param string|null $path optional relative path suffix. Omit to return base url. - * @return string|false - */ - public static function uriPath(?string $path = ''): string|false - { - if (config('site.url', false)) { - return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); - } - - return false; - } - - /** - * Wrapper for the copy function, but allows choosing if files may be overwritten. - * - * @param string $from The source file path. - * @param string $to The destination file path. - * @param bool $force If true, existing files will be overwritten. - * @return bool|int Returns true|false on copy() success|failure, or an error code on failure - */ - public static function copy(string $from, string $to, bool $force = false): bool|int - { - if (! file_exists($from)) { - return 404; - } - - if (file_exists($to) && ! $force) { - return 409; - } - - return copy($from, $to); - } -} diff --git a/packages/framework/src/Concerns/Internal/FluentPathHelpers.php b/packages/framework/src/Concerns/Internal/FluentPathHelpers.php deleted file mode 100644 index 9fe05626943..00000000000 --- a/packages/framework/src/Concerns/Internal/FluentPathHelpers.php +++ /dev/null @@ -1,82 +0,0 @@ - 0) { + $route .= str_repeat('../', $nestCount); + } + $route .= static::pageLink($destination); + + return str_replace('//', '/', $route); + } + + /** + * Get the current page path, or fall back to the root path. + */ + public static function currentPage(): string + { + return View::shared('currentPage', ''); + } + + /** + * Get the current page route, or fall back to null. + */ + public static function currentRoute(): ?RouteContract + { + return View::shared('currentRoute'); + } + + /** + * Gets a relative web link to the given image stored in the _site/media folder. + * Since v0.50.x you no longer have to supply a current page as it will be automatically retrieved from the View. + */ + public static function image(string $name, string $current = null): string + { + if ($current === null) { + $current = static::currentPage(); + } + + if (str_starts_with($name, 'http')) { + return $name; + } + + return static::relativeLink('media/'.basename($name), $current); + } + + /** + * Return a qualified URI path, if SITE_URL is set in .env, else return false. + * + * @param string|null $path optional relative path suffix. Omit to return base url. + * @return string|false + */ + public static function uriPath(?string $path = ''): string|false + { + if (config('site.url', false)) { + return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); + } + + return false; + } + + /** + * Wrapper for the copy function, but allows choosing if files may be overwritten. + * + * @param string $from The source file path. + * @param string $to The destination file path. + * @param bool $force If true, existing files will be overwritten. + * @return bool|int Returns true|false on copy() success|failure, or an error code on failure + */ + public static function copy(string $from, string $to, bool $force = false): bool|int + { + if (! file_exists($from)) { + return 404; + } + + if (file_exists($to) && ! $force) { + return 409; + } + + return copy($from, $to); + } + + /** + * Fluent file helper methods. + * + * Provides a more fluent way of getting either the absolute path + * to a model's source directory, or an absolute path to a file within it. + * + * These are intended to be used as a dynamic alternative to legacy code + * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo') + */ + public static function getModelSourcePath(string $model, string $path = ''): string + { + if (empty($path)) { + return static::path(DiscoveryService::getFilePathForModelClassFiles($model)); + } + + $path = unslash($path); + + return static::path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); + } + + public static function getBladePagePath(string $path = ''): string + { + return static::getModelSourcePath(BladePage::class, $path); + } + + public static function getMarkdownPagePath(string $path = ''): string + { + return static::getModelSourcePath(MarkdownPage::class, $path); + } + + public static function getMarkdownPostPath(string $path = ''): string + { + return static::getModelSourcePath(MarkdownPost::class, $path); + } + + public static function getDocumentationPagePath(string $path = ''): string + { + return static::getModelSourcePath(DocumentationPage::class, $path); + } + + /** + * Get the absolute path to the compiled site directory, or a file within it. + */ + public static function getSiteOutputPath(string $path = ''): string + { + if (empty($path)) { + return StaticPageBuilder::$outputPath; + } + + $path = unslash($path); + + return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path; + } + + /** + * Decode an absolute path created with a Hyde::path() helper into its relative counterpart. + */ + public static function pathToRelative(string $path): string + { + return str_starts_with($path, static::path()) ? unslash(str_replace( + static::path(), + '', + $path + )) : $path; + } } diff --git a/packages/framework/src/Services/DiscoveryService.php b/packages/framework/src/Services/DiscoveryService.php index 1ee7aa83101..5ea73060a8c 100644 --- a/packages/framework/src/Services/DiscoveryService.php +++ b/packages/framework/src/Services/DiscoveryService.php @@ -13,7 +13,7 @@ * helper methods for source file autodiscovery used in the building * process to determine where files are located and how to parse them. * - * @deprecated v0.48.0 as autodiscovery is now powered by the Router. The helpers here can be moved to FluentPathHelpers.php + * @deprecated v0.48.0 as autodiscovery is now powered by the Router. */ class DiscoveryService { diff --git a/packages/framework/tests/Feature/FluentPathHelpersTest.php b/packages/framework/tests/Feature/FluentPathHelpersTest.php index 7ff91376234..0f9b2efe0de 100644 --- a/packages/framework/tests/Feature/FluentPathHelpersTest.php +++ b/packages/framework/tests/Feature/FluentPathHelpersTest.php @@ -12,7 +12,7 @@ /** * Class FluentPathHelpersTest. * - * @covers \Hyde\Framework\Concerns\Internal\FluentPathHelpers + * @covers \Hyde\Framework\Hyde */ class FluentPathHelpersTest extends TestCase { diff --git a/packages/framework/tests/Feature/HydeHelperFacadeTest.php b/packages/framework/tests/Feature/HydeHelperFacadeTest.php index 0a607baffe9..17293091d8c 100644 --- a/packages/framework/tests/Feature/HydeHelperFacadeTest.php +++ b/packages/framework/tests/Feature/HydeHelperFacadeTest.php @@ -7,7 +7,7 @@ use Hyde\Testing\TestCase; /** - * @covers \Hyde\Framework\Helpers\HydeHelperFacade + * @covers \Hyde\Framework\Hyde */ class HydeHelperFacadeTest extends TestCase { diff --git a/packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php b/packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php index 79cc2e46499..ae3fdd7024c 100644 --- a/packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php +++ b/packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php @@ -8,7 +8,7 @@ /** * Class FileHelperPageLinkPrettyUrlTest. * - * @covers \Hyde\Framework\Concerns\Internal\FileHelpers + * @covers \Hyde\Framework\Hyde */ class FileHelperPageLinkPrettyUrlTest extends TestCase { diff --git a/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php b/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php index f2baa6f1410..8239d43032b 100644 --- a/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php +++ b/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php @@ -8,7 +8,7 @@ /** * Class FileHelperRelativeLinkTest. * - * @covers \Hyde\Framework\Concerns\Internal\FileHelpers + * @covers \Hyde\Framework\Hyde */ class FileHelperRelativeLinkTest extends TestCase { diff --git a/packages/framework/tests/Unit/FileHelpersImageTest.php b/packages/framework/tests/Unit/FileHelpersImageTest.php index 209da6730a3..fa641baf021 100644 --- a/packages/framework/tests/Unit/FileHelpersImageTest.php +++ b/packages/framework/tests/Unit/FileHelpersImageTest.php @@ -6,7 +6,7 @@ use Hyde\Testing\TestCase; /** - * @covers \Hyde\Framework\Concerns\Internal\FileHelpers::image + * @covers \Hyde\Framework\Hyde::image */ class FileHelpersImageTest extends TestCase { diff --git a/packages/framework/tests/Unit/HydeHelperFacadeMakeTitleTest.php b/packages/framework/tests/Unit/HydeHelperFacadeMakeTitleTest.php index 03f0d4a04aa..2fb3257c9a9 100644 --- a/packages/framework/tests/Unit/HydeHelperFacadeMakeTitleTest.php +++ b/packages/framework/tests/Unit/HydeHelperFacadeMakeTitleTest.php @@ -2,44 +2,44 @@ namespace Hyde\Framework\Testing\Unit; -use Hyde\Framework\Helpers\HydeHelperFacade; +use Hyde\Framework\Hyde; use Hyde\Testing\TestCase; class HydeHelperFacadeMakeTitleTest extends TestCase { public function test_make_title_helper_parses_kebab_case_into_title() { - $this->assertEquals('Hello World', HydeHelperFacade::makeTitle('hello-world')); + $this->assertEquals('Hello World', Hyde::makeTitle('hello-world')); } public function test_make_title_helper_parses_snake_case_into_title() { - $this->assertEquals('Hello World', HydeHelperFacade::makeTitle('hello_world')); + $this->assertEquals('Hello World', Hyde::makeTitle('hello_world')); } public function test_make_title_helper_parses_camel_case_into_title() { - $this->assertEquals('Hello World', HydeHelperFacade::makeTitle('helloWorld')); + $this->assertEquals('Hello World', Hyde::makeTitle('helloWorld')); } public function test_make_title_helper_parses_pascal_case_into_title() { - $this->assertEquals('Hello World', HydeHelperFacade::makeTitle('HelloWorld')); + $this->assertEquals('Hello World', Hyde::makeTitle('HelloWorld')); } public function test_make_title_helper_parses_title_case_into_title() { - $this->assertEquals('Hello World', HydeHelperFacade::makeTitle('Hello World')); + $this->assertEquals('Hello World', Hyde::makeTitle('Hello World')); } public function test_make_title_helper_parses_title_case_with_spaces_into_title() { - $this->assertEquals('Hello World', HydeHelperFacade::makeTitle('Hello World')); + $this->assertEquals('Hello World', Hyde::makeTitle('Hello World')); } public function test_make_title_helper_does_not_capitalize_auxiliary_words() { $this->assertEquals('The a an the in on by with of and or but', - HydeHelperFacade::makeTitle('the_a_an_the_in_on_by_with_of_and_or_but')); + Hyde::makeTitle('the_a_an_the_in_on_by_with_of_and_or_but')); } }