Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Commit

Permalink
Cleanup Dispatcher.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed May 27, 2014
1 parent a3ed566 commit e054312
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/Resources/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,43 @@ protected function findRestfulRoutable($verb, array $parameters = array())
* @param array $nested
* @return array
*/
protected function findResourceRoutable(
$verb,
array $parameters = array(),
array $nested = array()
) {
$last = array_pop($parameters);
$resources = array_keys($nested);
protected function findResourceRoutable($verb, array $parameters = array(), array $nested = array())
{
$action = $this->getActionName($verb, $parameters, $nested);
$parameters = array_values($nested);

return array($action, $parameters);
}

/**
* Get action name.
*
* @param string $verb
* @param array $parameters
* @param array $nested
* @return string
*/
protected function getActionName($verb, array $parameters = array(), array $nested = array())
{
$last = array_pop($parameters);
$resources = array_keys($nested);

$swappable = array(
'post' => 'store',
'put' => 'update',
'patch' => 'update',
'post' => 'store',
'put' => 'update',
'patch' => 'update',
'delete' => 'destroy',
);

if (isset($swappable[$verb])) {
$action = $swappable[$verb];
return $swappable[$verb];
} elseif (in_array($last, array('edit', 'create', 'delete'))) {
// Handle all possible GET routing.
$action = $last;
} elseif (! in_array($last, $resources) && ! empty($nested)) {
$action = 'show';
} else {
$action = 'index';
return $last;
} elseif (!in_array($last, $resources) && !empty($nested)) {
return 'show';
}

return array($action, $parameters);
return 'index';
}
}

0 comments on commit e054312

Please sign in to comment.