Skip to content

Commit

Permalink
Merge pull request #1218 from hydephp/finalize-page-model-classes
Browse files Browse the repository at this point in the history
Finalize page model classes and related helpers
  • Loading branch information
caendesilva authored Mar 6, 2023
2 parents 25efb9b + 171532c commit c70e912
Show file tree
Hide file tree
Showing 38 changed files with 607 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Hyde\Console\Concerns\AsksToRebuildSite;
use Hyde\Console\Concerns\Command;
use Hyde\Framework\Services\ViewDiffService;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use function array_key_exists;
Expand Down Expand Up @@ -113,7 +113,7 @@ protected function canExistingFileBeOverwritten(): bool
return true;
}

if (! file_exists(Hyde::getBladePagePath('index.blade.php'))) {
if (! file_exists(BladePage::path('index.blade.php'))) {
return true;
}

Expand All @@ -122,6 +122,6 @@ protected function canExistingFileBeOverwritten(): bool

protected function isTheExistingFileADefaultOne(): bool
{
return ViewDiffService::checksumMatchesAny(unixsum_file(Hyde::getBladePagePath('index.blade.php')));
return ViewDiffService::checksumMatchesAny(unixsum_file(BladePage::path('index.blade.php')));
}
}
14 changes: 9 additions & 5 deletions packages/framework/src/Console/Commands/RebuildPageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Hyde\Framework\Features\BuildTasks\BuildTask;
use Hyde\Framework\Services\BuildService;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Illuminate\Console\OutputStyle;
use function dirname;
use function file_exists;
Expand Down Expand Up @@ -82,11 +86,11 @@ protected function validate(): void
$directory = Hyde::pathToRelative(dirname($this->path));

$directories = [
Hyde::pathToRelative(Hyde::getBladePagePath()),
Hyde::pathToRelative(Hyde::getBladePagePath()),
Hyde::pathToRelative(Hyde::getMarkdownPagePath()),
Hyde::pathToRelative(Hyde::getMarkdownPostPath()),
Hyde::pathToRelative(Hyde::getDocumentationPagePath()),
Hyde::pathToRelative(BladePage::path()),
Hyde::pathToRelative(BladePage::path()),
Hyde::pathToRelative(MarkdownPage::path()),
Hyde::pathToRelative(MarkdownPost::path()),
Hyde::pathToRelative(DocumentationPage::path()),
];

if (! in_array($directory, $directories)) {
Expand Down
25 changes: 0 additions & 25 deletions packages/framework/src/Foundation/Concerns/ForwardsFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,6 @@ public function vendorPath(string $path = '', string $package = 'framework'): st
return $this->filesystem->vendorPath($path, $package);
}

public function getModelSourcePath(string $model, string $path = ''): string
{
return $this->filesystem->getModelSourcePath($model, $path);
}

public function getBladePagePath(string $path = ''): string
{
return $this->filesystem->getBladePagePath($path);
}

public function getMarkdownPagePath(string $path = ''): string
{
return $this->filesystem->getMarkdownPagePath($path);
}

public function getMarkdownPostPath(string $path = ''): string
{
return $this->filesystem->getMarkdownPostPath($path);
}

public function getDocumentationPagePath(string $path = ''): string
{
return $this->filesystem->getDocumentationPagePath($path);
}

public function mediaPath(string $path = ''): string
{
return $this->filesystem->mediaPath($path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getFile(string $filePath): SourceFile
public function getFiles(?string $pageClass = null): FileCollection
{
return $pageClass ? $this->filter(function (SourceFile $file) use ($pageClass): bool {
return $file->model === $pageClass;
return $file->pageClass === $pageClass;
}) : $this;
}
}
44 changes: 0 additions & 44 deletions packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
namespace Hyde\Foundation\Kernel;

use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Foundation\HydeKernel;
use Hyde\Foundation\PharSupport;
use Hyde\Pages\DocumentationPage;
use Illuminate\Support\Collection;
use function Hyde\normalize_slashes;
use function Hyde\path_join;
Expand Down Expand Up @@ -189,46 +185,6 @@ public function unlinkIfExists(string $path): bool
return false;
}

/**
* Fluent file helper methods.
*
* @param class-string<\Hyde\Pages\Concerns\HydePage> $model
*
* 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 function getModelSourcePath(string $model, string $path = ''): string
{
if (empty($path)) {
return $this->path($model::sourceDirectory());
}

return $this->path(path_join($model::sourceDirectory(), unslash($path)));
}

public function getBladePagePath(string $path = ''): string
{
return $this->getModelSourcePath(BladePage::class, $path);
}

public function getMarkdownPagePath(string $path = ''): string
{
return $this->getModelSourcePath(MarkdownPage::class, $path);
}

public function getMarkdownPostPath(string $path = ''): string
{
return $this->getModelSourcePath(MarkdownPost::class, $path);
}

public function getDocumentationPagePath(string $path = ''): string
{
return $this->getModelSourcePath(DocumentationPage::class, $path);
}

public function smartGlob(string $pattern, int $flags = 0): Collection
{
return collect(\Hyde\Facades\Filesystem::glob($pattern, $flags))
Expand Down
10 changes: 7 additions & 3 deletions packages/framework/src/Foundation/Kernel/PageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function addPage(HydePage $page): void
protected function runDiscovery(): void
{
$this->kernel->files()->each(function (SourceFile $file): void {
$this->addPage($file->model::parse(
$file->model::pathToIdentifier($file->getPath())
));
$this->addPage($this->parsePage($file->pageClass, $file->getPath()));
});
}

Expand All @@ -47,6 +45,12 @@ protected function runExtensionCallbacks(): void
}
}

/** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
protected static function parsePage(string $pageClass, string $path)
{
return $pageClass::parse($pageClass::pathToIdentifier($path));
}

public function getPage(string $sourcePath): HydePage
{
return $this->get($sourcePath) ?? throw new FileNotFoundException($sourcePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ class CreatesNewPageSourceFile
protected string $subDir = '';
protected bool $force;

public function __construct(string $title, string $type = MarkdownPage::class, bool $force = false)
public function __construct(string $title, string $pageClass = MarkdownPage::class, bool $force = false)
{
$this->validateType($type);
$this->validateType($pageClass);

$this->title = $this->parseTitle($title);
$this->filename = $this->fileName($title);
$this->force = $force;

$this->outputPath = $this->makeOutputPath($type);
$this->outputPath = $this->makeOutputPath($pageClass);

$this->createPage($type);
$this->createPage($pageClass);
}

public function getOutputPath(): string
Expand Down Expand Up @@ -80,11 +80,11 @@ protected function makeOutputPath(string $pageClass): string
return Hyde::path($pageClass::sourcePath($this->formatIdentifier()));
}

protected function createPage(string $type): void
protected function createPage(string $pageClass): void
{
$this->failIfFileCannotBeSaved($this->outputPath);

match ($type) {
match ($pageClass) {
BladePage::class => $this->createBladeFile(),
MarkdownPage::class => $this->createMarkdownFile(),
DocumentationPage::class => $this->createDocumentationFile(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Hyde\Framework\Exceptions\FileNotFoundException;

/**
* Validate the existence of a Page model's source file.
* Validate the existence of a Page class's source file.
*
* @see \Hyde\Framework\Testing\Unit\ValidatesExistenceTest
*/
Expand All @@ -17,13 +17,13 @@ trait ValidatesExistence
/**
* Check if a supplied source file exists or throw an exception.
*
* @param class-string<\Hyde\Pages\Concerns\HydePage> $model
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass
*
* @throws FileNotFoundException If the file does not exist.
*/
protected static function validateExistence(string $model, string $identifier): void
protected static function validateExistence(string $pageClass, string $identifier): void
{
$filepath = $model::sourcePath($identifier);
$filepath = $pageClass::sourcePath($identifier);

if (Filesystem::missing($filepath)) {
throw new FileNotFoundException($filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Hyde\Support\Contracts\SerializableContract;

/**
* Experimental class to contain the core data for a page being constructed.
* Data class to contain the core data for a page being constructed.
*
* It should contain immutable data known at the very start of construction.
* In addition to the front matter and markdown, the data should contain
Expand Down
32 changes: 16 additions & 16 deletions packages/framework/src/Framework/Factories/Concerns/HasFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@

trait HasFactory
{
public function constructFactoryData(PageDataFactory $factory): void
{
foreach ($factory->toArray() as $key => $value) {
$this->{$key} = $value;
}
}

protected function constructPageSchemas(): void
{
$this->constructFactoryData(new HydePageDataFactory($this->toCoreDataObject()));

if ($this instanceof MarkdownPost) {
$this->constructFactoryData(new BlogPostDataFactory($this->toCoreDataObject()));
}
}

public function toCoreDataObject(): CoreDataObject
{
return new CoreDataObject(
Expand All @@ -38,4 +22,20 @@ public function toCoreDataObject(): CoreDataObject
$this->getRouteKey(),
);
}

protected function constructFactoryData(): void
{
$this->assignFactoryData(new HydePageDataFactory($this->toCoreDataObject()));

if ($this instanceof MarkdownPost) {
$this->assignFactoryData(new BlogPostDataFactory($this->toCoreDataObject()));
}
}

protected function assignFactoryData(PageDataFactory $factory): void
{
foreach ($factory->toArray() as $key => $value) {
$this->{$key} = $value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ abstract class PageDataFactory implements Arrayable
*/
public const SCHEMA = [];

/**
* Get the generated data as an associative array.
*
* @return array<string, mixed>
*/
abstract public function toArray(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,35 @@ final class NavigationData extends ArrayObject implements NavigationSchema, Seri
{
use Serializable;

public readonly ?string $label;
public readonly string $label;
public readonly int $priority;
public readonly bool $hidden;
public readonly ?string $group;
public readonly ?bool $hidden;
public readonly ?int $priority;

public function __construct(?string $label = null, ?string $group = null, ?bool $hidden = null, ?int $priority = null)
public function __construct(string $label, int $priority, bool $hidden, string $group = null)
{
$this->label = $label;
$this->group = $group;
$this->hidden = $hidden;
$this->priority = $priority;
$this->hidden = $hidden;
$this->group = $group;

parent::__construct($this->toArray());
}

/** @param array{label: string|null, group: string|null, hidden: bool|null, priority: int|null} $data */
/** @param array{label: string, priority: int, hidden: bool, group: string|null} $data */
public static function make(array $data): self
{
return new self(
$data['label'] ?? null,
$data['group'] ?? null,
$data['hidden'] ?? null,
$data['priority'] ?? null,
);
return new self(...$data);
}

/** @return array{label: string|null, group: string|null, hidden: bool|null, priority: int|null} */
/** @return array{label: string, priority: int, hidden: bool, group: string|null} */
public function toArray(): array
{
return [
'label' => $this->label,
'group' => $this->group,
'hidden' => $this->hidden,
'priority' => $this->priority,
'hidden' => $this->hidden,
'group' => $this->group,
];
}
}
10 changes: 5 additions & 5 deletions packages/framework/src/Framework/Services/MarkdownService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MarkdownService
{
use SetsUpMarkdownConverter;

public string $markdown;
public ?string $sourceModel = null;
protected string $markdown;
protected ?string $pageClass = null;

protected array $config = [];
protected array $extensions = [];
Expand All @@ -48,9 +48,9 @@ class MarkdownService
protected array $preprocessors = [];
protected array $postprocessors = [];

public function __construct(string $markdown, ?string $sourceModel = null)
public function __construct(string $markdown, ?string $pageClass = null)
{
$this->sourceModel = $sourceModel;
$this->pageClass = $pageClass;
$this->markdown = $markdown;
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public function withPermalinks(): static

public function isDocumentationPage(): bool
{
return isset($this->sourceModel) && $this->sourceModel === DocumentationPage::class;
return isset($this->pageClass) && $this->pageClass === DocumentationPage::class;
}

public function withTableOfContents(): static
Expand Down
Loading

0 comments on commit c70e912

Please sign in to comment.