mirror of
https://github.com/flextype/flextype.git
synced 2025-08-06 13:16:45 +02:00
feat(directives): replace few directives with shortcodes analogs
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
<?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;
|
||||
use ChrisKonnertz\StringCalc\StringCalc;
|
||||
|
||||
// Directive: @calc[]
|
||||
emitter()->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']);
|
||||
});
|
||||
|
@@ -1,37 +0,0 @@
|
||||
<?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;
|
||||
|
||||
// Directive: @const[]
|
||||
emitter()->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']);
|
||||
});
|
@@ -1,37 +0,0 @@
|
||||
<?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;
|
||||
|
||||
// Directive: @field[]
|
||||
emitter()->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']);
|
||||
});
|
@@ -1,37 +0,0 @@
|
||||
<?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;
|
||||
|
||||
// Directive: @var[]
|
||||
emitter()->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']);
|
||||
});
|
@@ -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
|
||||
|
15
tests/fixtures/settings/settings.yaml
vendored
15
tests/fixtures/settings/settings.yaml
vendored
@@ -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
|
||||
|
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->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);
|
||||
});
|
@@ -1,26 +0,0 @@
|
||||
<?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']);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->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);
|
||||
});
|
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->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);
|
||||
});
|
Reference in New Issue
Block a user