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;
66 protected array $context;
76 protected string $locale;
81 protected \DateTime
$modified;
91 protected string $route;
96 protected array $routeParams;
99 * AuthorizationCheckerInterface instance
101 protected AuthorizationCheckerInterface
$checker;
104 * AccessDecisionManagerInterface instance
106 protected AccessDecisionManagerInterface
$decision;
109 * ManagerRegistry instance
111 protected ManagerRegistry
$doctrine;
114 * FacebookUtil instance
116 protected FacebookUtil
$facebook;
119 * FormFactoryInterface instance
121 protected FormFactoryInterface
$factory;
124 * Image util instance
126 protected ImageUtil
$image;
129 * MailerInterface instance
131 protected MailerInterface
$mailer;
134 * EntityManagerInterface instance
136 protected EntityManagerInterface
$manager;
139 * PackageInterface instance
141 protected PackageInterface
$package;
146 protected Request
$request;
151 protected RouterInterface
$router;
154 * Slugger util instance
156 protected SluggerUtil
$slugger;
161 protected Security
$security;
164 * RequestStack instance
166 protected RequestStack
$stack;
169 * Translator instance
171 protected TranslatorInterface
$translator;
174 * Twig\Environment instance
176 protected Environment
$twig;
179 * Abstract constructor
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
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
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) {
204 $this->checker
= $checker;
207 $this->config
= $container->getParameter(RapsysBlogBundle
::getAlias());
210 $this->container
= $container;
213 $this->decision
= $decision;
216 $this->doctrine
= $doctrine;
219 $this->facebook
= $facebook;
222 $this->factory
= $factory;
225 $this->image
= $image;
228 $this->limit
= $limit;
231 $this->mailer
= $mailer;
234 $this->manager
= $manager;
237 $this->package
= $package;
240 $this->router
= $router;
243 $this->slugger
= $slugger;
246 $this->security
= $security;
249 $this->stack
= $stack;
252 $this->translator
= $translator;
258 $this->request
= $this->stack
->getCurrentRequest();
261 $this->locale
= $this->request
->getLocale();
270 $this->page
= (int) $this->request
->query
->get('page');
273 if ($this->page
< 0) {
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');
284 $this->routeParams
= $this->request
->attributes
->get('_route_params');
286 //With route and routeParams
287 if ($this->route
!== null && $this->routeParams
!== null) {
289 $canonical = $this->router
->generate($this->route
, $this->routeParams
, UrlGeneratorInterface
::ABSOLUTE_URL
);
298 'alternates' => $alternates,
299 'canonical' => $canonical,
300 'icon' => $this->config
['icon'],
302 'locale' => str_replace('_', '-', $this->locale
),
304 'png' => $this->config
['logo']['png'],
305 'svg' => $this->config
['logo']['svg'],
306 'alt' => $this->translator
->trans($this->config
['logo']['alt'])
308 'root' => $this->config
['root'],
309 'site' => $this->translator
->trans($this->config
['title']),
312 'og:type' => 'article',
313 'og:site_name' => $this->translator
->trans($this->config
['title']),
314 'og:url' => $canonical,
315 //TODO: review this value
316 'fb:app_id' => $this->config
['facebook']['apps']
320 $this->translator
->trans($this->config
['title']) => [
321 'font' => 'irishgrover',
328 'address' => $this->config
['contact']['address'],
329 'name' => $this->translator
->trans($this->config
['contact']['name'])
332 'by' => $this->translator
->trans($this->config
['copy']['by']),
333 'link' => $this->config
['copy']['link'],
334 'long' => $this->translator
->trans($this->config
['copy']['long']),
335 'short' => $this->translator
->trans($this->config
['copy']['short']),
336 'title' => $this->config
['copy']['title']
339 'description' => null,
350 protected function render(string $view, array $parameters = [], Response
$response = null): Response
{
351 //Create response when null
352 $response ??= new Response();
355 if (empty($parameters['head']['alternates'])) {
356 //Iterate on locales excluding current one
357 foreach($this->config
['locales'] as $locale) {
359 $routeParams = $this->routeParams
;
361 //With current locale
362 if ($locale !== $this->locale
) {
366 //Set route params locale
367 $routeParams['_locale'] = $locale;
370 //XXX: slug is in current locale, better use a simple redirect for invalid slug than implement a hard to get translation here
371 unset($routeParams['slug']);
373 //Iterate on other locales
374 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
375 //Set other locale title
376 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
379 //Set locale locales context
380 $parameters['head']['alternates'][str_replace('_', '-', $locale)] = [
381 'absolute' => $this->router
->generate($this->route
, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
382 'relative' => $this->router
->generate($this->route
, $routeParams),
383 'title' => implode('/', $titles),
384 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
388 if (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
389 //Set locale locales context
390 $parameters['head']['alternates'][$shortCurrent] = $parameters['head']['alternates'][str_replace('_', '-', $locale)];
393 } elseif (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
397 //Set route params locale
398 $routeParams['_locale'] = $locale;
400 //Iterate on other locales
401 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
402 //Set other locale title
403 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
406 //Set locale locales context
407 $parameters['head']['alternates'][$shortCurrent] = [
408 'absolute' => $this->router
->generate($this->route
, $routeParams, UrlGeneratorInterface
::ABSOLUTE_URL
),
409 'relative' => $this->router
->generate($this->route
, $routeParams),
410 'title' => implode('/', $titles),
411 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
417 //With empty head title and section
418 if (empty($parameters['head']['title']) && !empty($parameters['section'])) {
420 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]);
421 //With empty head title
422 } elseif (empty($parameters['head']['title'])) {
424 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['head']['site']]);
427 //With empty head description and description
428 if (empty($parameters['head']['description']) && !empty($parameters['description'])) {
429 //Set head description
430 $parameters['head']['description'] = $parameters['description'];
433 //With empty facebook title and title
434 if (empty($parameters['head']['facebook']['og:title']) && !empty($parameters['title'])) {
436 $parameters['head']['facebook']['og:title'] = $parameters['title'];
439 //With empty facebook description and description
440 if (empty($parameters['head']['facebook']['og:description']) && !empty($parameters['description'])) {
441 //Set facebook description
442 $parameters['head']['facebook']['og:description'] = $parameters['description'];
446 if (!empty($this->locale
)) {
447 //Set facebook locale
448 $parameters['head']['facebook']['og:locale'] = $this->locale
;
451 //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber
452 //XXX: see https://stackoverflow.com/questions/20827882/in-open-graph-markup-whats-the-use-of-oglocalealternate-without-the-locati
453 if (!empty($parameters['head']['alternates'])) {
454 //Iterate on alternates
455 foreach($parameters['head']['alternates'] as $lang => $alternate) {
456 if (strlen($lang) == 5) {
457 //Set facebook locale alternate
458 $parameters['head']['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
465 if (!empty($this->count
)) {
467 if ($this->page
> 0) {
469 $parameters['head']['prev'] = $this->generateUrl($this->request
->get('_route'), ['page' => $this->page
- 1]+
$this->request
->get('_route_params'));
473 if ($this->count
> ($this->page +
1) * $this->limit
) {
475 $parameters['head']['next'] = $this->generateUrl($this->request
->get('_route'), ['page' => $this->page +
1]+
$this->request
->get('_route_params'));
479 //Without facebook image defined and texts
480 if (empty($parameters['head']['facebook']['og:image']) && !empty($this->request
) && !empty($parameters['head']['fbimage']['texts']) && !empty($this->modified
)) {
482 $parameters['head']['facebook'] +
= $this->facebook
->getImage($this->request
->getPathInfo(), $parameters['head']['fbimage']['texts'], $this->modified
->getTimestamp());
485 //Call twig render method
486 $content = $this->twig
->render($view, $parameters);
488 //Invalidate OK response on invalid form
489 if (200 === $response->getStatusCode()) {
490 foreach ($parameters as $v) {
491 if ($v instanceof FormInterface
&& $v->isSubmitted() && !$v->isValid()) {
492 $response->setStatusCode(422);
498 //Store content in response
499 $response->setContent($content);
508 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
510 public static function getSubscribedServices(): array {
511 //Return subscribed services
513 'security.authorization_checker' => AuthorizationCheckerInterface
::class,
514 'service_container' => ContainerInterface
::class,
515 'rapsys_user.access_decision_manager' => AccessDecisionManagerInterface
::class,
516 'doctrine' => ManagerRegistry
::class,
517 'rapsys_pack.facebook_util' => FacebookUtil
::class,
518 'form.factory' => FormFactoryInterface
::class,
519 'rapsys_pack.image_util' => ImageUtil
::class,
520 'mailer.mailer' => MailerInterface
::class,
521 'doctrine.orm.default_entity_manager' => EntityManagerInterface
::class,
522 'rapsys_pack.path_package' => PackageInterface
::class,
523 'router' => RouterInterface
::class,
524 'rapsys_pack.slugger_util' => SluggerUtil
::class,
525 'security' => Security
::class,
526 'stack' => RequestStack
::class,
527 'translator' => TranslatorInterface
::class,
528 'twig' => Environment
::class,