Skip to content

Commit

Permalink
Add helpers to make it easier to refactor source paths
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 15, 2022
1 parent 927fe66 commit 10e145e
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Concerns/Internal/SourcePathHelpers.php
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/Hyde.php
Expand Up @@ -3,6 +3,7 @@
namespace Hyde\Framework;

use Composer\InstalledVersions;
use Hyde\Framework\Concerns\Internal\SourcePathHelpers;
use Hyde\Framework\Concerns\Internal\AssetManager;
use Hyde\Framework\Concerns\Internal\FileHelpers;
use Hyde\Framework\Services\CollectionService;
Expand All @@ -22,6 +23,7 @@ class Hyde
{
use FileHelpers;
use AssetManager;
use SourcePathHelpers;

protected static string $basePath;

Expand Down
97 changes: 97 additions & 0 deletions tests/Unit/SourcePathHelpersTest.php
@@ -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()
);
}
}

0 comments on commit 10e145e

Please sign in to comment.