| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Anax\Page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Anax\Commons\ContainerInjectableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Anax\Commons\ContainerInjectableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * To ease rendering a page consisting of several views. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Page implements ContainerInjectableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     use ContainerInjectableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array $layout to hold tha layout view to be rendered last. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $layout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Set the view to be used for the layout. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param array $view configuration to create up the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function addLayout(array $view) : object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->layout = $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Utility method to add a view to the view collection for later | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * rendering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param array|string  $template the name of the template file to include | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      *                                or array with view details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param array         $data     variables to make available to the view, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      *                                default is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param string        $region   which region to attach the view, default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      *                                is "main". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param integer       $sort     which order to display the views. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function add( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $template, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         array $data = [], | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         string $region = "main", | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         int $sort = 0 | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     ) : object { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->di->get("view")->add($template, $data, $region, $sort); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Add the layout view to the region "layout and render all views | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * within the region "layout", and create a response from it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param array   $data   additional variables to expose to layout view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param integer $status code to use when delivering the result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function render(array $data = [], int $status = 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $view = $this->di->get("view"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $view->add($this->layout, $data, "layout"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $body = $view->renderBuffered("layout"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $response = $this->di->get("response"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $response->setBody($body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $response->setStatusCode($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |