1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 22:26:46 +02:00

feat(entries): New Entries API and Content API #552

- Entries API basic class for any other Data managing APIs
- Content API based on Entries API class
- Content Fields for Content API

BREAKING CHANGES

- use Content API for site content managing instead of directly using Entries API

  Example:
    `flextype('content')->fetch()` instead of `flextype('entries')->fetch()`

- all events should starts with `onContent` instead of `onEntries`

- settings section `flextype.settings.entries` updated
This commit is contained in:
Awilum
2021-07-16 21:20:10 +03:00
parent 4de750394c
commit b522291366
28 changed files with 334 additions and 755 deletions

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Foundation\Content;
use Atomastic\Arrays\Arrays;
use Atomastic\Macroable\Macroable;
use Flextype\Foundation\Entries;
class Content extends Entries
{
use Macroable;
}

View File

@@ -9,14 +9,14 @@ declare(strict_types=1);
use Atomastic\Arrays\Arrays;
if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->has('fetch.data.entries.fetch')) {
if (flextype('registry')->get('flextype.settings.entries.content.fields.entries.fetch.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->has('fetch.data.entries.fetch')) {
// Get fetch.
$original = flextype('entries')->storage()->get('fetch');
$original = flextype('content')->registry()->get('fetch');
$data = [];
switch (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.result')) {
switch (flextype('registry')->get('flextype.settings.entries.content.fields.content.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
@@ -28,11 +28,11 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en
}
// Modify fetch.
foreach (flextype('entries')->storage()->get('fetch.data.entries.fetch') as $field => $body) {
foreach (flextype('content')->registry()->get('fetch.data.content.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
is_callable([flextype('entries'), $body['options']['method']])) {
is_callable([flextype('content'), $body['options']['method']])) {
$fetchFromCallbackMethod = $body['options']['method'];
} else {
$fetchFromCallbackMethod = 'fetch';
@@ -40,7 +40,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en
$result = isset($body['result']) && in_array($body['result'], ['toArray', 'toObject']) ? $body['result'] : $resultTo;
$data[$field] = flextype('entries')->{$fetchFromCallbackMethod}($body['id'],
$data[$field] = flextype('content')->{$fetchFromCallbackMethod}($body['id'],
isset($body['options']) ?
$body['options'] :
[]);
@@ -49,9 +49,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en
}
// Save fetch.
flextype('entries')->storage()->set('fetch.id', $original['id']);
flextype('entries')->storage()->set('fetch.options', $original['options']);
flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
flextype('content')->registry()->set('fetch.id', $original['id']);
flextype('content')->registry()->set('fetch.options', $original['options']);
flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
}
});
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.created_at.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->get('fetch.data.created_at') === null) {
flextype('content')->registry()->set('fetch.data.created_at', (int) filesystem()->file(flextype('content')->getFileLocation(flextype('content')->registry()->get('fetch.id')))->lastModified());
} else {
flextype('content')->registry()->set('fetch.data.created_at', (int) strtotime((string) flextype('content')->registry()->get('fetch.data.created_at')));
}
});
flextype('emitter')->addListener('onContentCreate', static function (): void {
if (flextype('content')->registry()->get('create.data.created_at') !== null) {
return;
}
flextype('content')->registry()->set('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
});
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.created_by.enabled')) {
flextype('emitter')->addListener('onContentCreate', static function (): void {
if (flextype('content')->registry()->get('create.data.created_by') !== null) {
return;
}
flextype('content')->registry()->set('create.data.created_by', '');
});
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.id.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->get('fetch.data.id') !== null) {
return;
}
flextype('content')->registry()->set('fetch.data.id', (string) strings(flextype('content')->registry()->get('fetch.id'))->trimSlashes());
});
}

View File

