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; use Symfony\Component\Translation\Translator;
/** /**
* *
* This helper helps to find the string in current locale from translatable_strings * This helper helps to find the string in current locale from translatable_strings
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
* *
*/ */
class TranslatableStringHelper class TranslatableStringHelper implements TranslatableStringHelperInterface
{ {
/** /**
* *
* @var RequestStack * @var RequestStack
*/ */
private $requestStack; private $requestStack;
private $fallbackLocales; private $fallbackLocales;
public function __construct(RequestStack $requestStack, Translator $translator) public function __construct(RequestStack $requestStack, Translator $translator)
{ {
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
$this->fallbackLocales = $translator->getFallbackLocales(); $this->fallbackLocales = $translator->getFallbackLocales();
} }
/** /**
* return the string in current locale if it exists. * return the string in current locale if it exists.
* *
* If it does not exists; return the name in the first language available. * If it does not exists; return the name in the first language available.
* *
* Return a blank string if any strings are available. * Return a blank string if any strings are available.
* Return NULL if $translatableString is NULL * Return NULL if $translatableString is NULL
* *
* @param array $translatableStrings * @param array $translatableStrings
* @return string * @return string
*/ */
@ -61,32 +61,32 @@ class TranslatableStringHelper
if (NULL === $translatableStrings) { if (NULL === $translatableStrings) {
return NULL; return NULL;
} }
$language = $this->requestStack->getCurrentRequest()->getLocale(); $language = $this->requestStack->getCurrentRequest()->getLocale();
if (isset($translatableStrings[$language])) { if (isset($translatableStrings[$language])) {
return $translatableStrings[$language]; return $translatableStrings[$language];
} else { } else {
foreach ($this->fallbackLocales as $locale) { foreach ($this->fallbackLocales as $locale) {
if (array_key_exists($locale, $translatableStrings)) { if (array_key_exists($locale, $translatableStrings)) {
return $translatableStrings[$locale]; return $translatableStrings[$locale];
} }
} }
} }
// no fallback translation... trying the first available // no fallback translation... trying the first available
$langs = array_keys($translatableStrings); $langs = array_keys($translatableStrings);
if (count($langs) === 0) { if (count($langs) === 0) {
return ''; return '';
} }
return $translatableStrings[$langs[0]]; 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" - "@translator.default"
Chill\MainBundle\Templating\TranslatableStringHelper: '@chill.main.helper.translatable_string' Chill\MainBundle\Templating\TranslatableStringHelper: '@chill.main.helper.translatable_string'
Chill\MainBundle\Templating\TranslatableStringHelperInterface: Chill\MainBundle\Templating\TranslatableStringHelper
chill.main.twig.translatable_string: chill.main.twig.translatable_string:
class: Chill\MainBundle\Templating\TranslatableStringTwig class: Chill\MainBundle\Templating\TranslatableStringTwig
@ -19,14 +20,14 @@ services:
- [ setContainer, ["@service_container"]] - [ setContainer, ["@service_container"]]
tags: tags:
- { name: twig.extension } - { name: twig.extension }
chill.main.twig.widget: chill.main.twig.widget:
class: Chill\MainBundle\Templating\Widget\WidgetRenderingTwig class: Chill\MainBundle\Templating\Widget\WidgetRenderingTwig
arguments: arguments:
- "@event_dispatcher" - "@event_dispatcher"
tags: tags:
- { name: twig.extension } - { name: twig.extension }
chill.main.twig.csv_cell: chill.main.twig.csv_cell:
class: Chill\MainBundle\Templating\CSVCellTwig class: Chill\MainBundle\Templating\CSVCellTwig
tags: tags:

View File

@ -46,6 +46,11 @@ services:
tags: tags:
- { name: kernel.event_subscriber } - { name: kernel.event_subscriber }
Chill\PersonBundle\Templating\Entity\:
resource: '../src/Templating/Entity/'
tags:
- { name: chill.render_entity }
chill.person.security.authorization.person: chill.person.security.authorization.person:
class: Chill\PersonBundle\Security\Authorization\PersonVoter class: Chill\PersonBundle\Security\Authorization\PersonVoter
arguments: 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 * Give help to interact with the config for alt names
* *
* *
*/ */
class ConfigPersonAltNamesHelper class ConfigPersonAltNamesHelper implements ConfigPersonAltNamesHelperInterface
{ {
/** /**
* the raw config, directly from the container parameter * the raw config, directly from the container parameter
@ -16,32 +16,32 @@ class ConfigPersonAltNamesHelper
* @var array * @var array
*/ */
private $config = []; private $config = [];
public function __construct($config) public function __construct($config)
{ {
$this->config = $config; $this->config = $config;
} }
/** /**
* Return true if at least one alt name is configured * Return true if at least one alt name is configured
* *
* @return bool * @return bool
*/ */
public function hasAltNames(): bool public function hasAltNames(): bool
{ {
return count($this->config) > 0; return count($this->config) > 0;
} }
/** /**
* get the choices as key => values * get the choices as key => values
* *
* @return array * @return array
*/ */
public function getChoices(): array public function getChoices(): array
{ {
$choices = []; $choices = [];
foreach ($this->config as $entry) { foreach ($this->config as $entry) {
$labels = $entry['labels']; $labels = $entry['labels'];
$lang = false; $lang = false;
$label = false; $label = false;
@ -50,11 +50,11 @@ class ConfigPersonAltNamesHelper
if (key($labels) === 'lang') { if (key($labels) === 'lang') {
$lang = current($labels); $lang = current($labels);
} }
if (key($labels) === 'label') { if (key($labels) === 'label') {
$label = current($labels); $label = current($labels);
} }
if ($lang !== FALSE && $label !== FALSE) { if ($lang !== FALSE && $label !== FALSE) {
$choices[$entry['key']][$lang] = $label; $choices[$entry['key']][$lang] = $label;
$lang = false; $lang = false;
@ -63,7 +63,7 @@ class ConfigPersonAltNamesHelper
$cur = next($labels); $cur = next($labels);
} }
} }
return $choices; 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/search.yaml');
$loader->load('services/command.yaml'); $loader->load('services/command.yaml');
$loader->load('services/form.yaml'); $loader->load('services/form.yaml');
$loader->load('services/templating.yaml');
$loader->load('services/alt_names.yaml'); $loader->load('services/alt_names.yaml');
$loader->load('services/serializer.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\MainBundle\Templating\Entity\AbstractChillEntityRender;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
/** /**
* Render closing motive * Render closing motive
@ -13,21 +13,20 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
class ClosingMotiveRender extends AbstractChillEntityRender class ClosingMotiveRender extends AbstractChillEntityRender
{ {
private CONST SEPARATOR = ' > '; private CONST SEPARATOR = ' > ';
/** /**
* * @var TranslatableStringHelperInterface
* @var TranslatableStringHelper
*/ */
private $translatableStringHelper; private $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper) public function __construct(TranslatableStringHelperInterface $translatableStringHelper)
{ {
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
public function renderBox($entity, array $options): string public function renderBox($entity, array $options): string
{ {
return return
$this->getDefaultOpeningBox('closing-motive'). $this->getDefaultOpeningBox('closing-motive').
$this->renderString($entity, $options). $this->renderString($entity, $options).
$this->getDefaultClosingBox() $this->getDefaultClosingBox()
@ -35,28 +34,28 @@ class ClosingMotiveRender extends AbstractChillEntityRender
} }
/** /**
* *
* @param ClosingMotive $entity * @param ClosingMotive $entity
* @param array $options * @param array $options
* @return string * @return string
*/ */
public function renderString($entity, array $options): string public function renderString($entity, array $options): string
{ {
return $this->renderStringRecursive($entity, return $this->renderStringRecursive($entity,
'', //$this->translatableStringHelper->localize($entity->getName()), '', //$this->translatableStringHelper->localize($entity->getName()),
$options); $options);
} }
protected function renderStringRecursive(ClosingMotive $motive, $existing, array $options) protected function renderStringRecursive(ClosingMotive $motive, $existing, array $options)
{ {
$newExisting = $this->translatableStringHelper->localize($motive->getName()); $newExisting = $this->translatableStringHelper->localize($motive->getName());
if ($motive->hasParent()) { if ($motive->hasParent()) {
if (!empty($existing)) { if (!empty($existing)) {
$newExisting = $newExisting.self::SEPARATOR.$existing; $newExisting = $newExisting.self::SEPARATOR.$existing;
} }
return $this->renderStringRecursive($motive->getParent(), $newExisting, return $this->renderStringRecursive($motive->getParent(), $newExisting,
$options); $options);
} else { } else {

View File

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