Permalink
Browse files

Misc. fixes, some of which are hacky.

  • Loading branch information...
1 parent 1dfb074 commit c138622a81d22f5dc94309c1bec67e7a695da651 @nateabele committed Sep 28, 2012
Showing with 31 additions and 23 deletions.
  1. +13 −8 action/Resource.php
  2. +10 −6 action/resource/Responder.php
  3. +8 −9 net/http/Resources.php
View
@@ -83,7 +83,7 @@ protected function _binding() {
if ($this->_binding) {
return $this->_binding;
}
- return ($this->_binding = Libraries::locate('model', $this->_name()));
+ return ($this->_binding = Libraries::locate('models', $this->_name()));
}
/**
@@ -97,8 +97,9 @@ protected function _name() {
protected function _method($request, $params) {
$name = $this->_name();
+ $badIndex = ($request->method != 'GET' && $params['action'] == 'index');
- if ($action = $params['action']) {
+ if (($action = $params['action']) && !$badIndex) {
$methods = array_diff(get_class_methods($this), get_class_methods(__CLASS__));
if (!in_array($action, $methods) || strpos($action, '_') === 0) {
@@ -112,11 +113,12 @@ protected function _method($request, $params) {
$message = "The `{$name}` resource does not handle `{$request->method}` requests.";
throw new MethodNotAllowedException($message);
}
+ $requestParams = array_filter($request->params, function($val) { return $val !== null; });
foreach ($this->_methods[$request->method] as $action => $params) {
$params = (array) $params;
- if (array_intersect($params, array_keys($request->params)) !== $params) {
+ if (array_intersect($params, array_keys($requestParams)) !== $params) {
continue;
}
return $action;
@@ -210,7 +212,6 @@ public function __invoke($request, array $params = array()) {
$resources = $this->_get($method, $request);
$keys = array_map($keyMap, $resources);
- print_r($resources);die("!!");
$before = array_combine($keys, array_map($stateMap, $resources));
$result = call_user_func_array($invoke, array_merge(array($request), $resources));
@@ -229,15 +230,16 @@ public function __invoke($request, array $params = array()) {
}
/**
- * Gets the entity(ies) for the given resource method.
+ * Gets the bound parameters for the given resource method.
*
* @param string $method The name of the method to be invoked on this resource.
* @return array
*/
protected function _get($method, $request) {
$resources = $this->_classes['resources'];
$defs = $this->_parameters + array($method => null);
- return $resources::all($defs[$method] ?: $this->_default(), $this->_config(), $request);
+ $list = $defs[$method] ?: $this->_default($method);
+ return $resources::all($list, $this->_config(), $request);
}
/**
@@ -252,9 +254,12 @@ protected function _get($method, $request) {
protected function _default($method) {
$name = lcfirst($this->_name());
$isPlural = ($method == 'index');
- $call = array(true => 'first', $isPlural => 'index', $method == 'add' => 'create');
+ $call = array(true => 'first', $isPlural => 'all', ($method == 'add') => 'create');
$key = $isPlural ? Inflector::pluralize($name) : Inflector::singularize($name);
- return array($key => array($this->_binding(), 'call' => $call[true]));
+
+ return array($key => array(
+ $this->_binding(), 'call' => $call[true], 'required' => !$isPlural
+ ));
}
protected function _config() {
@@ -94,7 +94,8 @@ public static function requiresView($request) {
$config = $classes['media']::handlers($accepts);
if (!$classes['media']::type($accepts) || $config === null) {
- throw new MediaException();
+ $message = "The application does not understand the requested content type.";
+ throw new MediaException($message);
}
if ($config === array()) {
$config = $classes['media']::handlers('default');
@@ -118,13 +119,16 @@ protected static function _generators() {
return array(
'location' => function($request, $response, $data, $options) use ($classes) {
- unset($options['data'], $options['request']);
- print_r($options);
- print_r($data);
- die('!!!!');
+ $url = array('id' => $data->_id) + $options['params'];
+ $location = $classes['router']::match($url, $request, array('absolute' => true));
+ $response->headers("location", $location);
return $response;
},
'body' => function($request, $response, $data, $options) use ($classes) {
+ // @hack: Replace this with handlers():
+ if ($data instanceof Exception) {
+ $data = Responder::_responseFromException($request, $data);
+ }
return $classes['media']::render($response, $data, $options + compact('request'));
},
'errorBody' => function($request, $response, $data, $options) use ($classes) {
@@ -169,7 +173,7 @@ public static function state($object) {
* action.
* @return object Returns a `Response` object with the encoded error information.
*/
- protected static function _responseFromException($request, $e) {
+ public static function _responseFromException($request, $e) {
if (!$e instanceof Exception) {
return false;
}
View
@@ -20,6 +20,7 @@
class Resources extends \lithium\core\StaticObject {
protected static $_classes = array(
+ 'router' => 'lithium\net\http\Router',
'route' => 'lithium\net\http\Route',
'model' => 'lithium\data\Model'
);
@@ -128,8 +129,6 @@ public static function all($resources, array $config, $request) {
$resource += compact('name') + $defaults;
$resource['binding'] = $resource[0];
unset($resource[0]);
- print_r($resource);
- die();
if ($resource['in'] && !array_key_exists($resource['in'], $data)) {
list($key) = explode('.', $resource['in']);
@@ -140,8 +139,6 @@ public static function all($resources, array $config, $request) {
}
$map($key, $resources[$key]);
}
- print_r(compact('resource'));
- die();
$data[$name] = $self::get($request, compact('name', 'data') + $resource, $config);
};
@@ -194,8 +191,6 @@ public static function get($request, array $options = array()) {
return static::_filter($func, compact('request', 'options'), function($self, $params) {
$options = $params['options'];
$request = $params['request'];
- print_r($options);
- die();
if ($options['in']) {
return $self::queryCollection($options, $query);
@@ -295,7 +290,7 @@ public static function mapQuery(array $resource, $request) {
$params = $request->id ? 'id' : array();
}
if (is_string($params)) {
- $isModel = (is_object($model) || $model instanceof $classes['model']);
+ $isModel = (is_object($model) || is_a($model, $classes['model'], true));
$params = $isModel ? array($params => $model::key()) : array($params => $params);
}
@@ -342,6 +337,7 @@ public static function export(array $resources, array $options = array()) {
$defaults = array('prefix' => null);
$options += $defaults;
+ $classes = static::$_classes;
$remap = array();
$names = array();
@@ -350,14 +346,17 @@ public static function export(array $resources, array $options = array()) {
$resource = $config;
$config = array();
}
- $config += array('path' => Inflector::underscore($resource));
+ $config += array('path' => ($path = Inflector::underscore($resource)));
+ $first = substr($path, 0, 1);
+
$remap[$resource] = $config;
- $names[] = $config['path'];
+ $names[] = "[{$first}" . ucfirst($first) . "]" . substr($path, 1);
}
$template = $options['prefix'] . '/{:controller:' . join('|', $names) . '}';
$template .= '/{:id:(?:[0-9a-f]{24})|(?:\d+)}'; //'.{:type}';
return static::_instance('route', compact('template') + array(
+ 'formatters' => $classes['router']::formatters(),
'params' => array('action' => null, /*'type' => null,*/ 'id' => null)
));
}

0 comments on commit c138622

Please sign in to comment.