From 7047aba7deb4c7559fe8439a40977cd35974bd3b Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 27 May 2022 08:28:21 +0300 Subject: [PATCH] feat(directives): replace few directives with shortcodes analogs --- .../core/Entries/Directives/CalcDirective.php | 39 ------------------- .../Entries/Directives/ConstantsDirective.php | 37 ------------------ .../Entries/Directives/FieldsDirective.php | 37 ------------------ .../core/Entries/Directives/VarsDirective.php | 37 ------------------ src/flextype/settings.yaml | 12 ------ tests/fixtures/settings/settings.yaml | 15 ++----- .../Entries/Directives/CalcDirectiveTest.php | 24 ------------ .../Directives/ConstantsDirectiveTest.php | 26 ------------- .../Directives/FieldsDirectiveTest.php | 23 ----------- .../Entries/Directives/VarsDirectiveTest.php | 24 ------------ 10 files changed, 3 insertions(+), 271 deletions(-) delete mode 100644 src/flextype/core/Entries/Directives/CalcDirective.php delete mode 100644 src/flextype/core/Entries/Directives/ConstantsDirective.php delete mode 100644 src/flextype/core/Entries/Directives/FieldsDirective.php delete mode 100644 src/flextype/core/Entries/Directives/VarsDirective.php delete mode 100644 tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php delete mode 100644 tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php delete mode 100644 tests/src/flextype/core/Entries/Directives/FieldsDirectiveTest.php delete mode 100644 tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php diff --git a/src/flextype/core/Entries/Directives/CalcDirective.php b/src/flextype/core/Entries/Directives/CalcDirective.php deleted file mode 100644 index d9dc2808..00000000 --- a/src/flextype/core/Entries/Directives/CalcDirective.php +++ /dev/null @@ -1,39 +0,0 @@ -addListener('onEntriesFetchSingleField', static function (): void { - - if (! registry()->get('flextype.settings.entries.directives.calc.enabled')) { - return; - } - - $field = entries()->registry()->get('methods.fetch.field'); - $result = entries()->registry()->get('methods.fetch.result'); - - if (is_string($field['value'])) { - $field['value'] = preg_replace_callback('/@calc\[(.*?)\]/s', function($matches) { - return (new StringCalc())->calculate($matches[1]); - }, $field['value']); - } - - entries()->registry()->set('methods.fetch.field.key', $field['key']); - entries()->registry()->set('methods.fetch.field.value', $field['value']); -}); - diff --git a/src/flextype/core/Entries/Directives/ConstantsDirective.php b/src/flextype/core/Entries/Directives/ConstantsDirective.php deleted file mode 100644 index 2374fd48..00000000 --- a/src/flextype/core/Entries/Directives/ConstantsDirective.php +++ /dev/null @@ -1,37 +0,0 @@ -addListener('onEntriesFetchSingleField', static function (): void { - - if (! registry()->get('flextype.settings.entries.directives.constants.enabled')) { - return; - } - - $field = entries()->registry()->get('methods.fetch.field'); - - if (is_string($field['value'])) { - $field['value'] = strings($field['value']) - ->replace('@const[ROOT_DIR]', ROOT_DIR) - ->replace('@const[PATH_PROJECT]', PATH['project']) - ->toString(); - } - - entries()->registry()->set('methods.fetch.field.key', $field['key']); - entries()->registry()->set('methods.fetch.field.value', $field['value']); -}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Directives/FieldsDirective.php b/src/flextype/core/Entries/Directives/FieldsDirective.php deleted file mode 100644 index 14583db6..00000000 --- a/src/flextype/core/Entries/Directives/FieldsDirective.php +++ /dev/null @@ -1,37 +0,0 @@ -addListener('onEntriesFetchSingleField', static function (): void { - - if (! registry()->get('flextype.settings.entries.directives.fields.enabled')) { - return; - } - - $field = entries()->registry()->get('methods.fetch.field'); - $result = entries()->registry()->get('methods.fetch.result'); - - if (is_string($field['value'])) { - $field['value'] = preg_replace_callback('/@field\[(.*?)\]/s', function($matches) use ($result) { - return collection($result)->get($matches[1]); - }, $field['value']); - } - - entries()->registry()->set('methods.fetch.field.key', $field['key']); - entries()->registry()->set('methods.fetch.field.value', $field['value']); -}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Directives/VarsDirective.php b/src/flextype/core/Entries/Directives/VarsDirective.php deleted file mode 100644 index b61e6bf9..00000000 --- a/src/flextype/core/Entries/Directives/VarsDirective.php +++ /dev/null @@ -1,37 +0,0 @@ -addListener('onEntriesFetchSingleField', static function (): void { - - if (! registry()->get('flextype.settings.entries.directives.vars.enabled')) { - return; - } - - $field = entries()->registry()->get('methods.fetch.field'); - $result = entries()->registry()->get('methods.fetch.result'); - - if (is_string($field['value'])) { - $field['value'] = preg_replace_callback('/@var\[(.*?)\]/s', function($matches) use ($result) { - return collection($result['vars'])->get(trim($matches[1])); - }, $field['value']); - } - - entries()->registry()->set('methods.fetch.field.key', $field['key']); - entries()->registry()->set('methods.fetch.field.value', $field['value']); -}); \ No newline at end of file diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 46d1c6c7..a775ac81 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -81,18 +81,6 @@ entries: enabled: true enabled_globally: true path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php" - constants: - enabled: true - path: "/src/flextype/core/Entries/Directives/ConstantsDirective.php" - fields: - enabled: true - path: "/src/flextype/core/Entries/Directives/FieldsDirective.php" - vars: - enabled: true - path: "/src/flextype/core/Entries/Directives/VarsDirective.php" - calc: - enabled: true - path: "/src/flextype/core/Entries/Directives/CalcDirective.php" markdown: enabled: true enabled_globally: false diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index c620a324..bbca73b4 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -77,26 +77,17 @@ entries: enabled: true enabled_globally: true path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php" - constants: - enabled: true - path: "/src/flextype/core/Entries/Directives/ConstantsDirective.php" - fields: - enabled: true - path: "/src/flextype/core/Entries/Directives/FieldsDirective.php" - vars: - enabled: true - path: "/src/flextype/core/Entries/Directives/VarsDirective.php" - calc: - enabled: true - path: "/src/flextype/core/Entries/Directives/CalcDirective.php" markdown: enabled: true + enabled_globally: false path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" textile: enabled: true + enabled_globally: false path: "/src/flextype/core/Entries/Directives/TextileDirective.php" php: enabled: true + enabled_globally: false path: "/src/flextype/core/Entries/Directives/PhpDirective.php" types: enabled: true diff --git a/tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php deleted file mode 100644 index 65cf77ff..00000000 --- a/tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php +++ /dev/null @@ -1,24 +0,0 @@ -directory(PATH['project'] . '/entries')->create(); -}); - -afterEach(function (): void { - filesystem()->directory(PATH['project'] . '/entries')->delete(); -}); - -test('calc directive', function () { - registry()->set('flextype.settings.entries.directives.calc.enabled', true); - entries()->create('field', ['foo' => '@type[int] @calc[2+2]']); - expect(entries()->fetch('field')['foo'])->toBe(4); -}); - -test('calc directive disabled', function () { - registry()->set('flextype.settings.entries.directives.calc.enabled', false); - entries()->create('field', ['foo' => '@calc[2+2]']); - expect(entries()->fetch('field')['foo'])->toBe('@calc[2+2]'); - registry()->set('flextype.settings.entries.directives.calc.enabled', true); -}); \ 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 deleted file mode 100644 index a42890ca..00000000 --- a/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php +++ /dev/null @@ -1,26 +0,0 @@ -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']); -}); - -test('constants directive disabled', function () { - registry()->set('flextype.settings.entries.directives.constants.enabled', false); - entries()->create('const-root-dir-3', ['path' => '@const[ROOT_DIR]']); - expect(entries()->fetch('const-root-dir-3')['path'])->toBe('@const[ROOT_DIR]'); - registry()->set('flextype.settings.entries.directives.constants.enabled', true); -}); \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Directives/FieldsDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/FieldsDirectiveTest.php deleted file mode 100644 index a70a5b7a..00000000 --- a/tests/src/flextype/core/Entries/Directives/FieldsDirectiveTest.php +++ /dev/null @@ -1,23 +0,0 @@ -directory(PATH['project'] . '/entries')->create(); -}); - -afterEach(function (): void { - filesystem()->directory(PATH['project'] . '/entries')->delete(); -}); - -test('fields directive', function () { - entries()->create('field', ['foo' => '@field[id]']); - $this->assertEquals('field', entries()->fetch('field')['foo']); -}); - -test('fields directive disabled', function () { - registry()->set('flextype.settings.entries.directives.fields.enabled', false); - entries()->create('field', ['foo' => '@field[id]']); - $this->assertEquals('@field[id]', entries()->fetch('field')['foo']); - registry()->set('flextype.settings.entries.directives.fields.enabled', true); -}); \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php deleted file mode 100644 index ec94d58b..00000000 --- a/tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php +++ /dev/null @@ -1,24 +0,0 @@ -directory(PATH['project'] . '/entries')->create(); -}); - -afterEach(function (): void { - filesystem()->directory(PATH['project'] . '/entries')->delete(); -}); - -test('vars directive', function () { - entries()->create('type-vars', ['vars' => ['foo' => 'Foo'], 'title' => '@var[foo]']); - - $this->assertEquals('Foo', entries()->fetch('type-vars')['title']); -}); - -test('vars directive disabled', function () { - registry()->set('flextype.settings.entries.directives.vars.enabled', false); - entries()->create('type-vars', ['vars' => ['foo' => 'Foo'], 'title' => '@var[foo]']); - $this->assertEquals('@var[foo]', entries()->fetch('type-vars')['title']); - registry()->set('flextype.settings.entries.directives.vars.enabled', true); -}); \ No newline at end of file