Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new command to scaffold pages #55

Merged
merged 11 commits into from
Apr 1, 2022
131 changes: 131 additions & 0 deletions src/Actions/CreatesNewPageSourceFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Hyde\Framework\Actions;

use Exception;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\MarkdownPage;
use Illuminate\Support\Str;

/**
* Scaffold a new Markdown or Blade page.
*/
class CreatesNewPageSourceFile
{
/**
* The Page title.
*
* @var string
*/
public string $title;

/**
* The Page slug.
*/
public string $slug;

/**
* The file path.
*/
public string $path;

/**
* Construct the class.
*
* @param string $title - The page title, will be used to generate the slug
* @param string $type - The page type, either 'markdown' or 'blade'
* @param bool $force - Overwrite any existing files
*
* @throws Exception if the page type is not 'markdown' or 'blade'
*/
public function __construct(string $title, string $type = MarkdownPage::class, public bool $force = false)
{
$this->title = $title;
$this->slug = Str::slug($title);

$this->createPage($type);
}

/**
* Check if the file can be saved.
*
* @throws Exception if the file already exists and cannot be overwritten
*/
public function canSaveFile(string $path): void
{
if (file_exists($path) && ! $this->force) {
throw new Exception("File $path already exists!", 409);
}
}

/**
* Create the page.
*
* @param string $type - The page type, either 'markdown' or 'blade'
* @return int|false the size of the file created, or false on failure.
*
* @throws Exception if the page type is not 'markdown' or 'blade'
*/
public function createPage(string $type): int|false
{
// Check that the page type is either 'markdown' or 'blade'
if ($type === MarkdownPage::class) {
return $this->createMarkdownFile();
}
if ($type === BladePage::class) {
return $this->createBladeFile();
}

throw new Exception('The page type must be either "markdown" or "blade"');
}

/**
* Create the Markdown file.
*
* @return int|false the size of the file created, or false on failure.
*
* @throws Exception if the file cannot be saved.
*/
public function createMarkdownFile(): int|false
{
$this->path = Hyde::path("_pages/$this->slug.md");

$this->canSaveFile($this->path);

return file_put_contents(
$this->path,
"---\ntitle: $this->title\n---\n\n# $this->title\n"
);
}

/**
* Create the Blade file.
*
* @return int|false the size of the file created, or false on failure.
*
* @throws Exception if the file cannot be saved.
*/
public function createBladeFile(): int|false
{
$this->path = Hyde::path("resources/views/pages/$this->slug.blade.php");

$this->canSaveFile($this->path);

return file_put_contents(
$this->path,
<<<EOF
@extends('hyde::layouts.app')
@section('content')
@php(\$title = "$this->title")

<main class="mx-auto max-w-7xl py-16 px-8">
<h1 class="text-center text-3xl font-bold">$this->title</h1>
</main>

@endsection

EOF
);
}
}
94 changes: 94 additions & 0 deletions src/Commands/HydeMakePageCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Hyde\Framework\Commands;

use Exception;
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\MarkdownPage;
use LaravelZero\Framework\Commands\Command;

/**
* Hyde Command to scaffold a new Markdown or Blade page file.
*/
class HydeMakePageCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:page
{title : The name of the page file to create. Will be used to generate the slug}
{--type=markdown : The type of page to create (markdown or blade)}
{--force : Overwrite any existing files}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Scaffold a new Markdown or Blade page file';

/**
* The page title.
*/
public string $title;

/**
* The page type.
*/
public string $type;

/**
* Can the file be overwritten?
*/
public bool $force;

/**
* Execute the console command.
*
* @return int the exit code of the command.
*
* @throws Exception if the page type is invalid.
*/
public function handle(): int
{
$this->title('Creating a new page!');

$this->title = $this->argument('title');

$this->validateOptions();

$this->force = $this->option('force') ?? false;

$creator = new CreatesNewPageSourceFile($this->title, $this->type, $this->force);

$this->info("Created file $creator->path");

return 0;
}

/**
* Validate the options passed to the command.
*
* @return void
*
* @throws Exception if the page type is invalid.
*/
protected function validateOptions(): void
{
$type = strtolower($this->option('type') ?? 'markdown');

if (! in_array($type, ['markdown', 'blade'])) {
throw new Exception("Invalid page type: $type", 400);
}

// Set the type to the fully qualified class name
if ($type === 'markdown') {
$this->type = MarkdownPage::class;
} else {
$this->type = BladePage::class;
}
}
}
1 change: 1 addition & 0 deletions src/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function () {
Commands\HydeMakeValidatorCommand::class,
Commands\HydePublishStubsCommand::class,
Commands\HydeMakePostCommand::class,
Commands\HydeMakePageCommand::class,
Commands\HydeValidateCommand::class,
Commands\HydeDebugCommand::class,
]);
Expand Down