1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Controller
;
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
;
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
;
43 use Rapsys\PackBundle\Util\FacebookUtil
;
44 use Rapsys\PackBundle\Util\ImageUtil
;
45 use Rapsys\PackBundle\Util\SluggerUtil
;
48 * Provides common features needed in controllers.
52 abstract class AbstractController
extends BaseAbstractController
implements ServiceSubscriberInterface
{
53 ///AuthorizationCheckerInterface instance
54 protected AuthorizationCheckerInterface
$checker;
57 protected array $config;
60 protected array $context;
62 ///AccessDecisionManagerInterface instance
63 protected AccessDecisionManagerInterface
$decision;
65 ///ManagerRegistry instance
66 protected ManagerRegistry
$doctrine;
68 ///FacebookUtil instance
69 protected FacebookUtil
$facebook;
71 ///FormFactoryInterface instance
72 protected FormFactoryInterface
$factory;
74 ///Image util instance
75 protected ImageUtil
$image;
81 protected string $locale;
83 ///MailerInterface instance
84 protected MailerInterface
$mailer;
86 ///EntityManagerInterface instance
87 protected EntityManagerInterface
$manager;
90 protected \DateTime
$modified;
92 ///PackageInterface instance
93 protected PackageInterface
$package;
96 protected Request
$request;
99 protected string $route;
101 ///Route params array
102 protected array $routeParams;
105 protected RouterInterface
$router;
107 ///Slugger util instance
108 protected SluggerUtil
$slugger;
111 protected Security
$security;
113 ///RequestStack instance
114 protected RequestStack
$stack;
116 ///Translator instance
117 protected TranslatorInterface
$translator;
119 ///Twig\Environment instance
120 protected Environment
$twig;
123 * Abstract constructor
125 * @param AuthorizationCheckerInterface $checker The container instance
126 * @param ContainerInterface $container The container instance
127 * @param AccessDecisionManagerInterface $decision The decision instance
128 * @param ManagerRegistry $doctrine The doctrine instance
129 * @param FacebookUtil $facebook The facebook instance
130 * @param FormFactoryInterface $factory The factory instance
131 * @param ImageUtil $image The image instance
132 * @param MailerInterface $mailer The mailer instance
133 * @param EntityManagerInterface $manager The manager instance
134 * @param PackageInterface $package The package instance
135 * @param RouterInterface $router The router instance
136 * @param SluggerUtil $slugger The slugger instance
137 * @param Security $security The security instance
138 * @param RequestStack $stack The stack instance
139 * @param TranslatorInterface $translator The translator instance
140 * @param Environment $twig The twig environment instance
141 * @param integer $limit The page limit
143 * @TODO move all that stuff to setSlugger('@slugger') setters with a calls: [ setSlugger: [ '@slugger' ] ] to unbload classes ???
144 * @TODO add a calls: [ ..., prepare: ['@???'] ] that do all the logic that can't be done in constructor because various things are not available
146 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) {
148 $this->checker
= $checker;
151 $this->config
= $container->getParameter(RapsysBlogBundle
::getAlias());
154 $this->container
= $container;
157 $this->decision
= $decision;
160 $this->doctrine
= $doctrine;
163 $this->facebook
= $facebook;
166 $this->factory
= $factory;
169 $this->image
= $image;
172 $this->limit
= $limit;
175 $this->mailer
= $mailer;
178 $this->manager
= $manager;
181 $this->package
= $package;
184 $this->router
= $router;
187 $this->slugger
= $slugger;
190 $this->security
= $security;
193 $this->stack
= $stack;
196 $this->translator
= $translator;
202 $this->request
= $this->stack
->getMainRequest();
205 $this->locale
= $this->request
->getLocale();
214 //TODO: default to not found route ???
215 //TODO: when url not found, this attribute is not defined, how do we handle it ???
216 //XXX: do we generate a dummy default route by default ???
217 $this->route
= $this->request
->attributes
->get('_route');
220 $this->routeParams
= $this->request
->attributes
->get('_route_params');
222 //With route and routeParams
223 if ($this->route
!== null && $this->routeParams
!== null) {
225 $canonical = $this->router
->generate($this->route
, $this->routeParams
, UrlGeneratorInterface
::ABSOLUTE_URL
);
233 //TODO: review the structure
234 #'title' => $this->translator->trans($this->config['title']),
235 #'og:site_name' => $this->translator->trans($this->config['title']),
237 # 'donate' => $this->config['donate'],
238 # 'title' => $title = $this->translator->trans($this->config['site']['title']),
241 'alternates' => $alternates,
242 'canonical' => $canonical,
243 'icon' => $this->config
['icon'],
245 'locale' => str_replace('_', '-', $this->locale
),
247 'png' => $this->config
['logo']['png'],
248 'svg' => $this->config
['logo']['svg'],
249 'alt' => $this->translator
->trans($this->config
['logo']['alt'])
251 'root' => $this->config
['root'],
254 'og:type' => 'article',
255 'og:site_name' => $this->translator
->trans($this->config
['title']),
256 'og:url' => $canonical,
257 //TODO: review this value
258 'fb:app_id' => $this->config
['facebook']['apps']
262 $this->translator
->trans($this->config
['title']) => [
263 'font' => 'irishgrover',
270 'name' => $this->translator
->trans($this->config
['contact']['name']),
271 'mail' => $this->config
['contact']['mail']
274 'by' => $this->translator
->trans($this->config
['copy']['by']),
275 'link' => $this->config
['copy']['link'],
276 'long' => $this->translator
->trans($this->config
['copy']['long']),
277 'short' => $this->translator
->trans($this->config
['copy']['short']),
278 'title' => $this->config
['copy']['title']
282 'description' => null,
285 'icon' => $this->config
['icon'],
287 'png' => $this->config
['logo']['png'],
288 'svg' => $this->config
['logo']['svg'],
289 'alt' => $this->translator
->trans($this->config
['logo']['alt'])
291 'path' => $this->config
['path'],
292 'root' => $this->config
['root'],
293 'title' => $this->translator
->trans($this->config
['title'])
303 protected function render(string $view, array $parameters = [], Response
$response = null): Response
{
304 //Create response when null
305 $response ??= new Response();
308 if (empty($parameters['head']['alternates'])) {
309 //Iterate on locales excluding current one
310 foreach($this->config
['locales'] as $locale) {
312 $routeParams = $this->routeParams
;
314 //With current locale
315 if ($locale !== $this->locale
) {
319 //Set route params locale
320 $routeParams['_locale'] = $locale;
323 //XXX: slug is in current locale, better use a simple redirect for invalid slug than implement a hard to get translation here
324 unset($routeParams['slug']);
326 //Iterate on other locales
327 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
328 //Set other locale title
329 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
332 //Set locale locales context
333 $parameters['head']['alternates'][str_replace('_', '-', $locale)] = [
334 'absolute' => $this->router
->generate($this->route
, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
335 'relative' => $this->router
->generate($this->route
, $routeParams),
336 'title' => implode('/', $titles),
337 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
341 if (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
342 //Set locale locales context
343 $parameters['head']['alternates'][$shortCurrent] = $parameters['head']['alternates'][str_replace('_', '-', $locale)];
346 } elseif (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
350 //Set route params locale
351 $routeParams['_locale'] = $locale;
353 //Iterate on other locales
354 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
355 //Set other locale title
356 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
359 //Set locale locales context
360 $parameters['head']['alternates'][$shortCurrent] = [
361 'absolute' => $this->router
->generate($this->route
, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
362 'relative' => $this->router
->generate($this->route
, $routeParams),
363 'title' => implode('/', $titles),
364 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
370 //With empty head title and section
371 if (empty($parameters['head']['title']) && !empty($parameters['section'])) {
373 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $this->translator
->trans($this->config
['title'])]);
374 //With empty head title
375 } elseif (empty($parameters['head']['title'])) {
377 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $this->translator
->trans($this->config
['title'])]);
380 //With empty head description and description
381 if (empty($parameters['head']['description']) && !empty($parameters['description'])) {
382 //Set head description
383 $parameters['head']['description'] = $parameters['description'];
386 //With empty facebook title and title
387 if (empty($parameters['head']['facebook']['og:title']) && !empty($parameters['title'])) {
389 $parameters['head']['facebook']['og:title'] = $parameters['title'];
392 //With empty facebook description and description
393 if (empty($parameters['head']['facebook']['og:description']) && !empty($parameters['description'])) {
394 //Set facebook description
395 $parameters['head']['facebook']['og:description'] = $parameters['description'];
399 if (!empty($this->locale
)) {
400 //Set facebook locale
401 $parameters['head']['facebook']['og:locale'] = $this->locale
;
404 //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber
405 //XXX: see https://stackoverflow.com/questions/20827882/in-open-graph-markup-whats-the-use-of-oglocalealternate-without-the-locati
406 if (!empty($parameters['head']['alternates'])) {
407 //Iterate on alternates
408 foreach($parameters['head']['alternates'] as $lang => $alternate) {
409 if (strlen($lang) == 5) {
410 //Set facebook locale alternate
411 $parameters['head']['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
417 //Without facebook image defined and texts
418 if (empty($parameters['head']['facebook']['og:image']) && !empty($this->request
) && !empty($parameters['head']['fbimage']['texts']) && !empty($this->modified
)) {
420 $parameters['head']['facebook'] +
= $this->facebook
->getImage($this->request
->getPathInfo(), $parameters['head']['fbimage']['texts'], $this->modified
->getTimestamp());
423 //Call twig render method
424 $content = $this->twig
->render($view, $parameters);
426 //Invalidate OK response on invalid form
427 if (200 === $response->getStatusCode()) {
428 foreach ($parameters as $v) {
429 if ($v instanceof FormInterface
&& $v->isSubmitted() && !$v->isValid()) {
430 $response->setStatusCode(422);
436 //Store content in response
437 $response->setContent($content);
446 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
448 public static function getSubscribedServices(): array {
449 //Return subscribed services
451 'security.authorization_checker' => AuthorizationCheckerInterface
::class,
452 'service_container' => ContainerInterface
::class,
453 'rapsys_user.access_decision_manager' => AccessDecisionManagerInterface
::class,
454 'doctrine' => ManagerRegistry
::class,
455 'rapsys_pack.facebook_util' => FacebookUtil
::class,
456 'form.factory' => FormFactoryInterface
::class,
457 'rapsys_pack.image_util' => ImageUtil
::class,
458 'mailer.mailer' => MailerInterface
::class,
459 'doctrine.orm.default_entity_manager' => EntityManagerInterface
::class,
460 'rapsys_pack.path_package' => PackageInterface
::class,
461 'router' => RouterInterface
::class,
462 'rapsys_pack.slugger_util' => SluggerUtil
::class,
463 'security' => Security
::class,
464 'stack' => RequestStack
::class,
465 'translator' => TranslatorInterface
::class,
466 'twig' => Environment
::class,
471 * Get a user from the Security Helper.
473 * @throws \LogicException If SecurityBundle is not available
475 * @see TokenInterface::getUser()
476 * @see https://github.com/symfony/symfony/issues/44735
477 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
479 protected function getUser(): ?UserInterface
{
481 if (null === ($token = $this->security
->getToken())) {
486 return $token->getUser();