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

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 <[email protected]>
  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\Admin\BreadcrumbsBuilderInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Twig\Environment;
  18. final class DashboardAction
  19. {
  20.     /**
  21.      * @var array
  22.      */
  23.     private $dashboardBlocks;
  24.     /**
  25.      * @var BreadcrumbsBuilderInterface
  26.      */
  27.     private $breadcrumbsBuilder;
  28.     /**
  29.      * @var TemplateRegistryInterface
  30.      */
  31.     private $templateRegistry;
  32.     /**
  33.      * @var Pool
  34.      */
  35.     private $pool;
  36.     /**
  37.      * @var Environment
  38.      */
  39.     private $twig;
  40.     public function __construct(
  41.         array $dashboardBlocks,
  42.         BreadcrumbsBuilderInterface $breadcrumbsBuilder,
  43.         TemplateRegistryInterface $templateRegistry,
  44.         Pool $pool,
  45.         Environment $twig
  46.     ) {
  47.         $this->dashboardBlocks $dashboardBlocks;
  48.         $this->breadcrumbsBuilder $breadcrumbsBuilder;
  49.         $this->templateRegistry $templateRegistry;
  50.         $this->pool $pool;
  51.         $this->twig $twig;
  52.     }
  53.     public function __invoke(Request $request): Response
  54.     {
  55.         $blocks = [
  56.             'top' => [],
  57.             'left' => [],
  58.             'center' => [],
  59.             'right' => [],
  60.             'bottom' => [],
  61.         ];
  62.         foreach ($this->dashboardBlocks as $block) {
  63.             $blocks[$block['position']][] = $block;
  64.         }
  65.         $parameters = [
  66.             'base_template' => $request->isXmlHttpRequest() ?
  67.                 $this->templateRegistry->getTemplate('ajax') :
  68.                 $this->templateRegistry->getTemplate('layout'),
  69.             'admin_pool' => $this->pool,
  70.             'blocks' => $blocks,
  71.         ];
  72.         if (!$request->isXmlHttpRequest()) {
  73.             $parameters['breadcrumbs_builder'] = $this->breadcrumbsBuilder;
  74.         }
  75.         return new Response($this->twig->render($this->templateRegistry->getTemplate('dashboard'), $parameters));
  76.     }
  77. }