]> Raphaƫl G. Git Repositories - blogbundle/blobdiff - Controller/AbstractController.php
Update translations for new context layout
[blogbundle] / Controller / AbstractController.php
index e4c9d150f8ad33bc2994c2d32864a328e8d1cde8..fca2be926746fc610f97fe6d657fcdd72474f7db 100644 (file)
@@ -50,73 +50,129 @@ use Rapsys\PackBundle\Util\SluggerUtil;
  * {@inheritdoc}
  */
 abstract class AbstractController extends BaseAbstractController implements ServiceSubscriberInterface {
-       ///AuthorizationCheckerInterface instance
-       protected AuthorizationCheckerInterface $checker;
-
-       ///Config array
+       /**
+        * Config array
+        */
        protected array $config;
 
-       ///Context array
+       /**
+        * Count integer
+        */
+       protected int $count;
+
+       /**
+        * Context array
+        */
        protected array $context;
 
-       ///AccessDecisionManagerInterface instance
+       /**
+        * Limit integer
+        */
+       protected int $limit;
+
+       /**
+        * Locale string
+        */
+       protected string $locale;
+
+       /**
+        * Modified DateTime
+        */
+       protected \DateTime $modified;
+
+       /**
+        * Page integer
+        */
+       protected int $page;
+
+       /**
+        * Route string
+        */
+       protected string $route;
+
+       /**
+        * Route params array
+        */
+       protected array $routeParams;
+
+       /**
+        * AuthorizationCheckerInterface instance
+        */
+       protected AuthorizationCheckerInterface $checker;
+
+       /**
+        * AccessDecisionManagerInterface instance
+        */
        protected AccessDecisionManagerInterface $decision;
 
-       ///ManagerRegistry instance
+       /**
+        * ManagerRegistry instance
+        */
        protected ManagerRegistry $doctrine;
 
-       ///FacebookUtil instance
+       /**
+        * FacebookUtil instance
+        */
        protected FacebookUtil $facebook;
 
-       ///FormFactoryInterface instance
+       /**
+        * FormFactoryInterface instance
+        */
        protected FormFactoryInterface $factory;
 
-       ///Image util instance
+       /**
+        * Image util instance
+        */
        protected ImageUtil $image;
 
-       ///Limit integer
-       protected int $limit;
-
-       ///Locale string
-       protected string $locale;
-
-       ///MailerInterface instance
+       /**
+        * MailerInterface instance
+        */
        protected MailerInterface $mailer;
 
-       ///EntityManagerInterface instance
+       /**
+        * EntityManagerInterface instance
+        */
        protected EntityManagerInterface $manager;
 
-       ///Modified DateTime
-       protected \DateTime $modified;
-
-       ///PackageInterface instance
+       /**
+        * PackageInterface instance
+        */
        protected PackageInterface $package;
 
-       ///Request instance
+       /**
+        * Request instance
+        */
        protected Request $request;
 
-       ///Route string
-       protected string $route;
-
-       ///Route params array
-       protected array $routeParams;
-
-       ///Router instance
+       /**
+        * Router instance
+        */
        protected RouterInterface $router;
 
-       ///Slugger util instance
+       /**
+        * Slugger util instance
+        */
        protected SluggerUtil $slugger;
 
-       ///Security instance
+       /**
+        * Security instance
+        */
        protected Security $security;
 
-       ///RequestStack instance
+       /**
+        * RequestStack instance
+        */
        protected RequestStack $stack;
 
-       ///Translator instance
+       /**
+        * Translator instance
+        */
        protected TranslatorInterface $translator;
 
-       ///Twig\Environment instance
+       /**
+        * Twig\Environment instance
+        */
        protected Environment $twig;
 
        /**
@@ -199,7 +255,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                $this->twig = $twig;
 
                //Get main request
-               $this->request = $this->stack->getMainRequest();
+               $this->request = $this->stack->getCurrentRequest();
 
                //Get current locale
                $this->locale = $this->request->getLocale();
@@ -210,6 +266,14 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                //Set alternates
                $alternates = [];
 
+               //Get current page
+               $this->page = (int) $this->request->query->get('page');
+
+               //With negative page
+               if ($this->page < 0) {
+                       $this->page = 0;
+               }
+
                //Set route
                //TODO: default to not found route ???
                //TODO: when url not found, this attribute is not defined, how do we handle it ???
@@ -230,45 +294,11 @@ abstract class AbstractController extends BaseAbstractController implements Serv
 
                //Set the context
                $this->context = [
-                       //TODO: review the structure
-                       #'title' => $this->translator->trans($this->config['title']),
-                       #'og:site_name' => $this->translator->trans($this->config['title']),
-                       #'site' => [
-                       #       'donate' => $this->config['donate'],
-                       #       'title' => $title = $this->translator->trans($this->config['site']['title']),
-                       #],
-                       'head' => [
-                               'alternates' => $alternates,
-                               'canonical' => $canonical,
-                               'icon' => $this->config['icon'],
-                               'keywords' => null,
-                               'locale' => str_replace('_', '-', $this->locale),
-                               'logo' => [
-                                       'png' => $this->config['logo']['png'],
-                                       'svg' => $this->config['logo']['svg'],
-                                       'alt' => $this->translator->trans($this->config['logo']['alt'])
-                               ],
-                               'root' => $this->config['root'],
-                               'title' => null,
-                               'facebook' => [
-                                               'og:type' => 'article',
-                                               'og:site_name' => $this->translator->trans($this->config['title']),
-                                               'og:url' => $canonical,
-                                               //TODO: review this value
-                                               'fb:app_id' => $this->config['facebook']['apps']
-                               ],
-                               'fbimage' => [
-                                       'texts' => [
-                                               $this->translator->trans($this->config['title']) => [
-                                                       'font' => 'irishgrover',
-                                                       'size' => 110
-                                               ]
-                                       ]
-                               ]
-                       ],
+                       'alternates' => $alternates,
+                       'canonical' => $canonical,
                        'contact' => [
-                               'name' => $this->translator->trans($this->config['contact']['name']),
-                               'mail' => $this->config['contact']['mail']
+                               'address' => $this->config['contact']['address'],
+                               'name' => $this->translator->trans($this->config['contact']['name'])
                        ],
                        'copy' => [
                                'by' => $this->translator->trans($this->config['copy']['by']),
@@ -277,20 +307,41 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                'short' => $this->translator->trans($this->config['copy']['short']),
                                'title' => $this->config['copy']['title']
                        ],
-                       'forms' => [],
-                       'title' => null,
                        'description' => null,
-                       'section' => null,
-                       'site' => [
-                               'icon' => $this->config['icon'],
-                               'logo' => [
-                                       'png' => $this->config['logo']['png'],
-                                       'svg' => $this->config['logo']['svg'],
-                                       'alt' => $this->translator->trans($this->config['logo']['alt'])
-                               ],
-                               'path' => $this->config['path'],
-                               'root' => $this->config['root'],
-                               'title' => $this->translator->trans($this->config['title'])
+                       'donate' => $this->config['donate'],
+                       'facebook' => [
+                               'og:type' => 'article',
+                               'og:site_name' => $title = $this->translator->trans($this->config['title']),
+                               'og:url' => $canonical,
+                               #'fb:admins' => $this->config['facebook']['admins'],
+                               'fb:app_id' => $this->config['facebook']['apps']
+                       ],
+                       //XXX: TODO: only generate it when fb robot request the url ???
+                       'fbimage' => [
+                               'texts' => [
+                                       $title => [
+                                               //'font' => 'irishgrover',
+                                               //'size' => 110
+                                       ]
+                               ]
+                       ],
+                       'forms' => [],
+                       'icon' => $this->config['icon'],
+                       'keywords' => null,
+                       'locale' => str_replace('_', '-', $this->locale),
+                       #'logo' => [
+                       #       'png' => $this->config['logo']['png'],
+                       #       'svg' => $this->config['logo']['svg'],
+                       #       'alt' => $this->translator->trans($this->config['logo']['alt'])
+                       #],
+                       'logo' => $this->config['logo'],
+                       'next' => null,
+                       'prev' => null,
+                       'root' => $this->config['root'],
+                       'title' => [
+                               'page' => null,
+                               'section' => null,
+                               'site' => $title
                        ]
                ];
        }
@@ -305,7 +356,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                $response ??= new Response();
 
                //Without alternates
-               if (empty($parameters['head']['alternates'])) {
+               if (count($parameters['alternates']) <= 1) {
                        //Iterate on locales excluding current one
                        foreach($this->config['locales'] as $locale) {
                                //Set routeParams
@@ -330,7 +381,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                        }
 
                                        //Set locale locales context
-                                       $parameters['head']['alternates'][str_replace('_', '-', $locale)] = [
+                                       $parameters['alternates'][str_replace('_', '-', $locale)] = [
                                                'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL),
                                                'relative' => $this->router->generate($this->route, $routeParams),
                                                'title' => implode('/', $titles),
@@ -338,12 +389,12 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                        ];
 
                                        //Add shorter locale
-                                       if (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
+                                       if (empty($parameters['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
                                                //Set locale locales context
-                                               $parameters['head']['alternates'][$shortCurrent] = $parameters['head']['alternates'][str_replace('_', '-', $locale)];
+                                               $parameters['alternates'][$shortCurrent] = $parameters['alternates'][str_replace('_', '-', $locale)];
                                        }
                                //Add shorter locale
-                               } elseif (empty($parameters['head']['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
+                               } elseif (empty($parameters['alternates'][$shortCurrent = substr($locale, 0, 2)])) {
                                        //Set titles
                                        $titles = [];
 
@@ -357,7 +408,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                        }
 
                                        //Set locale locales context
-                                       $parameters['head']['alternates'][$shortCurrent] = [
+                                       $parameters['alternates'][$shortCurrent] = [
                                                'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL),
                                                'relative' => $this->router->generate($this->route, $routeParams),
                                                'title' => implode('/', $titles),
@@ -367,57 +418,63 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                        }
                }
 
-               //With empty head title and section
-               if (empty($parameters['head']['title']) && !empty($parameters['section'])) {
-                       //Set head title
-                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $this->translator->trans($this->config['title'])]);
-               //With empty head title
-               } elseif (empty($parameters['head']['title'])) {
-                       //Set head title
-                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $this->translator->trans($this->config['title'])]);
-               }
-
-               //With empty head description and description
-               if (empty($parameters['head']['description']) && !empty($parameters['description'])) {
-                       //Set head description
-                       $parameters['head']['description'] = $parameters['description'];
+               //With canonical
+               if (!empty($parameters['canonical'])) {
+                       //Set facebook url
+                       $parameters['facebook']['og:url'] = $parameters['canonical'];
                }
 
                //With empty facebook title and title
-               if (empty($parameters['head']['facebook']['og:title']) && !empty($parameters['title'])) {
+               if (empty($parameters['facebook']['og:title']) && !empty($parameters['title']['page'])) {
                        //Set facebook title
-                       $parameters['head']['facebook']['og:title'] = $parameters['title'];
+                       $parameters['facebook']['og:title'] = $parameters['title']['page'];
                }
 
                //With empty facebook description and description
-               if (empty($parameters['head']['facebook']['og:description']) && !empty($parameters['description'])) {
+               if (empty($parameters['facebook']['og:description']) && !empty($parameters['description'])) {
                        //Set facebook description
-                       $parameters['head']['facebook']['og:description'] = $parameters['description'];
+                       $parameters['facebook']['og:description'] = $parameters['description'];
                }
 
                //With locale
                if (!empty($this->locale)) {
                        //Set facebook locale
-                       $parameters['head']['facebook']['og:locale'] = $this->locale;
+                       $parameters['facebook']['og:locale'] = $this->locale;
 
                        //With alternates
                        //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber
                        //XXX: see https://stackoverflow.com/questions/20827882/in-open-graph-markup-whats-the-use-of-oglocalealternate-without-the-locati
-                       if (!empty($parameters['head']['alternates'])) {
+                       if (!empty($parameters['alternates'])) {
                                //Iterate on alternates
-                               foreach($parameters['head']['alternates'] as $lang => $alternate) {
+                               foreach($parameters['alternates'] as $lang => $alternate) {
                                        if (strlen($lang) == 5) {
                                                //Set facebook locale alternate
-                                               $parameters['head']['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
+                                               $parameters['facebook']['og:locale:alternate'] = str_replace('-', '_', $lang);
                                        }
                                }
                        }
                }
 
                //Without facebook image defined and texts
-               if (empty($parameters['head']['facebook']['og:image']) && !empty($this->request) && !empty($parameters['head']['fbimage']['texts']) && !empty($this->modified)) {
+               if (empty($parameters['facebook']['og:image']) && !empty($this->request) && !empty($parameters['fbimage']['texts']) && !empty($this->modified)) {
                        //Get facebook image
-                       $parameters['head']['facebook'] += $this->facebook->getImage($this->request->getPathInfo(), $parameters['head']['fbimage']['texts'], $this->modified->getTimestamp());
+                       //XXX: decode getPathInfo (see https://github.com/symfony/symfony/issues/2579)
+                       $parameters['facebook'] += $this->image->getFacebook(urldecode($this->request->getPathInfo()), $parameters['fbimage']['texts'], $this->modified->getTimestamp());
+               }
+
+               //With count
+               if (!empty($this->count)) {
+                       //With prev link
+                       if ($this->page > 0) {
+                               //Set head prev
+                               $parameters['prev'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page - 1]+$this->request->get('_route_params'));
+                       }
+
+                       //With next link
+                       if ($this->count > ($this->page + 1) * $this->limit) {
+                               //Set head next
+                               $parameters['next'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page + 1]+$this->request->get('_route_params'));
+                       }
                }
 
                //Call twig render method
@@ -466,23 +523,4 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                        'twig' => Environment::class,
                ];
        }
-
-       /**
-        * Get a user from the Security Helper.
-        *
-        * @throws \LogicException If SecurityBundle is not available
-        *
-        * @see TokenInterface::getUser()
-        * @see https://github.com/symfony/symfony/issues/44735
-        * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
-        */
-       protected function getUser(): ?UserInterface {
-               //Without token
-               if (null === ($token = $this->security->getToken())) {
-                       return null;
-               }
-
-               //With token
-               return $token->getUser();
-       }
 }