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

@@ -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');
}
}