1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys UserBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\UserBundle\Controller
; 
  14 use Psr\Log\LoggerInterface
; 
  15 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController 
as BaseAbstractController
; 
  16 use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait
; 
  17 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  18 use Symfony\Component\HttpFoundation\RequestStack
; 
  19 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  20 use Symfony\Component\Routing\RouterInterface
; 
  21 use Symfony\Component\Translation\TranslatorInterface
; 
  22 use Symfony\Contracts\Service\ServiceSubscriberInterface
; 
  24 use Rapsys\UserBundle\RapsysUserBundle
; 
  27  * Provides common features needed in controllers. 
  31 abstract class AbstractController 
extends BaseAbstractController 
implements ServiceSubscriberInterface 
{ 
  33                 //Rename render as baseRender 
  34                 render 
as protected baseRender
; 
  40         ///ContainerInterface instance 
  49         ///Translator instance 
  50         protected $translator; 
  58          * Stores container, router and translator interfaces 
  60          * Prepares context tree 
  62          * @param ContainerInterface $container The container instance 
  64         public function __construct(ContainerInterface 
$container) { 
  66                 $this->config 
= $container->getParameter(RapsysUserBundle
::getAlias()); 
  69                 $this->container 
= $container; 
  72                 $this->router 
= $container->get('router'); 
  75                 $this->translator 
= $container->get('translator'); 
  78                 $stack = $this->container
->get('request_stack'); 
  81                 $request = $stack->getCurrentRequest(); 
  84                 $this->locale 
= $request->getLocale(); 
  87                 $this->config
['context']['locale'] = str_replace('_', '-', $this->locale
); 
  92                 //Look for keys to translate 
  93                 if (!empty($this->config
['translate'])) { 
  94                         //Iterate on keys to translate 
  95                         foreach($this->config
['translate'] as $translate) { 
  99                                 foreach(array_reverse(explode('.', $translate)) as $curkey) { 
 100                                         $tmp = array_combine([$curkey], [$tmp]); 
 103                                 $translates = array_replace_recursive($translates, $tmp); 
 107                 //Inject every requested route in view and mail context 
 108                 foreach($this->config 
as $tag => $current) { 
 109                         //Look for entry with title subkey 
 110                         if (!empty($current['title'])) { 
 111                                 //Translate title value 
 112                                 $this->config
[$tag]['title'] = $this->translator
->trans($current['title']); 
 115                         //Look for entry with route subkey 
 116                         if (!empty($current['route'])) { 
 117                                 //Generate url for both view and mail 
 118                                 foreach(['view', 'mail'] as $view) { 
 119                                         //Check that context key is usable 
 120                                         if (isset($current[$view]['context']) && is_array($current[$view]['context'])) { 
 121                                                 //Merge with global context 
 122                                                 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
['context'], $this->config
[$tag][$view]['context']); 
 124                                                 //Process every routes 
 125                                                 foreach($current['route'] as $route => $key) { 
 127                                                         if ($route == 'confirm') { 
 128                                                                 //Skip route as it requires some parameters 
 133                                                         $value = $this->router
->generate( 
 134                                                                 $this->config
['route'][$route]['name'], 
 135                                                                 $this->config
['route'][$route]['context'], 
 136                                                                 //Generate absolute url for mails 
 137                                                                 $view=='mail'?UrlGeneratorInterface
::ABSOLUTE_URL
:UrlGeneratorInterface
::ABSOLUTE_PATH
 
 141                                                         if (strpos($key, '.') !== false) { 
 146                                                                 foreach(array_reverse(explode('.', $key)) as $curkey) { 
 147                                                                         $tmp = array_combine([$curkey], [$tmp]); 
 151                                                                 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp); 
 155                                                                 $this->config
[$tag][$view]['context'][$key] = $value; 
 159                                                 //Look for successful intersections 
 160                                                 if (!empty(array_intersect_key($translates, $this->config
[$tag][$view]['context']))) { 
 161                                                         //Iterate on keys to translate 
 162                                                         foreach($this->config
['translate'] as $translate) { 
 164                                                                 $keys = explode('.', $translate); 
 167                                                                 $tmp = $this->config
[$tag][$view]['context']; 
 170                                                                 foreach($keys as $curkey) { 
 172                                                                         if (!isset($tmp[$curkey])) { 
 178                                                                         $tmp = $tmp[$curkey]; 
 181                                                                 //Translate tmp value 
 182                                                                 $tmp = $this->translator
->trans($tmp); 
 185                                                                 foreach(array_reverse($keys) as $curkey) { 
 187                                                                         $tmp = array_combine([$curkey], [$tmp]); 
 191                                                                 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp); 
 196                                                 if ($view == 'view') { 
 198                                                         $pathInfo = $this->router
->getContext()->getPathInfo(); 
 200                                                         //Iterate on locales excluding current one 
 201                                                         foreach($this->config
['locales'] as $locale) { 
 205                                                                 //Iterate on other locales 
 206                                                                 foreach(array_diff($this->config
['locales'], [$locale]) as $other) { 
 207                                                                         $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other); 
 210                                                                 //Retrieve route matching path 
 211                                                                 $route = $this->router
->match($pathInfo); 
 214                                                                 $name = $route['_route']; 
 217                                                                 unset($route['_route']); 
 219                                                                 //With current locale 
 220                                                                 if ($locale == $this->locale
) { 
 221                                                                         //Set locale locales context 
 222                                                                         $this->config
[$tag][$view]['context']['canonical'] = $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
); 
 224                                                                         //Set locale locales context 
 225                                                                         $this->config
[$tag][$view]['context']['alternates'][$locale] = [ 
 226                                                                                 'absolute' => $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
), 
 227                                                                                 'relative' => $this->router
->generate($name, ['_locale' => $locale]+
$route), 
 228                                                                                 'title' => implode('/', $titles), 
 229                                                                                 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale) 
 234                                                                 if (empty($this->config
[$tag][$view]['context']['alternates'][$slocale = substr($locale, 0, 2)])) { 
 236                                                                         $this->config
[$tag][$view]['context']['alternates'][$slocale] = [ 
 237                                                                                 'absolute' => $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
), 
 238                                                                                 'relative' => $this->router
->generate($name, ['_locale' => $locale]+
$route), 
 239                                                                                 'title' => implode('/', $titles), 
 240                                                                                 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale) 
 254          * @see vendor/symfony/framework-bundle/Controller/AbstractController.php 
 256         public static function getSubscribedServices(): array { 
 257                 //Return subscribed services 
 259                         'logger' => LoggerInterface
::class, 
 260                         'request_stack' => RequestStack
::class, 
 261                         'router' => RouterInterface
::class, 
 262                         'translator' => TranslatorInterface
::class