Extract scheme logic from templates one and normalize structure

This commit is contained in:
Giuseppe Criscione 2020-12-18 15:46:58 +01:00
parent ec2a08ac35
commit 990ac85aed
11 changed files with 587 additions and 574 deletions

View File

@ -1,16 +1,18 @@
admin:
title: '{{user.role.admin}}'
permissions:
dashboard: true
cache: true
backup: true
pages: true
options: true
updates: true
users: true
title: Roles
data:
admin:
title: '{{user.role.admin}}'
permissions:
dashboard: true
cache: true
backup: true
pages: true
options: true
updates: true
users: true
user:
title: '{{user.role.user}}'
permissions:
dashboard: true
pages: true
user:
title: '{{user.role.user}}'
permissions:
dashboard: true
pages: true

View File

@ -1,89 +1,91 @@
section1:
type: header
label: '{{options.site.info}}'
title: Site
fields:
section1:
type: header
label: '{{options.site.info}}'
rows1:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.info.title}}'
column2:
type: column
width: 2-3
fields:
title:
type: text
required: true
rows1:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.info.title}}'
column2:
type: column
width: 2-3
fields:
title:
type: text
required: true
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.info.author}}'
column2:
type: column
width: 2-3
fields:
author:
type: text
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.info.author}}'
column2:
type: column
width: 2-3
fields:
author:
type: text
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.info.description}}'
column2:
type: column
width: 2-3
fields:
description:
type: textarea
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.info.description}}'
column2:
type: column
width: 2-3
fields:
description:
type: textarea
section2:
type: header
label: '{{options.site.advanced}}'
section2:
type: header
label: '{{options.site.advanced}}'
rows2:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.advanced.metadata}}'
column2:
type: column
width: 2-3
fields:
metadata:
type: array
associative: true
placeholder_key: '{{options.site.advanced.metadata.name}}'
placeholder_value: '{{options.site.advanced.metadata.content}}'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.advanced.aliases}}'
column2:
type: column
width: 2-3
fields:
aliases:
type: array
associative: true
placeholder_key: '{{options.site.advanced.aliases.alias}}'
placeholder_value: '{{options.site.advanced.aliases.route}}'
rows2:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.advanced.metadata}}'
column2:
type: column
width: 2-3
fields:
metadata:
type: array
associative: true
placeholder_key: '{{options.site.advanced.metadata.name}}'
placeholder_value: '{{options.site.advanced.metadata.content}}'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.site.advanced.aliases}}'
column2:
type: column
width: 2-3
fields:
aliases:
type: array
associative: true
placeholder_key: '{{options.site.advanced.aliases.alias}}'
placeholder_value: '{{options.site.advanced.aliases.route}}'

View File

@ -1,362 +1,364 @@
section1:
type: header
label: '{{options.system.date-and-time}}'
title: System
fields:
section1:
type: header
label: '{{options.system.date-and-time}}'
rows1:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.date-format}}'
column2:
type: column
width: 2-3
fields:
date.format:
type: select
import:
options: 'Formwork\Admin\Utils\DateFormats::date'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.hour-format}}'
column2:
type: column
width: 2-3
fields:
date.hour_format:
type: select
import:
options: 'Formwork\Admin\Utils\DateFormats::hour'
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.timezone}}'
column2:
type: column
width: 2-3
fields:
date.timezone:
type: select
import:
options: 'Formwork\Admin\Utils\DateFormats::timezones'
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.first-weekday}}'
column2:
type: column
width: 2-3
fields:
date.week_starts:
type: select
options:
0: '{{options.system.date-and-time.first-weekday.sunday}}'
1: '{{options.system.date-and-time.first-weekday.monday}}'
rows1:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.date-format}}'
column2:
type: column
width: 2-3
fields:
date.format:
type: select
import:
options: 'Formwork\Admin\Utils\DateFormats::date'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.hour-format}}'
column2:
type: column
width: 2-3
fields:
date.hour_format:
type: select
import:
options: 'Formwork\Admin\Utils\DateFormats::hour'
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.timezone}}'
column2:
type: column
width: 2-3
fields:
date.timezone:
type: select
import:
options: 'Formwork\Admin\Utils\DateFormats::timezones'
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.date-and-time.first-weekday}}'
column2:
type: column
width: 2-3
fields:
date.week_starts:
type: select
options:
0: '{{options.system.date-and-time.first-weekday.sunday}}'
1: '{{options.system.date-and-time.first-weekday.monday}}'
section2:
type: header
label: '{{options.system.languages}}'
section2:
type: header
label: '{{options.system.languages}}'
rows2:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.languages.available-languages}}'
column2:
type: column
width: 2-3
fields:
languages.available:
type: tags
placeholder: '{{options.system.languages.available-languages.no-languages}}'
pattern: '^[a-z]{2,3}$'
translate: [placeholder]
import:
options: 'Formwork\Languages\LanguageCodes::names'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.languages.preferred-language}}'
column2:
type: column
width: 2-3
fields:
languages.http_preferred:
type: togglegroup
options:
1: '{{options.system.languages.preferred-language.enabled}}'
0: '{{options.system.languages.preferred-language.disabled}}'
rows2:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.languages.available-languages}}'
column2:
type: column
width: 2-3
fields:
languages.available:
type: tags
placeholder: '{{options.system.languages.available-languages.no-languages}}'
pattern: '^[a-z]{2,3}$'
translate: [placeholder]
import:
options: 'Formwork\Languages\LanguageCodes::names'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.languages.preferred-language}}'
column2:
type: column
width: 2-3
fields:
languages.http_preferred:
type: togglegroup
options:
1: '{{options.system.languages.preferred-language.enabled}}'
0: '{{options.system.languages.preferred-language.disabled}}'
section3:
type: header
label: '{{options.system.files}}'
section3:
type: header
label: '{{options.system.files}}'
rows3:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.files.allowed-extensions}}'
column2:
type: column
width: 2-3
fields:
files.allowed_extensions:
type: tags
pattern: '^\.[a-zA-Z0-9]+$'
required: true
rows3:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.files.allowed-extensions}}'
column2:
type: column
width: 2-3
fields:
files.allowed_extensions:
type: tags
pattern: '^\.[a-zA-Z0-9]+$'
required: true
section4:
type: header
label: '{{options.system.cache}}'
section4:
type: header
label: '{{options.system.cache}}'
rows4:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.cache}}'
column2:
type: column
width: 2-3
fields:
cache.enabled:
type: togglegroup
options:
1: '{{options.system.cache.enabled}}'
0: '{{options.system.cache.disabled}}'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.cache.time}}'
column2:
type: column
width: 2-3
fields:
cache.time:
type: duration
min: 900
step: 900
intervals: [weeks, days, hours, minutes]
translate: false
required: true
rows4:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.cache}}'
column2:
type: column
width: 2-3
fields:
cache.enabled:
type: togglegroup
options:
1: '{{options.system.cache.enabled}}'
0: '{{options.system.cache.disabled}}'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.cache.time}}'
column2:
type: column
width: 2-3
fields:
cache.time:
type: duration
min: 900
step: 900
intervals: [weeks, days, hours, minutes]
translate: false
required: true
section5:
type: header
label: '{{options.system.admin-panel}}'
section5:
type: header
label: '{{options.system.admin-panel}}'
rows5:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.default-language}}'
column2:
type: column
width: 2-3
fields:
admin.lang:
type: select
translate: false
import:
options: 'Formwork\Admin\Translation::availableLanguages'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.logout-redirects-to}}'
column2:
type: column
width: 2-3
fields:
admin.logout_redirect:
type: togglegroup
options:
login: '{{options.system.admin-panel.logout-redirects-to.login}}'
home: '{{options.system.admin-panel.logout-redirects-to.home}}'
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.session-timeout}}'
column2:
type: column
width: 2-3
fields:
admin.session_timeout:
type: duration
min: 0
unit: minutes
intervals: [hours, minutes]
translate: false
required: true
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.default-color-scheme}}'
column2:
type: column
width: 2-3
fields:
admin.color_scheme:
type: togglegroup
options:
light: '{{options.system.admin-panel.default-color-scheme.light}}'
dark: '{{options.system.admin-panel.default-color-scheme.dark}}'
rows5:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.default-language}}'
column2:
type: column
width: 2-3
fields:
admin.lang:
type: select
translate: false
import:
options: 'Formwork\Admin\Translation::availableLanguages'
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.logout-redirects-to}}'
column2:
type: column
width: 2-3
fields:
admin.logout_redirect:
type: togglegroup
options:
login: '{{options.system.admin-panel.logout-redirects-to.login}}'
home: '{{options.system.admin-panel.logout-redirects-to.home}}'
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.session-timeout}}'
column2:
type: column
width: 2-3
fields:
admin.session_timeout:
type: duration
min: 0
unit: minutes
intervals: [hours, minutes]
translate: false
required: true
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.admin-panel.default-color-scheme}}'
column2:
type: column
width: 2-3
fields:
admin.color_scheme:
type: togglegroup
options:
light: '{{options.system.admin-panel.default-color-scheme.light}}'
dark: '{{options.system.admin-panel.default-color-scheme.dark}}'
section6:
type: header
label: '{{options.system.images}}'
section6:
type: header
label: '{{options.system.images}}'
rows6:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.jpeg-quality}}'
column2:
type: column
width: 2-3
fields:
images.jpeg_quality:
type: range
min: 0
max: 100
step: 5
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.png-compression-level}}'
column2:
type: column
width: 2-3
fields:
images.png_compression:
type: range
min: 0
max: 9
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.webp-quality}}'
column2:
type: column
width: 2-3
fields:
images.webp_quality:
type: range
min: 0
max: 100
step: 5
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.jpeg-save-progressive}}'
column2:
type: column
width: 2-3
fields:
images.jpeg_progressive:
type: togglegroup
options:
1: '{{options.system.images.jpeg-save-progressive.enabled}}'
0: '{{options.system.images.jpeg-save-progressive.disabled}}'
row5:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.process-uploads}}'
column2:
type: column
width: 2-3
fields:
images.process_uploads:
type: togglegroup
options:
1: '{{options.system.images.process-uploads.enabled}}'
0: '{{options.system.images.process-uploads.disabled}}'
rows6:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.jpeg-quality}}'
column2:
type: column
width: 2-3
fields:
images.jpeg_quality:
type: range
min: 0
max: 100
step: 5
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.png-compression-level}}'
column2:
type: column
width: 2-3
fields:
images.png_compression:
type: range
min: 0
max: 9
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.webp-quality}}'
column2:
type: column
width: 2-3
fields:
images.webp_quality:
type: range
min: 0
max: 100
step: 5
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.jpeg-save-progressive}}'
column2:
type: column
width: 2-3
fields:
images.jpeg_progressive:
type: togglegroup
options:
1: '{{options.system.images.jpeg-save-progressive.enabled}}'
0: '{{options.system.images.jpeg-save-progressive.disabled}}'
row5:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.images.process-uploads}}'
column2:
type: column
width: 2-3
fields:
images.process_uploads:
type: togglegroup
options:
1: '{{options.system.images.process-uploads.enabled}}'
0: '{{options.system.images.process-uploads.disabled}}'
section7:
type: header
label: '{{options.system.backup}}'
section7:
type: header
label: '{{options.system.backup}}'
rows7:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.backup.backup-files-to-keep}}'
column2:
type: column
width: 2-3
fields:
backup.max_files:
type: select
options:
5: 5
10: 10
15: 15
20: 20
rows7:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{options.system.backup.backup-files-to-keep}}'
column2:
type: column
width: 2-3
fields:
backup.max_files:
type: select
options:
5: 5
10: 10
15: 15
20: 20

