Debian Codegraph @

lib/Dancer2/Core/DSL.pm

raw
1 # ABSTRACT: Dancer2's Domain Specific Language (DSL)
2
3 package Dancer2::Core::DSL;
4 $Dancer2::Core::DSL::VERSION = '2.2.2';
5 use Moo;
6 use Carp;
7 use Path::Tiny ();
8 use Module::Runtime 'require_module';
9 use Ref::Util qw< is_arrayref is_hashref >;
10 use Dancer2::Core::Hook;
11 use Dancer2::Core::Response::Delayed;
12
13 with 'Dancer2::Core::Role::DSL';
14
15 sub hook_aliases { +{} }
16 sub supported_hooks { () }
17
18 sub _add_postponed_plugin_hooks {
19 my ( $self, $postponed_hooks) = @_;
20
21 $postponed_hooks = $postponed_hooks->{'plugin'};
22 return unless defined $postponed_hooks;
23
24 for my $plugin ( keys %{$postponed_hooks} ) {
25 for my $name ( keys %{$postponed_hooks->{$plugin} } ) {
26 my $hook = $postponed_hooks->{$plugin}{$name}{hook};
27 my $caller = $postponed_hooks->{$plugin}{$name}{caller};
28
29 $self->has_hook($name)
30 or croak "plugin $plugin does not support the hook `$name'. ("
31 . join( ", ", @{$caller} ) . ")";
32
33 $self->add_hook($hook);
34 }
35 }
36 }
37
38 sub dsl_keywords {
39
40 # the flag means : 1 = is global, 0 = is not global. global means can be
41 # called from anywhere. not global means must be called from within a route
42 # handler
43 { any => { is_global => 1 },
44 app => { is_global => 1 },
45 captures => { is_global => 0 },
46 config => { is_global => 1 },
47 content => { is_global => 0 },
48 content_type => { is_global => 0 },
49 context => { is_global => 0 },
50 cookie => { is_global => 0 },
51 cookies => { is_global => 0 },
52 dance => { is_global => 1 },
53 dancer_app => { is_global => 1 },
54 dancer_version => { is_global => 1 },
55 dancer_major_version => { is_global => 1 },
56 debug => { is_global => 1 },
57 decode_json => { is_global => 1 },
58 del => { is_global => 1 },
59 delayed => {
60 is_global => 0, prototype => '&@',
61 },
62 dirname => { is_global => 1 },
63 done => { is_global => 0 },
64 dsl => { is_global => 1 },
65 encode_json => { is_global => 1 },
66 engine => { is_global => 1 },
67 error => { is_global => 1 },
68 false => { is_global => 1 },
69 flush => { is_global => 0 },
70 forward => { is_global => 0 },
71 from_json => { is_global => 1 },
72 from_yaml => { is_global => 1 },
73 get => { is_global => 1 },
74 halt => { is_global => 0 },
75 header => { is_global => 0 },
76 headers => { is_global => 0 },
77 hook => { is_global => 1 },
78 info => { is_global => 1 },
79 log => { is_global => 1 },
80 mime => { is_global => 1 },
81 options => { is_global => 1 },
82 param => { is_global => 0 },
83 params => { is_global => 0 },
84 query_parameters => { is_global => 0 },
85 body_parameters => { is_global => 0 },
86 route_parameters => { is_global => 0 },
87 pass => { is_global => 0 },
88 patch => { is_global => 1 },
89 path => { is_global => 1 },
90 post => { is_global => 1 },
91 prefix => { is_global => 1 },
92 prepare_app => {
93 is_global => 1, prototype => '&',
94 },
95 psgi_app => { is_global => 1 },
96 push_header => { is_global => 0 },
97 push_response_header => { is_global => 0 },
98 put => { is_global => 1 },
99 redirect => { is_global => 0 },
100 request => { is_global => 0 },
101 request_data => { is_global => 0 },
102 request_header => { is_global => 0 },
103 response => { is_global => 0 },
104 response_header => { is_global => 0 },
105 response_headers => { is_global => 0 },
106 runner => { is_global => 1 },
107 send_as => { is_global => 0 },
108 send_error => { is_global => 0 },
109 send_file => { is_global => 0 },
110 session => { is_global => 0 },
111 set => { is_global => 1 },
112 setting => { is_global => 1 },
113 splat => { is_global => 0 },
114 start => { is_global => 1 },
115 status => { is_global => 0 },
116 template => { is_global => 1 },
117 to_app => { is_global => 1 },
118 to_json => { is_global => 1 },
119 to_yaml => { is_global => 1 },
120 true => { is_global => 1 },
121 upload => { is_global => 0 },
122 uri_for => { is_global => 0 },
123 uri_for_route => { is_global => 0 },
124 var => { is_global => 0 },
125 vars => { is_global => 0 },
126 warning => { is_global => 1 },
127 };
128 }
129
130 sub dancer_app { shift->app }
131 sub dancer_version { Dancer2->VERSION }
132
133 sub dancer_major_version {
134 return ( split /\./, dancer_version )[0];
135 }
136
137 sub log { shift->app->log( @_ ) }
138 sub debug { shift->app->log( debug => @_ ) }
139 sub info { shift->app->log( info => @_ ) }
140 sub warning { shift->app->log( warning => @_ ) }
141 sub error { shift->app->log( error => @_ ) }
142
143 sub true {1}
144 sub false {0}
145
146 sub _path_obj { Path::Tiny::path(@_) }
147 sub dirname { shift and _path_obj(@_)->parent->stringify }
148 sub path { shift and _path_obj(@_)->stringify }
149
150 sub config { shift->app->settings }
151
152 sub engine { shift->app->engine(@_) }
153
154 sub setting { shift->app->setting(@_) }
155
156 sub set { shift->setting(@_) }
157
158 sub template { shift->app->template(@_) }
159
160 sub session {
161 my ( $self, $key, $value ) = @_;
162
163 # shortcut reads if no session exists, so we don't
164 # instantiate sessions for no reason
165 if ( @_ == 2 ) {
166 return unless $self->app->has_session;
167 }
168
169 my $session = $self->app->session
170 || croak "No session available, a session engine needs to be set";
171
172 $self->app->setup_session;
173
174 # return the session object if no key
175 @_ == 1 and return $session;
176
177 # read if a key is provided
178 @_ == 2 and return $session->read($key);
179
180
181 # write to the session or delete if value is undef
182 if ( defined $value ) {
183 $session->write( $key => $value );
184 }
185 else {
186 $session->delete($key);
187 }
188 }
189
190 sub send_as { shift->app->send_as(@_) }
191
192 sub send_error { shift->app->send_error(@_) }
193
194 sub send_file { shift->app->send_file(@_) }
195
196 #
197 # route handlers & friends
198 #
199
200 sub hook {
201 my ( $self, $name, $code ) = @_;
202 $self->app->add_hook(
203 Dancer2::Core::Hook->new( name => $name, code => $code ) );
204 }
205
206 sub prefix {
207 my $app = shift->app;
208 @_ == 1
209 ? $app->prefix(@_)
210 : $app->lexical_prefix(@_);
211 }
212
213 sub halt { shift->app->halt(@_) }
214
215 sub del { shift->_normalize_route( [qw/delete /], @_ ) }
216 sub get { shift->_normalize_route( [qw/get head/], @_ ) }
217 sub options { shift->_normalize_route( [qw/options /], @_ ) }
218 sub patch { shift->_normalize_route( [qw/patch /], @_ ) }
219 sub post { shift->_normalize_route( [qw/post /], @_ ) }
220 sub put { shift->_normalize_route( [qw/put /], @_ ) }
221
222 sub prepare_app { push @{ shift->app->prep_apps }, @_ }
223
224 sub any {
225 my $self = shift;
226
227 # If they've supplied their own list of methods,
228 # expand del, otherwise give them the default list.
229 if ( is_arrayref($_[0]) ) {
230 s/^del$/delete/ for @{ $_[0] };
231 }
232 else {
233 unshift @_, [qw/delete get head options patch post put/];
234 }
235
236 $self->_normalize_route(@_);
237 }
238
239 sub _normalize_route {
240 my $app = shift->app;
241 my $methods = shift;
242 my %args;
243
244 # Options are optional, try to deduce their presence from arg length.
245 if ( @_ == 4 ) {
246 # @_ = ( NAME, REGEXP, OPTIONS, CODE )
247 # get 'foo', '/foo', { 'user_agent' => '...' }, sub {...}
248 @args{qw<name regexp options code>} = @_;
249 } elsif ( @_ == 2 ) {
250 # @_ = ( REGEXP, CODE )
251 # get '/foo', sub {...}
252 @args{qw<regexp code>} = @_;
253 } elsif ( @_ == 3 ) {
254 # @_ = ( REGEXP, OPTIONS, CODE )
255 # get '/foo', { 'user_agent' => '...' }, sub {...}
256 # @_ = ( NAME, REGEXP, CODE )
257 # get 'foo', '/foo',sub {...}
258 if ( is_hashref( $_[1] ) ) {
259 @args{qw<regexp options code>} = @_;
260 } else {
261 @args{qw<name regexp code>} = @_;
262 }
263 }
264
265 return map $app->add_route( %args, method => $_ ), @{$methods};
266 }
267
268 #
269 # Server startup
270 #
271
272 # access to the runner singleton
273 # will be populated on-the-fly when needed
274 # this singleton contains anything needed to start the application server
275 sub runner { Dancer2->runner }
276
277 # start the server
278 sub start { shift->runner->start }
279
280 sub dance { shift->start(@_) }
281
282 sub psgi_app {
283 my $self = shift;
284
285 $self->app->to_app;
286 }
287
288 sub to_app { shift->app->to_app }
289
290 #
291 # Response alterations
292 #
293
294 sub status {
295 $Dancer2::Core::Route::RESPONSE->status( $_[1] );
296 }
297
298 sub push_header {
299 Carp::croak "DEPRECATED: push_header keyword. Please use the 'push_response_header' keyword instead of 'push_header'";
300 }
301
302 sub push_response_header {
303 shift;
304 $Dancer2::Core::Route::RESPONSE->push_header(@_);
305 }
306
307 sub header {
308 Carp::croak "DEPRECATED: header keyword. Please use the 'response_header' keyword instead of 'header'";
309 }
310
311 sub response_header {
312 shift;
313 $Dancer2::Core::Route::RESPONSE->header(@_);
314 }
315
316 sub headers {
317 Carp::croak "DEPRECATED: headers keyword. Please use the 'response_headers' keyword instead of 'headers'";
318 }
319
320 sub response_headers {
321 shift;
322 $Dancer2::Core::Route::RESPONSE->header(@_);
323 }
324
325 sub content {
326 my $dsl = shift;
327
328 # simple synchronous response
329 my $responder = $Dancer2::Core::Route::RESPONDER
330 or croak 'Cannot use content keyword outside delayed response';
331
332 # flush if wasn't flushed before
333 if ( !$Dancer2::Core::Route::WRITER ) {
334 $Dancer2::Core::Route::WRITER = $responder->([
335 $Dancer2::Core::Route::RESPONSE->status,
336 $Dancer2::Core::Route::RESPONSE->headers_to_array,
337 ]);
338 }
339
340 eval {
341 $Dancer2::Core::Route::WRITER->write(@_);
342 1;
343 } or do {
344 my $error = $@ || 'Zombie Error';
345 $Dancer2::Core::Route::ERROR_HANDLER
346 ? $Dancer2::Core::Route::ERROR_HANDLER->($error)
347 : $dsl->app->logger_engine->log(
348 warning => "Error in delayed response: $error"
349 );
350 };
351 }
352
353 sub content_type {
354 shift;
355 $Dancer2::Core::Route::RESPONSE->content_type(@_);
356 }
357
358 sub delayed {
359 my ( $dsl, $cb, @args ) = @_;
360
361 @args % 2 == 0
362 or croak 'Arguments to delayed() keyword must be key/value pairs';
363
364 # first time, responder doesn't exist yet
365 my %opts = @args;
366 $Dancer2::Core::Route::RESPONDER
367 or return Dancer2::Core::Response::Delayed->new(
368 cb => $cb,
369 request => $Dancer2::Core::Route::REQUEST,
370 response => $Dancer2::Core::Route::RESPONSE,
371
372 ( error_cb => $opts{'on_error'} )x!! $opts{'on_error'},
373 );
374
375 # we're in an async request process
376 my $request = $Dancer2::Core::Route::REQUEST;
377 my $response = $Dancer2::Core::Route::RESPONSE;
378 my $responder = $Dancer2::Core::Route::RESPONDER;
379 my $writer = $Dancer2::Core::Route::WRITER;
380 my $handler = $Dancer2::Core::Route::ERROR_HANDLER;
381
382 return sub {
383 local $Dancer2::Core::Route::REQUEST = $request;
384 local $Dancer2::Core::Route::RESPONSE = $response;
385 local $Dancer2::Core::Route::RESPONDER = $responder;
386 local $Dancer2::Core::Route::WRITER = $writer;
387 local $Dancer2::Core::Route::ERROR_HANDLER = $handler;
388
389 $cb->(@_);
390 };
391 }
392
393 sub flush {
394 my $responder = $Dancer2::Core::Route::RESPONDER
395 or croak 'flush() called outside streaming response';
396
397 my $response = $Dancer2::Core::Route::RESPONSE;
398 $Dancer2::Core::Route::WRITER = $responder->([
399 $response->status, $response->headers_to_array,
400 ]);
401 }
402
403 sub done {
404 my $writer = $Dancer2::Core::Route::WRITER
405 or croak 'done() called outside streaming response';
406
407 $writer->close;
408 }
409
410 sub pass { shift->app->pass }
411
412 #
413 # Route handler helpers
414 #
415
416 sub context {
417 Carp::croak "DEPRECATED: context keyword. Please use the 'app' keyword instead of 'context'";
418 }
419
420 sub request { $Dancer2::Core::Route::REQUEST }
421
422 sub request_header { shift; $Dancer2::Core::Route::REQUEST->headers->header(@_) }
423
424 sub response { $Dancer2::Core::Route::RESPONSE }
425
426 sub upload { shift; $Dancer2::Core::Route::REQUEST->upload(@_); }
427
428 sub captures { $Dancer2::Core::Route::REQUEST->captures }
429
430 sub uri_for { shift; $Dancer2::Core::Route::REQUEST->uri_for(@_); }
431
432 sub uri_for_route { shift->app->uri_for_route(@_); }
433
434 sub splat { $Dancer2::Core::Route::REQUEST->splat }
435
436 sub params { shift; $Dancer2::Core::Route::REQUEST->params(@_); }
437
438 sub param { shift; $Dancer2::Core::Route::REQUEST->param(@_); }
439
440 sub query_parameters { shift; $Dancer2::Core::Route::REQUEST->query_parameters(@_); }
441 sub body_parameters { shift; $Dancer2::Core::Route::REQUEST->body_parameters(@_); }
442 sub route_parameters { shift; $Dancer2::Core::Route::REQUEST->route_parameters(@_); }
443
444 sub request_data { shift; $Dancer2::Core::Route::REQUEST->body_data(@_); }
445
446 sub redirect { shift->app->redirect(@_) }
447
448 sub forward { shift->app->forward(@_) }
449
450 sub vars { $Dancer2::Core::Route::REQUEST->vars }
451
452 sub var { shift; $Dancer2::Core::Route::REQUEST->var(@_); }
453
454 sub cookies { $Dancer2::Core::Route::REQUEST->cookies }
455 sub cookie { shift->app->cookie(@_) }
456
457 sub mime {
458 my $self = shift;
459 if ( $self->app ) {
460 return $self->app->mime_type;
461 }
462 else {
463 my $runner = $self->runner;
464 $runner->mime_type->reset_to_default;
465 return $runner->mime_type;
466 }
467 }
468
469 #
470 # engines
471 #
472
473 sub from_json {
474 shift; # remove first element
475 require_module('Dancer2::Serializer::JSON');
476 Dancer2::Serializer::JSON::from_json(@_);
477 }
478
479 sub to_json {
480 shift; # remove first element
481 require_module('Dancer2::Serializer::JSON');
482 Dancer2::Serializer::JSON::to_json(@_);
483 }
484
485 sub decode_json {
486 shift; # remove first element
487 require_module('Dancer2::Serializer::JSON');
488 Dancer2::Serializer::JSON::decode_json(@_);
489 }
490
491 sub encode_json {
492 shift; # remove first element
493 require_module('Dancer2::Serializer::JSON');
494 Dancer2::Serializer::JSON::encode_json(@_);
495 }
496
497 sub from_yaml {
498 shift; # remove first element
499 require_module('Dancer2::Serializer::YAML');
500 Dancer2::Serializer::YAML::from_yaml(@_);
501 }
502
503 sub to_yaml {
504 shift; # remove first element
505 require_module('Dancer2::Serializer::YAML');
506 Dancer2::Serializer::YAML::to_yaml(@_);
507 }
508
509 1;
510
511 __END__
512
513 =pod
514
515 =encoding UTF-8
516
517 =head1 NAME
518
519 Dancer2::Core::DSL - Dancer2's Domain Specific Language (DSL)
520
521 =head1 VERSION
522
523 version 2.2.2
524
525 =head1 FUNCTIONS
526
527 =head2 setting
528
529 Lets you define settings and access them:
530
531 setting('foo' => 42);
532 setting('foo' => 42, 'bar' => 43);
533 my $foo=setting('foo');
534
535 If settings were defined returns number of settings.
536
537 =head2 set ()
538
539 alias for L<setting>:
540
541 set('foo' => '42');
542 my $port=set('port');
543
544 =head1 SEE ALSO
545
546 L<http://advent.perldancer.org/2010/18>
547
548 =head1 AUTHOR
549
550 Dancer Core Developers
551
552 =head1 COPYRIGHT AND LICENSE
553
554 This software is copyright (c) 2026 by Alexis Sukrieh.
555
556 This is free software; you can redistribute it and/or modify it under
557 the same terms as the Perl 5 programming language system itself.
558
559 =cut