forked from PerlDancer/Dancer2
/
DSL.pm
389 lines (307 loc) · 9.8 KB
/
DSL.pm
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
384
385
386
387
388
389
# ABSTRACT: Dancer2's Domain Specific Language (DSL)
package Dancer2::Core::DSL;
use Moo;
use Carp;
use Class::Load 'load_class';
use Dancer2::Core::Hook;
use Dancer2::Core::Error;
use Dancer2::FileUtils;
with 'Dancer2::Core::Role::DSL';
sub dsl_keywords {
# the flag means : 1 = is global, 0 = is not global. global means can be
# called from anywhere. not global means must be called from within a route
# handler
{ any => { is_global => 1 },
app => { is_global => 1 },
captures => { is_global => 0 },
config => { is_global => 1 },
content_type => { is_global => 0 },
context => { is_global => 0 },
cookie => { is_global => 0 },
cookies => { is_global => 0 },
dance => { is_global => 1 },
dancer_app => { is_global => 1 },
dancer_version => { is_global => 1 },
dancer_major_version => { is_global => 1 },
debug => { is_global => 1 },
del => { is_global => 1 },
dirname => { is_global => 1 },
dsl => { is_global => 1 },
engine => { is_global => 1 },
error => { is_global => 1 },
false => { is_global => 1 },
forward => { is_global => 0 },
from_dumper => { is_global => 1 },
from_json => { is_global => 1 },
from_yaml => { is_global => 1 },
get => { is_global => 1 },
halt => { is_global => 0 },
header => { is_global => 0 },
headers => { is_global => 0 },
hook => { is_global => 1 },
info => { is_global => 1 },
log => { is_global => 1 },
mime => { is_global => 1 },
options => { is_global => 1 },
param => { is_global => 0 },
params => { is_global => 0 },
pass => { is_global => 0 },
patch => { is_global => 1 },
path => { is_global => 1 },
post => { is_global => 1 },
prefix => { is_global => 1 },
push_header => { is_global => 0 },
put => { is_global => 1 },
redirect => { is_global => 0 },
request => { is_global => 0 },
response => { is_global => 0 },
runner => { is_global => 1 },
send_error => { is_global => 0 },
send_file => { is_global => 0 },
session => { is_global => 0 },
set => { is_global => 1 },
setting => { is_global => 1 },
splat => { is_global => 0 },
start => { is_global => 1 },
status => { is_global => 0 },
template => { is_global => 0 },
to_dumper => { is_global => 1 },
to_json => { is_global => 1 },
to_yaml => { is_global => 1 },
true => { is_global => 1 },
upload => { is_global => 0 },
uri_for => { is_global => 0 },
var => { is_global => 0 },
vars => { is_global => 0 },
warning => { is_global => 1 },
};
}
sub dancer_app { shift->app }
sub dancer_version { Dancer2->VERSION }
sub dancer_major_version {
return ( split /\./, dancer_version )[0];
}
sub debug { shift->log( debug => @_ ) }
sub info { shift->log( info => @_ ) }
sub warning { shift->log( warning => @_ ) }
sub error { shift->log( error => @_ ) }
sub true {1}
sub false {0}
sub dirname { shift and Dancer2::FileUtils::dirname(@_) }
sub path { shift and Dancer2::FileUtils::path(@_) }
sub config { shift->app->settings }
sub engine { shift->app->engine(@_) }
=func setting
Lets you define settings and access them:
setting('foo' => 42);
setting('foo' => 42, 'bar' => 43);
my $foo=setting('foo');
If settings were defined returns number of settings.
=cut
sub setting { shift->app->setting(@_) }
=func set ()
alias for L<setting>:
set('foo' => '42');
my $port=set('port');
=cut
sub set { shift->setting(@_) }
sub template { shift->app->template(@_) }
sub session { shift->app->session(@_) }
sub send_file { shift->app->send_file(@_) }
#
# route handlers & friends
#
sub hook {
my ( $self, $name, $code ) = @_;
$self->app->add_hook(
Dancer2::Core::Hook->new( name => $name, code => $code ) );
}
sub prefix {
my $app = shift->app;
@_ == 1
? $app->prefix(@_)
: $app->lexical_prefix(@_);
}
sub halt { shift->app->context->response->halt }
sub _route_parameters {
my ( $regexp, $code, $options );
( scalar @_ == 3 )
? ( ( $regexp, $code, $options ) = ( $_[0], $_[2], $_[1] ) )
: ( ( $regexp, $code, $options ) = ( $_[0], $_[1], {} ) );
return ( $regexp, $code, $options );
}
sub get {
my $app = shift->app;
my ( $regexp, $code, $options ) = _route_parameters(@_);
for my $method (qw/get head/) {
$app->add_route(
method => $method,
regexp => $regexp,
code => $code,
options => $options
);
}
}
sub post {
my $app = shift->app;
my ( $regexp, $code, $options ) = _route_parameters(@_);
$app->add_route(
method => 'post',
regexp => $regexp,
code => $code,
options => $options
);
}
sub any {
my ( $self, $methods, @params ) = @_;
my $app = $self->app;
if ($methods) {
if ( ref($methods) ne 'ARRAY' ) {
unshift @params, $methods;
$methods = [qw(get post put del options patch)];
}
}
for my $method ( @{$methods} ) {
$self->$method(@params);
}
}
sub put {
my $app = shift->app;
my ( $regexp, $code, $options ) = _route_parameters(@_);
$app->add_route(
method => 'put',
regexp => $regexp,
code => $code,
options => $options,
);
}
sub del {
my $app = shift->app;
my ( $regexp, $code, $options ) = _route_parameters(@_);
$app->add_route(
method => 'delete',
regexp => $regexp,
code => $code,
options => $options,
);
}
sub options {
my $app = shift->app;
my ( $regexp, $code, $options ) = _route_parameters(@_);
$app->add_route(
method => 'options',
regexp => $regexp,
code => $code,
options => $options,
);
}
sub patch {
my $app = shift->app;
my ( $regexp, $code, $options ) = _route_parameters(@_);
$app->add_route(
method => 'patch',
regexp => $regexp,
code => $code,
options => $options,
);
}
#
# Server startup
#
# access to the runner singleton
# will be populated on-the-fly when needed
# this singleton contains anything needed to start the application server
sub runner { Dancer2->runner }
# start the server
sub start { shift->runner->start }
sub dance { shift->start(@_) }
#
# Response alterations
#
sub status { shift->response->status(@_) }
sub push_header { shift->response->push_header(@_) }
sub header { shift->response->header(@_) }
sub headers { shift->response->header(@_) }
sub content_type { shift->response->content_type(@_) }
sub pass { shift->response->pass }
#
# Route handler helpers
#
sub context { shift->app->context }
sub request { shift->context->request }
sub response { shift->context->response }
sub upload { shift->request->upload(@_) }
sub captures { shift->request->captures }
sub uri_for { shift->request->uri_for(@_) }
sub splat { shift->request->splat }
sub params { shift->request->params(@_) }
sub param { shift->request->param(@_) }
sub redirect { shift->context->redirect(@_) }
sub forward {
my $self = shift;
$self->request->forward($self->context, @_);
}
sub vars { shift->context->vars }
sub var { shift->context->var(@_) }
sub cookies { shift->context->cookies }
sub mime {
my $self = shift;
if ( $self->app ) {
return $self->app->mime_type;
}
else {
my $runner = $self->runner;
$runner->mime_type->reset_default;
return $runner->mime_type;
}
}
sub cookie { shift->context->cookie(@_) }
sub send_error {
my ( $self, $message, $status ) = @_;
my $serializer = $self->app->engine('serializer');
my $x = Dancer2::Core::Error->new(
message => $message,
context => $self->app->context,
( status => $status ) x !!$status,
( serializer => $serializer ) x !!$serializer,
)->throw;
$x;
}
#
# engines
#
sub from_json {
my $app = shift->app;
require 'Dancer2/Serializer/JSON.pm';
Dancer2::Serializer::JSON::from_json(@_);
}
sub to_json {
my $app = shift->app;
require 'Dancer2/Serializer/JSON.pm';
Dancer2::Serializer::JSON::to_json(@_);
}
sub from_yaml {
my $app = shift->app;
require 'Dancer2/Serializer/YAML.pm';
Dancer2::Serializer::YAML::from_yaml(@_);
}
sub to_yaml {
my $app = shift->app;
require 'Dancer2/Serializer/YAML.pm';
Dancer2::Serializer::YAML::to_yaml(@_);
}
sub from_dumper {
my $app = shift->app;
require 'Dancer2/Serializer/Dumper.pm';
Dancer2::Serializer::Dumper::from_dumper(@_);
}
sub to_dumper {
my $app = shift->app;
require 'Dancer2/Serializer/Dumper.pm';
Dancer2::Serializer::Dumper::to_dumper(@_);
}
sub log { shift->app->log(@_) }
=head1 SEE ALSO
L<http://advent.perldancer.org/2010/18>
=cut
1;