83 lines
1.5 KiB
PHP
83 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Classes;
|
|
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
|
|
class MarkdownManager
|
|
{
|
|
/**
|
|
* Original markdown content
|
|
*/
|
|
private ?string $originalContent = null;
|
|
|
|
/**
|
|
* Current markdown content
|
|
*/
|
|
private ?string $content = null;
|
|
|
|
public function __construct(protected string $filename, protected FilesystemAdapter $disk)
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* Return a boolean value indicating if the file actually exists on disk
|
|
*/
|
|
public function exists()
|
|
{
|
|
return $this->disk->exists($this->filename);
|
|
}
|
|
|
|
/**
|
|
* Load markdown file
|
|
*/
|
|
public function load()
|
|
{
|
|
$content = $this->disk->get($this->filename);
|
|
|
|
$this->set($content);
|
|
}
|
|
|
|
/**
|
|
* Return current markdown content
|
|
*/
|
|
public function get(): ?string
|
|
{
|
|
return $this->content;
|
|
}
|
|
|
|
/**
|
|
* Set current markdown content
|
|
*/
|
|
public function set(?string $content = null): void
|
|
{
|
|
$this->content = $content;
|
|
}
|
|
|
|
/**
|
|
* Return a boolean value indicating if the file has changed since it was
|
|
* loaded
|
|
*/
|
|
public function isDirty(): bool
|
|
{
|
|
return $this->originalContent !== $this->content;
|
|
}
|
|
|
|
/**
|
|
* Store file on disk
|
|
*/
|
|
public function save(): bool
|
|
{
|
|
if (!$this->isDirty()) {
|
|
return false;
|
|
}
|
|
|
|
$this->disk->put($this->filename, $this->content);
|
|
|
|
$this->originalContent = $this->content;
|
|
|
|
return true;
|
|
}
|
|
}
|