Skip to content

Commit

Permalink
Add the Core files (with temporary namespace)
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 21, 2022
1 parent c470d8d commit 816ad3a
Show file tree
Hide file tree
Showing 24 changed files with 1,495 additions and 15 deletions.
4 changes: 2 additions & 2 deletions composer.json
@@ -1,10 +1,10 @@
{
"name": "hyde/framework",
"name": "app/core",
"description": "The HydePHP Framework",
"license": "MIT",
"autoload": {
"psr-4": {
"Hyde\\Framework\\": "app/"
"App\\Core\\": "src/"
}
},
"authors": [
Expand Down
27 changes: 27 additions & 0 deletions src/Actions/ConvertsArrayToFrontMatter.php
@@ -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);
}
}
24 changes: 24 additions & 0 deletions src/Actions/ConvertsFooterMarkdown.php
@@ -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).'
));
}
}
34 changes: 34 additions & 0 deletions src/Actions/CreatesDefaultDirectories.php
@@ -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;
}
}
73 changes: 73 additions & 0 deletions src/Actions/CreatesNewMarkdownPostFile.php
@@ -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;
}
}
58 changes: 58 additions & 0 deletions src/Actions/GeneratesDocumentationSidebar.php
@@ -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;
}
}

0 comments on commit 816ad3a

Please sign in to comment.