|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the I18n Routing Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BenatEspina\I18nRoutingBundle\Twig; |
|
13
|
|
|
|
|
14
|
|
|
use BenatEspina\I18nRoutingBundle\Resolver\NotFoundLocaleResolver; |
|
15
|
|
|
use BenatEspina\I18nRoutingBundle\Resolver\ParametersResolverDoesNotExist; |
|
16
|
|
|
use BenatEspina\I18nRoutingBundle\Resolver\ParametersResolverRegistry; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
18
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Beñat Espiña <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class CurrentPathTranslationExtension extends \Twig_Extension |
|
24
|
|
|
{ |
|
25
|
|
|
private $requestStack; |
|
26
|
|
|
private $urlGenerator; |
|
27
|
|
|
private $parametersResolverRegistry; |
|
28
|
|
|
private $notFoundLocaleResolver; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
RequestStack $requestStack, |
|
32
|
|
|
UrlGeneratorInterface $urlGenerator, |
|
33
|
|
|
ParametersResolverRegistry $parametersResolverRegistry, |
|
34
|
|
|
NotFoundLocaleResolver $notFoundLocaleResolver |
|
35
|
|
|
) { |
|
36
|
|
|
$this->requestStack = $requestStack; |
|
37
|
|
|
$this->urlGenerator = $urlGenerator; |
|
38
|
|
|
$this->parametersResolverRegistry = $parametersResolverRegistry; |
|
39
|
|
|
$this->notFoundLocaleResolver = $notFoundLocaleResolver; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getFunctions() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
new \Twig_SimpleFunction('current_path_translation', [$this, 'currentPathTranslation']), |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function currentPathTranslation($newLocale, $parametersResolverAlias = null, $relative = false) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$request = $this->requestStack->getMasterRequest(); |
|
52
|
|
|
|
|
53
|
|
|
$locale = $request->getLocale(); |
|
54
|
|
|
$name = $request->get('_route'); |
|
55
|
|
|
$parameters = $request->get('_route_params'); |
|
56
|
|
|
$parameters['_locale'] = $newLocale; |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
$this->parametersResolver($parametersResolverAlias)->resolve($locale, $newLocale, $parameters); |
|
60
|
|
|
|
|
61
|
|
|
$url = $this->urlGenerator->generate( |
|
62
|
|
|
$name, |
|
63
|
|
|
$parameters, |
|
64
|
|
|
$relative |
|
65
|
|
|
? UrlGeneratorInterface::RELATIVE_PATH |
|
66
|
|
|
: UrlGeneratorInterface::ABSOLUTE_PATH |
|
67
|
|
|
); |
|
68
|
|
|
} catch (\Exception $exception) { |
|
69
|
|
|
$url = $this->notFoundLocaleResolver->generateUrl($request, $locale, $newLocale); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $url; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function parametersResolver($alias = null) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
if (null === $alias) { |
|
78
|
|
|
return $this->parametersResolverRegistry->getDefault(); |
|
79
|
|
|
} |
|
80
|
|
|
if (!$this->parametersResolverRegistry->has($alias)) { |
|
81
|
|
|
throw new ParametersResolverDoesNotExist($alias); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->parametersResolverRegistry->get($alias); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.