@@ -9,14 +9,14 @@ declare(strict_types=1);
use Atomastic\Arrays\Arrays;
if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetch.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->has('fetch.data.media.files.fetch')) {
if (flextype('registry')->get('flextype.settings.entries.content.fields.media.files.fetch.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->has('fetch.data.media.files.fetch')) {
// Get fetch.
$original = flextype('entries')->storage()->get('fetch');
$original = flextype('content')->registry()->get('fetch');
$data = [];
switch (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetch.result')) {
switch (flextype('registry')->get('flextype.settings.entries.content.fields.media.files.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
@@ -28,7 +28,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc
}
// Modify fetch.
foreach (flextype('entries')->storage()->get('fetch.data.media.files.fetch') as $field => $body) {
foreach (flextype('content')->registry()->get('fetch.data.media.files.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
@@ -50,23 +50,23 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc
}
// Save fetch.
flextype('entries')->storage()->set('fetch.id', $original['id']);
flextype('entries')->storage()->set('fetch.options', $original['options']);
flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
flextype('content')->registry()->set('fetch.id', $original['id']);
flextype('content')->registry()->set('fetch.options', $original['options']);
flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
}
});
}
if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fetch.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->has('fetch.data.media.folders.fetch')) {
if (flextype('registry')->get('flextype.settings.entries.content.fields.media.folders.fetch.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->has('fetch.data.media.folders.fetch')) {
// Get fetch.
$original = flextype('entries')->storage()->get('fetch');
$original = flextype('content')->registry()->get('fetch');
$data = [];
switch (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fetch.result')) {
switch (flextype('registry')->get('flextype.settings.entries.content.fields.media.folders.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
@@ -78,7 +78,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fe
}
// Modify fetch.
foreach (flextype('entries')->storage()->get('fetch.data.media.folders.fetch') as $field => $body) {
foreach (flextype('content')->registry()->get('fetch.data.media.folders.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
@@ -100,9 +100,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fe
}
// Save fetch.
flextype('entries')->storage()->set('fetch.id', $original['id']);
flextype('entries')->storage()->set('fetch.options', $original['options']);
flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
flextype('content')->registry()->set('fetch.id', $original['id']);
flextype('content')->registry()->set('fetch.options', $original['options']);
flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
}
});
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.modified_at.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->get('fetch.data.modified_at') !== null) {
return;
}
flextype('content')->registry()->set('fetch.data.modified_at', (int) filesystem()->file(flextype('content')->getFileLocation(flextype('content')->registry()->get('fetch.id')))->lastModified());
});
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.parsers.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
processParsersField();
});
}
function processParsersField(): void
{
if (flextype('content')->registry()->get('fetch.data.cache.enabled') == null) {
$cache = false;
} else {
$cache = (bool) flextype('content')->registry()->get('fetch.data.cache.enabled');
}
if (flextype('content')->registry()->get('fetch.data.parsers') != null) {
foreach (flextype('content')->registry()->get('fetch.data.parsers') as $parserName => $parserData) {
if (in_array($parserName, ['markdown', 'shortcode'])) {
if (flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) {
if (flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.fields') != null) {
if (is_array(flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.fields'))) {
foreach (flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) {
if (! in_array($field, flextype('registry')->get('flextype.settings.entries.content.fields'))) {
if ($parserName == 'markdown') {
if (arrays(flextype('content')->registry()->get('fetch.data'))->has($field)) {
flextype('content')->registry()->set('fetch.data.'.$field,
flextype('parsers')->markdown()->parse(flextype('content')->registry()->get('fetch.data.'.$field), $cache));
}
}
if ($parserName == 'shortcode') {
if (arrays(flextype('content')->registry()->get('fetch.data'))->has($field)) {
flextype('content')->registry()->set('fetch.data.'.$field,
flextype('parsers')->shortcode()->process(flextype('content')->registry()->get('fetch.data.'.$field), $cache));
}
}
}
}
}
}
}
}
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.published_at.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->get('fetch.data.published_at') === null) {
flextype('content')->registry()->set('fetch.data.published_at', (int) filesystem()->file(flextype('content')->getFileLocation(flextype('content')->registry()->get('fetch.id')))->lastModified());
} else {
flextype('content')->registry()->set('fetch.data.published_at', (int) strtotime((string) flextype('content')->registry()->get('fetch.data.published_at')));
}
});
flextype('emitter')->addListener('onContentCreate', static function (): void {
if (flextype('content')->registry()->get('create.data.published_at') !== null) {
return;
}
flextype('content')->registry()->set('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
});
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.published_by.enabled')) {
flextype('emitter')->addListener('onContentCreate', static function (): void {
if (flextype('content')->registry()->get('create.data.published_by') !== null) {
return;
}
flextype('content')->registry()->set('create.data.published_by', '');
});
}

View File

@@ -7,16 +7,16 @@ declare(strict_types=1);
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.registry.get.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->has('fetch.data.registry.get')) {
if (flextype('registry')->get('flextype.settings.entries.content.fields.registry.get.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->has('fetch.data.registry.get')) {
// Get fetch.
$original = flextype('entries')->storage()->get('fetch');
$original = flextype('content')->registry()->get('fetch');
$data = [];
// Modify fetch.
foreach (flextype('entries')->storage()->get('fetch.data.registry.get') as $field => $body) {
foreach (flextype('content')->registry()->get('fetch.data.registry.get') as $field => $body) {
$data = arrays($data)->merge(arrays($data)->set($field, flextype('registry')->get($body['key'],
isset($body['default']) ?
$body['default'] :
@@ -25,7 +25,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.registry.get.ena
}
// Save fetch.
flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
}
});
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.routable.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->get('fetch.data.routable') === null) {
flextype('content')->registry()->set('fetch.data.routable', true);
} else {
flextype('content')->registry()->set('fetch.data.routable', (bool) flextype('content')->registry()->get('fetch.data.routable'));
}
});
flextype('emitter')->addListener('onContentCreate', static function (): void {
if (flextype('content')->registry()->get('create.data.routable') === null) {
flextype('content')->registry()->set('create.data.routable', true);
} else {
flextype('content')->registry()->set('create.data.routable', (bool) flextype('content')->registry()->get('create.data.routable'));
}
});
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.slug.enabled')) {
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void {
if (flextype('content')->registry()->get('fetch.data.slug') !== null) {
return;
}
$parts = explode('/', ltrim(rtrim(flextype('content')->registry()->get('fetch.id'), '/'), '/'));
flextype('content')->registry()->set('fetch.data.slug', (string) end($parts));
});
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Ramsey\Uuid\Uuid;
if (flextype('registry')->get('flextype.settings.entries.content.fields.uuid.enabled')) {
flextype('emitter')->addListener('onContentCreate', static function (): void {
if (flextype('content')->registry()->get('create.data.uuid') !== null) {
return;
}
flextype('content')->registry()->set('create.data.uuid', Uuid::uuid4()->toString());
});
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.content.fields.visibility.enabled')) {
$visibility = [
'draft' => 'draft',
'hidden' => 'hidden',
'visible' => 'visible',
];
flextype('emitter')->addListener('onContentFetchSingleHasResult', static function () use ($visibility): void {
if (flextype('content')->registry()->get('fetch.data.visibility') !== null && in_array(flextype('content')->registry()->get('fetch.data.visibility'), $visibility)) {
flextype('content')->registry()->set('fetch.data.visibility', (string) $visibility[flextype('content')->registry()->get('fetch.data.visibility')]);
} else {
flextype('content')->registry()->set('fetch.data.visibility', (string) $visibility['visible']);
}
});
flextype('emitter')->addListener('onContentCreate', static function () use ($visibility): void {
if (flextype('content')->registry()->get('create.data.visibility') !== null && in_array(flextype('content')->registry()->get('create.data.visibility'), $visibility)) {
flextype('content')->registry()->set('create.data.visibility', (string) $visibility[flextype('content')->registry()->get('create.data.visibility')]);
} else {
flextype('content')->registry()->set('create.data.visibility', (string) $visibility['visible']);
}
});
}

View File

@@ -1,424 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Foundation\Entries;
use Atomastic\Arrays\Arrays;
use Atomastic\Macroable\Macroable;
use function array_merge;
use function arrays;
use function filesystem;
use function filter;
use function find;
use function flextype;
use function strings;
class Entries
{
use Macroable;
/**
* Entries Storage
*
* Used for storing current requested entries data
* and allow to change them on fly.
*
* @var Arrays
* @access private
*/
private Arrays $storage;
/**
* __construct
*/
public function __construct()
{
$this->storage = arrays();
}
/**
* Get Entries Storage
*
* @return Arrays
*/
public function storage(): Arrays
{
return $this->storage;
}
/**
* Fetch.
*
* @param string $id Unique identifier of the entry.
* @param array $options Options array.
*
* @access public
*
* @return Arrays Returns instance of The Arrays class with items.
*/
public function fetch(string $id, array $options = []): Arrays
{
// Store data
$this->storage()->set('fetch.id', $id);
$this->storage()->set('fetch.options', $options);
$this->storage()->set('fetch.data', []);
// Run event: onEntriesFetch
flextype('emitter')->emit('onEntriesFetch');
// Single fetch helper
$single = function ($id, $options) {
// Store data
$this->storage()->set('fetch.id', $id);
$this->storage()->set('fetch.options', $options);
$this->storage()->set('fetch.data', []);
// Run event: onEntriesFetchSingle
flextype('emitter')->emit('onEntriesFetchSingle');
// Get Cache ID for current requested entry
$entryCacheID = $this->getCacheID($this->storage()->get('fetch.id'));
// 1. Try to get current requested entry from cache
if (flextype('cache')->has($entryCacheID)) {
// Fetch entry from cache and Apply filter for fetch data
$this->storage()->set('fetch.data', filter(flextype('cache')->get($entryCacheID),
$this->storage()->get('fetch.options.filter', [])));
// Run event: onEntriesFetchSingleCacheHasResult
flextype('emitter')->emit('onEntriesFetchSingleCacheHasResult');
// Return entry from cache
return arrays($this->storage()->get('fetch.data'));
}
// 2. Try to get current requested entry from filesystem
if ($this->has($this->storage()->get('fetch.id'))) {
// Get entry file location
$entryFile = $this->getFileLocation($this->storage()->get('fetch.id'));
// Try to get requested entry from the filesystem
$entryFileContent = filesystem()->file($entryFile)->get();
if ($entryFileContent === false) {
// Run event: onEntriesFetchSingleNoResult
flextype('emitter')->emit('onEntriesFetchSingleNoResult');
return arrays($this->storage()->get('fetch.data'));
}
// Decode entry file content
$this->storage()->set('fetch.data', flextype('serializers')->frontmatter()->decode($entryFileContent));
// Run event: onEntriesFetchSingleHasResult
flextype('emitter')->emit('onEntriesFetchSingleHasResult');
// Apply filter for fetch data
$this->storage()->set('fetch.data', filter($this->storage()->get('fetch.data'),
$this->storage()->get('fetch.options.filter', [])));
// Set cache state
$cache = $this->storage()->get('fetch.data.cache.enabled',
flextype('registry')->get('flextype.settings.cache.enabled'));
// Save entry data to cache
if ($cache) {
flextype('cache')->set($entryCacheID, $this->storage()->get('fetch.data'));
}
// Return entry data
return arrays($this->storage()->get('fetch.data'));
}
// Run event: onEntriesFetchSingleNoResult
flextype('emitter')->emit('onEntriesFetchSingleNoResult');
// Return empty array if entry is not founded
return arrays($this->storage()->get('fetch.data'));
};
if (isset($this->storage['fetch']['options']['collection']) &&
strings($this->storage['fetch']['options']['collection'])->isTrue()) {
// Run event: onEntriesFetchCollection
flextype('emitter')->emit('onEntriesFetchCollection');
if (! $this->getDirectoryLocation($id)) {
// Run event: onEntriesFetchCollectionNoResult
flextype('emitter')->emit('onEntriesFetchCollectionNoResult');
// Return entries array
return arrays($this->storage()->get('fetch.data'));
}
// Find entries in the filesystem
$entries = find($this->getDirectoryLocation($id),
isset($options['find']) ?
$options['find'] :
[]);
// Walk through entries results
if ($entries->hasResults()) {
$data = [];
foreach ($entries as $currenEntry) {
if ($currenEntry->getType() !== 'file' || $currenEntry->getFilename() !== 'entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension')) {
continue;
}
$currentEntryID = strings($currenEntry->getPath())
->replace('\\', '/')
->replace(PATH['project'] . '/entries/', '')
->trim('/')
->toString();
$data[$currentEntryID] = $single($currentEntryID, [])->toArray();
}
$this->storage()->set('fetch.data', $data);
// Run event: onEntriesFetchCollectionHasResult
flextype('emitter')->emit('onEntriesFetchCollectionHasResult');
// Apply filter for fetch data
$this->storage()->set('fetch.data', filter($this->storage()->get('fetch.data'),
isset($options['filter']) ?
$options['filter'] :
[]));
}
// Run event: onEntriesFetchCollectionNoResult
flextype('emitter')->emit('onEntriesFetchCollectionNoResult');
// Return entries array
return arrays($this->storage()->get('fetch.data'));
} else {
return $single($this->storage['fetch']['id'],
$this->storage['fetch']['options']);
}
}
/**
* Move entry
*
* @param string $id Unique identifier of the entry.
* @param string $newID New Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function move(string $id, string $newID): bool
{
// Store data
$this->storage()->set('move.id', $id);
$this->storage()->set('move.newID', $newID);
// Run event: onEntriesMove
flextype('emitter')->emit('onEntriesMove');
if (! $this->has($this->storage()->get('move.newID'))) {
return filesystem()
->directory($this->getDirectoryLocation($this->storage()->get('move.id')))
->move($this->getDirectoryLocation($this->storage()->get('move.newID')));
}
return false;
}
/**
* Update entry
*
* @param string $id Unique identifier of the entry.
* @param array $data Data to update for the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function update(string $id, array $data): bool
{
// Store data
$this->storage()->set('update.id', $id);
$this->storage()->set('update.data', $data);
// Run event: onEntriesUpdate
flextype('emitter')->emit('onEntriesUpdate');
$entryFile = $this->getFileLocation($this->storage()->get('update.id'));
if (filesystem()->file($entryFile)->exists()) {
$body = filesystem()->file($entryFile)->get();
$entry = flextype('serializers')->frontmatter()->decode($body);
return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode(array_merge($entry, $this->storage()->get('update.data'))));
}
return false;
}
/**
* Create entry.
*
* @param string $id Unique identifier of the entry.
* @param array $data Data to create for the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function create(string $id, array $data = []): bool
{
// Store data
$this->storage()->set('create.id', $id);
$this->storage()->set('create.data', $data);
// Run event: onEntriesCreate
flextype('emitter')->emit('onEntriesCreate');
// Create entry directory first if it is not exists
$entryDir = $this->getDirectoryLocation($this->storage()->get('create.id'));
if (
! filesystem()->directory($entryDir)->exists() &&
! filesystem()->directory($entryDir)->create()
) {
return false;
}
// Create entry file
$entryFile = $entryDir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension');
if (! filesystem()->file($entryFile)->exists()) {
return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode($this->storage()->get('create.data')));
}
return false;
}
/**
* Delete entry.
*
* @param string $id Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function delete(string $id): bool
{
// Store data
$this->storage()->set('delete.id', $id);
// Run event: onEntriesDelete
flextype('emitter')->emit('onEntriesDelete');
return filesystem()
->directory($this->getDirectoryLocation($this->storage()->get('delete.id')))
->delete();
}
/**
* Copy entry.
*
* @param string $id Unique identifier of the entry.
* @param string $newID New Unique identifier of the entry.
*
* @return bool|null True on success, false on failure.
*
* @access public
*/
public function copy(string $id, string $newID): ?bool
{
// Store data
$this->storage()->set('copy.id', $id);
$this->storage()->set('copy.newID', $newID);
// Run event: onEntriesCopy
flextype('emitter')->emit('onEntriesCopy');
return filesystem()
->directory($this->getDirectoryLocation($this->storage()->get('copy.id')))
->copy($this->getDirectoryLocation($this->storage()->get('copy.newID')));
}
/**
* Check whether entry exists
*
* @param string $id Unique identifier of the entry(entries).
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function has(string $id): bool
{
// Store data
$this->storage()->set('has.id', $id);
// Run event: onEntriesHas
flextype('emitter')->emit('onEntriesHas');
return filesystem()->file($this->getFileLocation($this->storage()->get('has.id')))->exists();
}
/**
* Get entry file location
*
* @param string $id Unique identifier of the entry(entries).
*
* @return string entry file location
*
* @access public
*/
public function getFileLocation(string $id): string
{
return PATH['project'] . '/entries/' . $id . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension');
}
/**
* Get entry directory location
*
* @param string $id Unique identifier of the entry(entries).
*
* @return string entry directory location
*
* @access public
*/
public function getDirectoryLocation(string $id): string
{
return PATH['project'] . '/entries/' . $id;
}
/**
* Get Cache ID for entry
*
* @param string $id Unique identifier of the entry(entries).
*
* @return string Cache ID
*
* @access public
*/
public function getCacheID(string $id): string
{
if (flextype('registry')->get('flextype.settings.cache.enabled') === false) {
return '';
}
$entryFile = $this->getFileLocation($id);
if (filesystem()->file($entryFile)->exists()) {
return strings('entry' . $entryFile . (filesystem()->file($entryFile)->lastModified() ?: ''))->hash()->toString();
}
return strings('entry' . $entryFile)->hash()->toString();
}
}

View File

@@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->get('fetch.data.created_at') === null) {
flextype('entries')->storage()->set('fetch.data.created_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified());
} else {
flextype('entries')->storage()->set('fetch.data.created_at', (int) strtotime((string) flextype('entries')->storage()->get('fetch.data.created_at')));
}
});
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->storage()->get('create.data.created_at') !== null) {
return;
}
flextype('entries')->storage()->set('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
});
}

View File

@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.created_by.enabled')) {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->storage()->get('create.data.created_by') !== null) {
return;
}
flextype('entries')->storage()->set('create.data.created_by', '');
});
}

View File

@@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.id.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->get('fetch.data.id') !== null) {
return;
}
flextype('entries')->storage()->set('fetch.data.id', (string) strings(flextype('entries')->storage()->get('fetch.id'))->trimSlashes());
});
}

View File

@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.modified_at.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->get('fetch.data.modified_at') !== null) {
return;
}
flextype('entries')->storage()->set('fetch.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified());
});
}

View File

@@ -1,52 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.parsers.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
processParsersField();
});
}
function processParsersField(): void
{
if (flextype('entries')->storage()->get('fetch.data.cache.enabled') == null) {
$cache = false;
} else {
$cache = (bool) flextype('entries')->storage()->get('fetch.data.cache.enabled');
}
if (flextype('entries')->storage()->get('fetch.data.parsers') != null) {
foreach (flextype('entries')->storage()->get('fetch.data.parsers') as $parserName => $parserData) {
if (in_array($parserName, ['markdown', 'shortcode'])) {
if (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) {
if (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields') != null) {
if (is_array(flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields'))) {
foreach (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) {
if (! in_array($field, flextype('registry')->get('flextype.settings.entries.fields'))) {
if ($parserName == 'markdown') {
if (arrays(flextype('entries')->storage()->get('fetch.data'))->has($field)) {
flextype('entries')->storage()->set('fetch.data.'.$field,
flextype('parsers')->markdown()->parse(flextype('entries')->storage()->get('fetch.data.'.$field), $cache));
}
}
if ($parserName == 'shortcode') {
if (arrays(flextype('entries')->storage()->get('fetch.data'))->has($field)) {
flextype('entries')->storage()->set('fetch.data.'.$field,
flextype('parsers')->shortcode()->process(flextype('entries')->storage()->get('fetch.data.'.$field), $cache));
}
}
}
}
}
}
}
}
}
}
}

View File

@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.published_at.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->get('fetch.data.published_at') === null) {
flextype('entries')->storage()->set('fetch.data.published_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified());
} else {
flextype('entries')->storage()->set('fetch.data.published_at', (int) strtotime((string) flextype('entries')->storage()->get('fetch.data.published_at')));
}
});
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->storage()->get('create.data.published_at') !== null) {
return;
}
flextype('entries')->storage()->set('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
});
}

View File

@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.published_by.enabled')) {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->storage()->get('create.data.published_by') !== null) {
return;
}
flextype('entries')->storage()->set('create.data.published_by', '');
});
}

View File

@@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->get('fetch.data.routable') === null) {
flextype('entries')->storage()->set('fetch.data.routable', true);
} else {
flextype('entries')->storage()->set('fetch.data.routable', (bool) flextype('entries')->storage()->get('fetch.data.routable'));
}
});
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->storage()->get('create.data.routable') === null) {
flextype('entries')->storage()->set('create.data.routable', true);
} else {
flextype('entries')->storage()->set('create.data.routable', (bool) flextype('entries')->storage()->get('create.data.routable'));
}
});
}

View File

@@ -1,20 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.slug.enabled')) {
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (flextype('entries')->storage()->get('fetch.data.slug') !== null) {
return;
}
$parts = explode('/', ltrim(rtrim(flextype('entries')->storage()->get('fetch.id'), '/'), '/'));
flextype('entries')->storage()->set('fetch.data.slug', (string) end($parts));
});
}

View File

@@ -1,20 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Ramsey\Uuid\Uuid;
if (flextype('registry')->get('flextype.settings.entries.fields.uuid.enabled')) {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->storage()->get('create.data.uuid') !== null) {
return;
}
flextype('entries')->storage()->set('create.data.uuid', Uuid::uuid4()->toString());
});
}

