Remove templating.yaml, add interfaces.

This commit is contained in:
Pol Dellaiera 2021-05-06 21:23:19 +02:00
parent 5146419e9a
commit c019da9bcf
10 changed files with 105 additions and 74 deletions

View File

@ -23,36 +23,36 @@ use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\Translator;
/**
*
*
* This helper helps to find the string in current locale from translatable_strings
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class TranslatableStringHelper
class TranslatableStringHelper implements TranslatableStringHelperInterface
{
/**
*
*
* @var RequestStack
*/
private $requestStack;
private $fallbackLocales;
public function __construct(RequestStack $requestStack, Translator $translator)
{
$this->requestStack = $requestStack;
$this->fallbackLocales = $translator->getFallbackLocales();
}
/**
* return the string in current locale if it exists.
*
*
* If it does not exists; return the name in the first language available.
*
*
* Return a blank string if any strings are available.
* Return NULL if $translatableString is NULL
*
*
* @param array $translatableStrings
* @return string
*/
@ -61,32 +61,32 @@ class TranslatableStringHelper
if (NULL === $translatableStrings) {
return NULL;
}
$language = $this->requestStack->getCurrentRequest()->getLocale();
if (isset($translatableStrings[$language])) {
return $translatableStrings[$language];
} else {
foreach ($this->fallbackLocales as $locale) {
if (array_key_exists($locale, $translatableStrings)) {
return $translatableStrings[$locale];
}
}
}
// no fallback translation... trying the first available
$langs = array_keys($translatableStrings);
if (count($langs) === 0) {
return '';
}
return $translatableStrings[$langs[0]];
}
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Chill\MainBundle\Templating;
interface TranslatableStringHelperInterface
{
/**
* return the string in current locale if it exists.
*
* If it does not exists; return the name in the first language available.
*
* Return a blank string if any strings are available.
* Return NULL if $translatableString is NULL
*
* @param array $translatableStrings
* @return string
*/
public function localize(array $translatableStrings);
}

View File

@ -10,6 +10,7 @@ services:
- "@translator.default"
Chill\MainBundle\Templating\TranslatableStringHelper: '@chill.main.helper.translatable_string'
Chill\MainBundle\Templating\TranslatableStringHelperInterface: Chill\MainBundle\Templating\TranslatableStringHelper
chill.main.twig.translatable_string:
class: Chill\MainBundle\Templating\TranslatableStringTwig
@ -19,14 +20,14 @@ services:
- [ setContainer, ["@service_container"]]
tags:
- { name: twig.extension }
chill.main.twig.widget:
class: Chill\MainBundle\Templating\Widget\WidgetRenderingTwig
arguments:
- "@event_dispatcher"
tags:
- { name: twig.extension }
chill.main.twig.csv_cell:
class: Chill\MainBundle\Templating\CSVCellTwig
tags:

View File

@ -46,6 +46,11 @@ services:
tags:
- { name: kernel.event_subscriber }
Chill\PersonBundle\Templating\Entity\:
resource: '../src/Templating/Entity/'
tags:
- { name: chill.render_entity }
chill.person.security.authorization.person:
class: Chill\PersonBundle\Security\Authorization\PersonVoter
arguments:

View File

@ -1,12 +0,0 @@
services:
Chill\PersonBundle\Templating\Entity\PersonRender:
arguments:
$configAltNamesHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper'
tags:
- 'chill.render_entity'
Chill\PersonBundle\Templating\Entity\ClosingMotiveRender:
arguments:
$translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper'
tags:
- 'chill.render_entity'

View File

@ -5,10 +5,10 @@ namespace Chill\PersonBundle\Config;
/**
* Give help to interact with the config for alt names
*
*
*
*/
class ConfigPersonAltNamesHelper
class ConfigPersonAltNamesHelper implements ConfigPersonAltNamesHelperInterface
{
/**
* the raw config, directly from the container parameter
@ -16,32 +16,32 @@ class ConfigPersonAltNamesHelper
* @var array
*/
private $config = [];
public function __construct($config)
{
$this->config = $config;
}
/**
* Return true if at least one alt name is configured
*
*
* @return bool
*/
public function hasAltNames(): bool
{
return count($this->config) > 0;
}
/**
* get the choices as key => values
*
*
* @return array
*/
public function getChoices(): array
{
$choices = [];
foreach ($this->config as $entry) {
$labels = $entry['labels'];
$lang = false;
$label = false;
@ -50,11 +50,11 @@ class ConfigPersonAltNamesHelper
if (key($labels) === 'lang') {
$lang = current($labels);
}
if (key($labels) === 'label') {
$label = current($labels);
}
if ($lang !== FALSE && $label !== FALSE) {
$choices[$entry['key']][$lang] = $label;
$lang = false;
@ -63,7 +63,7 @@ class ConfigPersonAltNamesHelper
$cur = next($labels);
}
}
return $choices;
}

View File

@ -0,0 +1,20 @@
<?php
namespace Chill\PersonBundle\Config;
interface ConfigPersonAltNamesHelperInterface
{
/**
* Return true if at least one alt name is configured
*
* @return bool
*/
public function hasAltNames(): bool;
/**
* get the choices as key => values
*
* @return array
*/
public function getChoices(): array;
}

View File

@ -68,7 +68,6 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
$loader->load('services/search.yaml');
$loader->load('services/command.yaml');
$loader->load('services/form.yaml');
$loader->load('services/templating.yaml');
$loader->load('services/alt_names.yaml');
$loader->load('services/serializer.yaml');

View File

@ -4,7 +4,7 @@ namespace Chill\PersonBundle\Templating\Entity;
use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
/**
* Render closing motive
@ -13,21 +13,20 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
class ClosingMotiveRender extends AbstractChillEntityRender
{
private CONST SEPARATOR = ' > ';
/**
*
* @var TranslatableStringHelper
* @var TranslatableStringHelperInterface
*/
private $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
public function __construct(TranslatableStringHelperInterface $translatableStringHelper)
{
$this->translatableStringHelper = $translatableStringHelper;
}
public function renderBox($entity, array $options): string
{
return
return
$this->getDefaultOpeningBox('closing-motive').
$this->renderString($entity, $options).
$this->getDefaultClosingBox()
@ -35,28 +34,28 @@ class ClosingMotiveRender extends AbstractChillEntityRender
}
/**
*
*
* @param ClosingMotive $entity
* @param array $options
* @return string
*/
public function renderString($entity, array $options): string
{
return $this->renderStringRecursive($entity,
'', //$this->translatableStringHelper->localize($entity->getName()),
return $this->renderStringRecursive($entity,
'', //$this->translatableStringHelper->localize($entity->getName()),
$options);
}
protected function renderStringRecursive(ClosingMotive $motive, $existing, array $options)
{
$newExisting = $this->translatableStringHelper->localize($motive->getName());
if ($motive->hasParent()) {
if (!empty($existing)) {
$newExisting = $newExisting.self::SEPARATOR.$existing;
}
return $this->renderStringRecursive($motive->getParent(), $newExisting,
$options);
} else {

View File

@ -2,7 +2,7 @@
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
@ -22,7 +22,7 @@ namespace Chill\PersonBundle\Templating\Entity;
use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelperInterface;
/**
* Render a Person
@ -32,24 +32,24 @@ class PersonRender extends AbstractChillEntityRender
{
/**
*
* @var ConfigPersonAltNamesHelper
* @var ConfigPersonAltNamesHelperInterface
*/
protected $configAltNamesHelper;
public function __construct(ConfigPersonAltNamesHelper $configAltNamesHelper)
public function __construct(ConfigPersonAltNamesHelperInterface $configAltNamesHelper)
{
$this->configAltNamesHelper = $configAltNamesHelper;
}
/**
*
*
* @param Person $person
* @param array $options
* @return string
*/
public function renderBox($person, array $options): string
{
return
return
$this->getDefaultOpeningBox('person').
'<span class="chill-entity__person__first-name">'.$person->getFirstName().'</span>'.
' <span class="chill-entity__person__last-name">'.$person->getLastName().'</span>'.
@ -59,7 +59,7 @@ class PersonRender extends AbstractChillEntityRender
}
/**
*
*
* @param Person $person
* @param array $options
* @return string
@ -69,11 +69,11 @@ class PersonRender extends AbstractChillEntityRender
return $person->getFirstName().' '.$person->getLastName()
.$this->addAltNames($person, false);
}
protected function addAltNames(Person $person, bool $addSpan)
{
$str = '';
if ($this->configAltNamesHelper->hasAltNames()) {
$altNames = $this->configAltNamesHelper->getChoices();
$isFirst = true;
@ -91,18 +91,18 @@ class PersonRender extends AbstractChillEntityRender
$str .= '<span class="chill-entity__person__alt-name chill-entity__person__altname--'.$altName->getKey().'">';
}
$str .= $altName->getLabel();
if ($addSpan) {
$str .= "</span>";
}
}
}
if (!$isFirst) {
$str .= ")";
}
}
return $str;
}