| 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\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\AdminBundle\Admin\AdminHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sonata\AdminBundle\Admin\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Bridge\Twig\AppVariable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Bridge\Twig\Command\DebugCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Bridge\Twig\Extension\FormExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Bridge\Twig\Form\TwigRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Form\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Form\FormRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Twig\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | final class AppendFormFieldElementAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var Pool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var AdminHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $twig; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct(Environment $twig, Pool $pool, AdminHelper $helper) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->pool = $pool; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->helper = $helper; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->twig = $twig; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @throws NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function __invoke(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $code = $request->get('code'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $elementId = $request->get('elementId'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $objectId = $request->get('objectId'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $uniqid = $request->get('uniqid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $admin = $this->pool->getInstance($code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $admin->setRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($uniqid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $admin->setUniqid($uniqid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $subject = $admin->getObject($objectId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($objectId && !$subject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             throw new NotFoundHttpException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 'Could not find subject for id "%s"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $objectId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if (!$subject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $subject = $admin->getNewInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $admin->setSubject($subject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         list(, $form) = $this->helper->appendFormFieldElement($admin, $subject, $elementId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         assert($form instanceof Form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $view = $this->helper->getChildFormView($form->createView(), $elementId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // render the widget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $renderer = $this->getFormRenderer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $renderer->setTheme($view, $admin->getFormTheme()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return new Response($renderer->searchAndRenderBlock($view, 'widget')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return FormRenderer|TwigRenderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     private function getFormRenderer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         // BC for Symfony < 3.2 where this runtime does not exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if (!method_exists(AppVariable::class, 'getToken')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $extension = $this->twig->getExtension(FormExtension::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $extension->initRuntime($this->twig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return $extension->renderer; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         // BC for Symfony < 3.4 where runtime should be TwigRenderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if (!method_exists(DebugCommand::class, 'getLoaderPaths')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $runtime = $this->twig->getRuntime(TwigRenderer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $runtime->setEnvironment($this->twig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             return $runtime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $this->twig->getRuntime(FormRenderer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: