Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| CreatesNewPageSourceFile | |
100.00% |
29 / 29 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| canSaveFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| createPage | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| createMarkdownFile | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createBladeFile | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createDocumentationFile | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Actions; |
| 4 | |
| 5 | use Hyde\Framework\Exceptions\FileConflictException; |
| 6 | use Hyde\Framework\Exceptions\UnsupportedPageTypeException; |
| 7 | use Hyde\Framework\Hyde; |
| 8 | use Hyde\Framework\Models\BladePage; |
| 9 | use Hyde\Framework\Models\DocumentationPage; |
| 10 | use Hyde\Framework\Models\MarkdownPage; |
| 11 | use Illuminate\Support\Str; |
| 12 | |
| 13 | /** |
| 14 | * Scaffold a new Markdown, Blade, or documentation page. |
| 15 | * |
| 16 | * @see \Tests\Feature\Actions\CreatesNewPageSourceFileTest |
| 17 | */ |
| 18 | class CreatesNewPageSourceFile |
| 19 | { |
| 20 | public string $title; |
| 21 | public string $slug; |
| 22 | public string $outputPath; |
| 23 | |
| 24 | public function __construct(string $title, string $type = MarkdownPage::class, public bool $force = false) |
| 25 | { |
| 26 | $this->title = $title; |
| 27 | $this->slug = Str::slug($title); |
| 28 | |
| 29 | $this->createPage($type); |
| 30 | } |
| 31 | |
| 32 | public function canSaveFile(string $path): void |
| 33 | { |
| 34 | if (file_exists($path) && ! $this->force) { |
| 35 | throw new FileConflictException($path); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public function createPage(string $type): int|false |
| 40 | { |
| 41 | if ($type === MarkdownPage::class) { |
| 42 | return $this->createMarkdownFile(); |
| 43 | } |
| 44 | if ($type === BladePage::class) { |
| 45 | return $this->createBladeFile(); |
| 46 | } |
| 47 | |
| 48 | if ($type === DocumentationPage::class) { |
| 49 | return $this->createDocumentationFile(); |
| 50 | } |
| 51 | |
| 52 | throw new UnsupportedPageTypeException('The page type must be either "markdown", "blade", or "documentation"'); |
| 53 | } |
| 54 | |
| 55 | public function createMarkdownFile(): int|false |
| 56 | { |
| 57 | $this->outputPath = Hyde::path("_pages/$this->slug.md"); |
| 58 | |
| 59 | $this->canSaveFile($this->outputPath); |
| 60 | |
| 61 | return file_put_contents( |
| 62 | $this->outputPath, |
| 63 | "---\ntitle: $this->title\n---\n\n# $this->title\n" |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | public function createBladeFile(): int|false |
| 68 | { |
| 69 | $this->outputPath = Hyde::path("_pages/$this->slug.blade.php"); |
| 70 | |
| 71 | $this->canSaveFile($this->outputPath); |
| 72 | |
| 73 | return file_put_contents( |
| 74 | $this->outputPath, |
| 75 | <<<EOF |
| 76 | @extends('hyde::layouts.app') |
| 77 | @section('content') |
| 78 | @php(\$title = "$this->title") |
| 79 | |
| 80 | <main class="mx-auto max-w-7xl py-16 px-8"> |
| 81 | <h1 class="text-center text-3xl font-bold">$this->title</h1> |
| 82 | </main> |
| 83 | |
| 84 | @endsection |
| 85 | |
| 86 | EOF |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | public function createDocumentationFile(): int|false |
| 91 | { |
| 92 | $this->outputPath = Hyde::path("_docs/$this->slug.md"); |
| 93 | |
| 94 | $this->canSaveFile($this->outputPath); |
| 95 | |
| 96 | return file_put_contents( |
| 97 | $this->outputPath, |
| 98 | "# $this->title\n" |
| 99 | ); |
| 100 | } |
| 101 | } |