/
web-framework-roles
167 lines (140 loc) · 3.86 KB
/
web-framework-roles
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
# the controller holds information that applies to all actions
# there. The initialization will look in the config file if there is
# a definition to any of the attributes defined in the package.
role Controller {}
# this is a normal private action
role Action {
has Controller $.controller;
has Str $.private-name;
multi method begin {...}
multi method execute(*@_, *%_) {...}
multi method end {...}
}
role Action::Private {
has Callable $.begin-closure;
has Callable $.execute-closure;
has Callable $.end-closure;
multi method begin {
$.begin-closure.(self)
if $.begin-closure;
}
multi method execute(*@_, *%_) {
$.execute-closure.(self, |@_, |%_)
if $.execute-closure;
}
multi method end {
$.end-closure.(self)
if $.end-closure;
}
}
# this is an action that might be part of a chain
role Action::Chained {
has Action::Chained $.parent;
has Regex $.regex;
has Callable $.begin-closure;
has Callable $.execute-closure;
has Callable $.end-closure;
multi method begin {
$.parent.*begin
if $.parent;
$.begin-closure.(self)
if $.begin-closure;
}
multi method execute(*@_, :¢_parent_action_capture, *%_) {
$.parent.*execute(|¢_parent_action_capture)
if $.parent;
$.execute-closure.(self, |@_, |%_)
if $.execute-closure;
}
multi method end {
$.parent.*end
if $.parent;
$.end-closure.(self)
if $.end-closure;
}
}
# this is used to mask out the base-uri for the application
role Action::Root does Action::Chained {
has URI $.base;
method regex {
return / ^ $.base /;
}
}
# this is an action that is seen as an endpoint
role Action::Public does Action::Chained does Pattern {
has Int $.priority;
}
# the dispatcher catalogs all actions, and is responsible for
# actually trying to invoke them
role Dispatcher {
has %!actions;
has @!public;
has $.regex;
method register-action(Action $a) {
fail 'Duplicated action'
if %!actions.exists($a.private-name);
%!actions{$a.private-name} = $a;
if $a ~~ Action::Public {
@!public = (@!public, $a).sort { .priority };
}
}
# this method freezes the regexes, combining them into a single
# regular expression that will evaluate the request and return the
# desired action.
method compile {
my sub buildspec($act) {
if $act.parent {
my $r = buildspec($act.parent);
return / $<actcap> := ( $<_parent_action_capture> := <$r> <$act.regex> ) { make $act } /;
} else {
return / $<actcap> := <$act.regex> { make $act } /;
}
}
my @subregexes;
for @!public -> $action {
push @subregexes, buildspec($action);
}
$.regex = / $<action> := <@subregexes> /;
}
method dispatch() {
self.compile unless $.regex;
if $*request.uri.path ~~ $.regex {
self.run-action($<action><?>, |$<action><actcap>);
} else {
fail 'No action matched';
}
}
method run-action($action, *@_, *%_) {
my $errors is context<rw>;
try {
$action.*begin;
$action.*execute(|@_, |%_);
CATCH {
$_.handled = 1;
$errors = $_;
}
}
$action.*end;
CONTROL {
when ControlExceptionDetach {
self.run-action(%!actions{$_.path}, |$_.capture);
}
}
}
}
# An application has components and a dispatcher
role Application {
has %.components;
has Dispatcher $.dispatcher handles <register-action dispatch>;
# this is where the several steps performed by catalyst should
# reside, so application-wide plugins can modify
multi method prepare { ... }
multi method finalize { ... }
multi method handle($request? is context = $*request,
$response? is context = $*response) {
my $application is context = self;
self.*prepare;
self.*dispatch;
self.*finalize;
};
}