| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * (c) FSi sp. z o.o. <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace FSi\Bundle\AdminBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use FSi\Bundle\AdminBundle\Admin\Context\ContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use FSi\Bundle\AdminBundle\Admin\Context\ContextManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use FSi\Bundle\AdminBundle\Admin\Element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use FSi\Bundle\AdminBundle\Event\AdminEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use FSi\Bundle\AdminBundle\Event\AdminEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use FSi\Bundle\AdminBundle\Exception\ContextException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | abstract class ControllerAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var EngineInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $templating; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var ContextManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $contextManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var EventDispatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $contextManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string|null $resourceActionTemplate | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         EngineInterface $templating, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ContextManager $contextManager, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         EventDispatcherInterface $eventDispatcher, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $resourceActionTemplate = null | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->templating = $templating; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->contextManager = $contextManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->eventDispatcher = $eventDispatcher; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->template = $resourceActionTemplate; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param \FSi\Bundle\AdminBundle\Admin\Element $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param \Symfony\Component\HttpFoundation\Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return null|\Symfony\Component\HttpFoundation\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected function handleRequest(Element $element, Request $request, $route) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $event = new AdminEvent($element, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->eventDispatcher->dispatch(AdminEvents::CONTEXT_PRE_CREATE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($event->hasResponse()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return $event->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $context = $this->contextManager->createContext($route, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (!($context instanceof ContextInterface)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             throw new NotFoundHttpException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'Cannot find context builder that supports element with id "%s"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $element->getId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $response = $context->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if ($response instanceof Response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (!isset($this->template) && !$context->hasTemplateName()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             throw new ContextException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 "Context %s did not return any response and controller %s has no template", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 get_class($context), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 __CLASS__ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $this->templating->renderResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $context->hasTemplateName() ? $context->getTemplateName() : $this->template, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $context->getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |