Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

119 wiersze
3.6KB

  1. <%
  2. Class Router
  3. Private m_Routes
  4. Private Sub Class_Initialize()
  5. Set m_Routes = Server.CreateObject("Scripting.Dictionary")
  6. End Sub
  7. Private Sub Class_Terminate()
  8. If IsObject(m_Routes) Then
  9. Set m_Routes = Nothing
  10. End If
  11. End Sub
  12. Public Sub AddRoute(ByVal pattern, ByVal controllerName)
  13. Dim routeInfo
  14. Dim normalizedPattern
  15. If Len(Trim(CStr(pattern))) = 0 Then
  16. Err.Raise vbObjectError + 1200, "Router.AddRoute", "pattern is required."
  17. End If
  18. If Len(Trim(CStr(controllerName))) = 0 Then
  19. Err.Raise vbObjectError + 1201, "Router.AddRoute", "controllerName is required."
  20. End If
  21. normalizedPattern = NormalizePath(CStr(pattern))
  22. Set routeInfo = Server.CreateObject("Scripting.Dictionary")
  23. routeInfo.Add "Segments", SplitPath(normalizedPattern)
  24. routeInfo.Add "ControllerName", CStr(controllerName)
  25. m_Routes.Add m_Routes.Count, routeInfo
  26. End Sub
  27. ' Returns a Dictionary with "ControllerName" and "Params" (a Dictionary of
  28. ' captured {name}-style segments), or Nothing if no registered pattern matches.
  29. Public Function Match(ByVal route)
  30. Dim routeSegments
  31. Dim routeKey
  32. Dim routeInfo
  33. Dim patternSegments
  34. Dim params
  35. Dim isMatch
  36. Dim segmentIndex
  37. Dim result
  38. routeSegments = SplitPath(NormalizePath(CStr(route)))
  39. For Each routeKey In m_Routes.Keys
  40. Set routeInfo = m_Routes.Item(routeKey)
  41. patternSegments = routeInfo.Item("Segments")
  42. If UBound(patternSegments) = UBound(routeSegments) Then
  43. Set params = Server.CreateObject("Scripting.Dictionary")
  44. isMatch = True
  45. For segmentIndex = 0 To UBound(patternSegments)
  46. If IsParamSegment(patternSegments(segmentIndex)) Then
  47. params.Add ParamName(patternSegments(segmentIndex)), routeSegments(segmentIndex)
  48. ElseIf LCase(patternSegments(segmentIndex)) <> LCase(routeSegments(segmentIndex)) Then
  49. isMatch = False
  50. Exit For
  51. End If
  52. Next
  53. If isMatch Then
  54. Set result = Server.CreateObject("Scripting.Dictionary")
  55. result.Add "ControllerName", routeInfo.Item("ControllerName")
  56. result.Add "Params", params
  57. Set Match = result
  58. Exit Function
  59. End If
  60. End If
  61. Next
  62. Set Match = Nothing
  63. End Function
  64. Private Function IsParamSegment(ByVal segment)
  65. IsParamSegment = (Left(segment, 1) = "{" And Right(segment, 1) = "}")
  66. End Function
  67. Private Function ParamName(ByVal segment)
  68. ParamName = Mid(segment, 2, Len(segment) - 2)
  69. End Function
  70. Private Function NormalizePath(ByVal path)
  71. Dim result
  72. result = Trim(CStr(path))
  73. If Len(result) = 0 Then
  74. result = "/"
  75. End If
  76. If Left(result, 1) <> "/" Then
  77. result = "/" & result
  78. End If
  79. If Len(result) > 1 And Right(result, 1) = "/" Then
  80. result = Left(result, Len(result) - 1)
  81. End If
  82. NormalizePath = result
  83. End Function
  84. Private Function SplitPath(ByVal path)
  85. Dim trimmedPath
  86. trimmedPath = path
  87. If Left(trimmedPath, 1) = "/" Then
  88. trimmedPath = Mid(trimmedPath, 2)
  89. End If
  90. SplitPath = Split(trimmedPath, "/")
  91. End Function
  92. End Class
  93. %>

Powered by TurnKey Linux.