| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author @fabfuel <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @created 14.11.14, 08:39 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Fabfuel\Prophiler\Plugin\Phalcon\Mvc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Fabfuel\Prophiler\Benchmark\BenchmarkInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Fabfuel\Prophiler\Plugin\PluginAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Phalcon\Events\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Phalcon\Mvc\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Phalcon\Mvc\ViewInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class ViewPlugin | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class ViewPlugin extends PluginAbstract implements ViewPluginInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @var BenchmarkInterface[][] | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     private $benchmarks = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * All render levels as descriptive strings | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $renderLevels = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         View::LEVEL_ACTION_VIEW => 'action', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         View::LEVEL_AFTER_TEMPLATE => 'afterTemplate', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         View::LEVEL_BEFORE_TEMPLATE => 'beforeTemplate', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         View::LEVEL_LAYOUT => 'controller', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         View::LEVEL_MAIN_LAYOUT => 'main' | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * Start view benchmark | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @param Event $event | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @param ViewInterface $view | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |     public function beforeRenderView(Event $event, ViewInterface $view) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         $name = get_class($event->getSource()) . '::render: ' . basename($view->getActiveRenderPath()); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $metadata = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |             'view' => realpath($view->getActiveRenderPath()) ?: $view->getActiveRenderPath(), | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |             'level' => $this->getRenderLevel($view->getCurrentRenderLevel()), | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |         $this->setBenchmark($view, $this->getProfiler()->start($name, $metadata, 'View')); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * Stop view benchmark | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param Event $event | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param ViewInterface $view | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |     public function afterRenderView(Event $event, ViewInterface $view) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |         $benchmark = $this->getBenchmark($view); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |         $this->getProfiler()->stop($benchmark); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * Stop view benchmark | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param Event $event | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param ViewInterface $view | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 2 |  |     public function afterRender(Event $event, ViewInterface $view) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 2 |  |         foreach ($this->benchmarks as $views) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |             foreach ($views as $benchmark) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |                 $this->getProfiler()->stop($benchmark); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param int $renderLevelInt | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @return string Render level | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  |     public function getRenderLevel($renderLevelInt) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |         return isset($this->renderLevels[$renderLevelInt]) ? $this->renderLevels[$renderLevelInt] : ''; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 90 |  |  |      * @param ViewInterface $view | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @param BenchmarkInterface $benchmark | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     public function setBenchmark(ViewInterface $view, BenchmarkInterface $benchmark) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         $this->benchmarks[md5($view->getActiveRenderPath())][] = $benchmark; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @param ViewInterface $view | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @return BenchmarkInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |     public function getBenchmark(ViewInterface $view) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         return array_shift($this->benchmarks[md5($view->getActiveRenderPath())]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * @param ViewInterface $view | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     public function getIdentifier(ViewInterface $view) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         return md5($view->getActiveRenderPath()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |