Пример #1
0
 public function testRequestObserver()
 {
     $config = new Collection();
     $config->set('socket', '\\Juriya\\Controller');
     $request = new Request($config);
     $request->attach(new DummyObserver());
     ob_start();
     $request->execute();
     $this->assertEquals('Dummy observer now receive ' . get_class($request), ob_get_clean());
 }
Пример #2
0
 public function testDefaultExecuteCli()
 {
     $request = Request::factory('undefined.controller');
     $controller = new Controller($request);
     $this->assertInstanceOf('\\Juriya\\ResponseCli', $controller->executeCli());
 }
Пример #3
0
 /**
  * Handle a Request and return the Response
  *
  * @param  Request  Instance of Juriya\Input
  * @return Response Instance of Juriya\Output
  * @throws object RuntimeException
  * @throws object RangeException 
  * @codeCoverageIgnore 
  */
 public function handleRequest(Request $request)
 {
     // Check whether socket instance is ready
     if (is_null($request->getSocket())) {
         // Try to transform path into socket name
         try {
             $socket = Request::factory(implode('.', $request->getPath()), $request->getArguments(), $request->getTunnel())->getSocket();
         } catch (\Exception $e) {
             throw new \RuntimeException($e->getMessage());
         }
         if (class_exists($socket)) {
             $controllerClass = $socket;
             $arguments = array();
         } elseif (($candidates = explode('\\', $socket)) && ($rawSocket = end($candidates)) && class_exists($rawSocket, FALSE)) {
             $controllerClass = $rawSocket;
             $arguments = array();
         } else {
             // Check router configuration
             $routes = $this->getRoutes();
             $path = $request->getPath();
             if (empty($path)) {
                 // Lookup default routes
                 $defaultRoute = $routes->get(Juriya::APPLICATION . '.default.controller');
                 if (empty($defaultRoute)) {
                     throw new \RuntimeException(I18n::translate('route_default_empty'));
                 }
                 $controllerClass = $defaultRoute;
                 $arguments = array();
             } else {
                 // Dispatch request from application routes
                 $dispatchResult = self::dispatchRequestPathWithRoutes($routes, $path);
                 $isFound = $dispatchResult->get('found');
                 // If none of routes configuration match, do our best
                 if (!$isFound) {
                     throw new \RangeException(I18n::translate('route_not_found', implode('/', $path)));
                 } else {
                     $controllerClass = $dispatchResult->get('controllerClass');
                     $arguments = $dispatchResult->get('arguments');
                 }
             }
         }
     } else {
         $controllerClass = $request->getSocket();
         $arguments = $request->getArguments();
     }
     // Prepare the socket and corresponding executor
     $controller = new $controllerClass($request);
     $executor = $request->getTunnel() == 'HMVC' ? 'execute' : 'execute' . ucfirst(strtolower($request->getTunnel()));
     // Validate socket first
     if (!$controller instanceof Socket) {
         throw new \RangeException(I18n::translate('invalid', 'Controller'));
     }
     $response = call_user_func_array(array($controller, $executor), $arguments);
     // Validate the response
     if (!$response instanceof Output) {
         throw new \RangeException(I18n::translate('invalid', 'Response'));
     }
     return $response;
 }