]> Raphaƫl G. Git Repositories - blogbundle/blobdiff - Controller/AbstractController.php
Use new prev and next links
[blogbundle] / Controller / AbstractController.php
index 0bf792ad7ed572c3515ff6459d8a1a5aec38d86b..10353cbf421db5281fb3189231f89e4745624b03 100644 (file)
@@ -15,21 +15,23 @@ use Doctrine\ORM\EntityManagerInterface;
 use Doctrine\Persistence\ManagerRegistry;
 use Psr\Log\LoggerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController;
-use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
+use Symfony\Bundle\SecurityBundle\Security;
 use Symfony\Component\Asset\PackageInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
 use Symfony\Component\Filesystem\Filesystem;
 use Symfony\Component\Form\FormFactoryInterface;
-use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Mailer\MailerInterface;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Routing\RouterInterface;
 use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
-use Symfony\Contracts\Translation\TranslatorInterface;
+use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
+use Symfony\Component\Security\Core\User\UserInterface;
 use Symfony\Contracts\Service\ServiceSubscriberInterface;
+use Symfony\Contracts\Translation\TranslatorInterface;
 use Twig\Environment;
 
 use Rapsys\BlogBundle\Entity\Dance;
@@ -48,70 +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;
 
-       ///RequestStack instance
+       /**
+        * Security instance
+        */
+       protected Security $security;
+
+       /**
+        * RequestStack instance
+        */
        protected RequestStack $stack;
 
-       ///Translator instance
+       /**
+        * Translator instance
+        */
        protected TranslatorInterface $translator;
 
-       ///Twig\Environment instance
+       /**
+        * Twig\Environment instance
+        */
        protected Environment $twig;
 
        /**
@@ -129,6 +190,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
         * @param PackageInterface $package The package instance
         * @param RouterInterface $router The router instance
         * @param SluggerUtil $slugger The slugger instance
+        * @param Security $security The security instance
         * @param RequestStack $stack The stack instance
         * @param TranslatorInterface $translator The translator instance
         * @param Environment $twig The twig environment instance
@@ -137,7 +199,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
         * @TODO move all that stuff to setSlugger('@slugger') setters with a calls: [ setSlugger: [ '@slugger' ] ] to unbload classes ???
         * @TODO add a calls: [ ..., prepare: ['@???'] ] that do all the logic that can't be done in constructor because various things are not available
         */
-       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, RequestStack $stack, TranslatorInterface $translator, Environment $twig, int $limit = 5) {
+       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) {
                //Set checker
                $this->checker = $checker;
 
@@ -180,6 +242,9 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                //Set slugger
                $this->slugger = $slugger;
 
+               //Set security
+               $this->security = $security;
+
                //Set stack
                $this->stack = $stack;
 
@@ -190,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();
@@ -201,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 ???
@@ -221,13 +294,6 @@ 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,
@@ -240,6 +306,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                        'alt' => $this->translator->trans($this->config['logo']['alt'])
                                ],
                                'root' => $this->config['root'],
+                               'site' => $this->translator->trans($this->config['title']),
                                'title' => null,
                                'facebook' => [
                                                'og:type' => 'article',
@@ -258,8 +325,8 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                ]
                        ],
                        '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']),
@@ -269,20 +336,9 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                                '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'])
-                       ]
+                       'title' => null
                ];
        }
 
@@ -293,7 +349,7 @@ abstract class AbstractController extends BaseAbstractController implements Serv
         */
        protected function render(string $view, array $parameters = [], Response $response = null): Response {
                //Create response when null
-        $response ??= new Response();
+               $response ??= new Response();
 
                //Without alternates
                if (empty($parameters['head']['alternates'])) {
@@ -361,11 +417,11 @@ 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'])]);
+                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]);
                //With empty head title
                } elseif (empty($parameters['head']['title'])) {
                        //Set head title
-                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $this->translator->trans($this->config['title'])]);
+                       $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['head']['site']]);
                }
 
                //With empty head description and description
@@ -405,6 +461,21 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                        }
                }
 
+               //With count
+               if (!empty($this->count)) {
+                       //With prev link
+                       if ($this->page > 0) {
+                               //Set head prev
+                               $parameters['head']['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['head']['next'] = $this->generateUrl($this->request->get('_route'), ['page' => $this->page + 1]+$this->request->get('_route_params'));
+                       }
+               }
+
                //Without facebook image defined and texts
                if (empty($parameters['head']['facebook']['og:image']) && !empty($this->request) && !empty($parameters['head']['fbimage']['texts']) && !empty($this->modified)) {
                        //Get facebook image
@@ -415,20 +486,20 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                $content = $this->twig->render($view, $parameters);
 
                //Invalidate OK response on invalid form
-        if (200 === $response->getStatusCode()) {
-            foreach ($parameters as $v) {
-                if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
-                    $response->setStatusCode(422);
-                    break;
-                }
-            }
-        }
+               if (200 === $response->getStatusCode()) {
+                       foreach ($parameters as $v) {
+                               if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
+                                       $response->setStatusCode(422);
+                                       break;
+                               }
+                       }
+               }
 
                //Store content in response
-        $response->setContent($content);
+               $response->setContent($content);
 
                //Return response
-        return $response;
+               return $response;
        }
 
        /**
@@ -451,7 +522,8 @@ abstract class AbstractController extends BaseAbstractController implements Serv
                        'rapsys_pack.path_package' => PackageInterface::class,
                        'router' => RouterInterface::class,
                        'rapsys_pack.slugger_util' => SluggerUtil::class,
-                       'request_stack' => RequestStack::class,
+                       'security' => Security::class,
+                       'stack' => RequestStack::class,
                        'translator' => TranslatorInterface::class,
                        'twig' => Environment::class,
                ];