2
2
3
3
namespace Hyde \Framework \Actions ;
4
4
5
- use Exception ;
5
+ use Hyde \Framework \Exceptions \FileConflictException ;
6
+ use Hyde \Framework \Exceptions \UnsupportedPageTypeException ;
6
7
use Hyde \Framework \Hyde ;
7
8
use Hyde \Framework \Models \BladePage ;
8
9
use Hyde \Framework \Models \DocumentationPage ;
16
17
*/
17
18
class CreatesNewPageSourceFile
18
19
{
19
- /**
20
- * The Page title.
21
- *
22
- * @var string
23
- */
24
20
public string $ title ;
25
-
26
- /**
27
- * The Page slug.
28
- */
29
21
public string $ slug ;
22
+ public string $ outputPath ;
30
23
31
- /**
32
- * The file path.
33
- */
34
- public string $ path ;
35
-
36
- /**
37
- * Construct the class.
38
- *
39
- * @param string $title - The page title, will be used to generate the slug
40
- * @param string $type - The page type, FQDN of the page class
41
- * @param bool $force - Overwrite any existing files?
42
- *
43
- * @throws Exception if the page type is not supported or the file already exists
44
- */
45
24
public function __construct (string $ title , string $ type = MarkdownPage::class, public bool $ force = false )
46
25
{
47
26
$ this ->title = $ title ;
@@ -50,25 +29,13 @@ public function __construct(string $title, string $type = MarkdownPage::class, p
50
29
$ this ->createPage ($ type );
51
30
}
52
31
53
- /**
54
- * Check if the file can be saved.
55
- *
56
- * @throws Exception if the file already exists and cannot be overwritten
57
- */
58
32
public function canSaveFile (string $ path ): void
59
33
{
60
34
if (file_exists ($ path ) && ! $ this ->force ) {
61
- throw new Exception ( " File $ path already exists! " , 409 );
35
+ throw new FileConflictException ( $ path );
62
36
}
63
37
}
64
38
65
- /**
66
- * Create the page.
67
- *
68
- * @param string $type FQDN of the page class
69
- *
70
- * @throws Exception if the page type is not supported
71
- */
72
39
public function createPage (string $ type ): int |false
73
40
{
74
41
if ($ type === MarkdownPage::class) {
@@ -82,39 +49,29 @@ public function createPage(string $type): int|false
82
49
return $ this ->createDocumentationFile ();
83
50
}
84
51
85
- throw new Exception ('The page type must be either "markdown", "blade", or "documentation" ' );
52
+ throw new UnsupportedPageTypeException ('The page type must be either "markdown", "blade", or "documentation" ' );
86
53
}
87
54
88
- /**
89
- * Create the Markdown file.
90
- *
91
- * @throws Exception if the file cannot be saved.
92
- */
93
55
public function createMarkdownFile (): int |false
94
56
{
95
- $ this ->path = Hyde::path ("_pages/ $ this ->slug .md " );
57
+ $ this ->outputPath = Hyde::path ("_pages/ $ this ->slug .md " );
96
58
97
- $ this ->canSaveFile ($ this ->path );
59
+ $ this ->canSaveFile ($ this ->outputPath );
98
60
99
61
return file_put_contents (
100
- $ this ->path ,
62
+ $ this ->outputPath ,
101
63
"--- \ntitle: $ this ->title \n--- \n\n# $ this ->title \n"
102
64
);
103
65
}
104
66
105
- /**
106
- * Create the Blade file.
107
- *
108
- * @throws Exception if the file cannot be saved.
109
- */
110
67
public function createBladeFile (): int |false
111
68
{
112
- $ this ->path = Hyde::path ("_pages/ $ this ->slug .blade.php " );
69
+ $ this ->outputPath = Hyde::path ("_pages/ $ this ->slug .blade.php " );
113
70
114
- $ this ->canSaveFile ($ this ->path );
71
+ $ this ->canSaveFile ($ this ->outputPath );
115
72
116
73
return file_put_contents (
117
- $ this ->path ,
74
+ $ this ->outputPath ,
118
75
<<<EOF
119
76
@extends('hyde::layouts.app')
120
77
@section('content')
@@ -130,19 +87,14 @@ public function createBladeFile(): int|false
130
87
);
131
88
}
132
89
133
- /**
134
- * Create the Documentation file.
135
- *
136
- * @throws Exception if the file cannot be saved.
137
- */
138
90
public function createDocumentationFile (): int |false
139
91
{
140
- $ this ->path = Hyde::path ("_docs/ $ this ->slug .md " );
92
+ $ this ->outputPath = Hyde::path ("_docs/ $ this ->slug .md " );
141
93
142
- $ this ->canSaveFile ($ this ->path );
94
+ $ this ->canSaveFile ($ this ->outputPath );
143
95
144
96
return file_put_contents (
145
- $ this ->path ,
97
+ $ this ->outputPath ,
146
98
"# $ this ->title \n"
147
99
);
148
100
}
0 commit comments