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
{
56 protected array $config;
61 protected array $context;
71 protected string $locale;
76 protected \DateTime
$modified;
86 protected string $route;
91 protected array $routeParams;
94 * AuthorizationCheckerInterface instance
96 protected AuthorizationCheckerInterface
$checker;
99 * AccessDecisionManagerInterface instance
101 protected AccessDecisionManagerInterface
$decision;
104 * ManagerRegistry instance
106 protected ManagerRegistry
$doctrine;
109 * FacebookUtil instance
111 protected FacebookUtil
$facebook;
114 * FormFactoryInterface instance
116 protected FormFactoryInterface
$factory;
119 * Image util instance
121 protected ImageUtil
$image;
124 * MailerInterface instance
126 protected MailerInterface
$mailer;
129 * EntityManagerInterface instance
131 protected EntityManagerInterface
$manager;
134 * PackageInterface instance
136 protected PackageInterface
$package;
141 protected Request
$request;
146 protected RouterInterface
$router;
149 * Slugger util instance
151 protected SluggerUtil
$slugger;
156 protected Security
$security;
159 * RequestStack instance
161 protected RequestStack
$stack;
164 * Translator instance
166 protected TranslatorInterface
$translator;
169 * Twig\Environment instance
171 protected Environment
$twig;
174 * Abstract constructor
176 * @param AuthorizationCheckerInterface $checker The container instance
177 * @param ContainerInterface $container The container instance
178 * @param AccessDecisionManagerInterface $decision The decision instance
179 * @param ManagerRegistry $doctrine The doctrine instance
180 * @param FacebookUtil $facebook The facebook instance
181 * @param FormFactoryInterface $factory The factory instance
182 * @param ImageUtil $image The image instance
183 * @param MailerInterface $mailer The mailer instance
184 * @param EntityManagerInterface $manager The manager instance
185 * @param PackageInterface $package The package instance
186 * @param RouterInterface $router The router instance
187 * @param SluggerUtil $slugger The slugger instance
188 * @param Security $security The security instance
189 * @param RequestStack $stack The stack instance
190 * @param TranslatorInterface $translator The translator instance
191 * @param Environment $twig The twig environment instance
192 * @param integer $limit The page limit
194 * @TODO move all that stuff to setSlugger('@slugger') setters with a calls: [ setSlugger: [ '@slugger' ] ] to unbload classes ???
195 * @TODO add a calls: [ ..., prepare: ['@???'] ] that do all the logic that can't be done in constructor because various things are not available
197 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) {
199 $this->checker
= $checker;
202 $this->config
= $container->getParameter(RapsysBlogBundle
::getAlias());
205 $this->container
= $container;
208 $this->decision
= $decision;
211 $this->doctrine
= $doctrine;
214 $this->facebook
= $facebook;
217 $this->factory
= $factory;
220 $this->image
= $image;
223 $this->limit
= $limit;
226 $this->mailer
= $mailer;
229 $this->manager
= $manager;
232 $this->package
= $package;
235 $this->router
= $router;
238 $this->slugger
= $slugger;
241 $this->security
= $security;
244 $this->stack
= $stack;
247 $this->translator
= $translator;
253 $this->request
= $this->stack
->getCurrentRequest();
256 $this->locale
= $this->request
->getLocale();
265 $this->page
= (int) $this->request
->query
->get('page');
268 if ($this->page
< 0) {
273 //TODO: default to not found route ???
274 //TODO: when url not found, this attribute is not defined, how do we handle it ???
275 //XXX: do we generate a dummy default route by default ???
276 $this->route
= $this->request
->attributes
->get('_route');
279 $this->routeParams
= $this->request
->attributes
->get('_route_params');
281 //With route and routeParams
282 if ($this->route
!== null && $this->routeParams
!== null) {
284 $canonical = $this->router
->generate($this->route
, $this->routeParams
, UrlGeneratorInterface
::ABSOLUTE_URL
);
293 'alternates' => $alternates,
294 'canonical' => $canonical,
295 'icon' => $this->config
['icon'],
297 'locale' => str_replace('_', '-', $this->locale
),
299 'png' => $this->config
['logo']['png'],
300 'svg' => $this->config
['logo']['svg'],
301 'alt' => $this->translator
->trans($this->config
['logo']['alt'])
303 'root' => $this->config
['root'],
304 'site' => $this->translator
->trans($this->config
['title']),
307 'og:type' => 'article',
308 'og:site_name' => $this->translator
->trans($this->config
['title']),
309 'og:url' => $canonical,
310 //TODO: review this value
311 'fb:app_id' => $this->config
['facebook']['apps']
315 $this->translator
->trans($this->config
['title']) => [
316 'font' => 'irishgrover',
323 'address' => $this->config
['contact']['address'],
324 'name' => $this->translator
->trans($this->config
['contact']['name'])
327 'by' => $this->translator
->trans($this->config
['copy']['by']),
328 'link' => $this->config
['copy']['link'],
329 'long' => $this->translator
->trans($this->config
['copy']['long']),
330 'short' => $this->translator
->trans($this->config
['copy']['short']),
331 'title' => $this->config
['copy']['title']
334 'description' => null,
345 protected function render(string $view, array $parameters = [], Response
$response = null): Response
{
346 //Create response when null
347 $response ??= new Response();
350 if (empty($parameters['head']['alternates'])) {
351 //Iterate on locales excluding current one
352 foreach($this->config
['locales'] as $locale) {
354 $routeParams = $this->routeParams
;
356 //With current locale
357 if ($locale !== $this->locale
) {
361 //Set route params locale
362 $routeParams['_locale'] = $locale;
365 //XXX: slug is in current locale, better use a simple redirect for invalid slug than implement a hard to get translation here
366 unset($routeParams['slug']);
368 //Iterate on other locales
369 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
370 //Set other locale title
371 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
374 //Set locale locales context
375 $parameters['head']['alternates'][str_replace('_', '-', $locale)] = [
376 'absolute' => $this->router
->generate($this->route
, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
377 'relative' => $this->router
->generate($this->route
, $routeParams),
378 'title' => implode('/', $titles),
379 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
383 if (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
384 //Set locale locales context
385 $parameters['head']['alternates'][$shortCurrent] = $parameters['head']['alternates'][str_replace('_', '-', $locale)];
388 } elseif (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
392 //Set route params locale
393 $routeParams['_locale'] = $locale;
395 //Iterate on other locales
396 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
397 //Set other locale title
398 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
401 //Set locale locales context
402 $parameters['head']['alternates'][$shortCurrent] = [
403 'absolute' => $this->router
->generate($this->route
, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
404 'relative' => $this->router
->generate($this->route
, $routeParams),
405 'title' => implode('/', $titles),
406 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
412 //With empty head title and section
413 if (empty($parameters['head']['title']) && !empty($parameters['section'])) {
415 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]);
416 //With empty head title
417 } elseif (empty($parameters['head']['title'])) {
419 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['head']['site']]);
422 //With empty head description and description
423 if (empty($parameters['head']['description']) && !empty($parameters['description'])) {
424 //Set head description
425 $parameters['head']['description'] = $parameters['description'];
428 //With empty facebook title and title
429 if (empty($parameters['head']['facebook']['og:title']) && !empty($parameters['title'])) {
431 $parameters['head']['facebook']['og:title'] = $parameters['title'];
434 //With empty facebook description and description
435 if (empty($parameters['head']['facebook']['og:description']) && !empty($parameters['description'])) {
436 //Set facebook description
437 $parameters['head']['facebook']['og:description'] = $parameters['description'];
441 if (!empty($this->locale
)) {
442 //Set facebook locale
443 $parameters['head']['facebook']['og:locale'] = $this->locale
;
446 //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber
447 //XXX: see https://stackoverflow.com/questions/20827882/in-open-graph-markup-whats-the-use-of-oglocalealternate-without-the-locati
448 if (!empty($parameters['head']['alternates'])) {
449 //Iterate on alternates
450 foreach($parameters['head']['alternates'] as $lang => $alternate) {
451 if (strlen($lang) == 5) {
452 //Set facebook locale alternate
453 $parameters['head']['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
459 //Without facebook image defined and texts
460 if (empty($parameters['head']['facebook']['og:image']) && !empty($this->request
) && !empty($parameters['head']['fbimage']['texts']) && !empty($this->modified
)) {
462 $parameters['head']['facebook'] +
= $this->facebook
->getImage($this->request
->getPathInfo(), $parameters['head']['fbimage']['texts'], $this->modified
->getTimestamp());
465 //Call twig render method
466 $content = $this->twig
->render($view, $parameters);
468 //Invalidate OK response on invalid form
469 if (200 === $response->getStatusCode()) {
470 foreach ($parameters as $v) {
471 if ($v instanceof FormInterface
&& $v->isSubmitted() && !$v->isValid()) {
472 $response->setStatusCode(422);
478 //Store content in response
479 $response->setContent($content);
488 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
490 public static function getSubscribedServices(): array {
491 //Return subscribed services
493 'security.authorization_checker' => AuthorizationCheckerInterface
::class,
494 'service_container' => ContainerInterface
::class,
495 'rapsys_user.access_decision_manager' => AccessDecisionManagerInterface
::class,
496 'doctrine' => ManagerRegistry
::class,
497 'rapsys_pack.facebook_util' => FacebookUtil
::class,
498 'form.factory' => FormFactoryInterface
::class,
499 'rapsys_pack.image_util' => ImageUtil
::class,
500 'mailer.mailer' => MailerInterface
::class,
501 'doctrine.orm.default_entity_manager' => EntityManagerInterface
::class,
502 'rapsys_pack.path_package' => PackageInterface
::class,
503 'router' => RouterInterface
::class,
504 'rapsys_pack.slugger_util' => SluggerUtil
::class,
505 'security' => Security
::class,
506 'stack' => RequestStack
::class,
507 'translator' => TranslatorInterface
::class,
508 'twig' => Environment
::class,