-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCromis.MVC.Routing.pas
383 lines (314 loc) · 9 KB
/
Cromis.MVC.Routing.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
unit Cromis.MVC.Routing;
interface
uses
Windows, SysUtils, Classes, Masks,
// cromis units
Cromis.StringUtils;
type
TRouteType = (rtBase, rtMVC, rtCustom);
IBaseRoute = interface(IInterface)
['{C566C15E-93D0-43B3-A83C-854088F2336C}']
procedure Initialize;
function GetMask: string;
function GetRouteType: TRouteType;
procedure SetMask(const Value: string);
procedure SetRouteType(const Value: TRouteType);
property Mask: string read GetMask write SetMask;
property RouteType: TRouteType read GetRouteType write SetRouteType;
end;
// custom route procedure. if assigned it points to a proc to execute
TRouteProc = procedure(const Sender: IBaseRoute; const Data: IInterface) of Object;
ICustomRoute = interface(IBaseRoute)
['{E8E8BE0D-D6D6-4BAC-B724-565FA276659C}']
function GetRouteProc: TRouteProc;
procedure SetRouteProc(const Value: TRouteProc);
property RouteProc: TRouteProc read GetRouteProc write SetRouteProc;
end;
IMVCRoute = interface(IBaseRoute)
['{C566C15E-93D0-43B3-A83C-854088F2336C}']
function GetID: string;
function GetCommand: string;
function GetController: string;
procedure SetID(const Value: string);
procedure SetCommand(const Value: string);
procedure SetController(const Value: string);
property ID: string read GetID write SetID;
property Command: string read GetCommand write SetCommand;
property Controller: string read GetController write SetController;
end;
IWebRouting = interface(IInterface)
['{5CB8A82F-B787-406B-8F49-664FD882F863}']
function GetPathRoot: string;
procedure SetPathRoot(const Value: string);
property PathRoot: string read GetPathRoot write SetPathRoot;
procedure AddCustomRoute(const Mask, Controller, Command: string); overload;
procedure AddCustomRoute(const Mask: string; const RouteProc: TRouteProc); overload;
function FindRoute(const URL: string): IBaseRoute;
procedure ClearAllRoutes;
end;
function WebRouting: IWebRouting;
implementation
var
WebRoutingSingleton: IWebRouting;
type
TBaseRoute = class(TInterfacedObject, IBaseRoute)
private
FMask: string;
FRouteType: TRouteType;
function GetMask: string;
function GetRouteType: TRouteType;
procedure SetMask(const Value: string);
procedure SetRouteType(const Value: TRouteType);
public
constructor Create; virtual;
procedure Initialize; virtual;
property Mask: string read GetMask write SetMask;
property RouteType: TRouteType read GetRouteType write SetRouteType;
end;
TCustomRoute = class(TBaseRoute, ICustomRoute)
private
FRouteProc: TRouteProc;
function GetRouteProc: TRouteProc;
procedure SetRouteProc(const Value: TRouteProc);
public
constructor Create; override;
procedure Initialize; override;
property RouteProc: TRouteProc read GetRouteProc write SetRouteProc;
end;
TMVCRoute = class(TBaseRoute, IMVCRoute)
private
FID: string;
FCommand: string;
FController: string;
function GetID: string;
function GetCommand: string;
function GetController: string;
procedure SetID(const Value: string);
procedure SetCommand(const Value: string);
procedure SetController(const Value: string);
public
constructor Create; override;
procedure Initialize; override;
property ID: string read GetID write SetID;
property Command: string read GetCommand write SetCommand;
property Controller: string read GetController write SetController;
end;
TWebRouteWrapper = class
private
FWebRoute: IBaseRoute;
public
constructor Create(const WebRoute: IBaseRoute);
property WebRoute: IBaseRoute read FWebRoute;
end;
TWebRouting = class(TInterfacedObject, IWebRouting)
private
FPathRoot: string;
FCustomRoutes: TStringList;
function ParseURL(URL: string): IMVCRoute;
public
constructor Create;
destructor Destroy; override;
function GetPathRoot: string;
procedure SetPathRoot(const Value: string);
property PathRoot: string read GetPathRoot write SetPathRoot;
procedure AddCustomRoute(const Mask, Controller, Command: string); overload;
procedure AddCustomRoute(const Mask: string; const RouteProc: TRouteProc); overload;
function FindRoute(const URL: string): IBaseRoute;
procedure ClearAllRoutes;
end;
function WebRouting: IWebRouting;
begin
if WebRoutingSingleton = nil then
WebRoutingSingleton := TWebRouting.Create;
Result := WebRoutingSingleton;
end;
{ TMVCWebRouting }
procedure TWebRouting.AddCustomRoute(const Mask, Controller, Command: string);
var
CustomRoute: IMVCRoute;
begin
CustomRoute := TMVCRoute.Create;
CustomRoute.Controller := Controller;
CustomRoute.Command := Command;
CustomRoute.Mask := Mask;
FCustomRoutes.AddObject(Mask, TWebRouteWrapper.Create(CustomRoute));
end;
procedure TWebRouting.AddCustomRoute(const Mask: string; const RouteProc: TRouteProc);
var
CustomRoute: ICustomRoute;
begin
CustomRoute := TCustomRoute.Create;
CustomRoute.RouteProc := RouteProc;
CustomRoute.Mask := Mask;
FCustomRoutes.AddObject(Mask, TWebRouteWrapper.Create(CustomRoute));
end;
procedure TWebRouting.ClearAllRoutes;
var
I: Integer;
begin
for I := 0 to FCustomRoutes.Count - 1 do
FCustomRoutes.Objects[I].Free;
FCustomRoutes.Clear;
end;
constructor TWebRouting.Create;
begin
FCustomRoutes := TStringList.Create;
end;
destructor TWebRouting.Destroy;
begin
FCustomRoutes.Clear;
FreeAndNil(FCustomRoutes);
inherited;
end;
function TWebRouting.ParseURL(URL: string): IMVCRoute;
var
URLParts: TStringList;
begin
Result := TMVCRoute.Create;
Result.Mask := '/*/*/*';
Result.Initialize;
if Pos(FPathRoot, URL) = 1 then
URL := StrAfter(FPathRoot, URL);
if Length(URL) > 0 then
begin
URLParts := TStringList.Create;
try
URLParts.StrictDelimiter := True;
URLParts.Delimiter := '/';
case URL[1] = '/' of
True: URLParts.DelimitedText := Copy(URL, 2, Length(URL) - 1);
False: URLParts.DelimitedText := URL;
end;
if URLParts.Count > 0 then
Result.Controller := URLParts[0]
else
Result.Controller := 'Home';
if URLParts.Count > 1 then
Result.Command := URLParts[1]
else
Result.Command := 'Index';
if URLParts.Count > 2 then
begin
Result.ID := URLParts[2];
if Pos(Result.ID, '?') > 0 then
Result.ID := StrBefore('?', Result.ID);
end;
finally
URLParts.Free;
end;
end;
end;
procedure TWebRouting.SetPathRoot(const Value: string);
begin
FPathRoot := Value;
end;
function TWebRouting.FindRoute(const URL: string): IBaseRoute;
var
I: Integer;
begin
for I := 0 to FCustomRoutes.Count - 1 do
begin
if MatchesMask(FPathRoot + URL, FCustomRoutes[I]) then
begin
Result := TWebRouteWrapper(FCustomRoutes.Objects[I]).WebRoute;
Exit;
end;
end;
// make default routing
Result := ParseURL(URL);
end;
function TWebRouting.GetPathRoot: string;
begin
Result := FPathRoot;
end;
{ TMVCRoute }
constructor TMVCRoute.Create;
begin
inherited;
FRouteType := rtMVC;
end;
function TMVCRoute.GetCommand: string;
begin
Result := FCommand;
end;
function TMVCRoute.GetController: string;
begin
Result := FController;
end;
function TMVCRoute.GetID: string;
begin
Result := FID;
end;
procedure TMVCRoute.Initialize;
begin
inherited;
FID := '';
FCommand := '';
FController := '';
end;
procedure TMVCRoute.SetCommand(const Value: string);
begin
FCommand := Value;
end;
procedure TMVCRoute.SetController(const Value: string);
begin
FController := Value;
end;
procedure TMVCRoute.SetID(const Value: string);
begin
FID := Value
end;
{ TWebRouteWrapper }
constructor TWebRouteWrapper.Create(const WebRoute: IBaseRoute);
begin
FWebRoute := WebRoute;
end;
{ TBaseRoute }
constructor TBaseRoute.Create;
begin
FRouteType := rtBase;
end;
function TBaseRoute.GetMask: string;
begin
Result := FMask;
end;
function TBaseRoute.GetRouteType: TRouteType;
begin
Result := FRouteType;
end;
procedure TBaseRoute.Initialize;
begin
FRouteType := rtMVC;
FMask := '';
end;
procedure TBaseRoute.SetMask(const Value: string);
begin
FMask := Value;
end;
procedure TBaseRoute.SetRouteType(const Value: TRouteType);
begin
FRouteType := Value;
end;
{ TCustomRoute }
constructor TCustomRoute.Create;
begin
inherited;
FRouteType := rtCustom;
end;
function TCustomRoute.GetRouteProc: TRouteProc;
begin
Result := FRouteProc;
end;
procedure TCustomRoute.Initialize;
begin
inherited;
FRouteProc := nil;
end;
procedure TCustomRoute.SetRouteProc(const Value: TRouteProc);
begin
FRouteProc := Value;
end;
initialization
finalization
WebRoutingSingleton := nil;
end.