Skip to content

Commit

Permalink
Initial cleaning on routing component.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 21, 2016
1 parent 7573abf commit b75aca6
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 177 deletions.
30 changes: 10 additions & 20 deletions src/Illuminate/Routing/Redirector.php
Expand Up @@ -53,9 +53,7 @@ public function home($status = 302)
*/
public function back($status = 302, $headers = [], $fallback = false)
{
$back = $this->generator->previous($fallback);

return $this->createRedirect($back, $status, $headers);
return $this->createRedirect($this->generator->previous($fallback), $status, $headers);
}

/**
Expand Down Expand Up @@ -113,9 +111,7 @@ public function intended($default = '/', $status = 302, $headers = [], $secure =
*/
public function to($path, $status = 302, $headers = [], $secure = null)
{
$path = $this->generator->to($path, [], $secure);

return $this->createRedirect($path, $status, $headers);
return $this->createRedirect($this->generator->to($path, [], $secure), $status, $headers);
}

/**
Expand Down Expand Up @@ -155,9 +151,7 @@ public function secure($path, $status = 302, $headers = [])
*/
public function route($route, $parameters = [], $status = 302, $headers = [])
{
$path = $this->generator->route($route, $parameters);

return $this->to($path, $status, $headers);
return $this->to($this->generator->route($route, $parameters), $status, $headers);
}

/**
Expand All @@ -171,9 +165,7 @@ public function route($route, $parameters = [], $status = 302, $headers = [])
*/
public function action($action, $parameters = [], $status = 302, $headers = [])
{
$path = $this->generator->action($action, $parameters);

return $this->to($path, $status, $headers);
return $this->to($this->generator->action($action, $parameters), $status, $headers);
}

/**
Expand All @@ -186,15 +178,13 @@ public function action($action, $parameters = [], $status = 302, $headers = [])
*/
protected function createRedirect($path, $status, $headers)
{
$redirect = new RedirectResponse($path, $status, $headers);

if (isset($this->session)) {
$redirect->setSession($this->session);
}

$redirect->setRequest($this->generator->getRequest());
return tap(new RedirectResponse($path, $status, $headers), function ($redirect) {
if (isset($this->session)) {
$redirect->setSession($this->session);
}

return $redirect;
$redirect->setRequest($this->generator->getRequest());
});
}

/**
Expand Down
232 changes: 110 additions & 122 deletions src/Illuminate/Routing/ResourceRegistrar.php
Expand Up @@ -155,128 +155,6 @@ protected function getResourceMethods($defaults, $options)
return $defaults;
}

/**
* Get the base resource URI for a given resource.
*
* @param string $resource
* @return string
*/
public function getResourceUri($resource)
{
if (! Str::contains($resource, '.')) {
return $resource;
}

// Once we have built the base URI, we'll remove the parameter holder for this
// base resource name so that the individual route adders can suffix these
// paths however they need to, as some do not have any parameters at all.
$segments = explode('.', $resource);

$uri = $this->getNestedResourceUri($segments);

return str_replace('/{'.$this->getResourceWildcard(end($segments)).'}', '', $uri);
}

/**
* Get the URI for a nested resource segment array.
*
* @param array $segments
* @return string
*/
protected function getNestedResourceUri(array $segments)
{
// We will spin through the segments and create a place-holder for each of the
// resource segments, as well as the resource itself. Then we should get an
// entire string for the resource URI that contains all nested resources.
return implode('/', array_map(function ($s) {
return $s.'/{'.$this->getResourceWildcard($s).'}';
}, $segments));
}

/**
* Get the action array for a resource route.
*
* @param string $resource
* @param string $controller
* @param string $method
* @param array $options
* @return array
*/
protected function getResourceAction($resource, $controller, $method, $options)
{
$name = $this->getResourceName($resource, $method, $options);

$action = ['as' => $name, 'uses' => $controller.'@'.$method];

if (isset($options['middleware'])) {
$action['middleware'] = $options['middleware'];
}

return $action;
}

