| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\AdminBundle\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\AdminBundle\Admin\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sonata\AdminBundle\Templating\TemplateRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sonata\AdminBundle\Templating\TemplateRegistryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sonata\BlockBundle\Block\BlockContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sonata\BlockBundle\Block\Service\AbstractBlockService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Twig\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @final since sonata-project/admin-bundle 3.52 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @author Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class AdminListBlockService extends AbstractBlockService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var Pool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var TemplateRegistryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $templateRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * NEXT_MAJOR: Remove `$deprecatedTemplatingOrNull` argument. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param Environment|string $twigOrDeprecatedName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $twigOrDeprecatedName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ?EngineInterface $deprecatedTemplatingOrNull, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Pool $pool, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ?TemplateRegistryInterface $templateRegistry = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         // NEXT_MAJOR: Remove conditions, only pass the Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($deprecatedTemplatingOrNull instanceof EngineInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             @trigger_error(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 . ' and will throw a \TypeError in version 4.0. You must pass null instead.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 EngineInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 __METHOD__ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ), E_USER_DEPRECATED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             if (!\is_string($twigOrDeprecatedName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 throw new \TypeError(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     . ' You must pass an string instead.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     EngineInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             parent::__construct($twigOrDeprecatedName, $deprecatedTemplatingOrNull); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } elseif ($twigOrDeprecatedName instanceof Environment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if (!\is_null($deprecatedTemplatingOrNull)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 throw new \TypeError(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     Environment::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     EngineInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             parent::__construct($twigOrDeprecatedName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             throw new \TypeError(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 Environment::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->pool = $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->templateRegistry = $templateRegistry ?: new TemplateRegistry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $dashboardGroups = $this->pool->getDashboardGroups(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $settings = $blockContext->getSettings(); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $visibleGroups = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         foreach ($dashboardGroups as $name => $dashboardGroup) { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             if (!$settings['groups'] || \in_array($name, $settings['groups'], true)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 $visibleGroups[] = $dashboardGroup; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             'block' => $blockContext->getBlock(), | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             'settings' => $settings, | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             'admin_pool' => $this->pool, | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             'groups' => $visibleGroups, | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         ], $response); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return 'Admin List'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function configureSettings(OptionsResolver $resolver): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $resolver->setDefaults([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             'groups' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $resolver->setAllowedTypes('groups', ['bool', 'array']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                        
If you suppress an error, we recommend checking for the error condition explicitly: