]> Raphaël G. Git Repositories - userbundle/blob - Controller/AbstractController.php
Add twig service to default controller constructor
[userbundle] / Controller / AbstractController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
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 Rapsys\UserBundle\Controller;
13
14 use Doctrine\ORM\EntityManagerInterface;
15 use Doctrine\Persistence\ManagerRegistry;
16 use Psr\Log\LoggerInterface;
17 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController;
18 use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
19 use Symfony\Component\DependencyInjection\ContainerInterface;
20 use Symfony\Component\HttpFoundation\RequestStack;
21 use Symfony\Component\Mailer\MailerInterface;
22 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23 use Symfony\Component\Routing\RouterInterface;
24 use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
25 use Symfony\Contracts\Translation\TranslatorInterface;
26 use Symfony\Contracts\Service\ServiceSubscriberInterface;
27
28 use Rapsys\PackBundle\Util\SluggerUtil;
29
30 use Rapsys\UserBundle\RapsysUserBundle;
31
32 /**
33 * Provides common features needed in controllers.
34 *
35 * {@inheritdoc}
36 */
37 abstract class AbstractController extends BaseAbstractController implements ServiceSubscriberInterface {
38 ///Config array
39 protected $config;
40
41 ///Context array
42 protected $context;
43
44 ///ManagerRegistry
45 protected $doctrine;
46
47 ///UserPasswordHasherInterface
48 protected $hasher;
49
50 ///LoggerInterface
51 protected $logger;
52
53 ///MailerInterface
54 protected $mailer;
55
56 ///EntityManagerInterface
57 protected $manager;
58
59 ///Router instance
60 protected $router;
61
62 ///Slugger util
63 protected $slugger;
64
65 ///Translator instance
66 protected $translator;
67
68 ///Locale
69 protected $locale;
70
71 /**
72 * Abstract constructor
73 *
74 * @param ContainerInterface $container The container instance
75 * @param ManagerRegistry $doctrine The doctrine instance
76 * @param UserPasswordHasherInterface $hasher The password hasher instance
77 * @param LoggerInterface $logger The logger instance
78 * @param MailerInterface $mailer The mailer instance
79 * @param EntityManagerInterface $manager The manager instance
80 * @param RouterInterface $router The router instance
81 * @param SluggerUtil $slugger The slugger instance
82 * @param RequestStack $stack The stack instance
83 * @param TranslatorInterface $translator The translator instance
84 */
85 public function __construct(ContainerInterface $container, ManagerRegistry $doctrine, UserPasswordHasherInterface $hasher, LoggerInterface $logger, MailerInterface $mailer, EntityManagerInterface $manager, RouterInterface $router, SluggerUtil $slugger, RequestStack $stack, TranslatorInterface $translator) {
86 //Retrieve config
87 $this->config = $container->getParameter(RapsysUserBundle::getAlias());
88
89 //Set container
90 $this->container = $container;
91
92 //Set doctrine
93 $this->doctrine = $doctrine;
94
95 //Set hasher
96 $this->hasher = $hasher;
97
98 //Set logger
99 $this->logger = $logger;
100
101 //Set mailer
102 $this->mailer = $mailer;
103
104 //Set manager
105 $this->manager = $manager;
106
107 //Set router
108 $this->router = $router;
109
110 //Set slugger
111 $this->slugger = $slugger;
112
113 //Set translator
114 $this->translator = $translator;
115
116 //Get current request
117 $request = $stack->getCurrentRequest();
118
119 //Get current locale
120 $this->locale = $request->getLocale();
121
122 //Set locale
123 $this->config['context']['locale'] = str_replace('_', '-', $this->locale);
124
125 //Set translate array
126 $translates = [];
127
128 //Look for keys to translate
129 if (!empty($this->config['translate'])) {
130 //Iterate on keys to translate
131 foreach($this->config['translate'] as $translate) {
132 //Set tmp
133 $tmp = null;
134 //Iterate on keys
135 foreach(array_reverse(explode('.', $translate)) as $curkey) {
136 $tmp = array_combine([$curkey], [$tmp]);
137 }
138 //Append tree
139 $translates = array_replace_recursive($translates, $tmp);
140 }
141 }
142
143 //Inject every requested route in view and mail context
144 foreach($this->config as $tag => $current) {
145 //Look for entry with title subkey
146 if (!empty($current['title'])) {
147 //Translate title value
148 $this->config[$tag]['title'] = $this->translator->trans($current['title']);
149 }
150
151 //Look for entry with route subkey
152 if (!empty($current['route'])) {
153 //Generate url for both view and mail
154 foreach(['view', 'mail'] as $view) {
155 //Check that context key is usable
156 if (isset($current[$view]['context']) && is_array($current[$view]['context'])) {
157 //Merge with global context
158 $this->config[$tag][$view]['context'] = array_replace_recursive($this->config['context'], $this->config[$tag][$view]['context']);
159
160 //Process every routes
161 foreach($current['route'] as $route => $key) {
162 //With confirm route
163 if ($route == 'confirm') {
164 //Skip route as it requires some parameters
165 continue;
166 }
167
168 //Set value
169 $value = $this->router->generate(
170 $this->config['route'][$route]['name'],
171 $this->config['route'][$route]['context'],
172 //Generate absolute url for mails
173 $view=='mail'?UrlGeneratorInterface::ABSOLUTE_URL:UrlGeneratorInterface::ABSOLUTE_PATH
174 );
175
176 //Multi level key
177 if (strpos($key, '.') !== false) {
178 //Set tmp
179 $tmp = $value;
180
181 //Iterate on key
182 foreach(array_reverse(explode('.', $key)) as $curkey) {
183 $tmp = array_combine([$curkey], [$tmp]);
184 }
185
186 //Set value
187 $this->config[$tag][$view]['context'] = array_replace_recursive($this->config[$tag][$view]['context'], $tmp);
188 //Single level key
189 } else {
190 //Set value
191 $this->config[$tag][$view]['context'][$key] = $value;
192 }
193 }
194
195 //Look for successful intersections
196 if (!empty(array_intersect_key($translates, $this->config[$tag][$view]['context']))) {
197 //Iterate on keys to translate
198 foreach($this->config['translate'] as $translate) {
199 //Set keys
200 $keys = explode('.', $translate);
201
202 //Set tmp
203 $tmp = $this->config[$tag][$view]['context'];
204
205 //Iterate on keys
206 foreach($keys as $curkey) {
207 //Without child key
208 if (!isset($tmp[$curkey])) {
209 //Skip to next key
210 continue(2);
211 }
212
213 //Get child key
214 $tmp = $tmp[$curkey];
215 }
216
217 //Translate tmp value
218 $tmp = $this->translator->trans($tmp);
219
220 //Iterate on keys
221 foreach(array_reverse($keys) as $curkey) {
222 //Set parent key
223 $tmp = array_combine([$curkey], [$tmp]);
224 }
225
226 //Set value
227 $this->config[$tag][$view]['context'] = array_replace_recursive($this->config[$tag][$view]['context'], $tmp);
228 }
229 }
230
231 //With view context
232 if ($view == 'view') {
233 //Get context path
234 $pathInfo = $this->router->getContext()->getPathInfo();
235
236 //Iterate on locales excluding current one
237 foreach($this->config['locales'] as $locale) {
238 //Set titles
239 $titles = [];
240
241 //Iterate on other locales
242 foreach(array_diff($this->config['locales'], [$locale]) as $other) {
243 $titles[$other] = $this->translator->trans($this->config['languages'][$locale], [], null, $other);
244 }
245
246 //Retrieve route matching path
247 $route = $this->router->match($pathInfo);
248
249 //Get route name
250 $name = $route['_route'];
251
252 //Unset route name
253 unset($route['_route']);
254
255 //With current locale
256 if ($locale == $this->locale) {
257 //Set locale locales context
258 $this->config[$tag][$view]['context']['canonical'] = $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL);
259 } else {
260 //Set locale locales context
261 $this->config[$tag][$view]['context']['alternates'][$locale] = [
262 'absolute' => $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL),
263 'relative' => $this->router->generate($name, ['_locale' => $locale]+$route),
264 'title' => implode('/', $titles),
265 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale)
266 ];
267 }
268
269 //Add shorter locale
270 if (empty($this->config[$tag][$view]['context']['alternates'][$slocale = substr($locale, 0, 2)])) {
271 //Add shorter locale
272 $this->config[$tag][$view]['context']['alternates'][$slocale] = [
273 'absolute' => $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL),
274 'relative' => $this->router->generate($name, ['_locale' => $locale]+$route),
275 'title' => implode('/', $titles),
276 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale)
277 ];
278 }
279 }
280 }
281 }
282 }
283 }
284 }
285 }
286
287 /**
288 * {@inheritdoc}
289 *
290 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
291 */
292 public static function getSubscribedServices(): array {
293 //Return subscribed services
294 return [
295 'service_container' => ContainerInterface::class,
296 'doctrine' => ManagerRegistry::class,
297 'doctrine.orm.default_entity_manager' => EntityManagerInterface::class,
298 'logger' => LoggerInterface::class,
299 'mailer.mailer' => MailerInterface::class,
300 'rapsys_pack.slugger_util' => SluggerUtil::class,
301 'request_stack' => RequestStack::class,
302 'router' => RouterInterface::class,
303 'security.user_password_hasher' => UserPasswordHasherInterface::class,
304 'translator' => TranslatorInterface::class
305 ];
306 }
307 }