-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Core files (with temporary namespace)
- Loading branch information
1 parent
c470d8d
commit 816ad3a
Showing
24 changed files
with
1,495 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Core\Actions; | ||
|
||
class ConvertsArrayToFrontMatter | ||
{ | ||
/** | ||
* Convert an array into YAML Front Matter. | ||
* | ||
* @todo add support for nested arrays | ||
* | ||
* @param array $array | ||
* @return string $yaml front matter | ||
*/ | ||
public function execute(array $array): string | ||
{ | ||
$yaml = []; | ||
$yaml[] = "---"; | ||
foreach ($array as $key => $value) { | ||
$yaml[] = "$key: $value"; | ||
} | ||
|
||
$yaml[] = '---'; | ||
$yaml[] = ''; | ||
return implode("\n", $yaml); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Core\Actions; | ||
|
||
use Illuminate\Support\Str; | ||
use function config; | ||
|
||
class ConvertsFooterMarkdown | ||
{ | ||
/** | ||
* Convert the Markdown text if supplied in the config, | ||
* or fall back to default to generate HTML for the footer. | ||
* | ||
* @return string $html | ||
*/ | ||
public static function execute(): string | ||
{ | ||
return Str::markdown(config( | ||
'hyde.footer.markdown', | ||
'Site built with the Free and Open Source [HydePHP](https://github.com/hydephp/hyde). | ||
License [MIT](https://github.com/hydephp/hyde/blob/master/LICENSE.md).' | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Core\Actions; | ||
|
||
use App\Core\Hyde; | ||
use JetBrains\PhpStorm\Pure; | ||
|
||
class CreatesDefaultDirectories | ||
{ | ||
protected array $requiredDirectories = [ | ||
'_drafts', | ||
'_pages', | ||
'_posts', | ||
'_media', | ||
'_site', | ||
'_docs', | ||
'_site/posts', | ||
'_site/media', | ||
'_site/docs', | ||
]; | ||
|
||
public function __invoke(): void | ||
{ | ||
foreach ($this->requiredDirectories as $directory) { | ||
// Does the directory exist? // Otherwise, create it. | ||
is_dir(Hyde::path($directory)) || mkdir(Hyde::path($directory)); | ||
} | ||
} | ||
|
||
#[Pure] public static function getRequiredDirectories(): array | ||
{ | ||
return (new CreatesDefaultDirectories)->requiredDirectories; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace App\Core\Actions; | ||
|
||
use App\Core\Hyde; | ||
use Exception; | ||
use Illuminate\Support\Str; | ||
|
||
class CreatesNewMarkdownPostFile | ||
{ | ||
public string $title; | ||
public string $description; | ||
public string $category; | ||
public string $author; | ||
public string $date; | ||
public string $slug; | ||
|
||
/** | ||
* Construct the class. | ||
* | ||
* @param string $title | ||
* @param string|null $description | ||
* @param string|null $category | ||
* @param string|null $author | ||
* @param string|null $date | ||
* @param string|null $slug | ||
*/ | ||
public function __construct( | ||
string $title, | ||
?string $description, | ||
?string $category, | ||
?string $author, | ||
?string $date = null, | ||
?string $slug = null | ||
) { | ||
$this->title = $title ?? 'My Awesome Blog Post'; | ||
$this->description = $description ?? 'A short description used in previews and SEO'; | ||
$this->category = $category ?? 'blog'; | ||
$this->author = $author ?? 'Mr. Hyde'; | ||
if ($date === null) { | ||
$this->date = date('Y-m-d H:i'); | ||
} | ||
if ($slug === null) { | ||
$this->slug = Str::slug($title) ; | ||
} | ||
} | ||
|
||
/** | ||
* Save the class object to a Markdown file. | ||
* | ||
* @param bool $force Should the file be created even if a file with the same path already exists? | ||
* @return string|false Returns the path to the file if successful, or false if the file could not be saved. | ||
* @throws Exception if a file with the same slug already exists and the force flag is not set. | ||
*/ | ||
public function save(bool $force = false): string|false | ||
{ | ||
|
||
$path = Hyde::path("_posts/$this->slug.md"); | ||
|
||
if ($force !== true && file_exists($path)) { | ||
throw new Exception("File at $path already exists! ", 409); | ||
} | ||
|
||
$arrayWithoutSlug = ((array) $this); | ||
|
||
unset($arrayWithoutSlug['slug']); | ||
|
||
$contents = (new ConvertsArrayToFrontMatter)->execute($arrayWithoutSlug) . | ||
"\n## Write something awesome.\n\n"; | ||
|
||
return file_put_contents($path, $contents) ? $path : false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Core\Actions; | ||
|
||
use App\Core\Models\DocumentationPage; | ||
use App\Core\Services\CollectionService; | ||
use Illuminate\Support\Str; | ||
use function config; | ||
|
||
/** | ||
* Create the sidebar items for the documentation page. | ||
*/ | ||
class GeneratesDocumentationSidebar | ||
{ | ||
/** | ||
* Create and get the array. | ||
* | ||
* @param string $current | ||
* @return array | ||
*/ | ||
public static function get(string $current = ""): array | ||
{ | ||
$orderArray = config('hyde.documentationPageOrder'); | ||
|
||
foreach ($orderArray as $key => $value) { | ||
$orderArray[$key] = Str::slug($value); | ||
} | ||
|
||
$array = []; | ||
|
||
foreach (CollectionService::getSourceSlugsOfModels(DocumentationPage::class) as $slug) { | ||
if ($slug == 'index') { | ||
continue; | ||
} | ||
|
||
$order = array_search($slug, $orderArray); | ||
|
||
if ($order === false) { | ||
$order = 999; | ||
} | ||
|
||
$array[] = [ | ||
'slug' => $slug, | ||
'title' => Str::title(str_replace('-', ' ', $slug)), | ||
'active' => 'docs/' . $slug == $current, | ||
'order' => $order, | ||
]; | ||
} | ||
|
||
krsort($array); | ||
|
||
usort($array, function ($a, $b) { | ||
return $a['order'] <=> $b['order']; | ||
}); | ||
|
||
return $array; | ||
} | ||
} |
Oops, something went wrong.