vendor/sonata-project/admin-bundle/src/Action/DashboardAction.php line 66

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\Action;
  12. use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Twig\Environment;
  16. final class DashboardAction
  17. {
  18.     /**
  19.      * @var array<array<string, mixed>>
  20.      */
  21.     private array $dashboardBlocks = [];
  22.     private TemplateRegistryInterface $templateRegistry;
  23.     private Environment $twig;
  24.     /**
  25.      * @param array<array<string, mixed>> $dashboardBlocks
  26.      */
  27.     public function __construct(
  28.         array $dashboardBlocks,
  29.         TemplateRegistryInterface $templateRegistry,
  30.         Environment $twig
  31.     ) {
  32.         $this->dashboardBlocks $dashboardBlocks;
  33.         $this->templateRegistry $templateRegistry;
  34.         $this->twig $twig;
  35.     }
  36.     public function __invoke(Request $request): Response
  37.     {
  38.         $blocks = [
  39.             'top' => [],
  40.             'left' => [],
  41.             'center' => [],
  42.             'right' => [],
  43.             'bottom' => [],
  44.         ];
  45.         foreach ($this->dashboardBlocks as $block) {
  46.             $blocks[$block['position']][] = $block;
  47.         }
  48.         $parameters = [
  49.             'base_template' => $request->isXmlHttpRequest() ?
  50.                 $this->templateRegistry->getTemplate('ajax') :
  51.                 $this->templateRegistry->getTemplate('layout'),
  52.             'blocks' => $blocks,
  53.         ];
  54.         return new Response($this->twig->render($this->templateRegistry->getTemplate('dashboard'), $parameters));
  55.     }
  56. }