/**
* Get the name for a given resource.
*
* @param string $resource
* @param string $method
* @param array $options
* @return string
*/
protected function getResourceName($resource, $method, $options)
{
if (isset($options['names'])) {
if (is_string($options['names'])) {
$resource = $options['names'];
} elseif (isset($options['names'][$method])) {
return $options['names'][$method];
}
}

// If a global prefix has been assigned to all names for this resource, we will
// grab that so we can prepend it onto the name when we create this name for
// the resource action. Otherwise we'll just use an empty string for here.
$prefix = isset($options['as']) ? $options['as'].'.' : '';

if (! $this->router->hasGroupStack()) {
return $prefix.$resource.'.'.$method;
}

return $this->getGroupResourceName($prefix, $resource, $method);
}

/**
* Get the resource name for a grouped resource.
*
* @param string $prefix
* @param string $resource
* @param string $method
* @return string
*/
protected function getGroupResourceName($prefix, $resource, $method)
{
return trim("{$prefix}{$resource}.{$method}", '.');
}

/**
* Format a resource parameter for usage.
*
* @param string $value
* @return string
*/
public function getResourceWildcard($value)
{
if (isset($this->parameters[$value])) {
$value = $this->parameters[$value];
} elseif (isset(static::$parameterMap[$value])) {
$value = static::$parameterMap[$value];
} elseif ($this->parameters === 'singular' || static::$singularParameters) {
$value = Str::singular($value);
}

return str_replace('-', '_', $value);
}

/**
* Add the index method for a resourceful route.
*
Expand Down Expand Up @@ -403,6 +281,116 @@ protected function addResourceDestroy($name, $base, $controller, $options)
return $this->router->delete($uri, $action);
}

/**
* Get the base resource URI for a given resource.
*
* @param string $resource
* @return string
*/
public function getResourceUri($resource)
{
if (! Str::contains($resource, '.')) {
return $resource;
}

// Once we have built the base URI, we'll remove the parameter holder for this
// base resource name so that the individual route adders can suffix these
// paths however they need to, as some do not have any parameters at all.
$segments = explode('.', $resource);

$uri = $this->getNestedResourceUri($segments);

return str_replace('/{'.$this->getResourceWildcard(end($segments)).'}', '', $uri);
}

/**
* Get the URI for a nested resource segment array.
*
* @param array $segments
* @return string
*/
protected function getNestedResourceUri(array $segments)
{
// We will spin through the segments and create a place-holder for each of the
// resource segments, as well as the resource itself. Then we should get an
// entire string for the resource URI that contains all nested resources.
return implode('/', array_map(function ($s) {
return $s.'/{'.$this->getResourceWildcard($s).'}';
}, $segments));
}

/**
* Format a resource parameter for usage.
*
* @param string $value
* @return string
*/
public function getResourceWildcard($value)
{
if (isset($this->parameters[$value])) {
$value = $this->parameters[$value];
} elseif (isset(static::$parameterMap[$value])) {
$value = static::$parameterMap[$value];
} elseif ($this->parameters === 'singular' || static::$singularParameters) {
$value = Str::singular($value);
}

return str_replace('-', '_', $value);
}

/**
* Get the action array for a resource route.
*
* @param string $resource
* @param string $controller
* @param string $method
* @param array $options
* @return array
*/
protected function getResourceAction($resource, $controller, $method, $options)
{
$name = $this->getResourceRouteName($resource, $method, $options);

$action = ['as' => $name, 'uses' => $controller.'@'.$method];

if (isset($options['middleware'])) {
$action['middleware'] = $options['middleware'];
}

return $action;
}

/**
* Get the name for a given resource.
*
* @param string $resource
* @param string $method
* @param array $options
* @return string
*/
protected function getResourceRouteName($resource, $method, $options)
{
$name = $resource;

// If the names array has been provided to us we will check for an entry in the
// array first. We will also check for the specific method within this array
// so the names may be specified on a more "granular" level using methods.
if (isset($options['names'])) {
if (is_string($options['names'])) {
$name = $options['names'];
} elseif (isset($options['names'][$method])) {
return $options['names'][$method];
}
}

// If a global prefix has been assigned to all names for this resource, we will
// grab that so we can prepend it onto the name when we create this name for
// the resource action. Otherwise we'll just use an empty string for here.
$prefix = isset($options['as']) ? $options['as'].'.' : '';

return trim(sprintf('%s%s.%s', $prefix, $name, $method), '.');
}

/**
* Set or unset the unmapped global parameters to singular.
*
Expand Down

0 comments on commit b75aca6

Please sign in to comment.