Example #1
0
 public function dispatch(Route $route)
 {
     // obtain Reflection objects for the resource method selected
     $reflClass = new ReflectionClass($route->getResourceClassName());
     $reflMethod = $reflClass->getMethod($route->getResourceMethodName());
     // instantiate the selected resource class
     $resource = $this->_createResourceInstance($route->getResourceClassName());
     // inject Context variables into the resource class instance
     $contextInjections = array('Request' => $this->_request, 'UriInfo' => $this->_uriInfo);
     foreach ($route->getContexts() as $propertyName => $contextType) {
         try {
             $reflProperty = $reflClass->getProperty($propertyName);
         } catch (\ReflectionException $e) {
             continue;
         }
         $reflProperty->setAccessible(true);
         if (isset($contextInjections[$contextType])) {
             $reflProperty->setValue($resource, $contextInjections[$contextType]);
         }
     }
     // execute the pre-dispatch method on the resource class if one is
     // implemented
     if ($reflClass->hasMethod('preDispatch')) {
         $method = $reflClass->getMethod('preDispatch');
         if ($result = $method->invoke($resource)) {
             return $result;
         }
     }
     // execute the selected resource method using the generated method
     // arguments
     $methodArgs = $this->_getResourceMethodArguments($route, $reflMethod);
     try {
         $result = $reflMethod->invokeArgs($resource, $methodArgs);
         // execute the post-dispatch method on the resource class if one is
         // implemented
         if ($reflClass->hasMethod('postDispatch')) {
             $method = $reflClass->getMethod('postDispatch');
             $method->invoke($resource);
         }
         return $result;
     } catch (WebApplicationException $e) {
         return $e->getResponse();
     }
 }