| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Nono; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Because everyone and their grandmother rolls his own framework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Since I'm lazy this is rather minimal. =) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param Router    $router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param Request   $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Container $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         Router $router = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         Request $request = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         Container $container = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->router = $router ?: new Router(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->request = $request ?: new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->container = $container ?: new Container(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->container['app'] = $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->container['request'] = $this->request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return Container | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function container() | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         return $this->container; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string          $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param \Closure|string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function get($route, $action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->router->add('GET', $route, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string          $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param \Closure|string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function post($route, $action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->router->add('POST', $route, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string          $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param \Closure|string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function put($route, $action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->router->add('PUT', $route, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string          $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param \Closure|string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function delete($route, $action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->router->add('DELETE', $route, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param array           $verbs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string          $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param \Closure|string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function any(array $verbs, $route, $action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->router->any($verbs, $route, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Send content to browser. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function respond() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         echo $this->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Return response contents. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             list($action, $params) = $this->router->route( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $this->request->method(), $this->request->uri() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $params[0] = $this->request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $this->call($action, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $this->handleException($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return ob_get_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param \Closure|string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param array           $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     private function call($action, $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if ($action instanceof \Closure) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $action(...$params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } elseif (is_string($action) && strpos($action, '::')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             list($class, $method) = explode('::', $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             if (class_exists($class) && method_exists($class, $method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 (new $class($this->container))->$method(...$params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 throw new \Exception("Failed to call {$action}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             throw new \Exception('Failed to call callable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * Just echo the message for now. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param \Exception $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     private function handleException(\Exception $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         echo $e->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  |