Permalink
Browse files

use an interface to switch to the special mode

  • Loading branch information...
lsmith77 committed Aug 8, 2012
1 parent 5a72041 commit bb325546ddfcf4852ece01edea6bc69fba7c5ae3
@@ -18,13 +18,14 @@
use FOS\RestBundle\View\RedirectView;
use FOS\RestBundle\View\RouteRedirectView;
use FOS\Rest\Util\Codes;
+use FOS\RestBundle\Routing\ClassResourceInterface;
/**
* Base Controller for Controllers using the View functionality of FOSRestBundle.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
-abstract class FOSRestController extends Controller
+abstract class FOSRestController extends Controller implements ClassResourceInterface
{
/**
* Create a view
@@ -0,0 +1,22 @@
+<?php
+
+/*
+ * This file is part of the FOSRestBundle package.
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace FOS\RestBundle\Routing;
+
+/**
+ * Implement interface to define that missing resources in the methods should
+ * use the class name to identify the resource
+ *
+ * @author Lukas Kahwe Smith <smith@pooteeweet.org>
+ */
+interface ClassResourceInterface
+{
+}
@@ -113,12 +113,11 @@ public function getParents()
*
* @param RestRouteCollection $collection route collection to read into
* @param \ReflectionMethod $method method reflection
- * @param array $resource
- * @param Boolean $force
+ * @param array|null $resource
*
* @return Route
*/
- public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource, $force)
+ public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource)
{
// check that every route parent has non-empty singular name
foreach ($this->parents as $parent) {
@@ -136,14 +135,27 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
}
// if we can't get http-method and resources from method name - skip
- $httpMethodAndResources = $this->getHttpMethodAndResourcesFromMethod($method, $resource, $force);
+ $httpMethodAndResources = $this->getHttpMethodAndResourcesFromMethod($method);
if (!$httpMethodAndResources) {
return;
}
list($httpMethod, $resources) = $httpMethodAndResources;
$arguments = $this->getMethodArguments($method);
+ if ($resource) {
+ $first = reset($resources);
+ if (!$first || 'List' === $first) {
+ if ('List' === $first || in_array($httpMethod, $this->availableConventionalActions)) {
+ $resource[0] = Pluralization::pluralize($resource[0]);
+ }
+
+ $resources = $resource;
+ } else {
+ $resources = array_merge($resource, $resources);
+ }
+ }
+
// if we have only 1 resource & 1 argument passed, then it's object call, so
// we can set collection singular name
if (1 === count($resources) && 1 === count($arguments) - count($this->parents)) {
@@ -227,12 +239,10 @@ private function isMethodReadable(\ReflectionMethod $method)
* Returns HTTP method and resources list from method signature.
*
* @param \ReflectionMethod $method
- * @param array $resource
- * @param Boolean $force
*
* @return Boolean|array
*/
- private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource, $force)
+ private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method)
{
// if method doesn't match regex - skip
if (!preg_match('/([a-z][_a-z0-9]+)(.*)Action/', $method->getName(), $matches)) {
@@ -244,17 +254,6 @@ private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method,
'/([A-Z][^A-Z]*)/', $matches[2], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
- $first = reset($resources);
- if (!$first || 'List' === $first) {
- if ('List' === $first || in_array($httpMethod, $this->availableConventionalActions)) {
- $resource[0] = Pluralization::pluralize($resource[0]);
- }
-
- $resources = $resource;
- } elseif ($force) {
- $resources = array_merge($resource, $resources);
- }
-
return array($httpMethod, $resources);
}
@@ -69,16 +69,17 @@ public function read(\ReflectionClass $reflection)
$this->actionReader->setNamePrefix($annotation->value);
}
+ $resource = null;
// read route-resource annotation
if ($annotation = $this->readClassAnnotation($reflection, 'RouteResource')) {
$resource = explode('_', $annotation->resource);
- $force = true;
- } else {
- preg_match('/([A-Z][_a-zA-Z0-9]*)Controller/', $reflection->getShortName(), $matches);
+ } elseif ($reflection->implementsInterface('FOS\RestBundle\Routing\ClassResourceInterface')) {
$resource = preg_split(
- '/([A-Z][^A-Z]*)/', $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
+ '/([A-Z][^A-Z]*)Controller/', $reflection->getShortName(), -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
- $force = false;
+ if (empty($resource)) {
+ throw new \InvalidArgumentException("Controller '{$reflection->name}' does not identify a resource");
+ }
}
// trim '/' at the start of the prefix
@@ -88,7 +89,7 @@ public function read(\ReflectionClass $reflection)
// read action routes into collection
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
- $this->actionReader->read($collection, $method, $resource, $force);
+ $this->actionReader->read($collection, $method, $resource);
}
$this->actionReader->setRoutePrefix(null);

0 comments on commit bb32554

Please sign in to comment.