lib/Catalyst/Utils.pm

filter: Statementstmt 86.4% 224 / 259 Branchbran 68.7% 66 / 96 Conditioncond 53.3% 40 / 75 MC/DCmcdc 27.9% 12 / 43 Subroutinesub 95.2% 40 / 42 Podpod 100% 22 / 22 totaltotal 75.2% 404 / 537 CC 83 SCAR 28.8
CC
83
Cov
77%
term_width
98.3
class2tempdir
42.0
inject_component
13.3
CRAP 17.8
« lib/Catalyst/DispatchType/Chained.pm ↑ Summary lib/Catalyst/Engine.pm »
1 package Catalyst::Utils;
2 188 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 187 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 187 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 187 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 187 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 187 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 187 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 187 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 187 ▶use Class::Load 'is_class_loaded';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 187 ▶use String::RewritePrefix;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 187 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 187 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 187 ▶use Devel::InnerPackage;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 187 ▶use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 Catalyst::Utils - The Catalyst Utils
21  
22 =head1 SYNOPSIS
23  
24 See L<Catalyst>.
25  
26 =head1 DESCRIPTION
27  
28 Catalyst Utilities.
29  
30 =head1 METHODS
31  
32 =head2 appprefix($class)
33  
34 MyApp::Foo becomes myapp_foo
35  
36 =cut
37  
38 92 ▶sub appprefix {
Subroutine appprefix: called CC=1 SCAR=0.0
Pod documented
39 92 my $class = shift;
40 92 $class =~ s/::/_/g;
41 92 $class = lc($class);
42 92 return $class;
43 }
44  
45 =head2 class2appclass($class);
46  
47 MyApp::Controller::Foo::Bar becomes MyApp
48 My::App::Controller::Foo::Bar becomes My::App
49  
50 =cut
51  
52 6950 ▶sub class2appclass {
Subroutine class2appclass: called CC=3 SCAR=11.0
Pod documented
53 6950 ▶ my $class = shift || '';
Condition50%1/2
shift() || ''
l!l
69500
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
54 6950 my $appname = '';
55 6950 ▶ if ( $class =~ /^(.+?)::([MVC]|Model|View|Controller)::.+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^(.+?)::([MVC]|Model|View|Controller)::.+$/) 6949 1 6950
56 6949 $appname = $1;
57 }
58 6950 return $appname;
59 }
60  
61 =head2 class2classprefix($class);
62  
63 MyApp::Controller::Foo::Bar becomes MyApp::Controller
64 My::App::Controller::Foo::Bar becomes My::App::Controller
65  
66 =cut
67  
68 1 ▶sub class2classprefix {
Subroutine class2classprefix: called CC=3 SCAR=11.3
Pod documented
69 1 ▶ my $class = shift || '';
Condition50%1/2
shift() || ''
l!l
10
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
70 1 my $prefix;
71 1 ▶ if ( $class =~ /^(.+?::([MVC]|Model|View|Controller))::.+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^(.+?::([MVC]|Model|View|Controller))::.+$/) 1 0 1
72 1 $prefix = $1;
73 }
74 1 return $prefix;
75 }
76  
77 =head2 class2classsuffix($class);
78  
79 MyApp::Controller::Foo::Bar becomes Controller::Foo::Bar
80  
81 =cut
82  
83 6871 ▶sub class2classsuffix {
Subroutine class2classsuffix: called CC=3 SCAR=11.0
Pod documented
84 6871 ▶ my $class = shift || '';
Condition50%1/2
shift() || ''
l!l
68710
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
85 6871 ▶ my $prefix = class2appclass($class) || '';
Condition100%2/2
class2appclass($class) || ''
l!l
68701
MC/DC100%1/1
class2appclass($class) || ''
class2appclass($class)
Truth table100%2/2
A
A= class2appclass($class)
Aresult
11
00
86 6871 $class =~ s/$prefix\:://;
87 6871 return $class;
88 }
89  
90 =head2 class2env($class);
91  
92 Returns the environment name for class.
93  
94 MyApp becomes MYAPP
95 My::App becomes MY_APP
96  
97 =cut
98  
99 1062 ▶sub class2env {
Subroutine class2env: called CC=2 SCAR=7.1
Pod documented
100 1062 ▶ my $class = shift || '';
Condition50%1/2
shift() || ''
l!l
10620
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
101 1062 $class =~ s/::/_/g;
102 1062 return uc($class);
103 }
104  
105 =head2 class2prefix( $class, $case );
106  
107 Returns the uri prefix for a class. If case is false the prefix is converted to lowercase.
108  
109 My::App::Controller::Foo::Bar becomes foo/bar
110  
111 =cut
112  
113 6097 ▶sub class2prefix {
Subroutine class2prefix: called CC=5 SCAR=16.5
Pod documented
114 6097 ▶ my $class = shift || '';
Condition50%1/2
shift() || ''
l!l
60970
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
115 6097 ▶ my $case = shift || 0;
Condition50%1/2
shift() || 0
l!l
06097
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
116 6097 my $prefix;
117 6097 ▶ if ( $class =~ /^.+?::([MVC]|Model|View|Controller)::(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /^.+?::([MVC]|Model|View|Controller)::(.+)$/) 5946 151 6097
118 5946 ▶ $prefix = $case ? $2 : lc $2;
Branch: 1 branch
BranchTrueFalseTotal
$case ? : 0 5946 5946
119 5946 $prefix =~ s{::}{/}g;
120 }
121 6097 return $prefix;
122 }
123  
124 =head2 class2tempdir( $class [, $create ] );
125  
126 Returns a tempdir for a class. If create is true it will try to create the path.
127  
128 My::App becomes /tmp/my/app
129 My::App::Controller::Foo::Bar becomes /tmp/my/app/c/foo/bar
130  
131 =cut
132  
133 0 ▶sub class2tempdir {
Subroutine class2tempdir: not called CC=6 SCAR=37.4
Pod documented
134 0 ▶ my $class = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
135 0 ▶ my $create = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
136 0 my @parts = split '::', lc $class;
137  
138 0 my $tmpdir = dir( File::Spec->tmpdir, @parts )->cleanup;
139  
140 0 ▶ if ( $create && !-e $tmpdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create and not -e $tmpdir) 0 0 0
Condition0%0/3
$create and not -e $tmpdir
!ll&&!rl&&r
000
MC/DC0%0/2
$create and not -e $tmpdir
$create not -e $tmpdir
Truth table0%0/3
A and not(B)
A= $createB= not -e $tmpdir
ABresult
0X0
100
111
141  
142 0 eval { $tmpdir->mkpath; 1 }
143 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $tmpdir->mkpath; 1 } } or do { require Catalyst::Exception; "Catalyst::Exception"->throw("message", qq[Couldn't create tmpdir '${tmpdir}', "$@"]) } 0 0 0
144 # don't load Catalyst::Exception as a BEGIN in Utils,
145 # because Utils often gets loaded before MyApp.pm, and if
146 # Catalyst::Exception is loaded before MyApp.pm, it does
147 # not honor setting
148 # $Catalyst::Exception::CATALYST_EXCEPTION_CLASS in
149 # MyApp.pm
150 0 require Catalyst::Exception;
151 0 Catalyst::Exception->throw(
152 message => qq/Couldn't create tmpdir '$tmpdir', "$@"/ );
153 }
154 }
155  
156 0 return $tmpdir->stringify;
157 }
158  
159 =head2 home($class)
160  
161 Returns home directory for given class.
162  
163 =head2 dist_indicator_file_list
164  
165 Returns a list of files which can be tested to check if you're inside
166 a CPAN distribution which is not yet installed.
167  
168 These are:
169  
170 =over
171  
172 =item Makefile.PL
173  
174 =item Build.PL
175  
176 =item dist.ini
177  
178 =item L<cpanfile>
179  
180 =back
181  
182 =cut
183  
184 257 ▶sub dist_indicator_file_list {
Subroutine dist_indicator_file_list: called CC=1 SCAR=0.0
Pod documented
185 257 qw{Makefile.PL Build.PL dist.ini cpanfile};
186 }
187  
188 331 ▶sub home {
Subroutine home: called CC=10 SCAR=23.1
Pod documented
189 331 my $class = shift;
190  
191 # make an $INC{ $key } style string from the class name
192 331 (my $file = "$class.pm") =~ s{::}{/}g;
193  
194 331 ▶ if ( my $inc_entry = $INC{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inc_entry = $INC{$file}) 251 80 331
195 {
196 # look for an uninstalled Catalyst app
197  
198 # find the @INC entry in which $file was found
199 251 (my $path = $inc_entry) =~ s/$file$//;
200 251 ▶ $path ||= cwd() if !defined $path || !length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 1 250 251
Condition50%3/6
defined $path and length $path
!ll&&!rl&&r
01250
$path ||= cwd()
l!l&&r!l&&!r
010
MC/DC50%1/2
defined $path and length $path
defined $path length $path
MC/DC0%0/2
$path ||= cwd()
$path cwd()
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
Truth table33%1/3
A ||= B
A= $pathB= cwd()
ABresult
1X1
011
000
201 251 my $home = dir($path)->absolute->cleanup;
202  
203 # pop off /lib and /blib if they're there
204 251 ▶ $home = $home->parent while $home =~ /b?lib$/;
Branch: 1 branch
BranchTrueFalseTotal
$home =~ /b?lib$/ and do { $home = $home->parent } 232 251 483
205  
206 # only return the dir if it has a Makefile.PL or Build.PL or dist.ini
207 251 ▶ if (grep { -f $home->file($_) } dist_indicator_file_list()) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {-f $home->file($_);} dist_indicator_file_list()) 7 244 251
208 # clean up relative path:
209 # MyApp/script/.. -> MyApp
210  
211 7 my $dir;
212 7 my @dir_list = $home->dir_list();
213 7 ▶ while (($dir = pop(@dir_list)) && $dir eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir = pop @dir_list and $dir eq "..") 6 7 13
Condition66%2/3
$dir = pop @dir_list and $dir eq ".."
!ll&&!rl&&r
076
MC/DC50%1/2
$dir = pop @dir_list and $dir eq ".."
$dir = pop @dir_list $dir eq ".."
Truth table66%2/3
A and B
A= $dir = pop @dir_listB= $dir eq ".."
ABresult
0X0
100
111
214 6 $home = dir($home)->parent->parent;
215 }
216  
217 7 return $home->stringify;
218 }
219 }
220  
221 {
222 # look for an installed Catalyst app
223  
224 # trim the .pm off the thing ( Foo/Bar.pm -> Foo/Bar/ )
225 251 ( my $path = $inc_entry) =~ s/\.pm$//;
226 244 my $home = dir($path)->absolute->cleanup;
227  
228 # return if it's a valid directory
229 244 ▶ return $home->stringify if -d $home;
Branch: 1 branch
BranchTrueFalseTotal
if -d $home 216 28 244
230 }
231 }
232  
233 # we found nothing
234 108 return 0;
235 }
236  
237 =head2 prefix($class, $name);
238  
239 Returns a prefixed action.
240  
241 MyApp::Controller::Foo::Bar, yada becomes foo/bar/yada
242  
243 =cut
244  
245 0 ▶sub prefix {
Subroutine prefix: not called CC=2 SCAR=17.9
Pod documented
246 0 my ( $class, $name ) = @_;
247 0 my $prefix = &class2prefix($class);
248 0 ▶ $name = "$prefix/$name" if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
249 0 return $name;
250 }
251  
252 =head2 request($uri)
253  
254 Returns an L<HTTP::Request> object for a uri.
255  
256 =cut
257  
258 924 ▶sub request {
Subroutine request: called CC=4 SCAR=13.9
Pod documented
259 924 my $request = shift;
260 924 ▶ unless ( ref $request ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $request) 784 140 924
261 784 ▶ if ( $request =~ m/^http/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ /^http/i) { } 535 249 784
262 535 $request = URI->new($request);
263 }
264 else {
265 249 $request = URI->new( 'http://localhost' . $request );
266 }
267 }
268 924 ▶ unless ( ref $request eq 'HTTP::Request' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $request eq "HTTP::Request") 784 140 924
269 784 $request = HTTP::Request->new( 'GET', $request );
270 }
271 924 return $request;
272 }
273  
274 =head2 ensure_class_loaded($class_name, \%opts)
275  
276 Loads the class unless it already has been loaded.
277  
278 If $opts{ignore_loaded} is true always tries the require whether the package
279 already exists or not. Only pass this if you're either (a) sure you know the
280 file exists on disk or (b) have code to catch the file not found exception
281 that will result if it doesn't.
282  
283 =cut
284  
285 6831 ▶sub ensure_class_loaded {
Subroutine ensure_class_loaded: called CC=8 SCAR=20.8
Pod documented
286 6831 my $class = shift;
287 6831 my $opts = shift;
288  
289 6831 ▶ croak "Malformed class Name $class"
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /(?:\b\:\b|\:{3,})/ 0 6831 6831
290 if $class =~ m/(?:\b\:\b|\:{3,})/;
291  
292 6831 ▶ croak "Malformed class Name $class"
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /[^\w:]/ 2 6829 6831
293 if $class =~ m/[^\w:]/;
294  
295 6829 ▶ croak "ensure_class_loaded should be given a classname, not a filename ($class)"
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /\.pm$/ 0 6829 6829
296 if $class =~ m/\.pm$/;
297  
298 # $opts->{ignore_loaded} can be set to true, and this causes the class to be required, even
299 # if it already has symbol table entries. This is to support things like Schema::Loader, which
300 # part-generate classes in memory, but then also load some of their contents from disk.
301 return if !$opts->{ ignore_loaded }
302 6829 ▶ && is_class_loaded($class); # if a symbol entry exists we don't load again
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"ignore_loaded"} and is_class_loaded($class) 49 6780 6829
Condition100%3/3
not $opts->{"ignore_loaded"} and is_class_loaded($class)
!ll&&!rl&&r
661616449
MC/DC100%2/2
not $opts->{"ignore_loaded"} and is_class_loaded($class)
not $opts->{"ignore_loaded"} is_class_loaded($class)
Truth table100%3/3
not(A) and B
A= not $opts->{"ignore_loaded"}B= is_class_loaded($class)
ABresult
0X0
100
111
303  
304 # this hack is so we don't overwrite $@ if the load did not generate an error
305 6780 my $error;
306 {
307 6780 local $@;
308 6780 my $file = $class . '.pm';
309 6780 $file =~ s{::}{/}g;
310 6780 eval { CORE::require($file) };
311 6780 $error = $@;
312 }
313  
314 6780 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 2 6778 6780
315  
316 6778 ▶ warn "require $class was successful but the package is not defined."
Branch: 1 branch
BranchTrueFalseTotal
unless is_class_loaded($class) 1 6777 6778
317 unless is_class_loaded($class);
318  
319 6778 return 1;
320 }
321  
322 =head2 merge_hashes($hashref, $hashref)
323  
324 Base code to recursively merge two hashes together with right-hand precedence.
325  
326 =cut
327  
328 22232 ▶sub merge_hashes {
Subroutine merge_hashes: called CC=8 SCAR=20.8
Pod documented
329 22232 my ( $lefthash, $righthash ) = @_;
330  
331 22232 ▶ return $lefthash unless defined $righthash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $righthash 717 21515 22232
332  
333 21515 my %merged = %$lefthash;
334 21515 for my $key ( keys %$righthash ) {
335 8705 ▶ my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH';
Condition100%2/2
ref $righthash->{$key} || ''
l!l
11137592
MC/DC100%1/1
ref $righthash->{$key} || ''
ref $righthash->{$key}
Truth table100%2/2
A
A= ref $righthash->{$key}
Aresult
11
00
336 8705 ▶ my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH';
Condition100%5/5
exists $lefthash->{$key} && ref $lefthash->{$key} || ''
l!l
1668539
exists $lefthash->{$key} and ref $lefthash->{$key}
!ll&&!rl&&r
852712166
MC/DC100%2/2
exists $lefthash->{$key} && ref $lefthash->{$key} || ''
exists $lefthash->{$key} ref $lefthash->{$key}
Truth table100%3/3
A and B
A= exists $lefthash->{$key}B= ref $lefthash->{$key}
ABresult
111
0X0
100
337 8705 ▶ if( $right_ref and $left_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_ref and $left_ref) { } 80 8625 8705
Condition100%3/3
$right_ref and $left_ref
!ll&&!rl&&r
767595080
MC/DC100%2/2
$right_ref and $left_ref
$right_ref $left_ref
Truth table100%3/3
A and B
A= $right_refB= $left_ref
ABresult
0X0
100
111
338 $merged{ $key } = merge_hashes(
339 80 $lefthash->{ $key }, $righthash->{ $key }
340 );
341 }
342 else {
343 8625 $merged{ $key } = $righthash->{ $key };
344 }
345 }
346  
347 21515 return \%merged;
348 }
349  
350 =head2 env_value($class, $key)
351  
352 Checks for and returns an environment value. For instance, if $key is
353 'home', then this method will check for and return the first value it finds,
354 looking at $ENV{MYAPP_HOME} and $ENV{CATALYST_HOME}.
355  
356 =cut
357  
358 1061 ▶sub env_value {
Subroutine env_value: called CC=3 SCAR=11.0
Pod documented
359 1061 my ( $class, $key ) = @_;
360  
361 1061 $key = uc($key);
362 1061 my @prefixes = ( class2env($class), 'CATALYST' );
363  
364 1061 for my $prefix (@prefixes) {
365 2114 ▶ if ( defined( my $value = $ENV{"${prefix}_${key}"} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = $ENV{"${prefix}_$key"})) 26 2088 2114
366 26 return $value;
367 }
368 }
369  
370 1035 return;
371 }
372  
373 =head2 term_width
374  
375 Try to guess terminal width to use with formatting of debug output
376  
377 All you need to get this work, is:
378  
379 1) Install Term::Size::Any, or
380  
381 2) Export $COLUMNS from your shell.
382  
383 (Warning to bash users: 'echo $COLUMNS' may be showing you the bash
384 variable, not $ENV{COLUMNS}. 'export COLUMNS=$COLUMNS' and you should see
385 that 'env' now lists COLUMNS.)
386  
387 As last resort, default value of 80 chars will be used.
388  
389 Calling C<term_width> with a true value will cause it to be recalculated; you
390 can use this to cause it to get recalculated when your terminal is resized like
391 this
392  
393 $SIG{WINCH} = sub { Catalyst::Utils::term_width(1) };
394  
395 =cut
396  
397 187 my $_term_width;
398 my $_use_term_size_any;
399  
400 68 ▶sub term_width {
Subroutine term_width: called CC=14 SCAR=45.9
Pod documented
401 68 my $force_reset = shift;
402  
403 68 ▶ undef $_term_width if $force_reset;
Branch: 1 branch
BranchTrueFalseTotal
if $force_reset 0 68 68
404  
405 68 ▶ return $_term_width if $_term_width;
Branch: 1 branch
BranchTrueFalseTotal
if $_term_width 60 8 68
406  
407 8 ▶ if ($ENV{COLUMNS} && $ENV{COLUMNS} =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /\A\d+\z/) 0 8 8
Condition33%1/3
$ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /\A\d+\z/
!ll&&!rl&&r
800
MC/DC0%0/2
$ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /\A\d+\z/
$ENV{"COLUMNS"} $ENV{"COLUMNS"} =~ /\A\d+\z/
Truth table33%1/3
A and B
A= $ENV{"COLUMNS"}B= $ENV{"COLUMNS"} =~ /\A\d+\z/
ABresult
0X0
100
111
408 0 return $_term_width = $ENV{COLUMNS};
409 }
410  
411 8 ▶ if (!-t STDOUT && !-t STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-t STDOUT or -t STDERR) 8 0 8
Condition33%1/3
-t STDOUT or -t STDERR
l!l&&r!l&&!r
008
MC/DC0%0/2
-t STDOUT or -t STDERR
-t STDOUT -t STDERR
Truth table33%1/3
A or B
A= -t STDOUTB= -t STDERR
ABresult
1X1
011
000
412 8 return $_term_width = 80;
413 }
414  
415 0 ▶ if (!defined $_use_term_size_any) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_use_term_size_any) 0 0 0
416 eval {
417 0 require Term::Size::Any;
418 0 Term::Size::Any->import();
419 0 $_use_term_size_any = 1;
420 0 1;
421 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::Size::Any; "Term::Size::Any"->import; $_use_term_size_any = 1; 1 } } or do { if ($@ =~ m[Can't locate Term/Size/Any\.pm]) { warn "Term::Size::Any is not installed, can't autodetect terminal column width\n"; } else { warn "There was an error trying to detect your terminal size: $@\n"; } ; $_use_term_size_any = 0 } 0 0 0
422 0 ▶ if ( $@ =~ m[Can't locate Term/Size/Any\.pm] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ m[Can't locate Term/Size/Any\.pm]) { } 0 0 0
423 0 warn "Term::Size::Any is not installed, can't autodetect terminal column width\n";
424 }
425 else {
426 0 warn "There was an error trying to detect your terminal size: $@\n";
427 }
428 0 $_use_term_size_any = 0;
429 };
430 }
431  
432 0 my $width;
433  
434 0 ▶ if ($_use_term_size_any) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_use_term_size_any) 0 0 0
435 0 ▶ $width = Term::Size::Any::chars(*STDERR) || Term::Size::Any::chars(*STDOUT);
Condition0%0/3
Term::Size::Any::chars(*STDERR) || Term::Size::Any::chars(*STDOUT)
l!l&&r!l&&!r
000
MC/DC0%0/2
Term::Size::Any::chars(*STDERR) || Term::Size::Any::chars(*STDOUT)
Term::Size::Any::chars(*STDERR) Term::Size::Any::chars(*STDOUT)
Truth table0%0/3
A || B
A= Term::Size::Any::chars(*STDERR)B= Term::Size::Any::chars(*STDOUT)
ABresult
1X1
011
000
436 }
437  
438 0 ▶ if (!$width || $width < 80) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $width or $width < 80) 0 0 0
Condition0%0/3
not $width or $width < 80
l!l&&r!l&&!r
000
MC/DC0%0/2
not $width or $width < 80
not $width $width < 80
Truth table0%0/3
not(A) or B
A= not $widthB= $width < 80
ABresult
1X1
011
000
439 0 $width = 80;
440 }
441  
442 0 return $_term_width = $width;
443 }
444  
445  
446 =head2 resolve_namespace
447  
448 Method which adds the namespace for plugins and actions.
449  
450 __PACKAGE__->setup(qw(MyPlugin));
451  
452 # will load Catalyst::Plugin::MyPlugin
453  
454 =cut
455  
456  
457 2135 ▶sub resolve_namespace {
Subroutine resolve_namespace: called CC=2 SCAR=6.9
Pod documented
458 2135 my $appnamespace = shift;
459 2135 my $namespace = shift;
460 2135 my @classes = @_;
461 2135 ▶ return String::RewritePrefix->rewrite({
Branch: 1 branch
BranchTrueFalseTotal
defined $appnamespace ? : 1492 643 2135
462 q[] => qq[${namespace}::],
463 q[+] => q[],
464 (defined $appnamespace
465 ? (q[~] => qq[${appnamespace}::])
466 : ()
467 ),
468 }, @classes);
469 }
470  
471 =head2 build_middleware (@args)
472  
473 Internal application that converts a single middleware definition (see
474 L<Catalyst/psgi_middleware>) into an actual instance of middleware.
475  
476 =cut
477  
478 10 ▶sub build_middleware {
Subroutine build_middleware: called CC=7 SCAR=21.3
Pod documented
479 10 my ($class, $namespace, @init_args) = @_;
480  
481 10 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace =~ s/^\+// or $namespace =~ /^Plack::Middleware/ or $namespace =~ /^$class/) { } 3 7 10
Condition66%4/6
$namespace =~ s/^\+// or $namespace =~ /^Plack::Middleware/ or $namespace =~ /^$class/
l!l&&r!l&&!r
307
$namespace =~ s/^\+// or $namespace =~ /^Plack::Middleware/
l!l&&r!l&&!r
307
MC/DC33%1/3
$namespace =~ s/^\+// or $namespace =~ /^Plack::Middleware/ or $namespace =~ /^$class/
$namespace =~ s/^\+// $namespace =~ /^Plack::Middleware/ $namespace =~ /^$class/
Truth table50%2/4
A or B or C
A= $namespace =~ s/^\+//B= $namespace =~ /^Plack::Middleware/C= $namespace =~ /^$class/
ABCresult
1XX1
01X1
0011
0000
482 $namespace =~s/^\+// ||
483 $namespace =~/^Plack::Middleware/ ||
484 $namespace =~/^$class/
485 ) { ## the string is a full namespace
486 3 ▶ return Class::Load::try_load_class($namespace) ?
Branch: 1 branch
BranchTrueFalseTotal
Class::Load::try_load_class($namespace) ? : 3 0 3
487 $namespace->new(@init_args) :
488 die "Can't load class $namespace";
489 } else { ## the string is a partial namespace
490 7 ▶ if(Class::Load::try_load_class($class .'::Middleware::'. $namespace)) { ## Load Middleware from Project namespace
Branch: 2 branches
BranchTrueFalseTotal
if (Class::Load::try_load_class($class . '::Middleware::' . $namespace)) { } 0 7 7
elsif (Class::Load::try_load_class("Plack::Middleware::$namespace")) { } 6 1 7
491 0 my $ns = $class .'::Middleware::'. $namespace;
492 0 return $ns->new(@init_args);
493 } elsif(Class::Load::try_load_class("Plack::Middleware::$namespace")) { ## Act like Plack::Builder
494 6 return "Plack::Middleware::$namespace"->new(@init_args);
495 } else {
496 1 die "Can't load middleware via '$namespace'. It's not ".$class."::Middleware::".$namespace." or Plack::Middleware::$namespace";
497 }
498 }
499  
500 0 return; ## be sure we can count on a proper return when valid
501 }
502  
503 =head2 apply_registered_middleware ($psgi)
504  
505 Given a $psgi reference, wrap all the L<Catalyst/registered_middlewares>
506 around it and return the wrapped version.
507  
508 This exists to deal with the fact Catalyst registered middleware can be
509 either an object with a wrap method or a coderef.
510  
511 =cut
512  
513 136 ▶sub apply_registered_middleware {
Subroutine apply_registered_middleware: called CC=3 SCAR=11.0
Pod documented
514 136 my ($class, $psgi) = @_;
515 136 my $new_psgi = $psgi;
516 136 foreach my $middleware ($class->registered_middlewares) {
517 965 ▶ $new_psgi = Scalar::Util::blessed $middleware ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($middleware) ? : 963 2 965
518 $middleware->wrap($new_psgi) :
519 $middleware->($new_psgi);
520 }
521 136 return $new_psgi;
522 }
523  
524 =head2 inject_component
525  
526 Used to add components at runtime:
527  
528 into The Catalyst package to inject into (e.g. My::App)
529 component The component package to inject
530 traits (Optional) ArrayRef of L<Moose::Role>s that the component should consume.
531 as An optional moniker to use as the package name for the derived component
532  
533 For example:
534  
535 Catalyst::Utils::inject_component( into => My::App, component => Other::App::Controller::Apple )
536  
537 The above will create 'My::App::Controller::Other::App::Controller::Apple'
538  
539 Catalyst::Utils::inject_component( into => My::App, component => Other::App::Controller::Apple, as => Apple )
540  
541 The above will create 'My::App::Controller::Apple'
542  
543 Catalyst::Utils::inject_component( into => $myapp, component => 'MyRootV', as => 'Controller::Root' );
544  
545 Will inject Controller, Model, and View components into your Catalyst application
546 at setup (run)time. It does this by creating a new package on-the-fly, having that
547 package extend the given component, and then having Catalyst setup the new component
548 (via $app->setup_component).
549  
550 B<NOTE:> This is basically a core version of L<CatalystX::InjectComponent>. If you were using that
551 you can now use this safely instead. Going forward changes required to make this work will be
552 synchronized with the core method.
553  
554 B<NOTE:> The 'traits' option is unique to the L<Catalyst::Utils> version of this feature.
555  
556 B<NOTE:> These injected components really need to be a L<Catalyst::Component> and a L<Moose>
557 based class.
558  
559 =cut
560  
561 11 ▶sub inject_component {
Subroutine inject_component: called CC=13 SCAR=25.9
Pod documented
562 11 my %given = @_;
563 11 my ($into, $component, $as) = @given{qw/into component as/};
564  
565 11 ▶ croak "No Catalyst (package) given" unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 0 11 11
566 11 ▶ croak "No component (package) given" unless $component;
Branch: 1 branch
BranchTrueFalseTotal
unless $component 0 11 11
567  
568 11 Class::Load::load_class($component);
569  
570 11 ▶ $as ||= $component;
Condition66%2/3
$as ||= $component
l!l&&r!l&&!r
920
MC/DC0%0/2
$as ||= $component
$as $component
Truth table66%2/3
A ||= B
A= $asB= $component
ABresult
1X1
011
000
571 11 ▶ unless ( $as =~ m/^(?:Controller|Model|View)::/ || $given{skip_mvc_renaming} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($as =~ /^(?:Controller|Model|View)::/ or $given{"skip_mvc_renaming"}) 5 6 11
Condition66%2/3
$as =~ /^(?:Controller|Model|View)::/ or $given{"skip_mvc_renaming"}
l!l&&r!l&&!r
605
MC/DC50%1/2
$as =~ /^(?:Controller|Model|View)::/ or $given{"skip_mvc_renaming"}
$as =~ /^(?:Controller|Model|View)::/ $given{"skip_mvc_renaming"}
Truth table66%2/3
A or B
A= $as =~ /^(?:Controller|Model|View)::/B= $given{"skip_mvc_renaming"}
ABresult
1X1
011
000
572 5 my $category;
573 5 for (qw/ Controller Model View /) {
574 7 ▶ if ( $component->isa( "Catalyst::$_" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component->isa("Catalyst::$_")) 5 2 7
575 5 $category = $_;
576 5 last;
577 }
578 }
579 5 ▶ croak "Don't know what kind of component \"$component\" is" unless $category;
Branch: 1 branch
BranchTrueFalseTotal
unless $category 0 5 5
580 5 $as = "${category}::$as";
581 }
582 11 my $component_package = join '::', $into, $as;
583  
584 11 ▶ unless ( Class::Load::is_class_loaded $component_package ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Class::Load::is_class_loaded($component_package)) 11 0 11
585 11 ▶ eval "package $component_package; use base qw/$component/; 1;" or
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "package $component_package; use base qw/$component/; 1;" or croak(qq[Unable to build component package for "$component_package": $@]) 11 0 11
586 croak "Unable to build component package for \"$component_package\": $@";
587 11 ▶ Moose::Util::apply_all_roles($component_package, @{$given{traits}}) if $given{traits};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"traits"} 3 8 11
588 11 (my $file = "$component_package.pm") =~ s{::}{/}g;
589 11 ▶ $INC{$file} ||= 1;
Condition50%1/2
$INC{$file} ||= 1
l!l
011
MC/DC0%0/1
$INC{$file} ||= 1
$INC{$file}
Truth table50%1/2
A
A= $INC{$file}
Aresult
11
00
590 }
591  
592 11 ▶ my $_setup_component = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
593 11 my $into = shift;
594 11 my $component_package = shift;
595 11 $into->components->{$component_package} = $into->delayed_setup_component( $component_package );
596 11 };
597  
598 11 $_setup_component->( $into, $component_package );
599 }
600  
601 =head1 PSGI Helpers
602  
603 Utility functions to make it easier to work with PSGI applications under Catalyst
604  
605 =head2 env_at_path_prefix
606  
607 Localize C<$env> under the current controller path prefix:
608  
609 package MyApp::Controller::User;
610  
611 use Catalyst::Utils;
612  
613 use base 'Catalyst::Controller';
614  
615 sub name :Local {
616 my ($self, $c) = @_;
617 my $env = $c->Catalyst::Utils::env_at_path_prefix;
618 }
619  
620 Assuming you have a request like GET /user/name:
621  
622 In the example case C<$env> will have PATH_INFO of '/name' instead of
623 '/user/name' and SCRIPT_NAME will now be '/user'.
624  
625 =cut
626  
627 8 ▶sub env_at_path_prefix {
Subroutine env_at_path_prefix: called CC=2 SCAR=7.0
Pod documented
628 8 my $ctx = shift;
629 8 my $path_prefix = $ctx->controller->path_prefix;
630 8 my $env = $ctx->request->env;
631 8 my $path_info = $env->{PATH_INFO};
632 8 ▶ my $script_name = ($env->{SCRIPT_NAME} || '');
Condition50%1/2
$env->{'SCRIPT_NAME'} || ''
l!l
08
MC/DC0%0/1
$env->{'SCRIPT_NAME'} || ''
$env->{'SCRIPT_NAME'}
Truth table50%1/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
633  
634 8 $path_info =~ s/(^\/\Q$path_prefix\E)//;
635 8 $script_name = "$script_name$1";
636  
637 return +{
638 8 %$env,
639 PATH_INFO => $path_info,
640 SCRIPT_NAME => $script_name };
641 }
642  
643 =head2 env_at_action
644  
645 Localize C<$env> under the current action namespace.
646  
647 package MyApp::Controller::User;
648  
649 use Catalyst::Utils;
650  
651 use base 'Catalyst::Controller';
652  
653 sub name :Local {
654 my ($self, $c) = @_;
655 my $env = $c->Catalyst::Utils::env_at_action;
656 }
657  
658 Assuming you have a request like GET /user/name:
659  
660 In the example case C<$env> will have PATH_INFO of '/' instead of
661 '/user/name' and SCRIPT_NAME will now be '/user/name'.
662  
663 Alternatively, assuming you have a request like GET /user/name/foo:
664  
665 In this example case C<$env> will have PATH_INFO of '/foo' instead of
666 '/user/name/foo' and SCRIPT_NAME will now be '/user/name'.
667  
668 This is probably a common case where you want to mount a PSGI application
669 under an action but let the Args fall through to the PSGI app.
670  
671 =cut
672  
673 11 ▶sub env_at_action {
Subroutine env_at_action: called CC=2 SCAR=6.9
Pod documented
674 11 my $ctx = shift;
675 11 my $argpath = join '/', @{$ctx->request->arguments};
676 11 my $path = '/' . $ctx->request->path;
677  
678 11 $path =~ s/\/?\Q$argpath\E\/?$//;
679  
680 11 my $env = $ctx->request->env;
681 11 my $path_info = $env->{PATH_INFO};
682 11 ▶ my $script_name = ($env->{SCRIPT_NAME} || '');
Condition50%1/2
$env->{'SCRIPT_NAME'} || ''
l!l
011
MC/DC0%0/1
$env->{'SCRIPT_NAME'} || ''
$env->{'SCRIPT_NAME'}
Truth table50%1/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
683  
684 11 $path_info =~ s/(^\Q$path\E)//;
685 11 $script_name = "$script_name$1";
686  
687 return +{
688 11 %$env,
689 PATH_INFO => $path_info,
690 SCRIPT_NAME => $script_name };
691 }
692  
693 =head2 env_at_request_uri
694  
695 Localize C<$env> under the current request URI:
696  
697 package MyApp::Controller::User;
698  
699 use Catalyst::Utils;
700  
701 use base 'Catalyst::Controller';
702  
703 sub name :Local Args(1) {
704 my ($self, $c, $id) = @_;
705 my $env = $c->Catalyst::Utils::env_at_request_uri
706 }
707  
708 Assuming you have a request like GET /user/name/hello:
709  
710 In the example case C<$env> will have PATH_INFO of '/' instead of
711 '/user/name' and SCRIPT_NAME will now be '/user/name/hello'.
712  
713 =cut
714  
715 7 ▶sub env_at_request_uri {
Subroutine env_at_request_uri: called CC=2 SCAR=7.0
Pod documented
716 7 my $ctx = shift;
717 7 my $path = '/' . $ctx->request->path;
718 7 my $env = $ctx->request->env;
719 7 my $path_info = $env->{PATH_INFO};
720 7 ▶ my $script_name = ($env->{SCRIPT_NAME} || '');
Condition50%1/2
$env->{'SCRIPT_NAME'} || ''
l!l
07
MC/DC0%0/1
$env->{'SCRIPT_NAME'} || ''
$env->{'SCRIPT_NAME'}
Truth table50%1/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
721  
722 7 $path_info =~ s/(^\Q$path\E)//;
723 7 $script_name = "$script_name$1";
724  
725 return +{
726 7 %$env,
727 PATH_INFO => $path_info,
728 SCRIPT_NAME => $script_name };
729 }
730  
731 =head1 AUTHORS
732  
733 Catalyst Contributors, see Catalyst.pm
734  
735 =head1 COPYRIGHT
736  
737 This library is free software. You can redistribute it and/or modify it under
738 the same terms as Perl itself.
739  
740 =cut
741  
742 187 1;
« lib/Catalyst/DispatchType/Chained.pm ↑ Summary lib/Catalyst/Engine.pm »