I am currently in the process of developing an open source RMR framework. At the moment a resource representation is loaded and parsed in the resource itself like in the following example (this is at abstraction level and some parts cut out to shorten this post):
/**
* Processes a given request and returns a response. Makes use of a
* data source for data retrieval and an array of parameters
* (usually passed in from the route).
*
* @param Request $request
* @param mixed $dataSource
* @param array $parameters
* @return Response
*/
public function processRequest(Request $request, $dataSource, array $parameters)
{
$method = strtolower($request->getMethod());
$this->dataSource = $dataSource;
$this->parameters = $parameters;
$this->request = $request;
if(method_exists($this, $method))
{
// Something like get, post, head in resources
$data = $this->$method();
$response = new Response(StatusCode::HTTP_OK);
if(empty($data))
{
// Do some stuff, return 200 ok no message-body etc.
}
// Find preferred media type in request accept
$mediaType = $request->getMediaType();
// .. loop through find representation ..
// Representation is reponsible for adding its own headers and body to
// response, does this make sense to you?
$representation->parse($response, $data);
return $response;
}
else
{
return new Response(StatusCode::HTTP_METHOD_NOT_ALLOWED);
}
}
So my question really is does it make sense to let the resource handle loading the representation of itself and then the representation setting response headers and body?
What would be your recommended approach?
Leave that to a
RepresentationResolverwhich operates on Request, Response and Resource. Keep classes small and distinct.Then when you compile the response:
You can then code any type or
RepresentationResolveryou see fit, even families of such, e.g. working with a specific request type or for specific sort of media type you specify (e.g. by mime-type).