1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Rik van der Kemp <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
namespace Zicht\Bundle\UrlBundle\Url; |
7
|
|
|
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
11
|
|
|
use Symfony\Component\Routing\RouterInterface; |
12
|
|
|
use Zicht\Bundle\UrlBundle\Entity\StaticReference; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Static provider holds a set of urls |
16
|
|
|
*/ |
17
|
|
|
class DbStaticProvider implements Provider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityManager |
21
|
|
|
*/ |
22
|
|
|
private $em; |
23
|
|
|
|
24
|
|
|
/* |
|
|
|
|
25
|
|
|
* @var null|RequestStack $requestStack |
26
|
|
|
*/ |
27
|
|
|
private $requestStack; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Holds the static references |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $refs = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The locale to use when all things fail |
38
|
|
|
* |
39
|
|
|
* @var null|string |
40
|
|
|
*/ |
41
|
|
|
private $fallback_locale = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create the provider with a set of static references, i.e. mappings from name to url. |
45
|
|
|
* |
46
|
|
|
* @param EntityManager $em |
47
|
|
|
* @param RequestStack|null $requestStack |
48
|
|
|
*/ |
49
|
|
|
public function __construct(EntityManager $em, RequestStack $requestStack = null) |
50
|
|
|
{ |
51
|
|
|
$this->em = $em; |
52
|
|
|
$this->requestStack = $requestStack; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add the array as references |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function addAll() |
61
|
|
|
{ |
62
|
|
|
// Make sure refs are not null any more, else it keeps checking on every static ref |
63
|
|
|
$this->refs = []; |
64
|
|
|
|
65
|
|
|
/** @var StaticReference $repos */ |
66
|
|
|
$references = $this->em->getRepository('ZichtUrlBundle:StaticReference')->getAll($this->getLocale()); |
67
|
|
|
|
68
|
|
|
foreach ($references as $static_reference) { |
69
|
|
|
foreach ($static_reference->getTranslations() as $translation) { |
70
|
|
|
$this->refs[$static_reference->getMachineName()][$translation->getLocale()] = $translation->getUrl(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @{inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function supports($object) |
79
|
|
|
{ |
80
|
|
|
$this->checkRefsAreLoaded(); |
81
|
|
|
|
82
|
|
|
return is_string($object) && isset($this->refs[$object][$this->getLocale()]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @{inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function url($object, array $options = []) |
89
|
|
|
{ |
90
|
|
|
$this->checkRefsAreLoaded(); |
91
|
|
|
|
92
|
|
|
$url = $this->refs[$object][$this->getLocale()]; |
93
|
|
|
|
94
|
|
|
if (!preg_match('/^(http|https)/', $url) && (null !== ($request = $this->getMasterRequest()))) { |
95
|
|
|
$url = $request->getBaseUrl() . '/' . ltrim($url, '/'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $url; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the locale parameter for the current request, if any. |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function getLocale() |
108
|
|
|
{ |
109
|
|
|
if (null !== ($request = $this->getMasterRequest())) { |
110
|
|
|
$locale = $request->get('_locale'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!isset($locale)) { |
114
|
|
|
$locale = $this->fallback_locale; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $locale; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Load all static references from the database |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
private function checkRefsAreLoaded() |
126
|
|
|
{ |
127
|
|
|
if (is_null($this->refs)) { |
|
|
|
|
128
|
|
|
$this->addAll(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return null|Request |
134
|
|
|
*/ |
135
|
|
|
private function getMasterRequest() |
136
|
|
|
{ |
137
|
|
|
return (!is_null($this->requestStack)) ? $this->requestStack->getMasterRequest() : null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Fallback locale to use whenever the reference for a specific locale is not set. |
142
|
|
|
* |
143
|
|
|
* @param string $locale |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function setFallbackLocale($locale) |
147
|
|
|
{ |
148
|
|
|
$this->fallback_locale = $locale; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.