| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Fabiang\AsseticBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Laminas\EventManager\AbstractListenerAggregate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Laminas\EventManager\EventManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Laminas\Http\PhpEnvironment\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Laminas\Mvc\MvcEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Laminas\Stdlib\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function in_array; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class Listener extends AbstractListenerAggregate | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * Attach one or more listeners | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * Implementors may add an optional $priority argument; the EventManager | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 1 |  |      * implementation will pass this to the aggregate. | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |      * @param int $priority | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function attach(EventManagerInterface $events, $priority = 32): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->listeners[] = $events->attach( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |             MvcEvent::EVENT_DISPATCH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             [$this, 'renderAssets'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $priority | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->listeners[] = $events->attach( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             MvcEvent::EVENT_DISPATCH_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             [$this, 'renderAssets'], | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 | 2 |  |             $priority | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function renderAssets(MvcEvent $e): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         $sm = $e->getApplication()->getServiceManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         /** @var Configuration $config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $config = $sm->get('AsseticConfiguration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         if ($e->getName() === MvcEvent::EVENT_DISPATCH_ERROR) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $error = $e->getError(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             if ($error && ! in_array($error, $config->getAcceptableErrors())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 // break if not an acceptable error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         /** @var ResponseInterface|null $response */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $response = $e->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (! $response) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $response = new Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             $e->setResponse($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         /** @var Service $asseticService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $asseticService = $sm->get('AsseticService'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         // setup service if a matched route exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         $router = $e->getRouteMatch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($router) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $asseticService->setRouteName($router->getMatchedRouteName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $asseticService->setControllerName($router->getParam('controller')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             $asseticService->setActionName($router->getParam('action')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         // Create all objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $asseticService->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Init assets for modules | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |         $asseticService->setupRenderer($sm->get('ViewRenderer')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |