2024-04-17 11:41:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\Traits;
|
|
|
|
|
|
|
|
use App\Classes\MarkdownManager;
|
|
|
|
|
|
|
|
trait ManagesMarkdown
|
|
|
|
{
|
|
|
|
private array $markdownManagers = [];
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
/**
|
|
|
|
* Return an instance of markdown manager for specified filename
|
|
|
|
*/
|
|
|
|
public function markdown(?string $filename = 'index'): MarkdownManager
|
|
|
|
{
|
|
|
|
return $this->registerMarkdownManager($filename);
|
|
|
|
}
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
/**
|
|
|
|
* Register a markdown manager for specified filename
|
|
|
|
*/
|
|
|
|
private function registerMarkdownManager(string $filename): MarkdownManager
|
|
|
|
{
|
|
|
|
$filename = $this->getFilenameInBundle($filename, '.md');
|
|
|
|
|
|
|
|
if (!array_key_exists($filename, $this->markdownManagers)) {
|
2024-04-20 23:27:47 +02:00
|
|
|
$this->markdownManagers[$filename] = new MarkdownManager($filename, $this);
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->markdownManagers[$filename];
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:24:45 +02:00
|
|
|
/**
|
|
|
|
* Load all markdown files at once
|
|
|
|
*/
|
|
|
|
private function loadMarkdown()
|
|
|
|
{
|
|
|
|
foreach ($this->markdownManagers as $manager) {
|
|
|
|
$manager->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
/**
|
|
|
|
* Save all markdown files that needs to be
|
|
|
|
*/
|
|
|
|
private function saveMarkdown()
|
|
|
|
{
|
2024-04-26 23:05:49 +02:00
|
|
|
$allSaved = true;
|
|
|
|
|
2024-04-17 11:41:10 +02:00
|
|
|
foreach ($this->markdownManagers as $manager) {
|
2024-04-26 23:05:49 +02:00
|
|
|
if (!$manager->save()) {
|
|
|
|
$allSaved = false;
|
|
|
|
}
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
2024-04-26 23:05:49 +02:00
|
|
|
|
|
|
|
return $allSaved;
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-20 23:27:47 +02:00
|
|
|
* Lint all markdowns in the bundle
|
2024-04-17 11:41:10 +02:00
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
private function lintMarkdown()
|
2024-04-17 11:41:10 +02:00
|
|
|
{
|
2024-04-20 23:27:47 +02:00
|
|
|
foreach ($this->markdownManagers as $manager) {
|
|
|
|
$manager->lint();
|
|
|
|
}
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
}
|