]> Raphaël G. Git Repositories - blogbundle/blob - Controller/AbstractController.php
Replace sent parameter by a notice
[blogbundle] / Controller / AbstractController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle 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\BlogBundle\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\SecurityBundle\Security;
19 use Symfony\Component\Asset\PackageInterface;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
22 use Symfony\Component\Filesystem\Filesystem;
23 use Symfony\Component\Form\FormFactoryInterface;
24 use Symfony\Component\HttpFoundation\Request;
25 use Symfony\Component\HttpFoundation\RequestStack;
26 use Symfony\Component\HttpFoundation\Response;
27 use Symfony\Component\Mailer\MailerInterface;
28 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
29 use Symfony\Component\Routing\RouterInterface;
30 use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
31 use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
32 use Symfony\Component\Security\Core\User\UserInterface;
33 use Symfony\Contracts\Service\ServiceSubscriberInterface;
34 use Symfony\Contracts\Translation\TranslatorInterface;
35 use Twig\Environment;
36
37 use Rapsys\BlogBundle\Entity\Dance;
38 use Rapsys\BlogBundle\Entity\Location;
39 use Rapsys\BlogBundle\Entity\Slot;
40 use Rapsys\BlogBundle\Entity\User;
41 use Rapsys\BlogBundle\RapsysBlogBundle;
42
43 use Rapsys\PackBundle\Util\FacebookUtil;
44 use Rapsys\PackBundle\Util\ImageUtil;
45 use Rapsys\PackBundle\Util\SluggerUtil;
46
47 /**
48 * Provides common features needed in controllers.
49 *
50 * {@inheritdoc}
51 */
52 abstract class AbstractController extends BaseAbstractController implements ServiceSubscriberInterface {
53 /**
54 * Config array
55 */
56 protected array $config;
57
58 /**
59 * Count integer
60 */
61 protected int $count;
62
63 /**
64 * Context array
65 */
66 protected array $context;
67
68 /**
69 * Limit integer
70 */
71 protected int $limit;
72
73 /**
74 * Locale string
75 */
76 protected string $locale;
77
78 /**
79 * Modified DateTime
80 */
81 protected \DateTime $modified;
82
83 /**
84 * Page integer
85 */
86 protected int $page;
87
88 /**
89 * Route string
90 */
91 protected string $route;
92
93 /**
94 * Route params array
95 */
96 protected array $routeParams;
97
98 /**
99 * AuthorizationCheckerInterface instance
100 */
101 protected AuthorizationCheckerInterface $checker;
102
103 /**
104 * AccessDecisionManagerInterface instance
105 */
106 protected AccessDecisionManagerInterface $decision;
107
108 /**
109 * ManagerRegistry instance
110 */
111 protected ManagerRegistry $doctrine;
112
113 /**
114 * FacebookUtil instance
115 */
116 protected FacebookUtil $facebook;
117
118 /**
119 * FormFactoryInterface instance
120 */
121 protected FormFactoryInterface $factory;
122
123 /**
124 * Image util instance
125 */
126 protected ImageUtil $image;
127
128 /**
129 * MailerInterface instance
130 */
131 protected MailerInterface $mailer;
132
133 /**
134 * EntityManagerInterface instance
135 */
136 protected EntityManagerInterface $manager;
137
138 /**
139 * PackageInterface instance
140 */
141 protected PackageInterface $package;
142
143 /**
144 * Request instance
145 */
146 protected Request $request;
147
148 /**
149 * Router instance
150 */
151 protected RouterInterface $router;
152
153 /**
154 * Slugger util instance
155 */
156 protected SluggerUtil $slugger;
157
158 /**
159 * Security instance
160 */
161 protected Security $security;
162
163 /**
164 * RequestStack instance
165 */
166 protected RequestStack $stack;
167
168 /**
169 * Translator instance
170 */
171 protected TranslatorInterface $translator;
172
173 /**
174 * Twig\Environment instance
175 */
176 protected Environment $twig;
177
178 /**
179 * Abstract constructor
180 *
181 * @param AuthorizationCheckerInterface $checker The container instance
182 * @param ContainerInterface $container The container instance
183 * @param AccessDecisionManagerInterface $decision The decision instance
184 * @param ManagerRegistry $doctrine The doctrine instance
185 * @param FacebookUtil $facebook The facebook instance
186 * @param FormFactoryInterface $factory The factory instance
187 * @param ImageUtil $image The image instance
188 * @param MailerInterface $mailer The mailer instance
189 * @param EntityManagerInterface $manager The manager instance
190 * @param PackageInterface $package The package instance
191 * @param RouterInterface $router The router instance
192 * @param SluggerUtil $slugger The slugger instance
193 * @param Security $security The security instance
194 * @param RequestStack $stack The stack instance
195 * @param TranslatorInterface $translator The translator instance
196 * @param Environment $twig The twig environment instance
197 * @param integer $limit The page limit
198 *
199 * @TODO move all that stuff to setSlugger('@slugger') setters with a calls: [ setSlugger: [ '@slugger' ] ] to unbload classes ???
200 * @TODO add a calls: [ ..., prepare: ['@???'] ] that do all the logic that can't be done in constructor because various things are not available
201 */
202 public function __construct(AuthorizationCheckerInterface $checker, ContainerInterface $container, AccessDecisionManagerInterface $decision, ManagerRegistry $doctrine, FacebookUtil $facebook, FormFactoryInterface $factory, ImageUtil $image, MailerInterface $mailer, EntityManagerInterface $manager, PackageInterface $package, RouterInterface $router, SluggerUtil $slugger, Security $security, RequestStack $stack, TranslatorInterface $translator, Environment $twig, int $limit = 5) {
203 //Set checker
204 $this->checker = $checker;
205
206 //Retrieve config
207 $this->config = $container->getParameter(RapsysBlogBundle::getAlias());
208
209 //Set the container
210 $this->container = $container;
211
212 //Set decision
213 $this->decision = $decision;
214
215 //Set doctrine
216 $this->doctrine = $doctrine;
217
218 //Set facebook
219 $this->facebook = $facebook;
220
221 //Set factory
222 $this->factory = $factory;
223
224 //Set image
225 $this->image = $image;
226
227 //Set limit
228 $this->limit = $limit;
229
230 //Set mailer
231 $this->mailer = $mailer;
232
233 //Set manager
234 $this->manager = $manager;
235
236 //Set package
237 $this->package = $package;
238
239 //Set router
240 $this->router = $router;
241
242 //Set slugger
243 $this->slugger = $slugger;
244
245 //Set security
246 $this->security = $security;
247
248 //Set stack
249 $this->stack = $stack;
250
251 //Set translator
252 $this->translator = $translator;
253
254 //Set twig
255 $this->twig = $twig;
256
257 //Get main request
258 $this->request = $this->stack->getCurrentRequest();
259
260 //Get current locale
261 $this->locale = $this->request->getLocale();
262
263 //Set canonical
264 $canonical = null;
265
266 //Set alternates
267 $alternates = [];
268
269 //Get current page
270 $this->page = (int) $this->request->query->get('page');
271
272 //With negative page
273 if ($this->page < 0) {
274 $this->page = 0;
275 }
276
277 //Set route
278 //TODO: default to not found route ???
279 //TODO: when url not found, this attribute is not defined, how do we handle it ???
280 //XXX: do we generate a dummy default route by default ???
281 $this->route = $this->request->attributes->get('_route');
282
283 //Set route params
284 $this->routeParams = $this->request->attributes->get('_route_params');
285
286 //With route and routeParams
287 if ($this->route !== null && $this->routeParams !== null) {
288 //Set canonical
289 $canonical = $this->router->generate($this->route, $this->routeParams, UrlGeneratorInterface::ABSOLUTE_URL);
290
291 //Set alternates
292 $alternates = [];
293 }
294
295 //Set the context
296 $this->context = [
297 'alternates' => $alternates,
298 'canonical' => $canonical,
299 'contact' => [
300 'address' => $this->config['contact']['address'],
301 'name' => $this->translator->trans($this->config['contact']['name'])
302 ],
303 'copy' => [
304 'by' => $this->translator->trans($this->config['copy']['by']),
305 'link' => $this->config['copy']['link'],
306 'long' => $this->translator->trans($this->config['copy']['long']),
307 'short' => $this->translator->trans($this->config['copy']['short']),
308 'title' => $this->config['copy']['title']
309 ],
310 'description' => null,
311 'donate' => $this->config['donate'],
312 'facebook' => [
313 'og:type' => 'article',
314 'og:site_name' => $title = $this->translator->trans($this->config['title']),
315 'og:url' => $canonical,
316 #'fb:admins' => $this->config['facebook']['admins'],
317 'fb:app_id' => $this->config['facebook']['apps']
318 ],
319 //XXX: TODO: only generate it when fb robot request the url ???
320 'fbimage' => [
321 'texts' => [
322 $title => [
323 //'font' => 'irishgrover',
324 //'size' => 110
325 ]
326 ]
327 ],
328 'forms' => [],
329 'icon' => $this->config['icon'],
330 'keywords' => null,
331 'locale' => str_replace('_', '-', $this->locale),
332 #'logo' => [
333 # 'png' => $this->config['logo']['png'],
334 # 'svg' => $this->config['logo']['svg'],
335 # 'alt' => $this->translator->trans($this->config['logo']['alt'])
336 #],
337 'logo' => $this->config['logo'],
338 'next' => null,
339 'prev' => null,
340 'root' => $this->config['root'],
341 'title' => [
342 'page' => null,
343 'section' => null,
344 'site' => $title
345 ]
346 ];
347 }
348
349 /**
350 * Renders a view
351 *
352 * {@inheritdoc}
353 */
354 protected function render(string $view, array $parameters = [], Response $response = null): Response {
355 //Create response when null
356 $response ??= new Response();
357
358 //Without alternates
359 if (count($parameters['alternates']) <= 1) {
360 //Iterate on locales excluding current one
361 foreach($this->config['locales'] as $locale) {
362 //Set routeParams
363 $routeParams = $this->routeParams;
364
365 //With current locale
366 if ($locale !== $this->locale) {
367 //Set titles
368 $titles = [];
369
370 //Set route params locale
371 $routeParams['_locale'] = $locale;
372
373 //Unset slug
374 //XXX: slug is in current locale, better use a simple redirect for invalid slug than implement a hard to get translation here
375 unset($routeParams['slug']);
376
377 //Iterate on other locales
378 foreach(array_diff($this->config['locales'], [$locale]) as $other) {
379 //Set other locale title
380 $titles[$other] = $this->translator->trans($this->config['languages'][$locale], [], null, $other);
381 }
382
383 //Set locale locales context
384 $parameters['alternates'][str_replace('_', '-', $locale)] = [
385 'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL),
386 'relative' => $this->router->generate($this->route, $routeParams),
387 'title' => implode('/', $titles),
388 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale)
389 ];
390
391 //Add shorter locale
392 if (empty($parameters['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
393 //Set locale locales context
394 $parameters['alternates'][$shortCurrent] = $parameters['alternates'][str_replace('_', '-', $locale)];
395 }
396 //Add shorter locale
397 } elseif (empty($parameters['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
398 //Set titles
399 $titles = [];
400
401 //Set route params locale
402 $routeParams['_locale'] = $locale;
403
404 //Iterate on other locales
405 foreach(array_diff($this->config['locales'], [$locale]) as $other) {
406 //Set other locale title
407 $titles[$other] = $this->translator->trans($this->config['languages'][$locale], [], null, $other);
408 }
409
410 //Set locale locales context
411 $parameters['alternates'][$shortCurrent] = [
412 'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL),
413 'relative' => $this->router->generate($this->route, $routeParams),
414 'title' => implode('/', $titles),
415 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale)
416 ];
417 }
418 }
419 }
420
421 //With canonical
422 if (!empty($parameters['canonical'])) {
423 //Set facebook url
424 $parameters['facebook']['og:url'] = $parameters['canonical'];
425 }
426
427 //With empty facebook title and title
428 if (empty($parameters['facebook']['og:title']) && !empty($parameters['title']['page'])) {
429 //Set facebook title
430 $parameters['facebook']['og:title'] = $parameters['title']['page'];
431 }
432
433 //With empty facebook description and description
434 if (empty($parameters['facebook']['og:description']) && !empty($parameters['description'])) {
435 //Set facebook description
436 $parameters['facebook']['og:description'] = $parameters['description'];
437 }
438
439 //With locale
440 if (!empty($this->locale)) {
441 //Set facebook locale
442 $parameters['facebook']['og:locale'] = $this->locale;
443
444 //With alternates
445 //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber
446 //XXX: see https://stackoverflow.com/questions/20827882/in-open-graph-markup-whats-the-use-of-oglocalealternate-without-the-locati
447 if (!empty($parameters['alternates'])) {
448 //Iterate on alternates
449 foreach($parameters['alternates'] as $lang => $alternate) {
450 if (strlen($lang) == 5) {
451 //Set facebook locale alternate
452 $parameters['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
453 }
454 }
455 }
456 }
457
458 //Without facebook image defined and texts
459 if (empty($parameters['facebook']['og:image']) && !empty($this->request) && !empty($parameters['fbimage']['texts']) && !empty($this->modified)) {
460 //Get facebook image
461 //XXX: decode getPathInfo (see https://github.com/symfony/symfony/issues/2579)
462 $parameters['facebook'] += $this->image->getFacebook(urldecode($this->request->getPathInfo()), $parameters['fbimage']['texts'], $this->modified->getTimestamp());
463 }
464
465 //With count
466 if (!empty($this->count)) {
467 //With prev link
468 if ($this->page > 0) {
469 //Set head prev
470 $parameters['prev'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page - 1]+$this->request->get('_route_params'));
471 }
472
473 //With next link
474 if ($this->count > ($this->page + 1) * $this->limit) {
475 //Set head next
476 $parameters['next'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page + 1]+$this->request->get('_route_params'));
477 }
478 }
479
480 //Call twig render method
481 $content = $this->twig->render($view, $parameters);
482
483 //Invalidate OK response on invalid form
484 if (200 === $response->getStatusCode()) {
485 foreach ($parameters as $v) {
486 if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
487 $response->setStatusCode(422);
488 break;
489 }
490 }
491 }
492
493 //Store content in response
494 $response->setContent($content);
495
496 //Return response
497 return $response;
498 }
499
500 /**
501 * {@inheritdoc}
502 *
503 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
504 */
505 public static function getSubscribedServices(): array {
506 //Return subscribed services
507 return [
508 'security.authorization_checker' => AuthorizationCheckerInterface::class,
509 'service_container' => ContainerInterface::class,
510 'rapsys_user.access_decision_manager' => AccessDecisionManagerInterface::class,
511 'doctrine' => ManagerRegistry::class,
512 'rapsys_pack.facebook_util' => FacebookUtil::class,
513 'form.factory' => FormFactoryInterface::class,
514 'rapsys_pack.image_util' => ImageUtil::class,
515 'mailer.mailer' => MailerInterface::class,
516 'doctrine.orm.default_entity_manager' => EntityManagerInterface::class,
517 'rapsys_pack.path_package' => PackageInterface::class,
518 'router' => RouterInterface::class,
519 'rapsys_pack.slugger_util' => SluggerUtil::class,
520 'security' => Security::class,
521 'stack' => RequestStack::class,
522 'translator' => TranslatorInterface::class,
523 'twig' => Environment::class,
524 ];
525 }
526 }