mirror of
https://github.com/flextype/flextype.git
synced 2025-08-06 13:16:45 +02:00
feat(macros): add php
macros for entries
This commit is contained in:
27
src/flextype/core/Entries/Macros/PhpMacros.php
Normal file
27
src/flextype/core/Entries/Macros/PhpMacros.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void {
|
||||
if (! registry()->get('flextype.settings.entries.macros.php.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (entries()->registry()->has('methods.fetch.result.macros.php')) {
|
||||
ob_start();
|
||||
eval(entries()->registry()->get('methods.fetch.result.macros.php'));
|
||||
ob_get_clean();
|
||||
}
|
||||
});
|
@@ -89,6 +89,9 @@ entries:
|
||||
path: "/src/flextype/core/Entries/Directives/TypesDirective.php"
|
||||
macros:
|
||||
debug: false
|
||||
php:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/PhpMacros.php"
|
||||
registry:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/RegistryMacros.php"
|
||||
|
6
tests/fixtures/entries/blog-php-macros/entry.yaml
vendored
Normal file
6
tests/fixtures/entries/blog-php-macros/entry.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
title: Blog
|
||||
macros:
|
||||
php: |
|
||||
$entry = entries()->registry()->get('methods.fetch');
|
||||
$entry['result']['posts'] = entries()->fetch('blog', ['collection' => true, 'filter' => ['sort_by' => ['key' => 'date', 'direction' => 'ASC']]])->toArray();
|
||||
entries()->registry()->set('methods.fetch', $entry);
|
3
tests/fixtures/settings/settings.yaml
vendored
3
tests/fixtures/settings/settings.yaml
vendored
@@ -85,6 +85,9 @@ entries:
|
||||
path: "/src/flextype/core/Entries/Directives/TypesDirective.php"
|
||||
macros:
|
||||
debug: false
|
||||
php:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/PhpMacros.php"
|
||||
registry:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/RegistryMacros.php"
|
||||
|
23
tests/src/flextype/core/Entries/Macros/PhpMacrosTest.php
Normal file
23
tests/src/flextype/core/Entries/Macros/PhpMacrosTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('PhpMacros', function () {
|
||||
entries()->create('blog', serializers()->yaml()->decode(filesystem()->file(ROOT_DIR . '/tests/fixtures/entries/blog-php-macros/entry.yaml')->get()));
|
||||
$this->assertTrue(entries()->create('blog/post-1', ['title' => 'Post 1']));
|
||||
$this->assertTrue(entries()->create('blog/post-2', ['title' => 'Post 2']));
|
||||
$this->assertTrue(entries()->create('blog/post-3', ['title' => 'Post 3']));
|
||||
$this->assertTrue(entries()->create('categories', ['title' => 'Categories']));
|
||||
$this->assertTrue(entries()->create('categories/cat', ['title' => 'Cat']));
|
||||
$this->assertTrue(entries()->create('categories/dog', ['title' => 'Dog']));
|
||||
|
||||
expect(entries()->fetch('blog')->dot()->count())->toBe(44);
|
||||
});
|
Reference in New Issue
Block a user