1
0
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:
Awilum
2022-05-09 07:44:17 +03:00
parent ee9520eb7c
commit 7d610f0a0e
5 changed files with 62 additions and 0 deletions

View 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();
}
});

View File

@@ -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"

View 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);

View File

@@ -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"

View 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);
});