RouteGroup.php
TLDR
This file contains a RouteGroup
class that provides methods for merging route groups into a new array.
Methods
merge
This method merges two arrays representing route groups into a new array. It takes three parameters: $new
(array), $old
(array), and $prependExistingPrefix
(boolean). The method removes the domain
and controller
keys from the $old
array if they exist in the $new
array. Then, it merges the two arrays and formats the namespace
, prefix
, and where
keys using helper methods. The method returns the merged array.
formatNamespace
This method formats the namespace
key for the new group attributes. It takes two parameters: $new
(array) and $old
(array). If the namespace
key exists in the $new
array, it checks if the namespace
key also exists in the $old
array. If it does, it appends the $new
namespace to the $old
namespace. Otherwise, it returns the $new
namespace. If the namespace
key does not exist in the $new
array, it returns the $old
namespace or null
if it does not exist.
formatPrefix
This method formats the prefix
key for the new group attributes. It takes three parameters: $new
(array), $old
(array), and $prependExistingPrefix
(boolean). It assigns the value of the $old['prefix']
to the variable $old
. If $prependExistingPrefix
is true
, it appends the $new
prefix to the end of the $old
prefix after trimming any leading or trailing slashes. Otherwise, it prepends the $new
prefix to the start of the $old
prefix after trimming any leading or trailing slashes. If the prefix
key does not exist in the $new
array, it returns the $old
prefix.
formatWhere
This method formats the where
key for the new group attributes. It takes two parameters: $new
(array) and $old
(array). It merges the $old['where']
array with the $new['where']
array and returns the merged array.
formatAs
This method formats the as
key for the new group attributes. It takes two parameters: $new
(array) and $old
(array). If the $old
array has an as
key, it appends the $new['as']
to the end of the $old['as']
and returns the modified $new
array. Otherwise, it returns the $new
array.
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
class RouteGroup
{
/**
* Merge route groups into a new array.
*
* @param array $new
* @param array $old
* @param bool $prependExistingPrefix
* @return array
*/
public static function merge($new, $old, $prependExistingPrefix = true)
{
if (isset($new['domain'])) {
unset($old['domain']);
}
if (isset($new['controller'])) {
unset($old['controller']);
}
$new = array_merge(static::formatAs($new, $old), [
'namespace' => static::formatNamespace($new, $old),
'prefix' => static::formatPrefix($new, $old, $prependExistingPrefix),
'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']) && ! str_starts_with($new['namespace'], '\\')
? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\')
: trim($new['namespace'], '\\');
}
return $old['namespace'] ?? null;
}
/**
* Format the prefix for the new group attributes.
*
* @param array $new
* @param array $old
* @param bool $prependExistingPrefix
* @return string|null
*/
protected static function formatPrefix($new, $old, $prependExistingPrefix = true)
{
$old = $old['prefix'] ?? '';
if ($prependExistingPrefix) {
return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
}
return isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $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(
$old['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'].($new['as'] ?? '');
}
return $new;
}
}