diff --git a/src/flextype/core/Entries/Directives/ConstantsDirective.php b/src/flextype/core/Entries/Directives/ConstantsDirective.php new file mode 100644 index 00000000..5bc067fb --- /dev/null +++ b/src/flextype/core/Entries/Directives/ConstantsDirective.php @@ -0,0 +1,36 @@ +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); +}); \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php new file mode 100644 index 00000000..7f071fdc --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php @@ -0,0 +1,19 @@ +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']); +}); \ No newline at end of file