File Coverage

blib/lib/Dancer2.pm
Criterion Covered Total %
statement 74 74 100.0
branch 13 14 92.8
condition 9 13 69.2
subroutine 18 18 100.0
pod 0 3 0.0
total 114 122 93.4


line stmt bran cond sub pod time code
1             package Dancer2;
2             $Dancer2::VERSION = '2.1.0';
3             # ABSTRACT: Lightweight yet powerful web application framework
4              
5 155     155   20651044 use 5.12.0;
  155         631  
6 155     155   907 use strict;
  155         321  
  155         4634  
7 155     155   882 use warnings;
  155         392  
  155         12866  
8 155     155   1042 use List::Util 'first';
  155         454  
  155         14571  
9 155     155   69995 use Module::Runtime 'use_module';
  155         264851  
  155         1125  
10 155     155   91245 use Import::Into;
  155         97515  
  155         5931  
11 155     155   66108 use Dancer2::Core;
  155         435  
  155         5492  
12 155     155   110061 use Dancer2::Core::App;
  155         1217  
  155         8453  
13 155     155   95192 use Dancer2::Core::Runner;
  155         857  
  155         113896  
14              
15             our $AUTHORITY = 'SUKRIA';
16              
17 116 50   116 0 7677 sub VERSION { shift->SUPER::VERSION(@_) || '0.000000_000' }
18              
19             our $runner;
20              
21 2408     2408 0 1088905 sub runner {$runner}
22 32     32 0 537804 sub psgi_app { shift->runner->psgi_app(@_) }
23              
24             sub import {
25 228     228   304641 my ($class, @args) = @_;
26 228         988 my ($caller, $script) = caller;
27              
28 228         598 my @final_args;
29             my $clean_import;
30 228         714 foreach my $arg (@args) {
31              
32             # ignore, no longer necessary
33             # in the future these will warn as deprecated
34 39 100       212 grep +($arg eq $_), qw<:script :syntax :tests>
35             and next;
36              
37 36 100       100 if ($arg eq ':nopragmas') {
38 1         2 $clean_import++;
39 1         3 next;
40             }
41              
42 35 100       136 if (substr($arg, 0, 1) eq '!') {
43 9         29 push @final_args, $arg, 1;
44             }
45             else {
46 26         70 push @final_args, $arg;
47             }
48             }
49              
50             $clean_import
51             or $_->import::into($caller)
52 228   66     2854 for qw;
53              
54 228 100       278114 scalar @final_args % 2
55             and die q{parameters must be key/value pairs or '!keyword'};
56              
57 226         859 my %final_args = @final_args;
58              
59 226         653 my $appname = delete $final_args{appname};
60 226   66     1680 $appname ||= $caller;
61              
62             # never instantiated the runner, should do it now
63 226 100       873 if (not defined $runner) {
64 143         1311 $runner = Dancer2::Core::Runner->new();
65             }
66              
67             # Search through registered apps, creating a new app object
68             # if we do not find one with the same name.
69 226         572 my $app;
70 226     221   1490 ($app) = first { $_->name eq $appname } @{$runner->apps};
  221         1192  
  226         3419  
71              
72 226 100       1393 if (!$app) {
73              
74             # populating with the server's postponed hooks in advance
75             $app = Dancer2::Core::App->new(
76             name => $appname,
77             caller => $script,
78             environment => $runner->environment,
79 215   50     5969 postponed_hooks => $runner->postponed_hooks->{$appname} || {},
80             );
81              
82             # register the app within the runner instance
83 214         4776 $runner->register_application($app);
84             }
85              
86 225         1448 _set_import_method_to_caller($caller);
87              
88             # use config dsl class, must extend Dancer2::Core::DSL
89 225   100     1765 my $config_dsl = $app->setting('dsl_class') || 'Dancer2::Core::DSL';
90 225   66     10551 $final_args{dsl} ||= $config_dsl;
91              
92             # load the DSL, defaulting to Dancer2::Core::DSL
93 225         1278 my $dsl = use_module($final_args{dsl})->new(app => $app);
94 225         3103 $dsl->export_symbols_to($caller, \%final_args);
95             }
96              
97             sub _set_import_method_to_caller {
98 225     225   732 my ($caller) = @_;
99              
100             my $import = sub {
101 11     11   1955 my ($self, %options) = @_;
102              
103 11         42 my $with = $options{with};
104 11         5511 for my $key (keys %$with) {
105 5         32 $self->dancer_app->setting($key => $with->{$key});
106             }
107 225         1973 };
108              
109             {
110             ## no critic
111 155     155   1720 no strict 'refs';
  155         429  
  155         8760  
  225         557  
112 155     155   1242 no warnings 'redefine';
  155         506  
  155         17410  
113 225         564 *{"${caller}::import"} = $import;
  225         2323  
114             }
115             }
116              
117             1;
118              
119             __END__