refactor configurations (#2626)

* composition instead of inheritance
* refactored some twig extensions to runtime extensions
This commit is contained in:
Kevin Papst
2021-06-26 13:03:54 +02:00
committed by GitHub
parent 3d1fba650b
commit 6d196879b4
22 changed files with 255 additions and 358 deletions

View File

@@ -9,7 +9,7 @@
namespace App\Configuration;
class MailConfiguration
final class MailConfiguration
{
/**
* @var string

View File

@@ -11,6 +11,9 @@ namespace App\Configuration;
use App\Entity\Configuration;
/**
* @internal do NOT use this trait, but access your configs via SystemConfiguration
*/
trait StringAccessibleConfigTrait
{
/**

View File

@@ -376,4 +376,21 @@ class SystemConfiguration implements SystemBundleConfiguration
return $this->default('theme.color_choices');
}
// ========== Branding configurations ==========
public function getBrandingTitle(): ?string
{
return $this->find('theme.branding.title');
}
public function getBrandingCompany(): ?string
{
return $this->find('theme.branding.company');
}
public function isAllowTagCreation(): bool
{
return (bool) $this->find('theme.tags_create');
}
}

View File

@@ -10,43 +10,66 @@
namespace App\Configuration;
/**
* @internal will be deprecated soon, use SystemConfiguration instead
* @internal might be deprecated in the future, use SystemConfiguration instead
*/
class ThemeConfiguration implements SystemBundleConfiguration, \ArrayAccess
final class ThemeConfiguration implements \ArrayAccess
{
use StringAccessibleConfigTrait;
private $systemConfiguration;
public function getPrefix(): string
public function __construct(SystemConfiguration $systemConfiguration)
{
return 'theme';
}
public function isAutoReloadDatatable(): bool
{
@trigger_error('The configuration auto_reload_datatable is deprecated and was removed with 1.4', E_USER_DEPRECATED);
return false;
}
public function isAllowTagCreation(): bool
{
return (bool) $this->find('tags_create');
$this->systemConfiguration = $systemConfiguration;
}
/**
* Currently unused, as JS selects are always activated.
* @deprecated since 1.7 will be removed with 2.0
* @return bool
*/
public function getSelectPicker(): string
public function offsetExists($offset)
{
@trigger_error('getSelectPicker() is deprecated and will be removed with 2.0', E_USER_DEPRECATED);
return (string) $this->find('select_type');
return $this->systemConfiguration->has('theme.' . $offset);
}
/**
* @return mixed
*/
public function offsetGet($offset)
{
return $this->systemConfiguration->find('theme.' . $offset);
}
/**
* @param mixed $offset
* @param mixed $value
* @throws \BadMethodCallException
*/
public function offsetSet($offset, $value)
{
throw new \BadMethodCallException('ThemeConfiguration does not support offsetSet()');
}
/**
* @param mixed $offset
* @throws \BadMethodCallException
*/
public function offsetUnset($offset)
{
throw new \BadMethodCallException('ThemeConfiguration does not support offsetUnset()');
}
/**
* @deprecated since 1.15
*/
public function isAllowTagCreation(): bool
{
return (bool) $this->offsetGet('tags_create');
}
/**
* @deprecated since 1.15
*/
public function getTitle(): ?string
{
$title = $this->find('branding.title');
$title = $this->offsetGet('branding.title');
if (null === $title) {
return null;
}

View File

@@ -69,7 +69,7 @@ class AppExtension extends Extension
$container->setParameter('kimai.defaults', $config['defaults']); // @deprecated since 1.13
$this->createPermissionParameter($config['permissions'], $container);
$this->createThemeParameter($config['theme'], $container);
$container->setParameter('kimai.theme', $config['theme']); // @deprecated since 1.15
$container->setParameter('kimai.timesheet', $config['timesheet']); // @deprecated since 1.13
$container->setParameter('kimai.timesheet.rates', $config['timesheet']['rates']);
$container->setParameter('kimai.timesheet.rounding', $config['timesheet']['rounding']);
@@ -211,17 +211,6 @@ class AppExtension extends Extension
return $result;
}
/**
* @param array $config
* @param ContainerBuilder $container
*/
protected function createThemeParameter(array $config, ContainerBuilder $container)
{
$container->setParameter('kimai.theme', $config);
$container->setParameter('kimai.theme.select_type', $config['select_type']);
$container->setParameter('kimai.theme.show_about', $config['show_about']);
}
/**
* @return string
*/

View File

@@ -9,17 +9,14 @@
namespace App\Form\Type;
use App\Configuration\ThemeConfiguration;
use App\Configuration\SystemConfiguration;
use Symfony\Component\Form\AbstractType;
class TagsType extends AbstractType
final class TagsType extends AbstractType
{
/**
* @var ThemeConfiguration
*/
private $configuration;
public function __construct(ThemeConfiguration $configuration)
public function __construct(SystemConfiguration $configuration)
{
$this->configuration = $configuration;
}

View File

@@ -1,62 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Twig;
use App\Configuration\SystemConfiguration;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class ConfigExtension extends AbstractExtension
{
/**
* @var SystemConfiguration
*/
private $configuration;
public function __construct(SystemConfiguration $configuration)
{
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new TwigFunction('theme_config', [$this, 'getThemeConfig']),
];
}
/**
* @param string $name
* @return mixed
*/
public function getThemeConfig(string $name)
{
@trigger_error('The twig function "theme_config" was deprecated with 1.15, replace it with the global "kimai_config" variable.', E_USER_DEPRECATED);
switch ($name) {
case 'auto_reload_datatable':
@trigger_error('The configuration auto_reload_datatable is deprecated and was removed with 1.4', E_USER_DEPRECATED);
return false;
case 'soft_limit':
return $this->configuration->getTimesheetActiveEntriesHardLimit();
default:
$name = 'theme.' . $name;
break;
}
return $this->configuration->find($name);
}
}

View File

@@ -9,25 +9,29 @@
namespace App\Twig\Runtime;
use App\Configuration\SystemConfiguration;
use App\Constants;
use App\Entity\User;
use App\Event\PageActionsEvent;
use App\Event\ThemeEvent;
use App\Event\ThemeJavascriptTranslationsEvent;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;
final class ThemeExtension implements RuntimeExtensionInterface
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
private $translator;
private $configuration;
public function __construct(EventDispatcherInterface $dispatcher)
public function __construct(EventDispatcherInterface $dispatcher, TranslatorInterface $translator, SystemConfiguration $configuration)
{
$this->eventDispatcher = $dispatcher;
$this->translator = $translator;
$this->configuration = $configuration;
}
/**
@@ -95,4 +99,40 @@ final class ThemeExtension implements RuntimeExtensionInterface
return $class;
}
public function generateTitle(?string $prefix = null, string $delimiter = ' '): string
{
$title = $this->configuration->getBrandingTitle();
if (null === $title || \strlen($title) === 0) {
$title = Constants::SOFTWARE;
}
return ($prefix ?? '') . $title . $delimiter . $this->translator->trans('time_tracking', [], 'messages');
}
/**
* @param string $name
* @return mixed
* @deprecated since 1.15
*/
public function getThemeConfig(string $name)
{
@trigger_error('The twig function "theme_config" was deprecated with 1.15, replace it with the global "kimai_config" variable.', E_USER_DEPRECATED);
switch ($name) {
case 'auto_reload_datatable':
@trigger_error('The configuration auto_reload_datatable is deprecated and was removed with 1.4', E_USER_DEPRECATED);
return false;
case 'soft_limit':
return $this->configuration->getTimesheetActiveEntriesHardLimit();
default:
$name = 'theme.' . $name;
break;
}
return $this->configuration->find($name);
}
}

View File

@@ -28,8 +28,10 @@ class RuntimeExtensions extends AbstractExtension
return [
new TwigFunction('trigger', [ThemeExtension::class, 'trigger'], ['needs_environment' => true]),
new TwigFunction('actions', [ThemeExtension::class, 'actions']),
new TwigFunction('get_title', [ThemeExtension::class, 'generateTitle']),
new TwigFunction('progressbar_color', [ThemeExtension::class, 'getProgressbarClass']),
new TwigFunction('javascript_translations', [ThemeExtension::class, 'getJavascriptTranslations']),
new TwigFunction('theme_config', [ThemeExtension::class, 'getThemeConfig']),
new TwigFunction('active_timesheets', [TimesheetExtension::class, 'activeEntries']),
new TwigFunction('encore_entry_css_source', [EncoreExtension::class, 'getEncoreEntryCssSource']),
new TwigFunction('render_widget', [WidgetExtension::class, 'renderWidget'], ['is_safe' => ['html']]),

View File

@@ -1,50 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Twig;
use App\Configuration\ThemeConfiguration;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class TitleExtension extends AbstractExtension
{
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var ThemeConfiguration
*/
protected $configuration;
public function __construct(TranslatorInterface $translator, ThemeConfiguration $configuration)
{
$this->translator = $translator;
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new TwigFunction('get_title', [$this, 'generateTitle']),
];
}
public function generateTitle(?string $prefix = null, string $delimiter = ' '): string
{
$title = $this->configuration->getTitle() ?? 'Kimai';
return ($prefix ?? '') . ($title) . $delimiter . $this->translator->trans('time_tracking', [], 'messages');
}
}