* [MOD] Improved theme handling

* [MOD] Code refactoring

Signed-off-by: nuxsmin <nuxsmin@syspass.org>
This commit is contained in:
nuxsmin
2018-11-01 10:24:25 +01:00
parent 67d9a6a84a
commit ec97b603b0
10 changed files with 38 additions and 46 deletions

View File

@@ -129,7 +129,6 @@ abstract class ControllerBase
$this->view->assign('ctx_userIsAdminAcc', $this->userData->getIsAdminAcc());
$this->view->assign('themeUri', $this->view->getTheme()->getThemeUri());
$this->view->assign('isDemo', $this->configData->isDemoEnabled());
$this->view->assign('icons', $this->theme->getIcons());
$this->view->assign('configData', $this->configData);
$this->view->assign('sk', $this->session->isLoggedIn() ? $this->session->generateSecurityKey() : '');

View File

@@ -78,6 +78,9 @@ abstract class HelperBase
* @param ContextInterface $context
* @param EventDispatcher $eventDispatcher
* @param Container $container
*
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
*/
final public function __construct(Template $template, Config $config, ContextInterface $context, EventDispatcher $eventDispatcher, Container $container)
{

View File

@@ -31,7 +31,6 @@ use SP\Core\AppInfoInterface;
use SP\Core\Crypt\CryptPKI;
use SP\Core\Exceptions\SPException;
use SP\Core\Language;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
use SP\Html\DataGrid\Action\DataGridAction;
use SP\Http\Uri;
@@ -402,7 +401,7 @@ final class LayoutHelper extends HelperBase
*/
protected function initialize()
{
$this->theme = $this->dic->get(Theme::class);
$this->theme = $this->dic->get(ThemeInterface::class);
$this->loggedIn = $this->context->isLoggedIn();

View File

@@ -34,7 +34,7 @@ use SP\Core\Context\SessionContext;
use SP\Core\Events\EventDispatcher;
use SP\Core\Exceptions\SPException;
use SP\Core\PhpExtensionChecker;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
use SP\Http\Request;
use SP\Mvc\Controller\ControllerTrait;
@@ -64,7 +64,7 @@ trait WebControllerTrait
*/
protected $session;
/**
* @var Theme
* @var ThemeInterface
*/
protected $theme;
/**
@@ -133,7 +133,7 @@ trait WebControllerTrait
$this->config = $dic->get(Config::class);
$this->configData = $this->config->getConfigData();
$this->session = $dic->get(ContextInterface::class);
$this->theme = $dic->get(Theme::class);
$this->theme = $dic->get(ThemeInterface::class);
$this->eventDispatcher = $dic->get(EventDispatcher::class);
$this->router = $dic->get(Klein::class);
$this->request = $dic->get(Request::class);

View File

@@ -34,7 +34,7 @@ use SP\Core\Crypt\Session as CryptSession;
use SP\Core\Crypt\UUIDCookie;
use SP\Core\Language;
use SP\Core\ModuleBase;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
use SP\DataModel\ItemPreset\SessionTimeout;
use SP\Http\Address;
use SP\Plugin\PluginManager;
@@ -72,7 +72,7 @@ final class Init extends ModuleBase
*/
private $context;
/**
* @var Theme
* @var ThemeInterface
*/
private $theme;
/**
@@ -106,7 +106,7 @@ final class Init extends ModuleBase
parent::__construct($container);
$this->context = $container->get(ContextInterface::class);
$this->theme = $container->get(Theme::class);
$this->theme = $container->get(ThemeInterface::class);
$this->language = $container->get(Language::class);
$this->secureSessionService = $container->get(SecureSessionService::class);
$this->pluginManager = $container->get(PluginManager::class);
@@ -118,10 +118,12 @@ final class Init extends ModuleBase
*
* @param string $controller
*
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
* @throws \SP\Core\Exceptions\ConstraintException
* @throws \SP\Core\Exceptions\InvalidClassException
* @throws \SP\Core\Exceptions\QueryException
* @throws \SP\Core\Exceptions\SPException
* @throws \SP\Repositories\NoSuchItemException
* @throws \Exception
*/
@@ -143,7 +145,7 @@ final class Init extends ModuleBase
$this->language->setLanguage();
// Initialize theme
$this->theme->initialize();
$this->theme->initTheme();
} else {
logger('Browser reload');
@@ -156,7 +158,7 @@ final class Init extends ModuleBase
$this->language->setLanguage(true);
// Re-Initialize theme
$this->theme->initialize(true);
$this->theme->initTheme(true);
}
// Comprobar si es necesario cambiar a HTTPS

View File

@@ -31,6 +31,7 @@ use SP\Core\Acl\Acl;
use SP\Core\Acl\Actions;
use SP\Core\Context\ContextInterface;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
use SP\Http\Request;
use SP\Services\Account\AccountAclService;
use SP\Storage\Database\DatabaseConnectionData;
@@ -65,7 +66,7 @@ return [
},
Acl::class => \DI\autowire(Acl::class)
->constructorParameter('action', get(Actions::class)),
Theme::class => \DI\autowire(Theme::class)
ThemeInterface::class => \DI\autowire(Theme::class)
->constructorParameter('module', APP_MODULE),
PHPMailer::class => \DI\create(PHPMailer::class)
->constructor(true),

View File

@@ -104,36 +104,28 @@ final class Theme implements ThemeInterface
$this->module = $module;
}
/**
* @param bool $force
*
* @throws InvalidClassException
*/
public function initialize($force = false)
{
if (is_dir(VIEW_PATH)) {
$this->initTheme($force);
$this->initIcons();
}
}
/**
* Inicializar el tema visual a utilizar
*
* @param bool $force Forzar la detección del tema para los inicios de sesión
*
* @return void
* @throws InvalidClassException
*/
public function initTheme($force = false)
{
if (empty($this->themeName) || $force === true) {
$this->themeName = $this->getUserTheme() ?: $this->getGlobalTheme();
}
if (is_dir(VIEW_PATH)) {
if (empty($this->themeName) || $force === true) {
$this->themeName = $this->getUserTheme() ?: $this->getGlobalTheme();
}
$this->themeUri = Bootstrap::$WEBURI . '/app/modules/' . $this->module . 'themes' . $this->themeName;
$this->themePath = str_replace(APP_ROOT, '', VIEW_PATH) . DIRECTORY_SEPARATOR . $this->themeName;
$this->themePathFull = VIEW_PATH . DIRECTORY_SEPARATOR . $this->themeName;
$this->viewsPath = $this->themePathFull . DIRECTORY_SEPARATOR . 'views';
$this->themeUri = Bootstrap::$WEBURI . '/app/modules/' . $this->module . 'themes' . $this->themeName;
$this->themePath = str_replace(APP_ROOT, '', VIEW_PATH) . DIRECTORY_SEPARATOR . $this->themeName;
$this->themePathFull = VIEW_PATH . DIRECTORY_SEPARATOR . $this->themeName;
$this->viewsPath = $this->themePathFull . DIRECTORY_SEPARATOR . 'views';
$this->initIcons();
}
}
/**

View File

@@ -28,7 +28,6 @@ defined('APP_ROOT') || die();
use SP\Core\Acl\ActionsInterface;
use SP\Core\Exceptions\FileNotFoundException;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
use SP\Html\DataGrid\Action\DataGridActionInterface;
use SP\Html\DataGrid\Layout\DataGridHeader;
@@ -131,7 +130,7 @@ abstract class DataGridBase implements DataGridInterface
*/
protected $tableTemplate;
/**
* @var Theme
* @var ThemeInterface
*/
protected $theme;

View File

@@ -28,7 +28,6 @@ defined('APP_ROOT') || die();
use SP\Bootstrap;
use SP\Core\Exceptions\FileNotFoundException;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
/**
@@ -72,9 +71,9 @@ final class Template
private $upgraded = false;
/**
* @param Theme $theme
* @param ThemeInterface $theme
*/
public function __construct(Theme $theme)
public function __construct(ThemeInterface $theme)
{
$this->theme = $theme;
$this->vars = new TemplateVarCollection();
@@ -352,14 +351,10 @@ final class Template
*/
public function render()
{
if (count($this->templates) === 0) {
if (empty($this->templates)) {
throw new FileNotFoundException(__('La plantilla no contiene archivos'));
}
/**
* No more icons var.
* $icons = $this->vars->get('icons');
*/
$icons = $this->theme->getIcons();
$configData = $this->vars->get('configData');
$sk = $this->vars->get('sk');
@@ -368,6 +363,8 @@ final class Template
$_getvar = function ($key, $default = null) {
if (DEBUG && !$this->vars->exists($key)) {
logger(sprintf(__('No es posible obtener la variable "%s"'), $key), 'WARN');
return $default;
}
return $this->vars->get($key, $default);

View File

@@ -32,7 +32,7 @@ use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Core\Exceptions\SPException;
use SP\Core\Language;
use SP\Core\UI\Theme;
use SP\Core\UI\ThemeInterface;
use SP\DataModel\UserLoginData;
use SP\DataModel\UserPreferencesData;
use SP\Http\Request;
@@ -81,7 +81,7 @@ final class LoginService extends Service
*/
private $configData;
/**
* @var Theme
* @var ThemeInterface
*/
private $theme;
/**
@@ -429,7 +429,7 @@ final class LoginService extends Service
protected function initialize()
{
$this->configData = $this->config->getConfigData();
$this->theme = $this->dic->get(Theme::class);
$this->theme = $this->dic->get(ThemeInterface::class);
$this->userService = $this->dic->get(UserService::class);
$this->language = $this->dic->get(Language::class);
$this->trackService = $this->dic->get(TrackService::class);