blib/lib/Dancer/App.pm

filter: Statementstmt 90.1% 128 / 142 Branchbran 77.5% 45 / 58 Conditioncond 84.2% 16 / 19 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 96.5% 28 / 29 Podpod 5.8% 1 / 17 totaltotal 81.5% 226 / 277 SCAR 37.6
CC
39
Cov
86%
reload_apps
42.0
set_prefix
10.0
find_route
8.0
CRAP 42.9
« blib/lib/Dancer/Renderer.pm ↑ Summary blib/lib/Dancer/Serializer/XML.pm »
1 package Dancer::App;
2 185 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Base application class for Dancer.
4 185 $Dancer::App::VERSION = '1.3520';
5 185 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 185 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 185 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 185 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 185 ▶use Dancer::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 185 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 185 ▶use Dancer::Route::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 185 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 185 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 185 ▶use Dancer::Deprecation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 185 Dancer::App->attributes(qw(name app_prefix prefix registry settings on_lexical_prefix));
18  
19 # singleton that saves any app created, we want unicity for app names
20 185 my $_apps = {};
21 1951 ▶sub applications { values %$_apps }
Subroutine applications: called CC=1 SCAR=0.0
Pod undocumented
22  
23 sub app_exists {
24 1444 ▶ my ( $self, $name ) = @_;
Subroutine app_exists: called CC=1 SCAR=0.0
Pod undocumented
25 1444 grep { $_ eq $name } keys %$_apps;
26 }
27  
28 sub set_running_app {
29 16 ▶ my ($self, $name) = @_;
Subroutine set_running_app: called CC=2 SCAR=6.9
Pod undocumented
30 16 my $app = Dancer::App->get($name);
31 16 ▶ $app = Dancer::App->new(name => $name) unless defined $app;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $app 14 2 16
32 16 Dancer::App->current($app);
33 }
34  
35 sub set_app_prefix {
36 1 ▶ my ($self, $prefix) = @_;
Subroutine set_app_prefix: called CC=1 SCAR=0.0
Pod undocumented
37 1 $self->app_prefix($prefix);
38 1 $self->prefix($prefix);
39 }
40  
41 sub get_prefix {
42 # return the current prefix (if undefined, return an empty string)
43 3 ▶ return Dancer::App->current->prefix || '';
Subroutine get_prefix: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
'Dancer::App'->current->prefix || ''
l!l
12
MC/DC100%1/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table100%2/2
A
A= 'Dancer::App'->current->prefix
Aresult
11
00
44 }
45  
46 sub incr_lexical_prefix {
47 185 ▶ no warnings; # for undefined
Subroutine BEGIN: called CC=1 SCAR=0.0
48 15 ▶ $_[0]->on_lexical_prefix( $_[0]->on_lexical_prefix + 1 );
Subroutine incr_lexical_prefix: called CC=1 SCAR=0.0
Pod undocumented
49 }
50  
51 sub dec_lexical_prefix {
52 15 ▶ $_[0]->on_lexical_prefix( $_[0]->on_lexical_prefix - 1 );
Subroutine dec_lexical_prefix: called CC=1 SCAR=0.0
Pod undocumented
53 }
54  
55 sub set_prefix {
56 29 ▶ my ($self, $prefix, $cb) = @_;
Subroutine set_prefix: called CC=10 SCAR=23.0
Pod undocumented
57  
58 29 ▶ undef $prefix if defined($prefix) and $prefix eq "/";
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix and $prefix eq "/" 1 28 29
Condition100%3/3
defined $prefix and $prefix eq "/"
!ll&&!rl&&r
2261
MC/DC100%2/2
defined $prefix and $prefix eq "/"
defined $prefix $prefix eq "/"
Truth table100%3/3
A and B
A= defined $prefixB= $prefix eq "/"
ABresult
0X0
100
111
59  
60 29 ▶ raise core_app => "not a valid prefix: `$prefix', must start with a /"
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix and not $prefix =~ m[^/] 1 28 29
Condition100%3/3
defined $prefix and not $prefix =~ m[^/]
!ll&&!rl&&r
3251
MC/DC100%2/2
defined $prefix and not $prefix =~ m[^/]
defined $prefix not $prefix =~ m[^/]
Truth table100%3/3
A and not(B)
A= defined $prefixB= not $prefix =~ m[^/]
ABresult
0X0
100
111
61 if defined($prefix) && $prefix !~ /^\//;
62  
63 28 ▶ my $app_prefix = defined $self->app_prefix ? $self->app_prefix : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->app_prefix ? : 3 25 28
64 28 my $previous = Dancer::App->current->prefix;
65  
66 28 ▶ $prefix ||= "";
Condition100%2/2
$prefix ||= ""
l!l
253
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
67  
68 28 ▶ if (Dancer::App->current->on_lexical_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Dancer::App'->current->on_lexical_prefix) { } 10 18 28
69 10 Dancer::App->current->prefix($previous.$prefix);
70 } else {
71 18 Dancer::App->current->prefix($app_prefix.$prefix);
72 }
73  
74 28 ▶ if (ref($cb) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq "CODE") 15 13 28
75 15 Dancer::App->current->incr_lexical_prefix;
76 15 eval { $cb->() };
77 15 my $e = $@;
78 15 Dancer::App->current->dec_lexical_prefix;
79 15 Dancer::App->current->prefix($previous);
80 15 ▶ die $e if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 15 15
81 }
82 28 return 1; # prefix may have been set to undef
83 }
84  
85 sub routes {
86 2 ▶ my ($self, $method) = @_;
Subroutine routes: called CC=1 SCAR=0.0
Pod undocumented
87 2 map { $_->pattern } @{$self->registry->{'routes'}{$method}};
88 }
89  
90 sub reload_apps {
91 0 ▶ my ($class) = @_;
Subroutine reload_apps: not called CC=6 SCAR=37.4
Pod undocumented
92  
93 0 Dancer::Deprecation->deprecated(
94 feature => 'auto_reload',
95 reason => 'use plackup -r instead',
96 );
97  
98 0 my @missing_modules = grep { not Dancer::ModuleLoader->load($_) }
99 qw(Module::Refresh Clone);
100  
101 0 ▶ if (not @missing_modules) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @missing_modules) { } 0 0 0
102  
103 # saving apps & purging app registries
104 0 my $orig_apps = {};
105 0 ▶ while (my ($name, $app) = each %$_apps) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_apps) 0 0 0
106 0 $orig_apps->{$name} = $app->clone;
107 0 $app->registry->init();
108 }
109  
110 # reloading changed modules, getting apps reloaded
111 0 Module::Refresh->refresh;
112  
113 # make sure old apps that didn't get reloaded are kept
114 0 ▶ while (my ($name, $app) = each %$orig_apps) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$orig_apps) 0 0 0
115 0 ▶ $_apps->{$name} = $app unless defined $_apps->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_apps->{$name} 0 0 0
116 0 ▶ $_apps->{$name} = $app if $_apps->{$name}->registry->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $_apps->{$name}->registry->is_empty 0 0 0
117 }
118  
119 }
120 else {
121 0 carp "Modules required for auto_reload are missing. Install modules"
122 . " [@missing_modules] or unset 'auto_reload' in your config file.";
123 }
124 }
125  
126 sub find_route_through_apps {
127 585 ▶ my ($class, $request) = @_;
Subroutine find_route_through_apps: called CC=4 SCAR=13.9
Pod undocumented
128 585 for my $app (Dancer::App->current, Dancer::App->applications) {
129 620 my $route = $app->find_route($request);
130 620 ▶ if ($route) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route) 558 62 620
131 558 Dancer::App->current($route->app);
132 558 return $route;
133 }
134 62 ▶ return $route if $route;
Branch: 1 branch
BranchTrueFalseTotal
if $route 0 62 62
135 }
136 27 return;
137 }
138  
139 # instance
140  
141 sub find_route {
142 620 ▶ my ($self, $request) = @_;
Subroutine find_route: called CC=8 SCAR=20.8
Pod undocumented
143 620 my $method = lc($request->method);
144  
145 # if route cache is enabled, we check if we handled this path before
146 620 ▶ if (Dancer::Config::setting('route_cache')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Dancer::Config::setting("route_cache")) 84 536 620
147 84 my $route = Dancer::Route::Cache->get->route_from_path($method,
148 $request->path_info, $self->name);
149  
150 # NOTE maybe we should cache the match data as well
151 84 ▶ if ($route) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route) 36 48 84
152 36 $route->match($request);
153 36 return $route;
154 }
155 }
156  
157 584 my @routes = @{$self->registry->routes($method)};
158  
159 584 for my $r (@routes) {
160 1552 my $match = $r->match($request);
161  
162 1552 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 536 1016 1552
163 536 ▶ next if $r->has_options && (not $r->validate_options($request));
Branch: 1 branch
BranchTrueFalseTotal
if $r->has_options and not $r->validate_options($request) 14 522 536
Condition100%3/3
$r->has_options and not $r->validate_options($request)
!ll&&!rl&&r
5101214
MC/DC100%2/2
$r->has_options and not $r->validate_options($request)
$r->has_options not $r->validate_options($request)
Truth table100%3/3
A and not(B)
A= $r->has_optionsB= not $r->validate_options($request)
ABresult
0X0
100
111
164  
165 # if we have a route cache, store the result
166 522 ▶ if (Dancer::Config::setting('route_cache')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Dancer::Config::setting("route_cache")) 48 474 522
167 48 Dancer::Route::Cache->get->store_path($method,
168 $request->path_info => $r, $self->name);
169 }
170  
171 522 return $r;
172 }
173 }
174 62 return;
175 }
176  
177 sub init {
178 116 ▶ my ($self) = @_;
Subroutine init: called CC=3 SCAR=11.0
Pod documented
179 116 ▶ $self->name('main') unless defined $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->name 100 16 116
180  
181 raise core_app => "an app named '" . $self->name . "' already exists"
182 116 ▶ if exists $_apps->{$self->name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$_apps{$self->name} 1 115 116
183  
184 # default values for properties
185 115 $self->settings({});
186 115 $self->init_registry();
187  
188 115 $_apps->{$self->name} = $self;
189 }
190  
191 sub init_registry {
192 115 ▶ my ($self, $reg) = @_;
Subroutine init_registry: called CC=2 SCAR=8.1
Pod undocumented
193 115 ▶ $self->registry($reg || Dancer::Route::Registry->new);
Condition33%1/3
$reg || 'Dancer::Route::Registry'->new
l!l&&r!l&&!r
01150
MC/DC0%0/2
$reg || 'Dancer::Route::Registry'->new
$reg 'Dancer::Route::Registry'->new
Truth table33%1/3
A || B
A= $regB= 'Dancer::Route::Registry'->new
ABresult
1X1
011
000
194  
195 }
196  
197 # singleton that saves the current active Dancer::App object
198 185 my $_current;
199  
200 sub current {
201 8211 ▶ my ($class, $app) = @_;
Subroutine current: called CC=4 SCAR=13.9
Pod undocumented
202 8211 ▶ return $_current = $app if defined $app;
Branch: 1 branch
BranchTrueFalseTotal
if defined $app 1056 7155 8211
203  
204 7155 ▶ if (not defined $_current) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_current) 99 7056 7155
205 99 ▶ $_current = Dancer::App->get('main') || Dancer::App->new();
Condition66%2/3
'Dancer::App'->get('main') || 'Dancer::App'->new
l!l&&r!l&&!r
3960
MC/DC0%0/2
'Dancer::App'->get('main') || 'Dancer::App'->new
'Dancer::App'->get('main') 'Dancer::App'->new
Truth table66%2/3
A || B
A= 'Dancer::App'->get('main')B= 'Dancer::App'->new
ABresult
1X1
011
000
206 }
207  
208 7155 return $_current;
209 }
210  
211 sub get {
212 774 ▶ my ($class, $name) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
213 774 $_apps->{$name};
214 }
215  
216 sub setting {
217 2397 ▶ my $self = shift;
Subroutine setting: called CC=5 SCAR=16.1
Pod undocumented
218  
219 2397 ▶ if ($self->name eq 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name eq "main") 2255 142 2397
220 2255 ▶ return (@_ > 1)
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 26 2229 2255
221 ? Dancer::Config::setting( @_ )
222 : Dancer::Config::setting( $_[0] );
223 }
224  
225 142 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 38 104 142
226 38 $self->_set_settings(@_)
227 } else {
228 104 my $name = shift;
229 104 ▶ exists($self->settings->{$name}) ? $self->settings->{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->settings->{$name} ? : 45 59 104
230 : Dancer::Config::setting($name);
231 }
232 }
233  
234 sub _set_settings {
235 38 ▶ my $self = shift;
Subroutine _set_settings: called CC=3 SCAR=11.0
236 38 ▶ die "Odd number of elements in set" unless @_ % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 38 38
237 38 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 38 38 76
238 38 my $name = shift;
239 38 my $value = shift;
240 38 $self->settings->{$name} =
241 Dancer::Config->normalize_setting($name => $value);
242 }
243 }
244  
245  
246 185 1;
247  
248 __END__
« blib/lib/Dancer/Renderer.pm ↑ Summary blib/lib/Dancer/Serializer/XML.pm »