| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the takeit/AmpHtmlBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Rafał Muszyński <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Takeit\Bundle\AmpHtmlBundle\EventListener; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Takeit\Bundle\AmpHtmlBundle\Checker\AmpSupportCheckerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class AmpRouteListener | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var AmpSupportCheckerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $checker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var ControllerResolverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $controllerResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * AmpRouteListener constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param AmpSupportCheckerInterface  $checker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param ControllerResolverInterface $controllerResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         AmpSupportCheckerInterface $checker, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ControllerResolverInterface $controllerResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->checker = $checker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->controllerResolver = $controllerResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->controller = $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param FilterControllerEvent $event | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     public function onKernelController(FilterControllerEvent $event) | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         $request = $event->getRequest(); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         if ($request->query->has('amp') && !$request->isXmlHttpRequest() && $this->checker->isEnabled()) { | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |             $request->attributes->set('_controller', $this->controller); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |             $controller = $this->controllerResolver->getController($request); | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             if (!$controller) { | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |                 throw new \LogicException('Controller can not be determined!'); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             $event->setController($controller); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  |