View File

@ -1,118 +1,120 @@
rows1:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.fullname}}'
column2:
type: column
width: 2-3
fields:
fullname:
type: text
required: true
title: User
fields:
rows1:
type: rows
fields:
row1:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.fullname}}'
column2:
type: column
width: 2-3
fields:
fullname:
type: text
required: true
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.email}}'
column2:
type: column
width: 2-3
fields:
email:
type: email
required: true
row2:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.email}}'
column2:
type: column
width: 2-3
fields:
email:
type: email
required: true
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.password}}'
column2:
type: column
width: 2-3
fields:
password:
type: password
placeholder: '{{user.password.type-new-password}}'
disabled: true
pattern: '^.{8,}$'
autocomplete: new-password
row3:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.password}}'
column2:
type: column
width: 2-3
fields:
password:
type: password
placeholder: '{{user.password.type-new-password}}'
disabled: true
pattern: '^.{8,}$'
autocomplete: new-password
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.language}}'
column2:
type: column
width: 2-3
fields:
language:
type: select
required: true
translate: false
import:
options: 'Formwork\Admin\Translation::availableLanguages'
row4:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.language}}'
column2:
type: column
width: 2-3
fields:
language:
type: select
required: true
translate: false
import:
options: 'Formwork\Admin\Translation::availableLanguages'
row5:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.role}}'
column2:
type: column
width: 2-3
fields:
role:
type: select
disabled: true
import:
options: 'Formwork\Admin\Users\Users::availableRoles'
row5:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.role}}'
column2:
type: column
width: 2-3
fields:
role:
type: select
disabled: true
import:
options: 'Formwork\Admin\Users\Users::availableRoles'
row6:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.color-scheme}}'
column2:
type: column
width: 2-3
fields:
color-scheme:
type: togglegroup
options:
light: '{{user.color-scheme.light}}'
dark: '{{user.color-scheme.dark}}'
auto: '{{user.color-scheme.auto}}'
row6:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.color-scheme}}'
column2:
type: column
width: 2-3
fields:
color-scheme:
type: togglegroup
options:
light: '{{user.color-scheme.light}}'
dark: '{{user.color-scheme.dark}}'
auto: '{{user.color-scheme.auto}}'
row7:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.avatar}}'
column2:
type: column
width: 2-3
fields:
avatar:
type: file
accept: .jpg, .jpeg, .png, .gif
row7:
type: row
fields:
column1:
type: column
width: 1-3
label: '{{user.avatar}}'
column2:
type: column
width: 2-3
fields:
avatar:
type: file
accept: .jpg, .jpeg, .png, .gif

View File

@ -7,7 +7,7 @@ use Formwork\Utils\Notification;
use Formwork\Utils\Registry;
use Formwork\Core\Formwork;
use Formwork\Core\Page;
use Formwork\Template\Scheme;
use Formwork\Schemes\Scheme;
use Formwork\Utils\Header;
use Formwork\Utils\HTTPRequest;
use Formwork\Utils\Str;
@ -130,7 +130,7 @@ trait AdminTrait
*/
protected function scheme(string $template): Scheme
{
return new Scheme($template);
return new Scheme(Formwork::instance()->config()->get('templates.path') . 'schemes' . DS . $template . '.yml');
}
/**

View File

@ -9,6 +9,7 @@ use Formwork\Core\Formwork;
use Formwork\Data\DataGetter;
use Formwork\Parsers\JSON;
use Formwork\Parsers\YAML;
use Formwork\Schemes\Scheme;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTTPRequest;
use Formwork\Utils\HTTPResponse;
@ -38,7 +39,7 @@ class Options extends AbstractController
{
$this->ensurePermission('options.system');
$fields = new Fields(YAML::parseFile(Admin::SCHEMES_PATH . 'system.yml'));
$fields = new Fields((new Scheme(Admin::SCHEMES_PATH . 'system.yml'))->get('fields'));
if (HTTPRequest::method() === 'POST') {
$data = new DataGetter(HTTPRequest::postData());
@ -78,7 +79,7 @@ class Options extends AbstractController
{
$this->ensurePermission('options.site');
$fields = new Fields(YAML::parseFile(Admin::SCHEMES_PATH . 'site.yml'));
$fields = new Fields((new Scheme(Admin::SCHEMES_PATH . 'site.yml'))->get('fields'));
if (HTTPRequest::method() === 'POST') {
$data = new DataGetter(HTTPRequest::postData());

View File

@ -14,6 +14,7 @@ use Formwork\Data\DataSetter;
use Formwork\Files\Image;
use Formwork\Parsers\YAML;
use Formwork\Router\RouteParams;
use Formwork\Schemes\Scheme;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTTPRequest;
@ -111,7 +112,7 @@ class Users extends AbstractController
*/
public function profile(RouteParams $params): void
{
$fields = new Fields(YAML::parseFile(Admin::SCHEMES_PATH . 'user.yml'));
$fields = new Fields((new Scheme(Admin::SCHEMES_PATH . 'user.yml'))->get('fields'));
$user = Admin::instance()->users()->get($params->get('user'));

View File

@ -5,6 +5,7 @@ namespace Formwork\Admin\Users;
use Formwork\Admin\Admin;
use Formwork\Data\AssociativeCollection;
use Formwork\Parsers\YAML;
use Formwork\Schemes\Scheme;
use Formwork\Utils\FileSystem;
class Users extends AssociativeCollection
@ -21,7 +22,7 @@ class Users extends AssociativeCollection
*/
public static function load(): self
{
static::$roles = YAML::parseFile(Admin::SCHEMES_PATH . 'roles.yml');
static::$roles = (new Scheme(Admin::SCHEMES_PATH . 'roles.yml'))->get('data');
$users = [];
foreach (FileSystem::listFiles(Admin::ACCOUNTS_PATH) as $file) {
$parsedData = YAML::parseFile(Admin::ACCOUNTS_PATH . $file);

View File

@ -1,11 +1,11 @@
<?php
namespace Formwork\Template;
namespace Formwork\Schemes;
use Formwork\Core\Formwork;
use Formwork\Data\DataGetter;
use Formwork\Parsers\YAML;
use Formwork\Utils\Arr;
use Formwork\Utils\FileSystem;
class Scheme extends DataGetter
{
@ -26,15 +26,15 @@ class Scheme extends DataGetter
/**
* Create a new Scheme instance
*/
public function __construct(string $name)
public function __construct(string $path)
{
$this->path = Formwork::instance()->config()->get('templates.path') . 'schemes' . DS;
$this->name = $name;
$this->path = $path;
$this->name = FileSystem::name($path);
parent::__construct(YAML::parseFile($this->path . $this->name . '.yml'));
parent::__construct(YAML::parseFile($this->path));
if ($this->has('extend') && $this->get('extend') !== $this->name) {
$parent = new static($this->get('extend'));
$parent = new static(dirname($this->path) . DS . $this->get('extend') . '.yml');
$this->data = Arr::appendMissing($this->data, $parent->toArray());
}

View File

@ -3,6 +3,7 @@
namespace Formwork\Template;
use Formwork\Core\Page;
use Formwork\Schemes\Scheme;
class Layout extends Template
{

View File

@ -5,6 +5,7 @@ namespace Formwork\Template;
use Formwork\Core\Assets;
use Formwork\Core\Formwork;
use Formwork\Core\Page;
use Formwork\Schemes\Scheme;
use Formwork\Utils\FileSystem;
use BadMethodCallException;
use RuntimeException;
@ -112,7 +113,7 @@ class Template
if ($this->scheme !== null) {
return $this->scheme;
}
return $this->scheme = new Scheme($this->name);
return $this->scheme = new Scheme($this->path() . 'schemes' . DS . $this->name . '.yml');
}
/**