use settings container

This commit is contained in:
Jean-Christian Paul Denis 2023-03-16 23:56:53 +01:00
parent c21672138e
commit 9ff0b56fda
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
5 changed files with 161 additions and 145 deletions

View File

@ -54,31 +54,13 @@ class Config extends dcNsProcess
return true;
}
$s = new Settings();
# -- Set settings --
try {
$pack_nocomment = !empty($_POST['pack_nocomment']);
$pack_fixnewline = !empty($_POST['pack_fixnewline']);
$pack_overwrite = !empty($_POST['pack_overwrite']);
$pack_filename = (string) $_POST['pack_filename'];
$secondpack_filename = (string) $_POST['secondpack_filename'];
$pack_repository = (string) $_POST['pack_repository'];
$pack_excludefiles = (string) $_POST['pack_excludefiles'];
$check = Utils::is_configured(
Utils::getRepositoryDir($pack_repository),
$pack_filename,
$secondpack_filename
);
if ($check) {
$s = dcCore::app()->blog->settings->get(My::id());
$s->put('pack_nocomment', $pack_nocomment);
$s->put('pack_fixnewline', $pack_fixnewline);
$s->put('pack_overwrite', $pack_overwrite);
$s->put('pack_filename', $pack_filename);
$s->put('secondpack_filename', $secondpack_filename);
$s->put('pack_repository', $pack_repository);
$s->put('pack_excludefiles', $pack_excludefiles);
foreach ($s->listSettings() as $key) {
$s->writeSetting($key, $_POST[$key] ?? '');
}
dcPage::addSuccessNotice(
__('Configuration has been successfully updated.')
@ -88,7 +70,6 @@ class Config extends dcNsProcess
'conf' => '1',
'redir' => dcCore::app()->admin->__get('list')->getRedir(),
]);
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
@ -103,7 +84,7 @@ class Config extends dcNsProcess
}
# -- Get settings --
$s = dcCore::app()->blog->settings->get(My::id());
$s = new Settings();
# -- Display form --
echo
@ -112,7 +93,7 @@ class Config extends dcNsProcess
// pack_repository
(new Para())->items([
(new Label(__('Path to repository:')))->for('pack_repository'),
(new Input('pack_repository'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('pack_repository')),
(new Input('pack_repository'))->class('maximal')->size(65)->maxlenght(255)->value($s->pack_repository),
]),
(new Note())->class('form-note')->text(
sprintf(
@ -126,18 +107,18 @@ class Config extends dcNsProcess
// pack_filename
(new Para())->items([
(new Label(__('Name of exported package:')))->for('pack_filename'),
(new Input('pack_filename'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('pack_filename')),
(new Input('pack_filename'))->class('maximal')->size(65)->maxlenght(255)->value($s->pack_filename),
]),
(new Note())->text(sprintf(__('Preconization: %s'), '%type%-%id%'))->class('form-note'),
// secondpack_filename
(new Para())->items([
(new Label(__('Name of second exported package:')))->for('secondpack_filename'),
(new Input('secondpack_filename'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('secondpack_filename')),
(new Input('secondpack_filename'))->class('maximal')->size(65)->maxlenght(255)->value($s->secondpack_filename),
]),
(new Note())->text(sprintf(__('Preconization: %s'), '%type%-%id%-%version%'))->class('form-note'),
// pack_overwrite
(new Para())->items([
(new Checkbox('pack_overwrite', (bool) $s->get('pack_overwrite')))->value(1),
(new Checkbox('pack_overwrite', $s->pack_overwrite))->value(1),
(new Label(__('Overwrite existing package'), Label::OUTSIDE_LABEL_AFTER))->for('pack_overwrite')->class('classic'),
]),
]),
@ -145,17 +126,17 @@ class Config extends dcNsProcess
// pack_excludefiles
(new Para())->items([
(new Label(__('Extra files to exclude from package:')))->for('pack_excludefiles'),
(new Input('pack_excludefiles'))->class('maximal')->size(65)->maxlenght(255)->value((string) $s->get('pack_excludefiles')),
(new Input('pack_excludefiles'))->class('maximal')->size(65)->maxlenght(255)->value($s->pack_excludefiles),
]),
(new Note())->text(sprintf(__('Preconization: %s'), '*.zip,*.tar,*.tar.gz'))->class('form-note'),
// pack_nocomment
(new Para())->items([
(new Checkbox('pack_nocomment', (bool) $s->get('pack_nocomment')))->value(1),
(new Checkbox('pack_nocomment', $s->pack_nocomment))->value(1),
(new Label(__('Remove comments from files'), Label::OUTSIDE_LABEL_AFTER))->for('pack_nocomment')->class('classic'),
]),
// pack_fixnewline
(new Para())->items([
(new Checkbox('pack_fixnewline', (bool) $s->get('pack_fixnewline')))->value(1),
(new Checkbox('pack_fixnewline', $s->pack_fixnewline))->value(1),
(new Label(__('Fix newline style from files content'), Label::OUTSIDE_LABEL_AFTER))->for('pack_fixnewline')->class('classic'),
]),

View File

@ -21,55 +21,12 @@ use Exception;
class Install extends dcNsProcess
{
// Module specs
private static $mod_conf = [
[
'menu_plugins',
'Add link to pacKman in plugins page',
false,
'boolean',
],
[
'pack_nocomment',
'Remove comments from files',
false,
'boolean',
],
[
'pack_overwrite',
'Overwrite existing package',
false,
'boolean',
],
[
'pack_filename',
'Name of package',
'%type%-%id%',
'string',
],
[
'secondpack_filename',
'Name of second package',
'%type%-%id%-%version%',
'string',
],
[
'pack_repository',
'Path to package repository',
'',
'string',
],
[
'pack_excludefiles',
'Extra files to exclude from package',
'*.zip,*.tar,*.tar.gz,.directory,.hg',
'string',
],
];
public static function init(): bool
{
self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
if (defined('DC_CONTEXT_ADMIN')) {
self::$init = version_compare(phpversion(), My::PHP_MIN, '>=')
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
}
return self::$init;
}
@ -84,18 +41,6 @@ class Install extends dcNsProcess
// Upgrade
self::growUp();
// Set module settings
foreach (self::$mod_conf as $v) {
dcCore::app()->blog->settings->get(My::id())->put(
$v[0],
$v[2],
$v[3],
$v[1],
false,
true
);
}
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());

View File

@ -23,26 +23,16 @@ use dcNsProcess;
/* clearbricks ns */
use files;
use http;
use path;
/* php ns */
use Exception;
class Manage extends dcNsProcess
{
private static $plugins_path = '';
private static $themes_path = '';
public static function init(): bool
{
if (defined('DC_CONTEXT_ADMIN')) {
dcPage::checkSuper();
# Paths
$e = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
$p = array_pop($e);
self::$plugins_path = (string) path::real($p);
self::$themes_path = dcCore::app()->blog->themes_path;
self::$init = dcCore::app()->auth->isSuperAdmin() && version_compare(phpversion(), My::PHP_MIN, '>=');
}
return self::$init;
@ -59,8 +49,8 @@ class Manage extends dcNsProcess
$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : '';
# Settings
$s = dcCore::app()->blog->settings->get(My::id());
$dir = Utils::getRepositoryDir($s->get('pack_repository'));
$s = new Settings();
$dir = Utils::getRepositoryDir($s->pack_repository);
# Modules
if (!(dcCore::app()->themes instanceof dcThemes)) {
@ -73,7 +63,7 @@ class Manage extends dcNsProcess
# Rights
$is_writable = Utils::is_writable(
$dir,
$s->get('pack_filename')
$s->pack_filename
);
$is_editable = !empty($type)
&& !empty($_POST['modules'])
@ -85,13 +75,13 @@ class Manage extends dcNsProcess
if (isset($_REQUEST['package']) && empty($type)) {
$modules = [];
if ($type == 'plugins') {
$modules = Core::getPackages(self::$plugins_path);
$modules = Core::getPackages(Utils::getPluginsPath());
} elseif ($type == 'themes') {
$modules = Core::getPackages(self::$themes_path);
$modules = Core::getPackages(Utils::getThemesPath());
} else {
$modules = array_merge(
Core::getPackages(dirname($dir . '/' . $s->get('pack_filename'))),
Core::getPackages(dirname($dir . '/' . $s->get('secondpack_filename')))
Core::getPackages(dirname($dir . '/' . $s->pack_filename)),
Core::getPackages(dirname($dir . '/' . $s->secondpack_filename))
);
}
@ -140,19 +130,18 @@ class Manage extends dcNsProcess
$module['id'] = $id;
$module['type'] = $type == 'themes' ? 'theme' : 'plugin';
$files = [
(string) $s->get('pack_filename'),
(string) $s->get('secondpack_filename'),
];
$nocomment = (bool) $s->get('pack_nocomment');
$fixnewline = (bool) $s->get('pack_fixnewline');
$overwrite = (bool) $s->get('pack_overwrite');
$exclude = explode(',', (string) $s->get('pack_excludefiles'));
# --BEHAVIOR-- packmanBeforeCreatePackage
dcCore::app()->callBehavior('packmanBeforeCreatePackage', $module);
Core::pack($module, $dir, $files, $overwrite, $exclude, $nocomment, $fixnewline);
Core::pack(
$module,
$dir,
[$s->pack_filename, $s->secondpack_filename],
$s->pack_overwrite,
explode(',', $s->pack_excludefiles),
$s->pack_nocomment,
$s->pack_fixnewline
);
# --BEHAVIOR-- packmanAfterCreatePackage
dcCore::app()->callBehavior('packmanAfterCreatePackage', $module);
@ -224,9 +213,9 @@ class Manage extends dcNsProcess
} elseif (strpos($action, 'copy_to_') !== false) {
$dest = (string) $dir;
if ($action == 'copy_to_plugins') {
$dest = self::$plugins_path;
$dest = Utils::getPluginsPath();
} elseif ($action == 'copy_to_themes') {
$dest = self::$themes_path;
$dest = Utils::getThemesPath();
}
foreach ($_POST['modules'] as $root => $id) {
@ -250,9 +239,9 @@ class Manage extends dcNsProcess
} elseif (strpos($action, 'move_to_') !== false) {
$dest = (string) $dir;
if ($action == 'move_to_plugins') {
$dest = self::$plugins_path;
$dest = Utils::getPluginsPath();
} elseif ($action == 'move_to_themes') {
$dest = self::$themes_path;
$dest = Utils::getThemesPath();
}
foreach ($_POST['modules'] as $root => $id) {
@ -287,13 +276,13 @@ class Manage extends dcNsProcess
}
# Settings
$s = dcCore::app()->blog->settings->get(My::id());
$dir = Utils::getRepositoryDir($s->get('pack_repository'));
$s = new Settings();
$dir = Utils::getRepositoryDir($s->pack_repository);
$is_configured = Utils::is_configured(
$dir,
$s->get('pack_filename'),
$s->get('secondpack_filename')
$s->pack_filename,
$s->secondpack_filename
);
# Display
@ -319,13 +308,6 @@ class Manage extends dcNsProcess
'<a href="' . dcCore::app()->adminurl->get('admin.plugins', ['module' => My::id(), 'conf' => '1', 'redir' => dcCore::app()->adminurl->get('admin.plugin.' . My::id())]) . '">' . __('Configuration') . '</a>' .
'</div>';
} else {
$repo_path_modules = array_merge(
Core::getPackages(dirname($dir . '/' . $s->get('pack_filename'))),
Core::getPackages(dirname($dir . '/' . $s->get('secondpack_filename')))
);
$plugins_path_modules = Core::getPackages(self::$plugins_path);
$themes_path_modules = Core::getPackages(self::$themes_path);
Utils::modules(
Utils::getModules('plugins'),
'plugins',
@ -339,19 +321,22 @@ class Manage extends dcNsProcess
);
Utils::repository(
$plugins_path_modules,
Core::getPackages(Utils::getPluginsPath()),
'plugins',
__('Plugins root')
);
Utils::repository(
$themes_path_modules,
Core::getPackages(Utils::getThemesPath()),
'themes',
__('Themes root')
);
Utils::repository(
$repo_path_modules,
array_merge(
Core::getPackages(dirname($dir . '/' . $s->pack_filename)),
Core::getPackages(dirname($dir . '/' . $s->secondpack_filename))
),
'repository',
__('Packages repository')
);

92
src/Settings.php Normal file
View File

@ -0,0 +1,92 @@
<?php
/**
* @brief pacKman, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\pacKman;
use dcCore;
class Settings
{
// Remove comments from files
public readonly bool $pack_nocomment;
// Remove comments from files
public readonly bool $pack_fixnewline;
// Overwrite existing package
public readonly bool $pack_overwrite;
// Name of package
public readonly string $pack_filename;
// Name of second package
public readonly string $secondpack_filename;
// Path to package repository
public readonly string $pack_repository;
// Extra files to exclude from package
public readonly string $pack_excludefiles;
/**
* Constructor set up plugin settings
*/
public function __construct()
{
$s = dcCore::app()->blog->settings->get(My::id());
$this->pack_nocomment = (bool) ($s->get('pack_nocomment') ?? false);
$this->pack_fixnewline = (bool) ($s->get('pack_fixnewline') ?? false);
$this->pack_overwrite = (bool) ($s->get('pack_overwrite') ?? false);
$this->pack_filename = (string) ($s->get('pack_filename') ?? '%type%-%id%');
$this->secondpack_filename = (string) ($s->get('secondpack_filename') ?? '%type%-%id%-%version%');
$this->pack_repository = (string) ($s->get('pack_repository') ?? '');
$this->pack_excludefiles = (string) ($s->get('pack_excludefiles') ?? '*.zip,*.tar,*.tar.gz,.directory,.hg');
}
public function getSetting(string $key): mixed
{
return $this->{$key} ?? null;
}
/**
* Overwrite a plugin settings (in db)
*
* @param string $key The setting ID
* @param mixed $value The setting value
*
* @return bool True on success
*/
public function writeSetting(string $key, mixed $value): bool
{
if (property_exists($this, $key) && settype($value, gettype($this->{$key})) === true) {
dcCore::app()->blog->settings->get(My::id())->drop($key);
dcCore::app()->blog->settings->get(My::id())->put($key, $value, gettype($this->{$key}), '', true, true);
return true;
}
return false;
}
/**
* List defined settings keys
*
* @return array The settings keys
*/
public function listSettings(): array
{
return array_keys(get_class_vars(Settings::class));
}
}

View File

@ -29,6 +29,19 @@ use Exception;
class Utils
{
public static function getPluginsPath(): string
{
$e = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
$p = array_pop($e);
return (string) path::real($p);
}
public static function getThemesPath(): string
{
return dcCore::app()->blog->themes_path;
}
public static function is_configured(string $repo, string $file_a, string $file_b): bool
{
if (!is_dir(DC_TPL_CACHE) || !is_writable(DC_TPL_CACHE)) {