blib/lib/CatalystX/UriForStatic.pm

filter: Statementstmt 100% 19 / 19 Branchbran 80.0% 8 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 89.7% 35 / 39 CC 7 SCAR 16.7
CC
7
Cov
91%
uri_for_static
7.1
BEGIN
1.0
BEGIN
1.0
CRAP 5.3
↑ Summary
1 package CatalystX::UriForStatic;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 CatalystX::UriForStatic - Implements an uri_for_static method for Catalyst applications!
11  
12 =head1 VERSION
13  
14 Version 0.01
15  
16 =cut
17  
18 6 our $VERSION = '0.01';
19  
20  
21 =head1 SYNOPSIS
22  
23 Returns an URI for static files. It distinguishes between a local and productive environment
24 so it can return an URI pointing to a different host (eg. to a CDN) for productive environments.
25  
26 package MyApp;
27 use Moose;
28 use namespace::autoclean;
29
30 extends 'Catalyst';
31 with 'CatalystX::UriForStatic';
32
33 __PACKAGE__->config(
34 envkey => 'sysenv', # optional, this is the default
35 local_value => 'local', # optional, this is the default
36 static_host => 'http://static.example.net',
37 sysenv => 'local',
38 );
39  
40  
41 # In your template
42 <% $c->uri_for_static('/static/foo.png') %>
43  
44 =head1 DESCRIPTION
45  
46 =head2 uri_for_static
47  
48 Pass a path like you would do to L<Catalyst/uri_for>. Doesn't accept Controller paths or blessed references etc.
49  
50 On a C<local> environment it calls L<Catalyst/uri_for> and returns what it returns.
51  
52 On any other environment it prepends the C<static_host> to the path while it doesn't care about
53 SSL or if your passed path is valid.
54  
55 =cut
56  
57 4 ▶sub uri_for_static {
Subroutine uri_for_static: called CC=7 SCAR=19.7
Pod documented
58 4 my ($c, $path) = @_;
59  
60 4 ▶ my $envkey = $c->config->{envkey} ? $c->config->{envkey} : 'sysenv';
Branch: 1 branch
BranchTrueFalseTotal
$c->config->{'envkey'} ? : 2 2 4
61 4 ▶ my $sysenv = $c->config->{ $envkey } ? $c->config->{ $envkey } : 'local';
Branch: 1 branch
BranchTrueFalseTotal
$c->config->{$envkey} ? : 4 0 4
62 4 ▶ my $local_value = $c->config->{local_value} ? $c->config->{local_value} : 'local';
Branch: 1 branch
BranchTrueFalseTotal
$c->config->{'local_value'} ? : 2 2 4
63  
64 4 ▶ if ($sysenv eq $local_value || !$c->config->{static_host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sysenv eq $local_value or not $c->config->{"static_host"}) 2 2 4
Condition66%2/3
$sysenv eq $local_value or not $c->config->{"static_host"}
l!l&&r!l&&!r
202
MC/DC50%1/2
$sysenv eq $local_value or not $c->config->{"static_host"}
$sysenv eq $local_value not $c->config->{"static_host"}
Truth table66%2/3
A or not(B)
A= $sysenv eq $local_valueB= not $c->config->{"static_host"}
ABresult
1X1
011
000
65 2 return $c->uri_for($path);
66 }
67  
68 2 ▶ $path = '/' . $path unless $path =~ m,^/,;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 0 2 2
69  
70 2 return $c->config->{static_host} . $path;
71 }
72  
73 =head1 CONFIGURATION
74  
75 To work properly, CatalystX::UriForStatic needs some small configuration.
76  
77 =over 4
78  
79 =item envkey
80  
81 Specifies the key in the config that is responsible for defining if the environment is a development
82 or production environment. The default is B<sysenv>.
83  
84 =item local_value
85  
86 Specifies what the value of the L<envkey> is when the environment is a development/local environment.
87 The default is B<local>.
88  
89 =item sysenv
90  
91 This key's name is whatever L<envkey> is set to! Change this to the value of L<local_value> to tell
92 C<CatalystX::UriForStatic> if the environment is a development/local environment. If it differs
93 from L<local_value> the environment is considered as production.
94  
95 =item static_host
96  
97 Contains the URI to the static files and should include the protocol (http or https) and as well the
98 domain. Shouldn't contain a trailing slash.
99  
100 Examples:
101  
102 =over 8
103  
104 =item http://static.example.net
105  
106 =item http://static.example.net/my/static/files
107  
108 =item https://whatever.example.net
109  
110 =back
111  
112 =back
113  
114 =head1 AUTHOR
115  
116 Matthias Dietrich, C<< <perl@rainboxx.de> >>
117  
118 L<http://www.rainboxx.de>
119  
120 =head1 LICENSE AND COPYRIGHT
121  
122 Copyright 2011 Matthias Dietrich.
123  
124 This program is free software; you can redistribute it and/or modify it
125 under the terms of either: the GNU General Public License as published
126 by the Free Software Foundation; or the Artistic License.
127  
128 See http://dev.perl.org/licenses/ for more information.
129  
130  
131 =cut
132  
133 6 1; # End of CatalystX::UriForStatic
↑ Summary