|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Bundle\DefaultBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
6
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class LanguageSwitcherBlockService. |
|
15
|
|
|
*/ |
|
16
|
|
|
class LanguageSwitcherBlockService extends AbstractBlockService |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Request */ |
|
19
|
|
|
private $request; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $locales; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ProgramEventBlockService constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param EngineInterface $templating |
|
29
|
|
|
* @param RequestStack $requestStack |
|
30
|
|
|
* @param array $locales |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($name, EngineInterface $templating, RequestStack $requestStack, array $locales) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($name, $templating); |
|
35
|
|
|
|
|
36
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
|
37
|
|
|
$this->locales = $locales; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$position = $blockContext->getSetting('position'); |
|
46
|
|
|
$localesArr = []; |
|
47
|
|
|
foreach ($this->locales as $locale) { |
|
48
|
|
|
$localesArr[$locale] = $this->localizeRoute($this->request, $locale); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->renderResponse($blockContext->getTemplate(), [ |
|
52
|
|
|
'block' => $blockContext->getBlock(), |
|
53
|
|
|
'locales' => $localesArr, |
|
54
|
|
|
'position' => $position, |
|
55
|
|
|
], $response); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function configureSettings(OptionsResolver $resolver) |
|
62
|
|
|
{ |
|
63
|
|
|
$resolver->setDefaults([ |
|
64
|
|
|
'template' => 'ApplicationDefaultBundle:Redesign:language_switcher.html.twig', |
|
65
|
|
|
'position' => 'header', |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get inner sub string in position number. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $string |
|
73
|
|
|
* @param string $delim |
|
74
|
|
|
* @param int $keyNumber |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
private function getInnerSubstring($string, $delim, $keyNumber = 1) |
|
79
|
|
|
{ |
|
80
|
|
|
$string = explode($delim, $string, 3); |
|
81
|
|
|
|
|
82
|
|
|
return isset($string[$keyNumber]) ? $string[$keyNumber] : ''; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Localize current route. |
|
87
|
|
|
* |
|
88
|
|
|
* @param Request $request |
|
89
|
|
|
* @param string $locale |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
private function localizeRoute($request, $locale) |
|
94
|
|
|
{ |
|
95
|
|
|
$path = $request->getPathInfo(); |
|
96
|
|
|
$currentLocal = $this->getInnerSubstring($path, '/'); |
|
97
|
|
|
if (in_array($currentLocal, $this->locales)) { |
|
98
|
|
|
$path = preg_replace('/^\/'.$currentLocal.'\//', '/', $path); |
|
99
|
|
|
} |
|
100
|
|
|
$params = $request->query->all(); |
|
101
|
|
|
|
|
102
|
|
|
return $request->getBaseUrl().'/'.$locale.$path.($params ? '?'.http_build_query($params) : ''); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|