mirror of
https://github.com/flextype/flextype.git
synced 2025-08-06 05:07:41 +02:00
feat(entries): add directives functionality
This commit is contained in:
32
src/flextype/core/Entries/Directives/MarkdownDirective.php
Normal file
32
src/flextype/core/Entries/Directives/MarkdownDirective.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
use Glowy\Arrays\Arrays as Collection;
|
||||
|
||||
emitter()->addListener('onEntriesFetchSingleDirectives', static function (): void {
|
||||
|
||||
if (! registry()->get('flextype.settings.entries.directives.markdown.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$field = entries()->registry()->get('methods.fetch.field');
|
||||
|
||||
if (strings($field)->contains('@parser:markdown')) {
|
||||
$field = strings(parsers()->markdown()->parse($field))->replace('@parser:markdown', '')->trim()->toString();
|
||||
}
|
||||
|
||||
entries()->registry()->set('methods.fetch.field', $field);
|
||||
});
|
32
src/flextype/core/Entries/Directives/ShortcodesDirective.php
Normal file
32
src/flextype/core/Entries/Directives/ShortcodesDirective.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
use Glowy\Arrays\Arrays as Collection;
|
||||
|
||||
emitter()->addListener('onEntriesFetchSingleDirectives', static function (): void {
|
||||
|
||||
if (! registry()->get('flextype.settings.entries.directives.shortcodes.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$field = entries()->registry()->get('methods.fetch.field');
|
||||
|
||||
if (strings($field)->contains('@parser:shortcodes') && registry()->get('flextype.settings.entries.parsers.shortcodes.enabled') != false) {
|
||||
$field = strings(parsers()->shortcodes()->parse($field))->replace('@parser:shortcodes', '')->trim()->toString();
|
||||
}
|
||||
|
||||
entries()->registry()->set('methods.fetch.field', $field);
|
||||
});
|
43
src/flextype/core/Entries/Directives/TypesDirective.php
Normal file
43
src/flextype/core/Entries/Directives/TypesDirective.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
use Glowy\Arrays\Arrays as Collection;
|
||||
|
||||
emitter()->addListener('onEntriesFetchSingleDirectives', static function (): void {
|
||||
|
||||
if (! registry()->get('flextype.settings.entries.directives.types.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$field = entries()->registry()->get('methods.fetch.field');
|
||||
|
||||
strings($field)->contains('@type:integer') and $field = strings(strings($field)->replace('@type:integer', '')->trim())->toInteger();
|
||||
strings($field)->contains('@type:int') and $field = strings(strings($field)->replace('@type:int', '')->trim())->toInteger();
|
||||
strings($field)->contains('@type:float') and $field = strings(strings($field)->replace('@type:float', '')->trim())->toFloat();
|
||||
strings($field)->contains('@type:boolean') and $field = strings(strings($field)->replace('@type:boolean', '')->trim())->toBoolean();
|
||||
strings($field)->contains('@type:bool') and $field = strings(strings($field)->replace('@type:bool', '')->trim())->toBoolean();
|
||||
|
||||
if (strings($field)->contains('@type:array')) {
|
||||
$field = strings($field)->replace('@type:array', '')->trim();
|
||||
if (strings($field)->isJson()) {
|
||||
$field = serializers()->json()->decode($field->toString());
|
||||
} else {
|
||||
$field = strings($field)->toArray(',');
|
||||
}
|
||||
}
|
||||
|
||||
entries()->registry()->set('methods.fetch.field', $field);
|
||||
});
|
@@ -80,6 +80,7 @@ class Entries
|
||||
|
||||
$this->setRegistry($registry);
|
||||
$this->setOptions($options);
|
||||
$this->loadCollectionsDirectives();
|
||||
$this->loadCollectionsMacros();
|
||||
$this->loadCollectionsEvents();
|
||||
$this->loadCollectionsFields();
|
||||
@@ -123,6 +124,20 @@ class Entries
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Collections Directives
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
private function loadCollectionsDirectives(): void
|
||||
{
|
||||
foreach (registry()->get('flextype.settings.entries.directives') as $key => $value) {
|
||||
if (filesystem()->file(ROOT_DIR . $value['path'])->exists()) {
|
||||
include_once ROOT_DIR . $value['path'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Collections Events
|
||||
*
|
||||
@@ -354,6 +369,20 @@ class Entries
|
||||
// Get the result.
|
||||
$result = $this->registry()->get('methods.fetch.result');
|
||||
|
||||
// Directives processor
|
||||
$result = collection($result)->dot()->map(function ($field) {
|
||||
$this->registry()->set('methods.fetch.field', $field);
|
||||
if (is_string($field)) {
|
||||
|
||||
// Run event
|
||||
emitter()->emit('onEntriesFetchSingleDirectives');
|
||||
|
||||
return $this->registry()->get('methods.fetch.field');
|
||||
} else {
|
||||
return $this->registry()->get('methods.fetch.field');
|
||||
}
|
||||
})->undot();
|
||||
|
||||
// Apply `filterCollection` filter for fetch result
|
||||
$this->registry()->set('methods.fetch.result', filterCollection($result, $this->registry()->get('methods.fetch.params.options.filter', [])));
|
||||
|
||||
|
@@ -19,7 +19,7 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void
|
||||
if (! entries()->registry()->get('methods.fetch.collection.fields.id.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (entries()->registry()->get('methods.fetch.result.id') !== null) {
|
||||
return;
|
||||
}
|
||||
|
@@ -74,6 +74,19 @@ errors:
|
||||
# Entries
|
||||
entries:
|
||||
directory: '/entries'
|
||||
parsers:
|
||||
shortcodes:
|
||||
enabled: true
|
||||
directives:
|
||||
markdown:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php"
|
||||
shortcodes:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php"
|
||||
types:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/TypesDirective.php"
|
||||
macros:
|
||||
debug: false
|
||||
registry:
|
||||
|
13
tests/fixtures/settings/settings.yaml
vendored
13
tests/fixtures/settings/settings.yaml
vendored
@@ -70,6 +70,19 @@ errors:
|
||||
# Entries
|
||||
entries:
|
||||
directory: '/entries'
|
||||
parsers:
|
||||
shortcodes:
|
||||
enabled: true
|
||||
directives:
|
||||
markdown:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php"
|
||||
shortcodes:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php"
|
||||
types:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/TypesDirective.php"
|
||||
macros:
|
||||
debug: false
|
||||
registry:
|
||||
|
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('markdown directive', function () {
|
||||
entries()->create('markdown', ['foo' => '@parser:markdown **Hello world!**']);
|
||||
|
||||
$this->assertEquals('<p> <strong>Hello world!</strong></p>', entries()->fetch('markdown')['foo']);
|
||||
});
|
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('shortcodes directive', function () {
|
||||
entries()->create('shortcodes', ['foo' => '@parser:shortcodes [strings prepend="Hello "]World[/strings]']);
|
||||
|
||||
$this->assertEquals('Hello World', entries()->fetch('shortcodes')['foo']);
|
||||
});
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('types directive', function () {
|
||||
entries()->create('type-int', ['foo' => '@type:int 100']);
|
||||
entries()->create('type-integer', ['foo' => '@type:integer 100']);
|
||||
entries()->create('type-bool', ['foo' => '@type:bool true']);
|
||||
entries()->create('type-boolean', ['foo' => '@type:boolean false']);
|
||||
entries()->create('type-float', ['foo' => '@type:float 1.5']);
|
||||
entries()->create('type-array', ['foo' => '@type:array 1,2,3,4,5']);
|
||||
entries()->create('type-array-2', ['foo' => '@type:array [1,2,3,4,5]']);
|
||||
entries()->create('type-array-3', ['foo' => '@type:array {"foo": "Foo"}']);
|
||||
|
||||
$this->assertEquals(100, entries()->fetch('type-int')['foo']);
|
||||
$this->assertEquals(100, entries()->fetch('type-integer')['foo']);
|
||||
$this->assertEquals(true, entries()->fetch('type-bool')['foo']);
|
||||
$this->assertEquals(false, entries()->fetch('type-boolean')['foo']);
|
||||
$this->assertEquals(1.5, entries()->fetch('type-float')['foo']);
|
||||
$this->assertEquals([1,2,3,4,5], entries()->fetch('type-array')['foo']);
|
||||
$this->assertEquals([1,2,3,4,5], entries()->fetch('type-array-2')['foo']);
|
||||
$this->assertEquals(['foo' => 'Foo'], entries()->fetch('type-array-3')['foo']);
|
||||
});
|
Reference in New Issue
Block a user