src/Controller/Abstract/MainController.php line 121

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Abstract;
  3. use App\Service\Abstract\Traits\BasicData;
  4. use App\Service\Abstract\Traits\Main;
  5. use App\Service\ModulesManager;
  6. use App\Service\Utils\Options;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Asset\Package;
  9. use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. abstract class MainController extends AbstractController
  17. {
  18.     use Main {
  19.         Main::__construct as private mainConstruct;
  20.     }
  21.     use BasicData;
  22.     /**
  23.      * @var ModulesManager
  24.      */
  25.     protected ModulesManager $moduleManager;
  26.     /**
  27.      * @param EntityManagerInterface $em
  28.      * @param Security               $security
  29.      * @param Options                $options
  30.      * @param UrlGeneratorInterface  $router
  31.      * @param ModulesManager         $moduleManager
  32.      */
  33.     public function __construct(
  34.         EntityManagerInterface $em,
  35.         Security $security,
  36.         Options $options,
  37.         UrlGeneratorInterface $router,
  38.         ModulesManager $moduleManager
  39.     ) {
  40.         $this->mainConstruct($em$security$options$router);
  41.         $this->moduleManager $moduleManager;
  42.     }
  43.     /**
  44.      * @param Request     $request
  45.      * @param string|null $view
  46.      * @param array       $params
  47.      * @param array       $globalParams
  48.      * @param int         $statusCode
  49.      *
  50.      * @return Response
  51.      */
  52.     public function response(
  53.         Request $request,
  54.         ?string $view,
  55.         array $params = [],
  56.         array $globalParams = [],
  57.         int $statusCode 200,
  58.         bool $breakComponenets false
  59.     ): Response {
  60.         if ($view === null) {
  61.             $view $this->options->env('ERROR_404''errors\\404.html.twig');
  62.             $statusCode 404;
  63.         } elseif ($statusCode == 403) {
  64.             $view $this->options->env('ERROR_403''errors\\403.html.twig');
  65.         }
  66.         $globalVariables array_merge($this->options->globalVariables(), [
  67.             'route'      => $request->get('_route'),
  68.             'statusCode' => $statusCode,
  69.         ], $globalParams);
  70.         if (isset($params['fetch']) && is_array($params['fetch'])) {
  71.             $globalVariables array_merge($globalVariables$this->loadFromFetch($params['fetch']));
  72.         } else {
  73.             $globalVariables array_merge($globalVariables$this->loadDefaults());
  74.         }
  75.         $globalVariables['content'] = $this->renderView($viewarray_merge($globalVariables$params));
  76.         if ($request->getRealMethod() == 'POST') {
  77.             return new JsonResponse($globalVariables);
  78.         }
  79.         if ($statusCode !== 200) {
  80.             return $this->render($view$globalVariables)
  81.                         ->setStatusCode($statusCode);
  82.         } elseif ($breakComponenets) {
  83.             return $this->render($view$globalVariables);
  84.         }
  85.         $globalVariables['components'] = [
  86.             'menu' => $this->getMenu(),
  87.         ];
  88.         return $this->render($this->getBaseTwig(), $globalVariables)
  89.                     ->setStatusCode($statusCode);
  90.     }
  91.     /**
  92.      * @return string
  93.      */
  94.     private function getBaseTwig(): string
  95.     {
  96.         if ($this->getUser()) {
  97.             return 'main\\user\\base.html.twig';
  98.         }
  99.         return 'main\\website\\base.html.twig';
  100.     }
  101.     /**
  102.      * @return array
  103.      */
  104.     public function getMenu(): array
  105.     {
  106.         $links $this->moduleManager->getAllModules(true);
  107.         $links array_combine(array_column($links'id'), $links);
  108.         $child = [];
  109.         foreach ($links as $k => &$item) {
  110.             $item['classes'] = [$item['route']];
  111.             if (empty($item['parent'])) {
  112.                 $item['parent'] = 0;
  113.             } else {
  114.                 $links[$item['parent']]['classes'][$k] = $item['route'];
  115.             }
  116.             $child[$item['parent']][$k] = &$item;
  117.         }
  118.         unset($item);
  119.         foreach ($links as $k => &$item) {
  120.             if (isset($child[$k])) {
  121.                 $item['child'] = $child[$k];
  122.                 $item['classes'] = ($item['classes'] + array_column($child[$k], 'classes')[0]);
  123.             }
  124.         }
  125.         $links $child[0] ?? [];
  126.         uasort($links, function ($a$b) {
  127.             return $a['menu_position'] <=> $b['menu_position'];
  128.         });
  129.         return $links;
  130.     }
  131.     /**
  132.      * @param array $fetch
  133.      *
  134.      * @return array
  135.      */
  136.     private function loadFromFetch(array $fetch): array
  137.     {
  138.         $options = [
  139.             'userRoles'    => 'fetchUserRoles',
  140.             'userStatuses' => 'fetchStatuses',
  141.             'logoImage'    => 'logoImage',
  142.         ];
  143.         $results = [];
  144.         if (isset($fetch['loadDefaults'])) {
  145.             $results $this->loadDefaults();
  146.         }
  147.         foreach (array_intersect_key($fetch$options) as $key => $value) {
  148.             $results[$key] = $this->{$options[$key]}(...$value);
  149.         }
  150.         return $results;
  151.     }
  152.     /**
  153.      * @return array
  154.      */
  155.     private function loadDefaults(): array
  156.     {
  157.         return [
  158.             'logoImage' => $this->logoImage(),
  159.         ];
  160.     }
  161.     /**
  162.      * @return string
  163.      */
  164.     private function logoImage(): string
  165.     {
  166.         $package = new Package(new EmptyVersionStrategy());
  167.         return $package->getUrl('/pentrucomenzi/public/assets/img/logo.png');
  168.     }
  169. }