mirror of
https://github.com/flextype/flextype.git
synced 2025-08-11 15:44:49 +02:00
feat(entries): Simplify and improve Entries API #439
This commit is contained in:
@@ -9,24 +9,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace Flextype\Foundation\Entries;
|
||||
|
||||
use Flextype\Component\Arrays\Arrays;
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use Flextype\Component\Session\Session;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use function array_merge;
|
||||
use function count;
|
||||
use function date;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
use function is_bool;
|
||||
use function ltrim;
|
||||
use function md5;
|
||||
use function rename;
|
||||
use function rtrim;
|
||||
use function str_replace;
|
||||
use function strpos;
|
||||
use function strtotime;
|
||||
use function time;
|
||||
|
||||
class Entries
|
||||
{
|
||||
@@ -138,13 +129,14 @@ class Entries
|
||||
|
||||
// Try to get current requested entry from filesystem
|
||||
if ($this->has($this->entry_path)) {
|
||||
|
||||
// Get entry file location
|
||||
$entry_file = $this->getFileLocation($this->entry_path);
|
||||
|
||||
// Try to get requested entry from the filesystem
|
||||
$entry_file_content = Filesystem::read($entry_file);
|
||||
if ($entry_file_content === false) return [];
|
||||
if ($entry_file_content === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Decode entry file content
|
||||
$this->entry = $this->flextype['frontmatter']->decode($entry_file_content);
|
||||
@@ -153,8 +145,7 @@ class Entries
|
||||
$this->flextype['emitter']->emit('onEntryAfterInitialized');
|
||||
|
||||
// Set cache state
|
||||
$cache = isset($this->flextype['entries']->entry['cache']['enabled']) ?
|
||||
$this->flextype['entries']->entry['cache']['enabled'] :
|
||||
$cache = $this->flextype['entries']->entry['cache']['enabled'] ??
|
||||
$this->flextype['registry']->get('flextype.settings.cache.enabled');
|
||||
|
||||
// Save entry data to cache
|
||||
@@ -239,7 +230,6 @@ class Entries
|
||||
public function rename(string $path, string $new_path) : bool
|
||||
{
|
||||
if (! Filesystem::has($this->getDirLocation($new_path))) {
|
||||
|
||||
// Run event: onEntryRename
|
||||
$this->flextype['emitter']->emit('onEntryRename');
|
||||
|
||||
@@ -298,7 +288,6 @@ class Entries
|
||||
if (Filesystem::createDir($entry_dir)) {
|
||||
// Check if new entry file exists
|
||||
if (! Filesystem::has($entry_file = $entry_dir . '/entry' . '.' . $this->flextype->registry->get('flextype.settings.entries.extension'))) {
|
||||
|
||||
// Store data in the entry_create_data
|
||||
$this->entry_create_data = $data;
|
||||
|
||||
@@ -403,7 +392,7 @@ class Entries
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getCacheID($path) : string
|
||||
public function getCacheID(string $path) : string
|
||||
{
|
||||
if ($this->flextype['registry']->get('flextype.settings.cache.enabled') === false) {
|
||||
return '';
|
||||
|
@@ -9,13 +9,13 @@ declare(strict_types=1);
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) {
|
||||
$flextype->entries->entry['created_at'] = isset($flextype->entries->entry['created_at']) ?
|
||||
(int) strtotime($flextype->entries->entry['created_at']) :
|
||||
(int) Filesystem::getTimestamp($flextype->entries->getFileLocation($flextype->entries->entry_path));
|
||||
});
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) {
|
||||
if (isset($flextype->entries->entry_create_data['created_at'])) {
|
||||
$flextype->entries->entry_create_data['created_at'] = $flextype->entries->entry_create_data['created_at'];
|
||||
} else {
|
||||
|
@@ -7,7 +7,7 @@ declare(strict_types=1);
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) {
|
||||
if (isset($flextype->entries->entry_create_data['created_by'])) {
|
||||
$flextype->entries->entry_create_data['created_by'] = $flextype->entries->entry_create_data['created_by'];
|
||||
} else {
|
||||
|
@@ -9,6 +9,6 @@ declare(strict_types=1);
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) {
|
||||
$flextype->entries->entry['modified_at'] = (int) Filesystem::getTimestamp($flextype->entries->getFileLocation($flextype->entries->entry_path));
|
||||
});
|
||||
|
@@ -10,7 +10,7 @@ declare(strict_types=1);
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use Flextype\Component\Arrays\Arrays;
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) {
|
||||
processParsersField($flextype);
|
||||
});
|
||||
|
||||
|
@@ -9,13 +9,13 @@ declare(strict_types=1);
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) {
|
||||
$flextype->entries->entry['published_at'] = isset($flextype->entries->entry['published_at']) ?
|
||||
(int) strtotime($flextype->entries->entry['published_at']) :
|
||||
(int) Filesystem::getTimestamp($flextype->entries->getFileLocation($flextype->entries->entry_path));
|
||||
});
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) {
|
||||
if (isset($flextype->entries->entry_create_data['published_at'])) {
|
||||
$flextype->entries->entry_create_data['published_at'] = $flextype->entries->entry_create_data['published_at'];
|
||||
} else {
|
||||
|
@@ -7,7 +7,7 @@ declare(strict_types=1);
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) {
|
||||
if (isset($flextype->entries->entry_create_data['published_by'])) {
|
||||
$flextype->entries->entry_create_data['published_by'] = $flextype->entries->entry_create_data['published_by'];
|
||||
} else {
|
||||
|
@@ -9,13 +9,13 @@ declare(strict_types=1);
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) {
|
||||
$flextype->entries->entry['routable'] = isset($flextype->entries->entry['routable']) ?
|
||||
(bool) $flextype->entries->entry['routable'] :
|
||||
true;
|
||||
});
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) {
|
||||
if (isset($flextype->entries->entry_create_data['routable']) && is_bool($flextype->entries->entry_create_data['routable'])) {
|
||||
$flextype->entries->entry_create_data['routable'] = $flextype->entries->entry_create_data['routable'];
|
||||
} else {
|
||||
|
@@ -9,6 +9,6 @@ declare(strict_types=1);
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) {
|
||||
$flextype->entries->entry['slug'] = isset($flextype->entries->entry['slug']) ? (string) $flextype->entries->entry['slug'] : (string) ltrim(rtrim($flextype->entries->entry_path, '/'), '/');
|
||||
});
|
||||
|
@@ -10,7 +10,7 @@ declare(strict_types=1);
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) {
|
||||
if (isset($flextype->entries->entry_create_data['uuid'])) {
|
||||
$flextype->entries->entry_create_data['uuid'] = $flextype->entries->entry_create_data['uuid'];
|
||||
} else {
|
||||
|
@@ -15,7 +15,7 @@ $visibility = [
|
||||
'visible' => 'visible',
|
||||
];
|
||||
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype, $visibility) {
|
||||
$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype, $visibility) {
|
||||
if (isset($flextype->entries->entry['visibility']) && in_array($flextype->entries->entry['visibility'], $visibility)) {
|
||||
$flextype->entries->entry['visibility'] = (string) $visibility[$flextype->entries->entry['visibility']];
|
||||
} else {
|
||||
@@ -23,7 +23,7 @@ $flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flex
|
||||
}
|
||||
});
|
||||
|
||||
$flextype->emitter->addListener('onEntryCreate', function() use ($flextype, $visibility) {
|
||||
$flextype->emitter->addListener('onEntryCreate', function () use ($flextype, $visibility) {
|
||||
if (isset($flextype->entries->entry_create_data['visibility']) && in_array($flextype->entries->entry_create_data['visibility'], $visibility)) {
|
||||
$flextype->entries->entry_create_data['visibility'] = $flextype->entries->entry_create_data['visibility'];
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user