Permalink
Browse files

Merge pull request #17 from thizzle/master

Various bug fixes for URI classes
  • Loading branch information...
2 parents a375814 + cb4ba7e commit a82fac83615489ae2e227024b4d532f19cc87e2a @davehauenstein committed Apr 13, 2012
@@ -145,11 +145,7 @@ public function run(RequestInterface $request)
// object is a scalar value: construct a new Response
if (is_scalar($result)) {
- $response = new Response(
- 200,
- $result,
- array('Content-Type' => $selectedVariant->getMediaType())
- );
+ $response = new Response(200, $result);
// object is already a Response
} else if ($result instanceof Response) {
@@ -158,17 +154,34 @@ public function run(RequestInterface $request)
// object implements the Renderable interface: construct a Response
// using the reprsentation produced by render()
} else if ($result instanceof Renderable) {
- $response = new Response(
- 200,
- $result->render($selectedVariant),
- array('Content-Type' => $selectedVariant->getMediaType())
- );
+ $response = new Response(200, $result->render($selectedVariant));
// cannot determine how to handle the object returned
} else {
throw new MalformedResourceRepresentationException;
}
+ // ensure a Content-Type header is present
+ if (!$response->hasHeader('Content-Type')
+ && $response->getStatusCode() < 300
+ && $response->getContent()
+ ) {
+ $response->setHeaders(
+ array(
+ 'Content-Type' => $selectedVariant->getMediaType()
+ )
+ );
+ }
+
+ // ensure a Content-Length header is present
+ if (!$response->hasHeader('Content-Length')) {
+ $response->setHeaders(
+ array(
+ 'Content-Length' => strlen($response->getContent())
+ )
+ );
+ }
+
// process any HTTP status filter callbacks
$statusCode = $response->getStatusCode();
if (isset($this->_responseFilters[$statusCode])) {
@@ -42,5 +42,5 @@ public function render(Variant $mediaType);
*
* @return object
*/
- public function unrender($representation, Variant $mediaType);
+ public static function unrender($representation, Variant $mediaType);
}
@@ -59,7 +59,7 @@ public function setBasePath($basePath)
*/
public function getBasePath()
{
- return $this->_basePath;
+ return $this->_basePath ?: '/';
}
/**
@@ -207,7 +207,7 @@ protected function _extractClassParams(ReflectionClass $class)
// Examine Annotation: @Path
$annot = $this->_reader->getClassAnnotation(
$class,
- '\Sonno\Annotation\Path'
+ 'Sonno\Annotation\Path'
);
if ($annot) {
$params['classPath'] = $annot->getPath();
@@ -216,7 +216,7 @@ protected function _extractClassParams(ReflectionClass $class)
// Examine Annotation: @Consumes
$annot = $this->_reader->getClassAnnotation(
$class,
- '\Sonno\Annotation\Consumes'
+ 'Sonno\Annotation\Consumes'
);
if ($annot) {
$params['consumes'] = $annot->getMediaTypes();
@@ -225,7 +225,7 @@ protected function _extractClassParams(ReflectionClass $class)
// Examine Annotation: @Produces
$annot = $this->_reader->getClassAnnotation(
$class,
- '\Sonno\Annotation\Produces'
+ 'Sonno\Annotation\Produces'
);
if ($annot) {
$params['produces'] = $annot->getMediaTypes();
@@ -247,7 +247,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @{HTTP_VERB}
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\HttpMethod'
+ 'Sonno\Annotation\HttpMethod'
);
if ($annot) {
$params['httpMethod'] = (string) $annot;
@@ -258,7 +258,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @Path
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\Path'
+ 'Sonno\Annotation\Path'
);
if ($annot) {
$params['methodPath'] = $annot->getPath();
@@ -267,7 +267,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @Consumes
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\Consumes'
+ 'Sonno\Annotation\Consumes'
);
if ($annot) {
$params['consumes'] = $annot->getMediaTypes();
@@ -276,7 +276,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @Produces
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\Produces'
+ 'Sonno\Annotation\Produces'
);
if ($annot) {
$params['produces'] = $annot->getMediaTypes();
@@ -285,7 +285,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @PathParam
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\PathParam'
+ 'Sonno\Annotation\PathParam'
);
if ($annot) {
$params['pathParams'] = $annot->getParams();
@@ -294,7 +294,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @CookieParam
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\CookieParam'
+ 'Sonno\Annotation\CookieParam'
);
if ($annot) {
$params['cookieParams'] = $annot->getParams();
@@ -303,7 +303,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @FormParam
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\FormParam'
+ 'Sonno\Annotation\FormParam'
);
if ($annot) {
$params['formParams'] = $annot->getParams();
@@ -312,7 +312,7 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @HeaderParam
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\HeaderParam'
+ 'Sonno\Annotation\HeaderParam'
);
if ($annot) {
$params['headerParams'] = $annot->getParams();
@@ -321,10 +321,10 @@ protected function _extractMethodParams(ReflectionMethod $method)
// Examine Annotation: @QueryParam
$annot = $this->_reader->getMethodAnnotation(
$method,
- '\Sonno\Annotation\QueryParam'
+ 'Sonno\Annotation\QueryParam'
);
if ($annot) {
- $params['queryParam'] = $annot->getParams();
+ $params['queryParams'] = $annot->getParams();
}
return $params;
@@ -343,7 +343,7 @@ public function _extractPropertyParams(ReflectionProperty $property)
// Examine Annotation: @Context
$annot = $this->_reader->getPropertyAnnotation(
$property,
- '\Sonno\Annotation\Context'
+ 'Sonno\Annotation\Context'
);
if ($annot) {
$params['contexts'] = array(
@@ -13,8 +13,9 @@
namespace Sonno\Dispatcher;
use Sonno\Application\WebApplicationException,
- Sonno\Http\Request\RequestInterface,
Sonno\Configuration\Route,
+ Sonno\Http\Request\RequestInterface,
+ Sonno\Http\Variant,
Sonno\Uri\UriInfo,
ReflectionClass,
ReflectionMethod;
@@ -155,6 +156,8 @@ protected function _getResourceMethodArguments(
ReflectionMethod $method
)
{
+ $clsRenderable = 'Sonno\Application\Renderable';
+
$pathParamValues = $this->_uriInfo->getPathParameters();
$queryParamValues = $this->_uriInfo->getQueryParameters();
$headerParamValues = $this->_request->getHeaders();
@@ -168,6 +171,21 @@ protected function _getResourceMethodArguments(
foreach ($method->getParameters() as $idx => $reflParam) {
$parameterName = $reflParam->getName();
+ $parameterClass = $reflParam->getClass();
+
+ // if the parameter is a type that implements Renderable, use the
+ // implementation's unrender() function to generate an instance
+ // of the class from the request body as the parameter value
+ if (null !== $parameterClass
+ && $parameterClass->implementsInterface($clsRenderable)
+ ) {
+ $parameterClassName = $parameterClass->getName();
+ $parameterValue = $parameterClassName::unrender(
+ $this->_request->getRequestBody(),
+ new Variant(null, null, $this->_request->getContentType())
+ );
+ $resourceMethodArgs[$idx] = $parameterValue;
+ }
// search for an argument value in the Path parameter collection
if (in_array($parameterName, $pathParams)
@@ -24,8 +24,15 @@
*/
class MethodNotAllowedException extends WebApplicationException
{
- public function __construct()
+ /**
+ * @param array $allowedMethods HTTP methods that are allowed.
+ */
+ public function __construct(array $allowedMethods = array())
{
parent::__construct(405);
+
+ $this->_response->setHeaders(
+ array('Allow' => implode(', ', $allowedMethods))
+ );
}
}
@@ -130,14 +130,16 @@ public function match(RequestInterface $request, &$pathParameters = array())
// filter candidate routes further by matching the incoming request
// method
+ $allowedMethods = array();
foreach ($candidateRoutes as $i => $route) {
if ($route->getHttpMethod() != $requestMethod) {
+ $allowedMethods[] = $route->getHttpMethod();
unset($candidateRoutes[$i]);
}
}
if (empty($candidateRoutes)) {
- throw new MethodNotAllowedException;
+ throw new MethodNotAllowedException($allowedMethods);
}
// filter candidate routes further by matching the incoming media type
@@ -169,13 +169,20 @@ public function resourcePath(
$resourceClassName,
$resourceMethodName = null)
{
+ $foundClass = false;
+
foreach ($this->_config->getRoutes() as $route) {
if ($resourceClassName == $route->getResourceClassName()) {
- $this->path($route->getClassPath());
+ if (!$foundClass) {
+ $foundClass = true;
+ $this->path($route->getClassPath());
+ }
if ($resourceMethodName == $route->getResourceMethodName()) {
$this->path($route->getMethodPath());
+ return $this;
}
+
}
}
@@ -272,7 +279,7 @@ public function build(array $values = array())
// perform URI template value substitution
$countMatches = preg_match_all('/{([^}]*)}/', $uri, $matches);
if ($countMatches > count($values)) {
- throw new LengthException(
+ throw new \LengthException(
sprintf(
'Need %d URI template values, but only %d values supplied.',
$countMatches,
@@ -295,26 +302,14 @@ public function build(array $values = array())
* @param array $values An associative array of URI template parameter
* values.
* @return string
- * @throws LengthException if there are any URI template parameters without
- * a supplied value
*/
public function buildFromMap(array $values = array())
{
$uri = $this->_concatUriComponents();
- // perform URI template value substitution
- $countMatches = preg_match_all('/{([^}]*)}/', $uri, $matches);
- if ($countMatches > count($values)) {
- throw new LengthException(
- sprintf(
- 'Need %d URI template values, but only %d values supplied.',
- $countMatches,
- count($values)
- )
- );
- } else if ($countMatches == count($values)) {
- foreach ($matches[0] as $idx => $varName) {
- $uri = str_replace($varName, $values[$matches[1][$idx]], $uri);
+ foreach ($values as $varName => $varValue) {
+ if (is_string($varValue)) {
+ $uri = str_replace("{{$varName}}", $varValue, $uri);
}
}
@@ -114,7 +114,7 @@ public function getAbsolutePathBuilder()
*/
public function getBaseUri()
{
- return $this->_config->getBaseUri();
+ return $this->_config->getBasePath();
}
/**
@@ -125,7 +125,7 @@ public function getBaseUri()
public function getBaseUriBuilder()
{
$builder = new UriBuilder($this->_config, $this->_request);
- return $builder->replacePath($this->_config->getBaseUri());
+ return $builder->replacePath($this->getBaseUri());
}
/**
Oops, something went wrong.

0 comments on commit a82fac8

Please sign in to comment.