示例#1
0
 public function testRouteNotAllowedByIp()
 {
     $this->router->setPath('/auth/ip/allow');
     $this->router->setMethod('GET');
     $this->router->setIp('111.111.111.111');
     try {
         $this->router->getRoute();
     } catch (\Exception $e) {
         $this->assertEquals($e->getMessage(), 'Route for path not found.');
         return;
     }
     $this->fail('Failed asserting Exception for invalid route.');
 }
示例#2
0
 /**
  * Run the application.
  */
 public function run()
 {
     if (empty($this->packagesMetadata)) {
         $this->setup();
         if (empty($this->packagesMetadata)) {
             // @codeCoverageIgnoreStart
             $e = new PackagesNotFoundException('Application has no packages.');
             $e->setAdditionalData('Application Path', $this->appDirectory);
             $this->error($e);
             return;
             // @codeCoverageIgnoreEnd
         }
     }
     $this->router = new Router();
     $this->router->setRoutes($this->routes);
     $this->router->setMethod($this->request->getRequestMethod());
     $this->router->setPath($this->request->getRequestUri());
     $this->router->setIp($this->request->getClientIp());
     try {
         $route = $this->router->getRoute();
     } catch (RouteNotFoundException $e) {
         $this->notFound($e);
         return;
     }
     $middleware = false;
     if ($this->hasMiddleware()) {
         $middleware = new MiddlewareProcessor($this->config['Middleware']);
     }
     $package = $this->getPackage($route['package']);
     $runnable = $route['runnable'];
     $runnables = explode('::', $runnable);
     // Check if it's a view script only.
     if ($route['view.directRender'] === true) {
         try {
             if ($middleware) {
                 $middleware->runBefore($this->request, $this->response);
             }
             $view = $this->getViewFor($package['package_name'], $runnables[0], $runnables[1]);
             if ($route['view.layout'] !== false) {
                 $view->setLayout($route['view.layout']);
             }
             $view->setResponse($this->response);
             $view->render();
             if ($middleware) {
                 $middleware->runAfter();
             }
             return;
             // @codeCoverageIgnoreStart
         } catch (\Exception $e) {
             $this->error($e);
             return;
         }
     }
     $classString = $package['namespace'] . '\\src\\' . $runnables[0];
     if (class_exists($classString)) {
         try {
             if ($middleware) {
                 $middleware->runBefore($this->request, $this->response);
             }
             $instance = $this->getRunnable($package['package_name'], $runnables[0], $runnables[1], $classString);
             if (is_callable(array($instance, $runnables[1]))) {
                 if ($route['view.layout'] !== false) {
                     $instance->getView()->setLayout($route['view.layout']);
                 }
                 call_user_func_array(array($instance, $runnables[1]), $route['params']);
                 $instance->finalise();
                 if ($middleware) {
                     $middleware->runAfter();
                 }
                 return;
             }
             // @codeCoverageIgnoreStart
         } catch (\Exception $e) {
             $this->error($e);
             return;
         }
         $this->notFound(new RunnableNotFoundException('No such runnable: ' . $classString . '::' . $runnables[1]));
     }
     $this->notFound(new RunnableNotFoundException('No such runnable: ' . $classString . '::' . $runnables[1]));
 }