View File

@@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
if (flextype('registry')->get('flextype.settings.entries.fields.visibility.enabled')) {
$visibility = [
'draft' => 'draft',
'hidden' => 'hidden',
'visible' => 'visible',
];
flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function () use ($visibility): void {
if (flextype('entries')->storage()->get('fetch.data.visibility') !== null && in_array(flextype('entries')->storage()->get('fetch.data.visibility'), $visibility)) {
flextype('entries')->storage()->set('fetch.data.visibility', (string) $visibility[flextype('entries')->storage()->get('fetch.data.visibility')]);
} else {
flextype('entries')->storage()->set('fetch.data.visibility', (string) $visibility['visible']);
}
});
flextype('emitter')->addListener('onEntriesCreate', static function () use ($visibility): void {
if (flextype('entries')->storage()->get('create.data.visibility') !== null && in_array(flextype('entries')->storage()->get('create.data.visibility'), $visibility)) {
flextype('entries')->storage()->set('create.data.visibility', (string) $visibility[flextype('entries')->storage()->get('create.data.visibility')]);
} else {
flextype('entries')->storage()->set('create.data.visibility', (string) $visibility['visible']);
}
});
}

View File

@@ -17,7 +17,7 @@ use Cocur\Slugify\Slugify;
use DateTimeZone;
use Flextype\Foundation\Actions;
use Flextype\Foundation\Cors;
use Flextype\Foundation\Entries\Entries;
use Flextype\Foundation\Content\Content;
use Flextype\Foundation\Flextype;
use Flextype\Foundation\Media\Media;
use Flextype\Foundation\Plugins;
@@ -371,9 +371,9 @@ flextype()->container()['images'] = static function () {
};
/**
* Add entries service to Flextype container
* Add content service to Flextype container
*/
flextype()->container()['entries'] = static fn () => new Entries();
flextype()->container()['content'] = static fn () => new Content(flextype('registry')->get('flextype.settings.entries.content'));
/**
* Add media service to Flextype container
@@ -437,22 +437,6 @@ foreach ($shortcodes as $shortcodeName => $shortcode) {
include_once $shortcodeFilePath;
}
/**
* Init entries fields
*
* Load Flextype Entries fields from directory /flextype/Foundation/Entries/Fields/ based on flextype.settings.entries.fields array
*/
$entryFields = flextype('registry')->get('flextype.settings.entries.fields');
foreach ($entryFields as $fieldName => $field) {
$entryFieldFilePath = ROOT_DIR . '/src/flextype/Foundation/Entries/Fields/' . str_replace('_', '', ucwords($fieldName, '_')) . 'Field.php';
if (! file_exists($entryFieldFilePath)) {
continue;
}
include_once $entryFieldFilePath;
}
/**
* Init plugins
*/