robotpony / Presto

Simple tools for building RESTfull interfaces. This is an old and unmaintained project.
http://presto.napkinware.ca
Other
11 stars 3 forks source link

Calling restrictTo() in constructor #87

Closed ngallagher87 closed 11 years ago

ngallagher87 commented 11 years ago

If you call $this->restrictTo(array('json', 'html')); in a constructor, you get the following error:

{
  "error": "Trying to get property of non-object",
  "file": "/Users/nathangallagher/Sites/app.rubrix.com/lib/presto-1.1/lib/api.php",
  "line": 85,
  "status": 500
}

I'm probably using restrictTo() in the wrong context, but even so presto should throw a better error here.

Presto errors on this line:

private function supports_contentType($t) {
    $in = self::$ctx->class . '::' . self::$ctx->method . '()';     <-- Line 85
    $type = self::$ctx->type;

    if (!is_array($t)) $t = array($t);
    if (!in_array($type, $t)) throw new Exception("Unsupported media type '$type' for '$in'.", 415);
}
ghost commented 11 years ago

I'll add a test for this case. I've seen the bug too, and I think the restrictTo should be possible to set globally for a route.

ghost commented 11 years ago

Unfortunately, restrictTo is only available in a routing context, and it does not save the state (only checks the state of the current call). This means that it would need to be reworked to cover all calls in the current API.

I've added a better error message for 1.2, and we can look at reworking the call if we decide we want the feature more globally.