148 lines
3.7 KiB
PHP
148 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Classes;
|
|
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Class MetadataManager
|
|
*
|
|
* This class is designed to handle metadata for articles or any other items that
|
|
* might have associated metadata stored in a JSON format. It uses Laravel's
|
|
* Collections for flexible data manipulation and the disk for storage.
|
|
*/
|
|
class MetadataManager
|
|
{
|
|
protected $originalContent;
|
|
|
|
protected $content;
|
|
|
|
/**
|
|
* Constructor for the MetadataManager class.
|
|
*
|
|
* @param string $filename The filename where metadata is stored.
|
|
* @param disk $disk The disk abstraction provided by Laravel.
|
|
*/
|
|
public function __construct(protected string $filename, protected FilesystemAdapter $disk)
|
|
{
|
|
$this->load();
|
|
}
|
|
|
|
/**
|
|
* Loads metadata from disk using the disk, with caching to improve performance.
|
|
*/
|
|
public function load()
|
|
{
|
|
$data = $this->readFromDisk();
|
|
|
|
$this->originalContent = new Collection($data);
|
|
$this->content = new Collection($data);
|
|
}
|
|
|
|
/**
|
|
* Reads metadata from disk. Caches the content to reduce file system reads.
|
|
*
|
|
* @return array The data read from the file.
|
|
*/
|
|
protected function readFromDisk()
|
|
{
|
|
$data = $this->disk->get($this->filename);
|
|
|
|
return json_decode($data, true) ?? [];
|
|
}
|
|
|
|
/**
|
|
* Checks if the content has been modified since it was last loaded or saved.
|
|
*
|
|
* @return bool True if the content is dirty (modified), false otherwise.
|
|
*/
|
|
public function isDirty(): bool
|
|
{
|
|
return $this->originalContent->toJson() != $this->content->toJson();
|
|
}
|
|
|
|
/**
|
|
* Saves the metadata to disk if it has been modified (is dirty).
|
|
*
|
|
* @return bool True if the data was saved successfully, false otherwise.
|
|
*/
|
|
public function save(): bool
|
|
{
|
|
if (!$this->isDirty()) {
|
|
return false;
|
|
}
|
|
|
|
$this->writeToDisk($this->content->all());
|
|
|
|
$this->originalContent = new Collection($this->content->all());
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Writes the provided data to disk as JSON.
|
|
*
|
|
* @param array $data The data to write to disk.
|
|
*/
|
|
protected function writeToDisk(array $data)
|
|
{
|
|
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
|
$this->disk->put($this->filename, $json);
|
|
}
|
|
|
|
/**
|
|
* Sets a metadata value for a specified key.
|
|
*
|
|
* @param mixed $key The key under which to store the value.
|
|
* @param mixed $value The value to store.
|
|
*/
|
|
public function set($key, $value)
|
|
{
|
|
$this->content->put($key, $value);
|
|
}
|
|
|
|
/**
|
|
* Sets multiple metadata at once.
|
|
*/
|
|
public function setMany(array $array)
|
|
{
|
|
foreach ($array as $key => $value) {
|
|
$this->content->put($key, $value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves a metadata value by key.
|
|
*
|
|
* @param mixed $key The key of the value to retrieve.
|
|
* @param mixed $default The default value to return if the key does not exist.
|
|
* @return mixed The value of the specified key, or the default value.
|
|
*/
|
|
public function get($key, $default = null)
|
|
{
|
|
return $this->content->get($key, $default);
|
|
}
|
|
|
|
/**
|
|
* Retrieves all metadata as an associative array.
|
|
*
|
|
* @return array The content of the metadata.
|
|
*/
|
|
public function all()
|
|
{
|
|
return $this->content->all();
|
|
}
|
|
|
|
/**
|
|
* Removes a metadata entry by key.
|
|
*
|
|
* @param mixed $key The key of the entry to remove.
|
|
*/
|
|
public function remove($key)
|
|
{
|
|
$this->content->forget($key);
|
|
}
|
|
}
|