Skip to content

Commit

Permalink
extract some classes for router
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 21, 2016
1 parent eecf6ec commit b208a4f
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 267 deletions.
85 changes: 85 additions & 0 deletions src/Illuminate/Routing/MiddlewareNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Illuminate\Routing;

use Closure;

class MiddlewareNameResolver
{
/**
* Resolve the middleware name to a class name(s) preserving passed parameters.
*
* @param string $name
* @param array $map
* @param array $middlewareGroups
* @return string|array
*/
public static function resolve($name, $map, $middlewareGroups)
{
// When the middleware is simply a Closure, we will return this Closure instance
// directly so that Closures can be registered as middleware inline, which is
// convenient on occasions when the developers are experimenting with them.
if ($name instanceof Closure) {
return $name;
} elseif (isset($map[$name]) && $map[$name] instanceof Closure) {
return $map[$name];

// If the middleware is the name of a middleware group, we will return the array
// of middlewares that belong to the group. This allows developers to group a
// set of middleware under single keys that can be conveniently referenced.
} elseif (isset($middlewareGroups[$name])) {
return static::parseMiddlewareGroup(
$name, $map, $middlewareGroups
);

// Finally, when the middleware is simply a string mapped to a class name the
// middleware name will get parsed into the full class name and parameters
// which may be run using the Pipeline which accepts this string format.
} else {
list($name, $parameters) = array_pad(explode(':', $name, 2), 2, null);

return (isset($map[$name]) ? $map[$name] : $name).
(! is_null($parameters) ? ':'.$parameters : '');
}
}

/**
* Parse the middleware group and format it for usage.
*
* @param string $name
* @param array $middlewareGroups
* @return array
*/
protected static function parseMiddlewareGroup($name, $map, $middlewareGroups)
{
$results = [];

foreach ($middlewareGroups[$name] as $middleware) {
// If the middleware is another middleware group we will pull in the group and
// merge its middleware into the results. This allows groups to conveniently
// reference other groups without needing to repeat all their middlewares.
if (isset($middlewareGroups[$middleware])) {
$results = array_merge($results, static::parseMiddlewareGroup(
$middleware, $map, $middlewareGroups
));

continue;
}

list($middleware, $parameters) = array_pad(
explode(':', $middleware, 2), 2, null
);

// If this middleware is actually a route middleware, we will extract the full
// class name out of the middleware list now. Then we'll add the parameters
// back onto this class' name so the pipeline will properly extract them.
if (isset($map[$middleware])) {
$middleware = $map[$middleware];
}

$results[] = $middleware.($parameters ? ':'.$parameters : '');
}

return $results;
}
}
95 changes: 95 additions & 0 deletions src/Illuminate/Routing/RouteGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Illuminate\Routing;

use Illuminate\Support\Arr;

class RouteGroup
{
/**
* Merge route groups into a new array.
*
* @param array $new
* @param array $old
* @return array
*/
public static function merge($new, $old)
{
if (isset($new['domain'])) {
unset($old['domain']);
}

$new = array_merge(static::formatAs($new, $old), [
'namespace' => static::formatNamespace($new, $old),
'prefix' => static::formatPrefix($new, $old),
'where' => static::formatWhere($new, $old),
]);

return array_merge_recursive(Arr::except(
$old, ['namespace', 'prefix', 'where', 'as']
), $new);
}

/**
* Format the namespace for the new group attributes.
*
* @param array $new
* @param array $old
* @return string|null
*/
protected static function formatNamespace($new, $old)
{
if (isset($new['namespace'])) {
return isset($old['namespace'])
? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\')
: trim($new['namespace'], '\\');
}

return isset($old['namespace']) ? $old['namespace'] : null;
}

/**
* Format the prefix for the new group attributes.
*
* @param array $new
* @param array $old
* @return string|null
*/
protected static function formatPrefix($new, $old)
{
$old = Arr::get($old, 'prefix');

return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
}

/**
* Format the "wheres" for the new group attributes.
*
* @param array $new
* @param array $old
* @return array
*/
protected static function formatWhere($new, $old)
{
return array_merge(
isset($old['where']) ? $old['where'] : [],
isset($new['where']) ? $new['where'] : []
);
}

/**
* Format the "as" clause of the new group attributes.
*
* @param array $new
* @param array $old
* @return array
*/
protected static function formatAs($new, $old)
{
if (isset($old['as'])) {
$new['as'] = $old['as'].Arr::get($new, 'as', '');
}

return $new;
}
}
Loading

0 comments on commit b208a4f

Please sign in to comment.