File Coverage

blib/lib/Dancer2.pm
Criterion Covered Total %
statement 77 77 100.0
branch 13 14 92.8
condition 9 13 69.2
subroutine 19 19 100.0
pod 0 3 0.0
total 118 126 93.6


line stmt bran cond sub pod time code
1             package Dancer2;
2             $Dancer2::VERSION = '2.0.1';
3             # ABSTRACT: Lightweight yet powerful web application framework
4              
5 151     156   21169724 use 5.12.0;
  151         640  
6 151     151   906 use strict;
  151         364  
  151         4798  
7 151     151   827 use warnings;
  151         355  
  151         10675  
8 151     151   1010 use List::Util 'first';
  151         543  
  151         16476  
9 151     151   69827 use Module::Runtime 'use_module';
  151         262239  
  151         1167  
10 151     151   88704 use Import::Into;
  151         98380  
  151         5978  
11 151     151   67009 use Dancer2::Core;
  151         508  
  151         5087  
12 151     151   107650 use Dancer2::Core::App;
  151         840  
  151         11384  
13 151     151   96177 use Dancer2::Core::Runner;
  151         774  
  151         7747  
14 151     151   1466 use Dancer2::FileUtils;
  151         356  
  151         116949  
15              
16             our $AUTHORITY = 'SUKRIA';
17              
18 891 50   891 0 70648 sub VERSION { shift->SUPER::VERSION(@_) || '0.000000_000' }
19              
20             our $runner;
21              
22 3599     3599 0 1115131 sub runner {$runner}
23 31     31 0 523179 sub psgi_app { shift->runner->psgi_app(@_) }
24              
25             sub import {
26 223     223   284230 my ($class, @args) = @_;
27 223         1017 my ($caller, $script) = caller;
28              
29 223         564 my @final_args;
30             my $clean_import;
31 223         733 foreach my $arg (@args) {
32              
33             # ignore, no longer necessary
34             # in the future these will warn as deprecated
35 39 100       193 grep +($arg eq $_), qw<:script :syntax :tests>
36             and next;
37              
38 36 100       105 if ($arg eq ':nopragmas') {
39 1         3 $clean_import++;
40 1         3 next;
41             }
42              
43 35 100       111 if (substr($arg, 0, 1) eq '!') {
44 9         24 push @final_args, $arg, 1;
45             }
46             else {
47 26         83 push @final_args, $arg;
48             }
49             }
50              
51             $clean_import
52             or $_->import::into($caller)
53 223   66     2659 for qw;
54              
55 223 100       289615 scalar @final_args % 2
56             and die q{parameters must be key/value pairs or '!keyword'};
57              
58 221         703 my %final_args = @final_args;
59              
60 221         601 my $appname = delete $final_args{appname};
61 221   66     5801 $appname ||= $caller;
62              
63             # never instantiated the runner, should do it now
64 221 100       862 if (not defined $runner) {
65 140         1391 $runner = Dancer2::Core::Runner->new();
66             }
67              
68             # Search through registered apps, creating a new app object
69             # if we do not find one with the same name.
70 221         577 my $app;
71 221     219   1533 ($app) = first { $_->name eq $appname } @{$runner->apps};
  219         883  
  221         2456  
72              
73 221 100       1323 if (!$app) {
74              
75             # populating with the server's postponed hooks in advance
76             $app = Dancer2::Core::App->new(
77             name => $appname,
78             caller => $script,
79             environment => $runner->environment,
80 210   50     5970 postponed_hooks => $runner->postponed_hooks->{$appname} || {},
81             );
82              
83             # register the app within the runner instance
84 209         4815 $runner->register_application($app);
85             }
86              
87 220         1382 _set_import_method_to_caller($caller);
88              
89             # use config dsl class, must extend Dancer2::Core::DSL
90 220   100     1808 my $config_dsl = $app->setting('dsl_class') || 'Dancer2::Core::DSL';
91 220   66     10226 $final_args{dsl} ||= $config_dsl;
92              
93             # load the DSL, defaulting to Dancer2::Core::DSL
94 220         1192 my $dsl = use_module($final_args{dsl})->new(app => $app);
95 220         3210 $dsl->export_symbols_to($caller, \%final_args);
96             }
97              
98             sub _set_import_method_to_caller {
99 220     220   767 my ($caller) = @_;
100              
101             my $import = sub {
102 11     11   1326 my ($self, %options) = @_;
103              
104 11         40 my $with = $options{with};
105 11         5247 for my $key (keys %$with) {
106 5         32 $self->dancer_app->setting($key => $with->{$key});
107             }
108 220         2098 };
109              
110             {
111             ## no critic
112 151     151   1442 no strict 'refs';
  151         414  
  151         8387  
  220         539  
113 151     151   966 no warnings 'redefine';
  151         369  
  151         17639  
114 220         497 *{"${caller}::import"} = $import;
  220         2389  
115             }
116             }
117              
118             1;
119              
120             __END__