Skip to content

Commit

Permalink
refactor config array to public Config service
Browse files Browse the repository at this point in the history
  • Loading branch information
gebi84 committed Jul 11, 2023
1 parent 51bbcc2 commit 83a628b
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/Api/DeeplApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Guave\DeeplBundle\Api;

use Guave\DeeplBundle\Config\Config;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -14,10 +15,10 @@ class DeeplApi
{
protected string $apiKey;

public function __construct(string $deeplApiKey, bool $freeApi = false)
public function __construct(string $deeplApiKey, Config $config)
{
$url = 'https://api.deepl.com';
if ($freeApi) {
if ($config->isFreeApi()) {
$url = 'https://api-free.deepl.com';
}

Expand Down
79 changes: 79 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Guave\DeeplBundle\Config;

class Config
{
private bool $enabled = false;

private bool $freeApi = false;

private string $defaultLanguage = 'de';

private array $tables = [];

public function __construct(array $config)
{
if (isset($config['enabled'])) {
$this->setEnabled($config['enabled']);
}
if (isset($config['freeApi'])) {
$this->setFreeApi($config['freeApi']);
}
if (isset($config['defaultLanguage'])) {
$this->setDefaultLanguage($config['defaultLanguage']);
}
if (isset($config['tables'])) {
$this->setTables($config['tables']);
}
}

public function isEnabled(): bool
{
return $this->enabled;
}

public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}

public function isFreeApi(): bool
{
return $this->freeApi;
}

public function setFreeApi(bool $freeApi): void
{
$this->freeApi = $freeApi;
}

public function getDefaultLanguage(): string
{
return $this->defaultLanguage;
}

public function setDefaultLanguage(string $defaultLanguage): void
{
$this->defaultLanguage = $defaultLanguage;
}

public function getTables(): array
{
return $this->tables;
}

public function setTables(array $tables): void
{
$this->tables = $tables;
}

public function getDeeplConfigByTable(string $table): array
{
$tables = $this->getTables();

return $tables[$table] ?? [];
}
}
28 changes: 13 additions & 15 deletions src/Controller/Backend/DeeplButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@
use Contao\Controller;
use Contao\DataContainer;
use Contao\Image;
use Guave\DeeplBundle\Config\Config;
use Guave\DeeplBundle\Resolver\ActiveLanguageResolverInterface;

class DeeplButtons extends Backend
{
protected string $defaultLanguage;

protected array $tables;
protected iterable $activeLanguageResolver;

protected string $activeLang;

protected iterable $activeLanguageResolver;
protected Config $config;

public function __construct(string $defaultLanguage, array $tables, iterable $activeLanguageResolver)
public function __construct(Config $config, iterable $activeLanguageResolver)
{
$this->defaultLanguage = $defaultLanguage;
$this->tables = $tables;
$this->activeLanguageResolver = $activeLanguageResolver;
$this->config = $config;

parent::__construct();
}
Expand All @@ -36,19 +34,19 @@ public function registerDeepl(DataContainer $dc)
}

$activeLang = $this->getActiveLang($dc);
if ($activeLang === $this->defaultLanguage) {
if ($activeLang === $this->config->getDefaultLanguage()) {
return;
}

Controller::loadLanguageFile('modules');

$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/guavedeepl/assets/translate.js';

foreach ($this->tables[$dc->table]['fields'] as $field) {
foreach ($this->config->getTables()[$dc->table]['fields'] as $field) {
$GLOBALS['TL_DCA'][$dc->table]['fields'][$field]['xlabel'][] = [self::class, 'translateButton'];
}

foreach ($this->tables[$dc->table]['multiColumnFields'] as $field => $fields) {
foreach ($this->config->getTables()[$dc->table]['multiColumnFields'] as $field => $fields) {
$GLOBALS['TL_DCA'][$dc->table]['fields'][$field]['xlabel'][] = [self::class, 'translateMultiColumnButton'];
}

Expand All @@ -70,7 +68,7 @@ public function translateMultiColumnButton(DataContainer $dc)
{
$field = $dc->field;

return $this->getMulticolumnTranslateButton($field, $this->tables[$dc->table]['multiColumnFields'][$field]['fields']);
return $this->getMulticolumnTranslateButton($field, $this->config->getTables()[$dc->table]['multiColumnFields'][$field]['fields']);
}

public function addTranslateAllButton($arrButtons)
Expand All @@ -80,7 +78,7 @@ public function addTranslateAllButton($arrButtons)
$this->activeLang,
sprintf(
$GLOBALS['TL_LANG']['guave_deepl']['translateAll'][0],
$this->defaultLanguage,
$this->config->getDefaultLanguage(),
$this->activeLang,
Image::getHtml('pasteinto.svg')
)
Expand All @@ -97,7 +95,7 @@ public function getTranslateButton(string $field): string
$this->activeLang,
sprintf(
$GLOBALS['TL_LANG']['guave_deepl']['translate'][0],
$this->defaultLanguage,
$this->config->getDefaultLanguage(),
$this->activeLang,
Image::getHtml('pasteinto.svg')
)
Expand All @@ -113,7 +111,7 @@ public function getMulticolumnTranslateButton(string $field, array $fields): str
$this->activeLang,
sprintf(
$GLOBALS['TL_LANG']['guave_deepl']['translate'][0],
$this->defaultLanguage,
$this->config->getDefaultLanguage(),
$this->activeLang,
Image::getHtml('pasteinto.svg')
)
Expand All @@ -134,7 +132,7 @@ protected function getActiveLang(DataContainer $dc): string
}

if (!$language) {
$language = $this->defaultLanguage;
$language = $this->config->getDefaultLanguage();
}

$this->activeLang = $language;
Expand Down
17 changes: 3 additions & 14 deletions src/DependencyInjection/GuaveDeeplExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Guave\DeeplBundle\DependencyInjection;

use Guave\DeeplBundle\Api\DeeplApi;
use Guave\DeeplBundle\Config\Config;
use Guave\DeeplBundle\Controller\Backend\DeeplButtons;
use Guave\DeeplBundle\EventListener\LoadDataContainerListener;
use Guave\DeeplBundle\EventListener\LoadFallbackTranslationsListener;
Expand All @@ -23,19 +24,7 @@ public function loadInternal(array $mergedConfig, ContainerBuilder $container):
);
$loader->load('services.yml');

$definition = $container->getDefinition(LoadDataContainerListener::class);

$definition->setArgument(0, $mergedConfig['enabled']);
$definition->setArgument(1, $mergedConfig['tables']);

$definition2 = $container->getDefinition(DeeplButtons::class);
$definition2->setArgument(0, $mergedConfig['defaultLanguage']);
$definition2->setArgument(1, $mergedConfig['tables']);

$definition3 = $container->getDefinition(LoadFallbackTranslationsListener::class);
$definition3->setArgument(0, $mergedConfig['defaultLanguage']);

$definition4 = $container->getDefinition(DeeplApi::class);
$definition4->setArgument(1, $mergedConfig['freeApi']);
$configDefinition = $container->getDefinition(Config::class);
$configDefinition->setArgument(0, $mergedConfig);
}
}
13 changes: 6 additions & 7 deletions src/EventListener/LoadDataContainerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@

use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\Input;
use Guave\DeeplBundle\Config\Config;
use Guave\DeeplBundle\Controller\Backend\DeeplButtons;

/**
* @Hook("loadDataContainer")
*/
class LoadDataContainerListener
{
protected bool $enabled = false;
protected array $tables;
protected Config $config;

public function __construct(bool $enabled, array $tables)
public function __construct(Config $config)
{
$this->enabled = $enabled;
$this->tables = $tables;
$this->config = $config;
}

public function __invoke(string $table): void
{
if (!$this->enabled) {
if (!$this->config->isEnabled()) {
return;
}

Expand Down Expand Up @@ -52,7 +51,7 @@ public function __invoke(string $table): void
return;
}

if (array_key_exists($table, $this->tables)) {
if (array_key_exists($table, $this->config->getTables())) {
$GLOBALS['TL_DCA'][$table]['config']['onload_callback'][] = [DeeplButtons::class, 'registerDeepl'];

// register fallback translation
Expand Down
11 changes: 6 additions & 5 deletions src/EventListener/LoadFallbackTranslationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Contao\DataContainer;
use Contao\Model;
use DC_Multilingual;
use Guave\DeeplBundle\Config\Config;
use Guave\DeeplBundle\Model\MultilingualModel;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

Expand All @@ -18,13 +19,13 @@ class LoadFallbackTranslationsListener
{
protected SessionInterface $session;

protected string $defaultLanguage;
protected Config $config;

public function __construct(
string $defaultLanguage,
Config $config,
SessionInterface $session
) {
$this->defaultLanguage = $defaultLanguage;
$this->config = $config;
$this->session = $session;
}

Expand All @@ -40,7 +41,7 @@ public function loadFallbackTranslation(DataContainer $dc)

$id = (int) $dc->id;
$activeLang = $this->getActiveLang($dc->table, $id);
if ($activeLang === $this->defaultLanguage) {
if ($activeLang === $this->config->getDefaultLanguage()) {
return;
}

Expand Down Expand Up @@ -87,7 +88,7 @@ protected function getActiveLang(string $table, int $id): string
$sessionKey = 'dc_multilingual:' . $table . ':' . $id;


return $objSessionBag->get($sessionKey) ?? $this->defaultLanguage;
return $objSessionBag->get($sessionKey) ?? $this->config->getDefaultLanguage();
}

protected function getModel(string $table, int $id): ?Model
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ services:
public: true
tags:
- { name: 'deepl.resolver', priority: 19 }

Guave\DeeplBundle\Config\Config:
public: true

Guave\DeeplBundle\Translate\TranslateDCA:
public: true
Loading

0 comments on commit 83a628b

Please sign in to comment.