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

feat(entries): move Entries API to /entries and reorganize all fields #563

This commit is contained in:
Awilum
2021-08-17 16:28:49 +03:00
parent 0d40f1cb4f
commit 0f719e2965
17 changed files with 363 additions and 38 deletions

View File

@@ -7,9 +7,10 @@ declare(strict_types=1);
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
namespace Flextype\Entries;
use Atomastic\Arrays\Arrays;
use Atomastic\Macroable\Macroable;
use function array_merge;
use function arrays;
@@ -27,6 +28,8 @@ use function strings;
class Entries
{
use Macroable;
/**
* Entries Registry.
*
@@ -74,8 +77,10 @@ class Entries
$this->registry()->set('collectionOptions', $this->options['collections']['default']);
foreach ($this->options['collections'] as $collection) {
if (boolval(preg_match_all('#^' . $collection['pattern'] . '$#', $id, $matches, PREG_OFFSET_CAPTURE))) {
$this->registry()->set('collectionOptions', $collection);
if (isset($collection['pattern'])) {
if (boolval(preg_match_all('#^' . $collection['pattern'] . '$#', $id, $matches, PREG_OFFSET_CAPTURE))) {
$this->registry()->set('collectionOptions', $collection);
}
}
}
@@ -310,7 +315,7 @@ class Entries
* @access public
*/
public function move(string $id, string $newID): bool
{
{
// Entry data
$this->registry()->set('move.id', $id);
$this->registry()->set('move.newID', $newID);

View File

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

View File

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

View File

@@ -9,19 +9,18 @@ declare(strict_types=1);
use Atomastic\Arrays\Arrays;
emitter()->addListener('onDefaultFetchSingleHasResult', static function (): void {
emitter()->addListener('onContentFetchSingleHasResult', static function (): void {
if (! registry()->get('flextype.settings.entries.content.fields.content.enabled')) {
if (! registry()->get('flextype.settings.entries.collections.default.fields.entries.enabled')) {
return;
}
if (content()->registry()->has('fetch.data.content.fetch')) {
if (entries()->registry()->has('fetch.data.entries.fetch')) {
// Get fetch.
$original = content()->registry()->get('fetch');
$original = entries()->registry()->get('fetch');
$data = [];
switch (registry()->get('flextype.settings.entries.content.fields.content.fetch.result')) {
switch (registry()->get('flextype.settings.entries.collections.default.fields.entries.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
@@ -33,7 +32,7 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void
}
// Modify fetch.
foreach (content()->registry()->get('fetch.data.content.fetch') as $field => $body) {
foreach (entries()->registry()->get('fetch.data.entries.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
@@ -45,7 +44,7 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void
$result = isset($body['result']) && in_array($body['result'], ['toArray', 'toObject']) ? $body['result'] : $resultTo;
$data[$field] = content()->{$fetchFromCallbackMethod}($body['id'],
$data[$field] = entries()->{$fetchFromCallbackMethod}($body['id'],
isset($body['options']) ?
$body['options'] :
[]);
@@ -54,8 +53,8 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void
}
// Save fetch.
content()->registry()->set('fetch.id', $original['id']);
content()->registry()->set('fetch.options', $original['options']);
content()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
entries()->registry()->set('fetch.id', $original['id']);
entries()->registry()->set('fetch.options', $original['options']);
entries()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
}
});

View File

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

View File

@@ -7,39 +7,39 @@ declare(strict_types=1);
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
emitter()->addListener('onContentFetchSingleHasResult', static function (): void {
emitter()->addListener('onDefaultFetchSingleHasResult', static function (): void {
if (! registry()->get('flextype.settings.entries.content.fields.parsers.enabled')) {
if (! registry()->get('flextype.settings.entries.collections.default.fields.parsers.enabled')) {
return;
}
if (content()->registry()->get('fetch.data.cache.enabled') == null) {
if (entries()->registry()->get('fetch.data.cache.enabled') == null) {
$cache = false;
} else {
$cache = (bool) content()->registry()->get('fetch.data.cache.enabled');
$cache = (bool) entries()->registry()->get('fetch.data.cache.enabled');
}
if (content()->registry()->get('fetch.data.parsers') != null) {
if (entries()->registry()->get('fetch.data.parsers') != null) {
foreach (content()->registry()->get('fetch.data.parsers') as $parserName => $parserData) {
foreach (entries()->registry()->get('fetch.data.parsers') as $parserName => $parserData) {
if (in_array($parserName, ['shortcodes', 'markdown'])) {
if (content()->registry()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) {
if (content()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') != null) {
if (is_array(content()->registry()->get('fetch.data.parsers.'.$parserName.'.fields'))) {
foreach (content()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) {
if (! in_array($field, registry()->get('flextype.settings.entries.content.fields'))) {
if (entries()->registry()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) {
if (entries()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') != null) {
if (is_array(entries()->registry()->get('fetch.data.parsers.'.$parserName.'.fields'))) {
foreach (entries()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) {
if (! in_array($field, registry()->get('flextype.settings.entries.collections.default.fields'))) {
if ($parserName == 'markdown') {
if (arrays(content()->registry()->get('fetch.data'))->has($field)) {
content()->registry()->set('fetch.data.'.$field,
parsers()->markdown()->parse(content()->registry()->get('fetch.data.'.$field), $cache));
if (arrays(entries()->registry()->get('fetch.data'))->has($field)) {
entries()->registry()->set('fetch.data.'.$field,
parsers()->markdown()->parse(entries()->registry()->get('fetch.data.'.$field), $cache));
}
}
if ($parserName == 'shortcodes') {
if (arrays(content()->registry()->get('fetch.data'))->has($field)) {
content()->registry()->set('fetch.data.'.$field,
parsers()->shortcodes()->parse(content()->registry()->get('fetch.data.'.$field), $cache));
if (arrays(entries()->registry()->get('fetch.data'))->has($field)) {
entries()->registry()->set('fetch.data.'.$field,
parsers()->shortcodes()->parse(entries()->registry()->get('fetch.data.'.$field), $cache));
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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