1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 13:16:45 +02:00

feat(vars): new Vars service to store global variables

This commit is contained in:
Awilum
2022-09-09 14:26:18 +03:00
parent 9060fa30a4
commit 7de0b86eed
4 changed files with 139 additions and 16 deletions

View File

@@ -0,0 +1,70 @@
<?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.
*/
namespace Flextype;
use Glowy\Arrays\Arrays as Collection;
class Vars extends Collection
{
/**
* Vars instance
*/
protected static Vars|null $instance = null;
/**
* Vars registry storage
*/
protected static Collection|null $registry = null;
/**
* Gets the instance via lazy initialization (created on first usage)
*/
public static function getInstance(): Vars
{
if (static::$instance === null) {
static::$instance = new self();
}
if (static::$registry === null) {
static::$registry = new Collection();
}
return static::$instance;
}
/**
* Is not allowed to call from outside to prevent from creating multiple instances,
* to use the Registry, you have to obtain the instance from Registry::getInstance() instead.
*/
protected function __construct()
{
}
/**
* Prevent the instance from being cloned (which would create a second instance of it)
*/
protected function __clone()
{
}
/**
* Prevent from being unserialized (which would create a second instance of it)
*/
public function __wakeup(): void
{
}
}

View File

@@ -48,16 +48,6 @@ if (! function_exists('container')) {
}
}
if (! function_exists('expression')) {
/**
* Get Flextype Expression Service.
*/
function expression()
{
return flextype()->container()->get('expression');
}
}
if (! function_exists('console')) {
/**
* Get Flextype Console Service.
@@ -158,6 +148,16 @@ if (! function_exists('actions')) {
}
}
if (! function_exists('vars')) {
/**
* Get Flextype Vars Service.
*/
function vars()
{
return flextype()->container()->get('vars');
}
}
if (! function_exists('csrf')) {
/**
* Get Flextype CSRF Service.

View File

@@ -0,0 +1,30 @@
<?php
use Flextype\Component\Filesystem\Filesystem;
use function Glowy\Filesystem\filesystem;
use function Flextype\entries;
beforeEach(function() {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->create();
});
afterEach(function (): void {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->delete();
});
test('var expression', function () {
entries()->create('var', [
'title' => 'Title',
// Set
'test-set-1' => '[% vars().set("test-set-1", "Foo!") %]',
// Get
'test-get-1' => '[[ vars().get("test-set-1") ]]',
'test-get-2' => '[[ var("test-set-1") ]]',
]);
expect(entries()->fetch('var')['test-get-1'])->toBe('Foo!');
expect(entries()->fetch('var')['test-get-2'])->toBe('Foo!');
});

View File

@@ -16,14 +16,37 @@ afterEach(function () {
});
test('var shortcode', function () {
expect(entries()->create('foo', ['vars' => ['foo' => 'Foo'], 'title' => '(var:foo) (var get:foo)']))->toBeTrue();
expect(entries()->fetch('foo')['title'])->toBe('Foo Foo');
expect(entries()->create('foo', [
'title' => 'Title',
// set
'test-set-1' => '(var set:bar1 value:Bar1)(var:bar1)',
'test-set-2' => '(var set:bar2)Bar2(/var)(var:bar2)',
'test-set-3' => '(var set:level1.level2.level3)Multilevel(/var)(var:level1.level2.level3)',
'test-set-4' => '(var set:_.foo)Foo(/var)(var:_.foo)',
expect(entries()->create('bar', ['title' => '(var set:bar value:Bar)(var:bar)']))->toBeTrue();
expect(entries()->fetch('bar')['title'])->toBe('Bar');
// get
'test-get-1' => '(var:bar1)',
'test-get-3' => '(var get:foo default:Foo)',
'test-get-4' => '(var get:foo)Foo(/var)',
// unset
'test-unset-1' => '(var unset:bar1)(var:bar1)',
expect(entries()->create('zed', ['title' => '(var set:zed)Zed(/var)(var:zed)']))->toBeTrue();
expect(entries()->fetch('zed')['title'])->toBe('Zed');
// delete
'test-delete-1' => '(var delete:bar1)(var:bar1)',
]))->toBeTrue();
$foo = entries()->fetch('foo');
expect($foo['test-get-3'])->toBe('Foo');
expect($foo['test-get-4'])->toBe('Foo');
expect($foo['test-set-1'])->toBe('Bar1');
expect($foo['test-set-2'])->toBe('Bar2');
expect($foo['test-set-3'])->toBe('Multilevel');
expect($foo['test-set-4'])->toBe('Foo');
expect($foo['test-unset-1'])->toBe('');
expect($foo['test-delete-1'])->toBe('');
});
test('var shortcode disabled', function () {