Skip to content

Commit

Permalink
Merge pull request #660 from hydephp/customizable-source-roots
Browse files Browse the repository at this point in the history
Add an easy configuration option to store all source directories in a subfolder
  • Loading branch information
caendesilva authored Nov 12, 2022
2 parents 604c70d + f641cb7 commit f4f5f57
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 22 deletions.
13 changes: 13 additions & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
'port' => env('SERVER_PORT', 8080),
],

/*
|--------------------------------------------------------------------------
| Source Root Directory
|--------------------------------------------------------------------------
|
| HydePHP will by default look for the underscored source directories in the
| root of your project. For example, you might want everything in a 'src'
| subdirectory. That's easy enough, just set the value below to "src"!
|
*/

'source_root' => '',

/*
|--------------------------------------------------------------------------
| Global Site Meta Tags
Expand Down
13 changes: 13 additions & 0 deletions packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
'port' => env('SERVER_PORT', 8080),
],

/*
|--------------------------------------------------------------------------
| Source Root Directory
|--------------------------------------------------------------------------
|
| HydePHP will by default look for the underscored source directories in the
| root of your project. For example, you might want everything in a 'src'
| subdirectory. That's easy enough, just set the value below to "src"!
|
*/

'source_root' => '',

/*
|--------------------------------------------------------------------------
| Global Site Meta Tags
Expand Down
10 changes: 10 additions & 0 deletions packages/framework/src/Foundation/Concerns/ManagesHydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public function getBasePath(): string
{
return $this->basePath;
}

public function setSourceRoot(string $sourceRoot): void
{
$this->sourceRoot = rtrim($sourceRoot, '/\\');
}

public function getSourceRoot(): string
{
return $this->sourceRoot;
}
}
4 changes: 3 additions & 1 deletion packages/framework/src/Foundation/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HydeKernel implements Arrayable, JsonSerializable
protected static HydeKernel $instance;

protected string $basePath;
protected string $sourceRoot;

protected Filesystem $filesystem;
protected Hyperlinks $hyperlinks;
Expand All @@ -58,9 +59,10 @@ class HydeKernel implements Arrayable, JsonSerializable

public const VERSION = '1.0.0-dev';

public function __construct(?string $basePath = null)
public function __construct(?string $basePath = null, string $sourceRoot = '')
{
$this->setBasePath($basePath ?? getcwd());
$this->setSourceRoot($sourceRoot);
$this->filesystem = new Filesystem($this);
$this->hyperlinks = new Hyperlinks($this);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/framework/src/Framework/Actions/PublishesHydeViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Hyde\Hyde;
use Illuminate\Support\Facades\File;

/*
* Publish one or more of the Hyde Blade views.
*
* @see \Hyde\Framework\Testing\Feature\Actions\PublishesHomepageViewTest
*/
/**
* Publish one or more of the Hyde Blade views.
*
* @see \Hyde\Framework\Testing\Feature\Actions\PublishesHomepageViewTest
*/
class PublishesHydeViews
{
public static array $options = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Framework\Concerns;

use Hyde\Facades\Site;
use Hyde\Hyde;
use Hyde\Pages\Concerns\HydePage;

/**
Expand All @@ -20,7 +21,8 @@ trait RegistersFileLocations
{
/**
* Register the default source directories for the given page classes.
* Location string should be relative to the root of the application.
* Location string should be relative to the source root, which is
* usually the root of the project.
*
* @example registerSourceDirectories([HydePage::class => '_pages'])
*
Expand All @@ -30,7 +32,7 @@ protected function registerSourceDirectories(array $directoryMapping): void
{
foreach ($directoryMapping as $class => $location) {
/** @var HydePage $class */
$class::$sourceDirectory = unslash($location);
$class::$sourceDirectory = unslash(Hyde::getSourceRoot().'/'.unslash($location));
}
}

Expand Down Expand Up @@ -73,6 +75,6 @@ protected function discoverBladeViewsIn(string $directory): void
*/
protected function storeCompiledSiteIn(string $directory): void
{
Site::$outputPath = $directory;
Site::$outputPath = unslash($directory);
}
}
9 changes: 7 additions & 2 deletions packages/framework/src/Framework/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Hyde\Markdown\MarkdownConverter;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\HtmlPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Illuminate\Support\Facades\Blade;
Expand All @@ -41,21 +42,25 @@ public function register(): void
return new MarkdownConverter();
});

Hyde::setSourceRoot(config('hyde.source_root', ''));

$this->registerSourceDirectories([
HtmlPage::class => '_pages',
BladePage::class => '_pages',
MarkdownPage::class => '_pages',
MarkdownPost::class => '_posts',
DocumentationPage::class => '_docs',
]);

$this->registerOutputDirectories([
HtmlPage::class => '',
BladePage::class => '',
MarkdownPage::class => '',
MarkdownPost::class => 'posts',
DocumentationPage::class => unslash(config('docs.output_directory', 'docs')),
DocumentationPage::class => config('docs.output_directory', 'docs'),
]);

$this->storeCompiledSiteIn(unslash(config('site.output_directory', '_site')));
$this->storeCompiledSiteIn(config('site.output_directory', '_site'));

$this->discoverBladeViewsIn(BladePage::sourceDirectory());

Expand Down
18 changes: 11 additions & 7 deletions packages/framework/src/Framework/Services/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Hyde\Facades\Features;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Support\Models\ValidationResult as Result;

