Cake/Utils.pm

filter: Statementstmt 35.0% 40 / 114 Branchbran 15.9% 7 / 44 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 36.0% 9 / 25 Podpod 7.6% 1 / 13 totaltotal 26.3% 57 / 216 CC 29 SCAR 24.8
CC
29
Cov
27%
to_epoch
30.0
create_file
20.0
combineHashes
20.0
CRAP 11.9
« Cake/Exception.pm ↑ Summary Cake/View/TT.pm »
1 package Cake::Utils;
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Cake::Utils::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 our @EXPORT = qw(
8 run_once
9 get_file
10 create_file
11 create_folder
12 crlf
13 serialize
14 );
15
16 8 our $VERSION = '0.004';
17
18 # copied from Plack which in return copied from HTTP::Status
19 8 my %StatusCode = (
20 100 => 'Continue',
21 101 => 'Switching Protocols',
22 102 => 'Processing', # RFC 2518 (WebDAV)
23 200 => 'OK',
24 201 => 'Created',
25 202 => 'Accepted',
26 203 => 'Non-Authoritative Information',
27 204 => 'No Content',
28 205 => 'Reset Content',
29 206 => 'Partial Content',
30 207 => 'Multi-Status', # RFC 2518 (WebDAV)
31 300 => 'Multiple Choices',
32 301 => 'Moved Permanently',
33 302 => 'Found',
34 303 => 'See Other',
35 304 => 'Not Modified',
36 305 => 'Use Proxy',
37 307 => 'Temporary Redirect',
38 400 => 'Bad Request',
39 401 => 'Unauthorized',
40 402 => 'Payment Required',
41 403 => 'Forbidden',
42 404 => 'Not Found',
43 405 => 'Method Not Allowed',
44 406 => 'Not Acceptable',
45 407 => 'Proxy Authentication Required',
46 408 => 'Request Timeout',
47 409 => 'Conflict',
48 410 => 'Gone',
49 411 => 'Length Required',
50 412 => 'Precondition Failed',
51 413 => 'Request Entity Too Large',
52 414 => 'Request-URI Too Large',
53 415 => 'Unsupported Media Type',
54 416 => 'Request Range Not Satisfiable',
55 417 => 'Expectation Failed',
56 422 => 'Unprocessable Entity', # RFC 2518 (WebDAV)
57 423 => 'Locked', # RFC 2518 (WebDAV)
58 424 => 'Failed Dependency', # RFC 2518 (WebDAV)
59 425 => 'No code', # WebDAV Advanced Collections
60 426 => 'Upgrade Required', # RFC 2817
61 449 => 'Retry with', # unofficial Microsoft
62 500 => 'Internal Server Error',
63 501 => 'Not Implemented',
64 502 => 'Bad Gateway',
65 503 => 'Service Unavailable',
66 504 => 'Gateway Timeout',
67 505 => 'HTTP Version Not Supported',
68 506 => 'Variant Also Negotiates', # RFC 2295
69 507 => 'Insufficient Storage', # RFC 2518 (WebDAV)
70 509 => 'Bandwidth Limit Exceeded', # unofficial
71 510 => 'Not Extended', # RFC 2774
72 );
73
74 =head2 get_file
75
76 Get content of a file
77
78 Cake::Utils::get_file('/path/to/some/file');
79
80 =cut
81
82 0 ▶sub get_file {
Subroutine get_file: not called CC=2 SCAR=17.9
Pod documented
83 0 my $file = shift;
84 0 ▶ open my $cont, '<', $file or croak "Can't open $file for input:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cont, "<", $file or croak("Can't open $file for input:\n$!") 0 0 0
85 0 local $/;
86 0 my $content = <$cont>;
87 0 close $cont;
88 0 return $content;
89 }
90
91 0 ▶sub create_file {
Subroutine create_file: not called CC=4 SCAR=30.0
Pod undocumented
92 0 my ($options) = @_;
93 0 my $folder = $options->{folder};
94 0 ▶ my $file = $options->{file} || croak "You must provide a file name";
Condition0%0/2
$options->{'file'} || croak('You must provide a file name')
l!l
00
MC/DC0%0/1
$options->{'file'} || croak('You must provide a file name')
$options->{'file'}
Truth table0%0/2
A
A= $options->{'file'}
Aresult
11
00
95 0 ▶ my $content = $options->{content} || "";
Condition0%0/2
$options->{'content'} || ''
l!l
00
MC/DC0%0/1
$options->{'content'} || ''
$options->{'content'}
Truth table0%0/2
A
A= $options->{'content'}
Aresult
11
00
96
97 0 ▶ if ($folder){
Branch: 1 branch
BranchTrueFalseTotal
if ($folder) { } 0 0 0
98 #create_folder($folder);
99 0 $folder =~ s/\/$//;
100 0 $file =~ s/^\///;
101 0 $file = $folder.'/'.$file;
102 } else {
103 0 my @paths = split '/', $file;
104 0 my $file = pop @paths;
105 0 my $folder = join '/', @paths;
106 0 $file = $folder.'/'.$file;
107 }
108
109 0 return $file;
110 }
111
112 0 ▶sub create_folder {
Subroutine create_folder: not called CC=3 SCAR=24.8
Pod undocumented
113 0 my $folder = shift;
114 0 ▶ if (! -e "$folder") {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e "$folder") 0 0 0
115 0 ▶ mkdir($folder) or croak "Can't create $folder : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $folder or croak("Can't create $folder : $!\n") 0 0 0
116 }
117 }
118
119 0 ▶sub combineHashes {
Subroutine combineHashes: not called CC=4 SCAR=30.0
Pod undocumented
120 0 my ($hash1,$hash2) = @_;
121 0 ▶ while (my($key,$value) = each(%{$hash2})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hash2;}) 0 0 0
122
123 0 ▶ if ($hash1->{$key}){
Branch: 1 branch
BranchTrueFalseTotal
if ($hash1->{$key}) { } 0 0 0
124 0 ▶ if (!ref $hash1->{$key}){
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $hash1->{$key}) { } 0 0 0
125 0 my $holds = [$hash1->{$key},$value];
126 0 $hash1->{$key} = $holds;
127 } else {
128 0 push (@{$hash1->{$key}},$value);
129 }
130 } else {
131 0 $hash1->{$key} = $value;
132 }
133 }
134 0 return $hash1;
135 }
136
137 0 ▶sub content_length {
Subroutine content_length: not called CC=4 SCAR=30.0
Pod undocumented
138 0 my $body = shift;
139 0 ▶ return unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 0 0
140 0 ▶ if (!ref $body) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $body) { } 0 0 0
elsif (ref $body eq 'GLOB') { } 0 0 0
141 0 return length($body);
142 } elsif ( ref $body eq 'GLOB' ) {
143 0 return tell($body);
144 }
145 0 return;
146 }
147
148 #============================================================================
149 # Require & noRequire
150 #============================================================================
151 4 ▶sub Require {
Subroutine Require: called CC=1 SCAR=0.0
Pod undocumented
152 4 return noRequire(@_,'Require');
153 }
154
155 8 ▶sub noRequire {
Subroutine noRequire: called CC=6 SCAR=18.2
Pod undocumented
156 8 my $class = shift;
157 8 my $namespace = shift;
158 8 my $require = shift;
159 8 ▶ if ($namespace){
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) 8 0 8
160 8 ▶ unless ($class =~ s/^\+//){
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ s/^\+//) 6 2 8
161 6 $class = $namespace.'::'.$class;
162 }
163 }
164
165 8 my $package = $class;
166 8 $package =~ s/::/\//g;
167 8 $package .= '.pm';
168
169 8 ▶ if ($require){
Branch: 1 branch
BranchTrueFalseTotal
if ($require) 4 4 8
170 4 ▶ if (!$INC->{$package}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC->{$package}) 4 0 4
171 4 eval 'require "$package"';
172 4 ▶ } if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
173 0 croak ($@);
174 }
175 }
176 8 return $class;
177 }
178
179 0 ▶sub get_status_code {
Subroutine get_status_code: not called CC=3 SCAR=24.8
Pod undocumented
180 0 my $env = shift;
181 0 my $message = $StatusCode{$_[0]};
182 0 ▶ if ($env->{'http.version'} && $env->{'http.version'} == 1.0){
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"http.version"} and $env->{"http.version"} == 1) 0 0 0
Condition0%0/3
$env->{"http.version"} and $env->{"http.version"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$env->{"http.version"} and $env->{"http.version"} == 1
$env->{"http.version"} $env->{"http.version"} == 1
Truth table0%0/3
A and B
A= $env->{"http.version"}B= $env->{"http.version"} == 1
ABresult
0X0
100
111
183 0 return "HTTP/1.0 $_[0] $message\015\012";
184 }
185 0 return "HTTP/1.1 $_[0] $message\015\012";
186 }
187
188 #============================================================================
189 # convert givin date to epoch
190 #============================================================================
191
192 =head1 to_epoch('1m')
193
194 convert predefined values to Unix machine time
195
196 Cake::Utils::to_epoch('1h');
197
198 #values
199
200 y = year
201 M = month
202 d = day
203 h = hour
204 m = minute
205 "othing" = seconds
206
207 =cut
208
209 0 ▶sub to_epoch {
Subroutine to_epoch: not called CC=5 SCAR=34.0
Pod undocumented
210 0 ▶ my $length = shift || '1h'; ##one hour is the default value
Condition0%0/2
shift() || '1h'
l!l
00
MC/DC0%0/1
shift() || '1h'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
211 my $types = {
212 0 ▶ 'm' => sub {return 60*$_[0]}, ##minute
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 ▶ 'h' => sub {return 3600*$_[0]}, ##hour
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ 'd' => sub {return (24*3600)*$_[0]}, ##day
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 ▶ 'M' => sub {return (30*24*3600)*$_[0]}, ##month
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 0 ▶ 'y' => sub {return (365*24*3600)*$_[0]}, ##year
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 };
218
219 0 my ($num,$type) = $length =~ /(\d*)(\w)/;
220 0 ▶ $num = 1 if !$num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 0 0 0
221 0 my $expire;
222 0 ▶ if ($types->{$type}){
Branch: 1 branch
BranchTrueFalseTotal
if ($types->{$type}) { } 0 0 0
223 0 $expire = $types->{$type}->($num);
224 } else {
225 0 ▶ $expire = $length || 3600;
Condition0%0/2
$length || 3600
l!l
00
MC/DC0%0/1
$length || 3600
$length
Truth table0%0/2
A
A= $length
Aresult
11
00
226 }
227 0 return $expire+time();
228 }
229
230 #============================================================================
231 # OS crlf : copied from CGI::Simple
232 #============================================================================
233 0 ▶sub crlf {
Subroutine crlf: not called CC=4 SCAR=30.0
Pod undocumented
234 #return "\n";
235 0 my ( $self, $CRLF ) = @_;
236 0 ▶ $self->{'app.crlf'} = $CRLF if $CRLF; # allow value to be set manually
Branch: 1 branch
BranchTrueFalseTotal
if $CRLF 0 0 0
237 0 ▶ unless ( $self->{'app.crlf'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"app.crlf"}) 0 0 0
238 0 my $OS = $^O;
239 0 ▶ $self->{'app.crlf'}
Branch: 1 branch
BranchTrueFalseTotal
$OS =~ /VMS/i ? : 0 0 0
240 = ( $OS =~ m/VMS/i ) ? "\n"
241 : ( "\t" ne "\011" ) ? "\r\n"
242 : "\015\012";
243 }
244
245 0 return $self->{'app.crlf'};
246 }
247
248 #============================================================================
249 # Random String Generator
250 #============================================================================
251 0 ▶sub random_string {
Subroutine random_string: not called CC=2 SCAR=17.9
Pod undocumented
252 0 ▶ my $num = shift || 16;
Condition0%0/2
shift() || 16
l!l
00
MC/DC0%0/1
shift() || 16
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
253 0 return time().join '',map { sprintf q|%X|, rand($num) } 1 .. 36;
254 }
255
256 0 ▶sub serialize {
Subroutine serialize: not called CC=1 SCAR=6.9
Pod undocumented
257 0 shift; #shift cake class
258 0 return Cake::Utils::Serializer->new(shift);
259 }
260 #============================================================================
261 # Check for persistance
262 # FIXME: I'm not sure if this has no bugs at all
263 #
264 # FROM perldocs
265 # --------------------------------------------------------------------------
266 # The CHECK and INIT blocks in code compiled by require, string do, or string
267 # eval will not be executed if they occur after the end of the main compilation
268 # phase; that can be a problem in mod_perl and other persistent environments
269 # which use those functions to load code at runtime.
270 #
271 # Which exactly what I'm using as an advantage :)
272 #============================================================================
273 8 my $run_once = 0;
274
275 {
276 8 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
277 5 ▶ INIT {
Subroutine INIT: called CC=1 SCAR=0.0
278 5 $run_once = 1;
279 }
280 }
281
282 8 ▶sub run_once {
Subroutine run_once: not called CC=2 SCAR=15.1
Pod undocumented
283 0 my $self = shift;
284 0 ▶ if (exists $self->env->{run_once}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->env->{"run_once"}) 0 0 0
285 0 return $self->env->{run_once};
286 }
287 0 return $run_once;
288 }
289
290 8 1;
291
292 __END__
« Cake/Exception.pm ↑ Summary Cake/View/TT.pm »