mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 06:06:45 +02:00
feat(directives): add constants directive
This commit is contained in:
36
src/flextype/core/Entries/Directives/ConstantsDirective.php
Normal file
36
src/flextype/core/Entries/Directives/ConstantsDirective.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?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.constants.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$field = entries()->registry()->get('methods.fetch.field');
|
||||
|
||||
if (is_string($field)) {
|
||||
if (strings($field)->contains('@const:ROOT_DIR')) {
|
||||
$field = strings($field)->replace('@const:ROOT_DIR', ROOT_DIR)->trim()->toString();
|
||||
} elseif (strings($field)->contains('@const:PATH_PROJECT')) {
|
||||
$field = strings($field)->replace('@const:PATH_PROJECT', PATH['project'])->trim()->toString();
|
||||
}
|
||||
}
|
||||
|
||||
entries()->registry()->set('methods.fetch.field', $field);
|
||||
});
|
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('constants directive', function () {
|
||||
entries()->create('const-root-dir', ['path' => '@const:ROOT_DIR']);
|
||||
entries()->create('const-root-dir-2', ['path' => '@const:PATH_PROJECT']);
|
||||
|
||||
$this->assertEquals(ROOT_DIR, entries()->fetch('const-root-dir')['path']);
|
||||
$this->assertEquals(PATH['project'], entries()->fetch('const-root-dir-2')['path']);
|
||||
});
|
Reference in New Issue
Block a user