/**
Expand Down Expand Up @@ -42,8 +45,8 @@ public function check_validators_can_run(Result $result): Result

public function check_site_has_a_404_page(Result $result): Result
{
if (file_exists(Hyde::path('_pages/404.md'))
|| file_exists(Hyde::path('_pages/404.blade.php'))
if (file_exists(MarkdownPage::path('404.md'))
|| file_exists(BladePage::path('404.blade.php'))
) {
return $result->pass('Your site has a 404 page');
}
Expand All @@ -54,8 +57,9 @@ public function check_site_has_a_404_page(Result $result): Result

public function check_site_has_an_index_page(Result $result): Result
{
if (file_exists(Hyde::path('_pages/index.md'))
|| file_exists('_pages/index.blade.php')) {
if (file_exists(MarkdownPage::path('index.md'))
|| file_exists(BladePage::path('index.blade.php'))
) {
return $result->pass('Your site has an index page');
}

Expand All @@ -75,11 +79,11 @@ public function check_documentation_site_has_an_index_page(Result $result): Resu
->withTip('Skipped because: There are no documentation pages');
}

if (file_exists('_docs/index.md')) {
if (file_exists(DocumentationPage::path('index.md'))) {
return $result->pass('Your documentation site has an index page');
}

if (file_exists('_docs/README.md')) {
if (file_exists(DocumentationPage::path('README.md'))) {
return $result->fail('Could not find an index.md file in the _docs directory!')
->withTip('However, a _docs/readme.md file was found. A suggestion would be to copy the _docs/readme.md to _docs/index.md.');
}
Expand All @@ -89,7 +93,7 @@ public function check_documentation_site_has_an_index_page(Result $result): Resu

public function check_site_has_an_app_css_stylesheet(Result $result): Result
{
if (file_exists(Hyde::path('_site/media/app.css')) || file_exists(Hyde::path('_media/app.css'))) {
if (file_exists(Hyde::sitePath('media/app.css')) || file_exists(Hyde::path('_media/app.css'))) {
return $result->pass('Your site has an app.css stylesheet');
}

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 @@ -38,6 +38,7 @@
* @method static string makeTitle(string $slug)
* @method static string currentPage()
* @method static string getBasePath()
* @method static string getSourceRoot()
* @method static Features features()
* @method static FileCollection files()
* @method static PageCollection pages()
Expand All @@ -51,6 +52,7 @@
* @method static bool unlink(array|string $path)
* @method static void setInstance(HydeKernel $instance)
* @method static void setBasePath(string $basePath)
* @method static void setSourceRoot(string $sourceRoot)
* @method static void shareViewData(HydePage $page)
* @method static array toArray()
* @method static void boot()
Expand Down
9 changes: 9 additions & 0 deletions packages/framework/src/Pages/Concerns/HydePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Support\Models\Route;
use Hyde\Support\Models\RouteKey;
use function unslash;

/**
* The base class for all Hyde pages.
Expand Down Expand Up @@ -149,6 +150,14 @@ public static function outputPath(string $identifier): string
return RouteKey::fromPage(static::class, $identifier).'.html';
}

/**
* Get an absolute file path to the page's source directory, or a file within it.
*/
public static function path(string $path = ''): string
{
return Hyde::path(unslash(static::sourceDirectory().'/'.unslash($path)));
}

/**
* Compile the page into static HTML.
*
Expand Down
74 changes: 74 additions & 0 deletions packages/framework/tests/ConfigurableSourceRootsFeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing;

use function app;
use function config;
use function file_put_contents;
use Hyde\Framework\HydeServiceProvider;
use Hyde\Hyde;
use Hyde\Pages\MarkdownPage;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\File;
use function mkdir;

/**
* Test the overall functionality of the configurable source roots feature.
*
* Also see these tests which cover specific implementation details:
*
* @see \Hyde\Framework\Testing\Feature\HydeKernelTest
* @see \Hyde\Framework\Testing\Unit\HydeServiceProviderTest
*/
class ConfigurableSourceRootsFeatureTest extends TestCase
{
public function test_default_config_value_is_empty_string()
{
$this->assertSame('', config('hyde.source_root'));
}

public function test_files_in_custom_source_root_can_be_discovered()
{
$this->setupCustomSourceRoot();

$this->assertCount(1, MarkdownPage::files());
$this->assertCount(1, MarkdownPage::all());

File::deleteDirectory(Hyde::path('custom'));
}

public function test_files_in_custom_source_root_can_be_compiled()
{
$this->setupCustomSourceRoot();

$this->artisan('build');

$this->assertFileExists(Hyde::path('_site/markdown.html'));

File::deleteDirectory(Hyde::path('custom'));
File::deleteDirectory(Hyde::path('_site'));
}

public function test_hyde_page_path_method_supports_custom_source_roots()
{
config(['hyde.source_root' => 'custom']);
(new HydeServiceProvider(app()))->register();

$this->assertSame(
Hyde::path('custom/_pages/foo.md'), MarkdownPage::path('foo.md')
);
}

protected function setupCustomSourceRoot(): void
{
mkdir(Hyde::path('custom'));
mkdir(Hyde::path('custom/_pages'));

config(['hyde.source_root' => 'custom']);
(new HydeServiceProvider(app()))->register();

file_put_contents(Hyde::path('custom/_pages/markdown.md'), 'Hello, world!');
}
}
11 changes: 11 additions & 0 deletions packages/framework/tests/Feature/HydeKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,15 @@ public function test_version_method_returns_version_constant()
{
$this->assertSame(HydeKernel::VERSION, Hyde::version());
}

public function test_can_get_source_root()
{
$this->assertEquals('', Hyde::getSourceRoot());
}

public function test_can_set_source_root()
{
Hyde::setSourceRoot('foo');
$this->assertEquals('foo', Hyde::getSourceRoot());
}
}
Loading

0 comments on commit f4f5f57

Please sign in to comment.