-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers to make it easier to refactor source paths
- Loading branch information
1 parent
927fe66
commit 10e145e
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Concerns\Internal; | ||
|
||
use Hyde\Framework\Services\DiscoveryService; | ||
|
||
use Hyde\Framework\Models\BladePage; | ||
use Hyde\Framework\Models\MarkdownPage; | ||
use Hyde\Framework\Models\MarkdownPost; | ||
use Hyde\Framework\Models\DocumentationPage; | ||
|
||
/** | ||
* Offloads file helper methods for the Hyde Facade. | ||
* | ||
* 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') | ||
* | ||
* @see \Hyde\Framework\Hyde | ||
* @see \Tests\Unit\SourcePathHelpersTest | ||
*/ | ||
trait SourcePathHelpers | ||
{ | ||
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); | ||
} | ||
|
||
public static function getModelSourcePath(string $model, string $path = ''): string | ||
{ | ||
if (empty($path)) { | ||
return static::path(DiscoveryService::getFilePathForModelClassFiles($model)); | ||
} | ||
|
||
$path = trim($path, '/\\'); | ||
|
||
return static::path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Tests\TestCase; | ||
|
||
use Hyde\Framework\Models\BladePage; | ||
use Hyde\Framework\Models\MarkdownPage; | ||
use Hyde\Framework\Models\MarkdownPost; | ||
use Hyde\Framework\Models\DocumentationPage; | ||
|
||
/** | ||
* Class SourcePathHelpersTest. | ||
* | ||
* @covers \Hyde\Framework\Concerns\Internal\SourcePathHelpers | ||
*/ | ||
class SourcePathHelpersTest extends TestCase | ||
{ | ||
public function test_get_model_source_path_method_returns_path_for_model_classes() | ||
{ | ||
$this->assertEquals( | ||
Hyde::path('_posts'), | ||
Hyde::getModelSourcePath(MarkdownPost::class) | ||
); | ||
|
||
$this->assertEquals( | ||
Hyde::path('_pages'), | ||
Hyde::getModelSourcePath(MarkdownPage::class) | ||
); | ||
|
||
$this->assertEquals( | ||
Hyde::path('_docs'), | ||
Hyde::getModelSourcePath(DocumentationPage::class) | ||
); | ||
|
||
$this->assertEquals( | ||
Hyde::path('_pages'), | ||
Hyde::getModelSourcePath(BladePage::class) | ||
); | ||
} | ||
|
||
public function test_get_model_source_path_method_returns_path_to_file_for_model_classes() | ||
{ | ||
$this->assertEquals( | ||
Hyde::path('_posts'.DIRECTORY_SEPARATOR.'foo.md'), | ||
Hyde::getModelSourcePath(MarkdownPost::class, 'foo.md') | ||
); | ||
|
||
$this->assertEquals( | ||
Hyde::path('_pages'.DIRECTORY_SEPARATOR.'foo.md'), | ||
Hyde::getModelSourcePath(MarkdownPage::class, 'foo.md') | ||
); | ||
|
||
$this->assertEquals( | ||
Hyde::path('_docs'.DIRECTORY_SEPARATOR.'foo.md'), | ||
Hyde::getModelSourcePath(DocumentationPage::class, 'foo.md') | ||
); | ||
|
||
$this->assertEquals( | ||
Hyde::path('_pages'.DIRECTORY_SEPARATOR.'foo.md'), | ||
Hyde::getModelSourcePath(BladePage::class, 'foo.md') | ||
); | ||
} | ||
|
||
public function test_helper_for_blade_pages() | ||
{ | ||
$this->assertEquals( | ||
Hyde::path('_pages'), | ||
Hyde::getBladePagePath() | ||
); | ||
} | ||
|
||
public function test_helper_for_markdown_pages() | ||
{ | ||
$this->assertEquals( | ||
Hyde::path('_pages'), | ||
Hyde::getMarkdownPagePath() | ||
); | ||
} | ||
|
||
public function test_helper_for_markdown_posts() | ||
{ | ||
$this->assertEquals( | ||
Hyde::path('_posts'), | ||
Hyde::getMarkdownPostPath() | ||
); | ||
} | ||
|
||
public function test_helper_for_documentation_pages() | ||
{ | ||
$this->assertEquals( | ||
Hyde::path('_docs'), | ||
Hyde::getDocumentationPagePath() | ||
); | ||
} | ||
} |