update: completed routing style feature all test pass

This commit is contained in:
Patrick Stewart 2024-12-31 12:16:24 -07:00
parent 50a12e4535
commit 56fbaa62d3

View file

@ -40,7 +40,11 @@ class LaravelStyle<T> implements RoutingStyle<T> {
Route<T> route(String method, String path, T handler, Route<T> route(String method, String path, T handler,
{List<T> middleware = const [], String? name}) { {List<T> middleware = const [], String? name}) {
var allMiddleware = [..._currentMiddleware, ...middleware]; var allMiddleware = [..._currentMiddleware, ...middleware];
var route = _router.addRoute(method.toUpperCase(), path, handler,
// If we're in a group, prepend the group prefix to the path
var fullPath = _groupPrefix != null ? '$_groupPrefix$path' : path;
var route = _router.addRoute(method.toUpperCase(), fullPath, handler,
middleware: allMiddleware); middleware: allMiddleware);
// Auto-generate route name if not provided // Auto-generate route name if not provided
@ -50,33 +54,45 @@ class LaravelStyle<T> implements RoutingStyle<T> {
// Convert path to route name (e.g., /users/{id} -> users.show) // Convert path to route name (e.g., /users/{id} -> users.show)
var segments = path.split('/').where((s) => s.isNotEmpty).toList(); var segments = path.split('/').where((s) => s.isNotEmpty).toList();
if (segments.isNotEmpty) { if (segments.isNotEmpty) {
var lastSegment = segments.last; // Clean up segments
var prefix = segments.length > 1 ? segments[segments.length - 2] : ''; var cleanSegments =
var groupPrefix = _groupName ?? _groupPrefix?.replaceAll('/', '.'); segments.map((s) => s.replaceAll(RegExp(r'[:{}/]'), '')).toList();
var namePrefix = groupPrefix != null ? '$groupPrefix.' : '';
// Get the resource name from the last non-parameter segment
var resourceName = cleanSegments.last;
var hasParams =
segments.last.contains(':') || segments.last.contains('{');
if (hasParams && segments.length > 1) {
// If the last segment is a parameter, use the previous segment
resourceName = cleanSegments[segments.length - 2];
}
var prefix = _groupPrefix
?.replaceAll('/', '.')
.replaceAll(RegExp(r'^\.|\.$'), '') ??
'';
var namePrefix = prefix.isEmpty ? '' : '$prefix.';
// Generate name based on method and path // Generate name based on method and path
switch (method.toUpperCase()) { switch (method.toUpperCase()) {
case 'GET': case 'GET':
route.name = lastSegment.contains(':') || lastSegment.contains('{') route.name = hasParams
? '$namePrefix${prefix.isEmpty ? lastSegment : prefix}.show' ? '$namePrefix$resourceName.show'
: '$namePrefix${lastSegment}.index'; : '$namePrefix$resourceName.index';
break; break;
case 'POST': case 'POST':
route.name = route.name = '$namePrefix$resourceName.store';
'$namePrefix${prefix.isEmpty ? lastSegment : prefix}.store';
break; break;
case 'PUT': case 'PUT':
case 'PATCH': case 'PATCH':
route.name = route.name = '$namePrefix$resourceName.update';
'$namePrefix${prefix.isEmpty ? lastSegment : prefix}.update';
break; break;
case 'DELETE': case 'DELETE':
route.name = route.name = '$namePrefix$resourceName.destroy';
'$namePrefix${prefix.isEmpty ? lastSegment : prefix}.destroy';
break; break;
default: default:
route.name = '$namePrefix$lastSegment'; route.name = '$namePrefix$resourceName';
} }
} }
} }
@ -144,38 +160,25 @@ class LaravelStyle<T> implements RoutingStyle<T> {
var middleware = attributes['middleware'] as List<T>? ?? const []; var middleware = attributes['middleware'] as List<T>? ?? const [];
var groupName = attributes['name'] as String? ?? ''; var groupName = attributes['name'] as String? ?? '';
// Create new router for group // Store current state
var groupRouter = Router<T>(); var previousPrefix = _groupPrefix;
var previousName = _groupName;
var previousMiddleware = _currentMiddleware;
// Create new style instance for group // Update group context
var groupStyle = LaravelStyle<T>(groupRouter); _groupPrefix = previousPrefix != null ? '$previousPrefix$prefix' : prefix;
_groupName = groupName.isNotEmpty ? groupName : prefix.replaceAll('/', '.');
_currentMiddleware = [...previousMiddleware, ...middleware];
// Set group prefix and name for route naming // Execute callback in group context
groupStyle._groupPrefix = prefix;
groupStyle._groupName = groupName;
groupStyle._currentMiddleware = [..._currentMiddleware, ...middleware];
// Store previous active instance
var previousInstance = _activeInstance;
// Set group style as active instance
_activeInstance = groupStyle;
// Execute callback with group context
callback(); callback();
// Mount group router with prefix // Restore previous state
var route = _router.mount(prefix, groupRouter); _groupPrefix = previousPrefix;
if (groupName.isNotEmpty) { _groupName = previousName;
route.name = groupName; _currentMiddleware = previousMiddleware;
}
// Restore previous active instance
_activeInstance = previousInstance;
} }
// Track active instance for group context
static LaravelStyle? _activeInstance;
/// Add a name to the last registered route. /// Add a name to the last registered route.
/// ///
/// ```dart /// ```dart