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

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.      * @param array<array<string, mixed>> $dashboardBlocks
  20.      */
  21.     public function __construct(
  22.         private array $dashboardBlocks,
  23.         private TemplateRegistryInterface $templateRegistry,
  24.         private Environment $twig,
  25.     ) {
  26.     }
  27.     public function __invoke(Request $request): Response
  28.     {
  29.         $blocks = [
  30.             'top' => [],
  31.             'left' => [],
  32.             'center' => [],
  33.             'right' => [],
  34.             'bottom' => [],
  35.         ];
  36.         foreach ($this->dashboardBlocks as $block) {
  37.             $blocks[$block['position']][] = $block;
  38.         }
  39.         $parameters = [
  40.             'base_template' => $request->isXmlHttpRequest() ?
  41.                 $this->templateRegistry->getTemplate('ajax') :
  42.                 $this->templateRegistry->getTemplate('layout'),
  43.             'blocks' => $blocks,
  44.         ];
  45.         return new Response($this->twig->render($this->templateRegistry->getTemplate('dashboard'), $parameters));
  46.     }
  47. }