OpenInteract2/Util.pm

filter: Statementstmt 46.6% 110 / 236 Branchbran 25.0% 26 / 104 Conditioncond 8.8% 3 / 34 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 55.8% 19 / 34 Podpod 0.0% 0 / 13 totaltotal 35.7% 158 / 442 CC 71 SCAR 35.8
CC
71
Cov
37%
_send_email_attachment
110.0
_clean_attachment_filename
110.0
send_email
72.0
CRAP 36.0
« OpenInteract2/Error.pm ↑ Summary OpenInteract2/Observer/Controller/GetFiltersFromRequest.pm »
1 package OpenInteract2::Util;
2  
3 # $Id: Util.pm,v 1.23 2005/03/18 04:09:48 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use File::Spec::Functions qw( catdir catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use Mail::Sendmail ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use MIME::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 87 ▶use SPOPS::Secure qw( :level :verbose );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 87 $OpenInteract2::Util::VERSION = sprintf("%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/);
19  
20 87 my ( $log );
21 my ( $CTX );
22  
23 87 ▶use constant DEFAULT_SUBJECT => 'Mail sent from OpenInteract';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 87 ▶use constant DEFAULT_ATTACH_MESSAGE => 'Emailing attachments';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # All other types except those listed here are 'base64' encoding
27  
28 87 my %TEXT_ENCODINGS = map { $_ => '8bit' }
29 qw( text/csv text/html text/html text/plain text/xml
30 application/x-javascript application/x-perl );
31  
32 0 ▶sub _load_context {
Subroutine _load_context: not called CC=2 SCAR=17.9
33 0 ▶ unless ( $CTX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CTX) 0 0 0
34 0 require OpenInteract2::Context;
35 0 $CTX = OpenInteract2::Context->instance;
36 }
37 0 return $CTX;
38 }
39  
40 ########################################
41 # DATE ROUTINES
42  
43 # Return a { time } (or the current time) formatted with { format }
44 #
45 # Signature: $time_string = $class->now( [ { format => $strftime_format,
46 # time => $time_in_seconds } ] );
47  
48 0 ▶sub now {
Subroutine now: not called CC=3 SCAR=24.8
Pod undocumented
49 0 my ( $class, $p ) = @_;
50 0 ▶ $p->{format} ||= '%Y-%m-%d %T';
Condition0%0/2
$p->{"format"} ||= "%Y-%m-%d %T"
l!l
00
MC/DC0%0/1
$p->{"format"} ||= "%Y-%m-%d %T"
$p->{"format"}
Truth table0%0/2
A
A= $p->{"format"}
Aresult
11
00
51 0 ▶ $p->{time} ||= time;
Condition0%0/2
$p->{"time"} ||= time
l!l
00
MC/DC0%0/1
$p->{"time"} ||= time
$p->{"time"}
Truth table0%0/2
A
A= $p->{"time"}
Aresult
11
00
52 0 _load_context();
53 return $CTX->create_date({ epoch => $p->{time} })
54 0 ->strftime( $p->{format} );
55 }
56  
57  
58 # Return the current time formatted 'yyyy-mm-dd'
59 #
60 # Signature: $date_string = $class->today();
61  
62 0 ▶sub today {
Subroutine today: not called CC=1 SCAR=6.9
Pod undocumented
63 0 return $_[0]->now({ format => '%Y-%m-%e' });
64 }
65  
66 # evaluate a given time specification as number of seconds
67 5 ▶sub time_duration_as_seconds {
Subroutine time_duration_as_seconds: called CC=7 SCAR=19.8
Pod undocumented
68 5 my ( $class, $time_spec ) = @_;
69 5 ▶ return undef unless ( defined $time_spec and $time_spec !~ /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time_spec and not $time_spec =~ /^\s*$/ 0 5 5
Condition33%1/3
defined $time_spec and not $time_spec =~ /^\s*$/
!ll&&!rl&&r
005
MC/DC0%0/2
defined $time_spec and not $time_spec =~ /^\s*$/
defined $time_spec not $time_spec =~ /^\s*$/
Truth table33%1/3
A and not(B)
A= defined $time_specB= not $time_spec =~ /^\s*$/
ABresult
0X0
100
111
70  
71 # default: spec is just number of seconds
72 5 my $secs = $time_spec;
73  
74 5 ▶ if ( $time_spec =~ /^\s*(\d+)(\w)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_spec =~ /^\s*(\d+)(\w)\s*$/) 5 0 5
75 5 my $time = $1;
76 5 my $spec = lc $2;
77 5 ▶ $secs = $time * 60 if ( $spec eq 'm' );
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "m" 2 3 5
78 5 ▶ $secs = $time * 3600 if ( $spec eq 'h' );
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "h" 1 4 5
79 5 ▶ $secs = $time * 86400 if ( $spec eq 'd' );
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "d" 1 4 5
80 }
81 5 return $secs;
82  
83 }
84  
85 ########################################
86 # FILE ROUTINES
87  
88 0 ▶sub read_file {
Subroutine read_file: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my ( $class, $filename ) = @_;
90 0 ▶ unless ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 0 0
91 0 oi_error "Cannot open '$filename': file does not exist";
92 }
93 0 ▶ open( MOD, '<', $filename )
Branch: 1 branch
BranchTrueFalseTotal
open MOD, "<", $filename or oi_error("Cannot read '${filename}': $!") 0 0 0
94 || oi_error "Cannot read '$filename': $!";
95 0 my $contents = join( '', <MOD> );
96 0 close( MOD );
97 0 return $contents;
98 }
99  
100 0 ▶sub read_file_lines {
Subroutine read_file_lines: not called CC=7 SCAR=40.3
Pod undocumented
101 0 my ( $class, $filename ) = @_;
102 0 ▶ unless ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 0 0
103 0 oi_error "Cannot open '$filename': file does not exist";
104 }
105 0 ▶ eval { open( MOD, '<', $filename ) || die $! };
Branch: 1 branch
BranchTrueFalseTotal
open MOD, "<", $filename or die $! 0 0 0
106 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 0 oi_error "Cannot read '$filename': $@";
108 }
109 0 my @lines = ();
110 0 ▶ while ( <MOD> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MOD)) 0 0 0
111 0 ▶ next if ( /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
112 0 ▶ next if ( /^\s*\#/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*\#/ 0 0 0
113 0 chomp;
114 0 push @lines, $_;
115 }
116 0 return \@lines;
117 }
118  
119  
120 # Read in a file and evaluate it as Perl code
121  
122 12 ▶sub read_file_perl {
Subroutine read_file_perl: called CC=5 SCAR=16.4
Pod undocumented
123 12 my ( $class, $filename ) = @_;
124 12 ▶ unless ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 12 12
125 0 oi_error "Cannot open '$filename': file does not exist";
126 }
127 12 ▶ eval { open( PF, '<', $filename ) || die $! };
Branch: 1 branch
BranchTrueFalseTotal
open PF, "<", $filename or die $! 12 0 12
128 12 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
129 0 oi_error "Cannot read '$filename': $@";
130 }
131 12 local $/ = undef;
132 12 my $raw = <PF>;
133 12 my ( $data );
134 {
135 87 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 12 $data = eval $raw;
137 12 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 11 12
138 1 oi_error "Cannot evaluate [$filename] as perl code: $@";
139 }
140 }
141 11 return $data;
142 }
143  
144 0 ▶sub is_same_file {
Subroutine is_same_file: not called CC=4 SCAR=30.0
Pod undocumented
145 0 my ( $class, $src_file, $dest_file ) = @_;
146 0 ▶ return 0 unless ( -f $src_file and -f $dest_file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $src_file and -f $dest_file 0 0 0
Condition0%0/3
-f $src_file and -f $dest_file
!ll&&!rl&&r
000
MC/DC0%0/2
-f $src_file and -f $dest_file
-f $src_file -f $dest_file
Truth table0%0/3
A and B
A= -f $src_fileB= -f $dest_file
ABresult
0X0
100
111
147 0 my $src_size = (stat( $src_file ))[7];
148 0 my $dest_size = (stat( $dest_file ))[7];
149 0 ▶ return 0 unless ( $src_size == $dest_size );
Branch: 1 branch
BranchTrueFalseTotal
unless $src_size == $dest_size 0 0 0
150 0 my $src_md5 = $class->digest_file( $src_file );
151 0 my $dest_md5 = $class->digest_file( $dest_file );
152 0 return ( $src_md5 eq $dest_md5 );
153 }
154  
155 0 ▶sub digest_file {
Subroutine digest_file: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my ( $class, $file ) = @_;
157 0 my $md5 = Digest::MD5->new();
158 0 ▶ open( IN, '<', $file ) || die "Cannot read '$file' for digest: $!";
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<", $file or die "Cannot read '${file}' for digest: $!" 0 0 0
159 0 binmode( IN );
160 0 $md5->addfile( *IN );
161 0 close( IN );
162 0 return $md5->hexdigest;
163 }
164  
165 0 ▶sub digest_content {
Subroutine digest_content: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my ( $class, $content ) = @_;
167 0 my $md5 = Digest::MD5->new();
168 0 $md5->add( $content );
169 0 return $md5->hexdigest;
170 }
171  
172  
173 0 ▶sub decode_base64_and_store {
Subroutine decode_base64_and_store: not called CC=4 SCAR=30.0
Pod undocumented
174 0 my ( $class, $encoded_content, $output_file ) = @_;
175 0 my ( $fh );
176 0 ▶ if ( $output_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_file) { } 0 0 0
177 0 ▶ open( $fh, '>', $output_file )
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $output_file or oi_error("Cannot write decoded base 64 content ", "to '${output_file}': $!") 0 0 0
178 || oi_error "Cannot write decoded base 64 content ",
179 "to '$output_file': $!";
180 }
181 else {
182 0 ( $fh, $output_file ) = tempfile( 'OIPKGXXXXXX', UNLINK => 1 );
183 0 ▶ unless ( $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
184 0 oi_error "Cannot open writeable temp file";
185 }
186 }
187 0 print $fh decode_base64( $$encoded_content );
188 0 close( $fh );
189 0 return $output_file;
190 }
191  
192 ########################################
193 # EMAIL ROUTINES
194  
195 0 ▶sub send_email {
Subroutine send_email: not called CC=8 SCAR=42.8
Pod undocumented
196 0 my ( $class, $p ) = @_;
197 0 ▶ return $class->_send_email_attachment( $p ) if ( $p->{attach} );
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"attach"} 0 0 0
198 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
199 0 my %header_info = $class->_build_header_info( $p );
200 0 my $smtp_host = $class->_get_smtp_host( $p );
201 my %mail = (
202 %header_info,
203 smtp => $smtp_host,
204 message => $p->{message},
205 0 );
206 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info(qq[Trying to send to [$p->{"email"}]]) 0 0 0
207 $log->info( "Trying to send to [$p->{email}]" );
208 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug(qq[Message being sent: $p->{"message"}]) 0 0 0
209 $log->debug( "Message being sent: $p->{message}" );
210 0 ▶ eval { Mail::Sendmail::sendmail( %mail ) || die $Mail::Sendmail::error };
Branch: 1 branch
BranchTrueFalseTotal
Mail::Sendmail::sendmail(%mail) or die $Mail::Sendmail::error 0 0 0
211 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
212 0 oi_error "Cannot send email. Error: $@";
213 }
214 $log->is_info &&
215 0 ▶ $log->info( "Mail seems to have been sent ok" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Mail seems to have been sent ok") 0 0 0
216 0 return 1;
217 }
218  
219  
220 0 ▶sub _send_email_attachment {
Subroutine _send_email_attachment: not called CC=10 SCAR=47.0
221 0 my ( $class, $p ) = @_;
222 0 ▶ return $class->send_email( $p ) unless ( $p->{attach} );
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"attach"} 0 0 0
223 my $attachments = ( ref $p->{attach} eq 'ARRAY' )
224 0 ▶ ? $p->{attach} : [ $p->{attach} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $p->{'attach'} eq 'ARRAY' ? : 0 0 0
225 0 ▶ unless ( scalar @{ $attachments } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$attachments;} > 0) 0 0 0
226 0 return $class->send_email( $p );
227 }
228  
229 0 my %header_info = $class->_build_header_info( $p );
230 0 ▶ my $initial_text = $p->{message} || DEFAULT_ATTACH_MESSAGE;
Condition0%0/2
$p->{'message'} || 'Emailing attachments'
l!l
00
MC/DC0%0/1
$p->{'message'} || 'Emailing attachments'
$p->{'message'}
Truth table0%0/2
A
A= $p->{'message'}
Aresult
11
00
231 0 my $msg = MIME::Lite->new( %header_info,
232 Data => $initial_text,
233 Type => 'text/plain' );
234 0 foreach my $filename ( @{ $attachments } ) {
235 0 my $cleaned_name = $class->_clean_attachment_filename( $filename );
236 0 ▶ next unless ( $cleaned_name );
Branch: 1 branch
BranchTrueFalseTotal
unless $cleaned_name 0 0 0
237 0 my ( $ext ) = $cleaned_name =~ /\.(\w+)$/;
238 0 my $mime_type = eval {
239 0 OpenInteract2::ContentType
240 ->mime_type_by_extension( lc $ext );
241 };
242 0 ▶ my $encoding = $TEXT_ENCODINGS{ $mime_type } || 'base64';
Condition0%0/2
$TEXT_ENCODINGS{$mime_type} || 'base64'
l!l
00
MC/DC0%0/1
$TEXT_ENCODINGS{$mime_type} || 'base64'
$TEXT_ENCODINGS{$mime_type}
Truth table0%0/2
A
A= $TEXT_ENCODINGS{$mime_type}
Aresult
11
00
243 0 $msg->attach( Type => $mime_type,
244 Encoding => $encoding,
245 Path => $cleaned_name );
246 }
247  
248 0 my $smtp_host = $class->_get_smtp_host( $p );
249 0 MIME::Lite->send( 'smtp', $smtp_host, Timeout => 10 );
250 0 ▶ eval { $msg->send || die "Cannot send message: $!" };
Branch: 1 branch
BranchTrueFalseTotal
$msg->send or die "Cannot send message: $!" 0 0 0
251 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
252 0 oi_error "Cannot send email. Error: $@";
253 }
254  
255 }
256  
257  
258 0 ▶sub _build_header_info {
Subroutine _build_header_info: not called CC=4 SCAR=30.0
259 0 my ( $class, $p ) = @_;
260 0 _load_context();
261 0 my $mail_config = $CTX->lookup_mail_config;
262 return ( To => $p->{to} || $p->{email},
263 From => $p->{from} || $mail_config->{admin_email},
264 0 ▶ Subject => $p->{subject} || DEFAULT_SUBJECT );
Condition0%0/8
$p->{'to'} || $p->{'email'}
l!l&&r!l&&!r
000
$p->{'from'} || $mail_config->{'admin_email'}
l!l&&r!l&&!r
000
$p->{'subject'} || 'Mail sent from OpenInteract'
l!l
00
MC/DC0%0/2
$p->{'to'} || $p->{'email'}
$p->{'to'} $p->{'email'}
MC/DC0%0/2
$p->{'from'} || $mail_config->{'admin_email'}
$p->{'from'} $mail_config->{'admin_email'}
MC/DC0%0/1
$p->{'subject'} || 'Mail sent from OpenInteract'
$p->{'subject'}
Truth table0%0/3
A || B
A= $p->{'to'}B= $p->{'email'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $p->{'from'}B= $mail_config->{'admin_email'}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $p->{'subject'}
Aresult
11
00
265 }
266  
267  
268 0 ▶sub _get_smtp_host {
Subroutine _get_smtp_host: not called CC=2 SCAR=17.9
269 0 my ( $class, $p ) = @_;
270 0 _load_context();
271 0 my $mail_config = $CTX->lookup_mail_config;
272 return $p->{smtp} ||
273 0 ▶ $mail_config->{smtp_host};
Condition0%0/3
$p->{'smtp'} || $mail_config->{'smtp_host'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p->{'smtp'} || $mail_config->{'smtp_host'}
$p->{'smtp'} $mail_config->{'smtp_host'}
Truth table0%0/3
A || B
A= $p->{'smtp'}B= $mail_config->{'smtp_host'}
ABresult
1X1
011
000
274 }
275  
276  
277 # Ensure that no absolute filenames are used, no directory traversals
278 # (../), and that the filename exists
279  
280 0 ▶sub _clean_attachment_filename {
Subroutine _clean_attachment_filename: not called CC=10 SCAR=47.0
281 0 my ( $class, $filename ) = @_;
282 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
283 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Attachment filename begin [$filename]") 0 0 0
284 $log->debug( "Attachment filename begin [$filename]" );
285  
286 # First, see if they use an absolute. If so, strip off the leading
287 # '/' and assume they meant the absolute website directory
288  
289 0 ▶ if ( $filename =~ s|\.\.||g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ s/\.\.//g) 0 0 0
290 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("File had '..'; now [$filename]") 0 0 0
291 $log->debug( "File had '..'; now [$filename]" );
292 }
293  
294 0 ▶ if ( $filename =~ s|^/+|| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ s[^/+][]) 0 0 0
295 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("File started '/'; now [$filename]") 0 0 0
296 $log->debug( "File started '/'; now [$filename]" );
297 }
298  
299 0 _load_context();
300 0 my $website_dir = $CTX->lookup_directory( 'website' );
301 0 my $cleaned_filename = catfile( $website_dir, $filename );
302 0 ▶ if ( -f $cleaned_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cleaned_filename) 0 0 0
303 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Existing file [$cleaned_filename]") 0 0 0
304 $log->debug( "Existing file [$cleaned_filename]" );
305 0 return $cleaned_filename;
306 }
307 $log->is_debug &&
308 0 ▶ $log->debug( "Nonexisting file [$cleaned_filename]" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Nonexisting file [$cleaned_filename]") 0 0 0
309 0 return undef;
310 }
311  
312 ########################################
313 # SECURITY ROUTINES
314  
315 87 my %VERBOSE_SECURITY = (
316 SEC_LEVEL_NONE_VERBOSE() => SEC_LEVEL_NONE,
317 SEC_LEVEL_SUMMARY_VERBOSE() => SEC_LEVEL_SUMMARY,
318 SEC_LEVEL_READ_VERBOSE() => SEC_LEVEL_READ,
319 SEC_LEVEL_WRITE_VERBOSE() => SEC_LEVEL_WRITE,
320 );
321  
322 0 ▶sub verbose_to_level {
Subroutine verbose_to_level: not called CC=1 SCAR=6.9
Pod undocumented
323 0 my ( $class, $verbose ) = @_;
324 0 return $VERBOSE_SECURITY{ uc $verbose };
325 }
326  
327 ########################################
328 # FACTORY IMPLEMENTATIONS
329  
330 # Find all subclasses of a particular factory class anywhere on @INC
331  
332 87 my %FACTORY_FILES = ();
333  
334 349 ▶sub find_factory_subclasses {
Subroutine find_factory_subclasses: called CC=10 SCAR=23.1
Pod undocumented
335 349 my ( $class, $factory_class, @dirs ) = @_;
336 349 ▶ unless ( @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@dirs) 1 348 349
337 1 @dirs = @INC;
338 }
339 349 %FACTORY_FILES = ();
340  
341 349 foreach my $lib_dir ( @dirs ) {
342 2792 ▶ next unless ( $lib_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $lib_dir 0 2792 2792
343 2792 my $manage_dir = catdir( $lib_dir, split( '::', $factory_class ) );
344 2792 ▶ next unless ( -d $manage_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $manage_dir 2096 696 2792
345 696 eval { _find_descend( $manage_dir ) };
346 696 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 696 696
347 0 carp "Error trying to find subclasses of '$factory_class' in $lib_dir: $@\n";
348 }
349 }
350  
351 # Now grab the class names from the files stored in %FACTORY_FILES
352 # so we don't try to include the same class from different files
353 # -- this normally only happens for developers who have the OI2
354 # lib directory in their PERL5LIB and who are running tests
355  
356 349 my %FACTORY_CLASSES = ();
357 349 foreach my $file ( sort keys %FACTORY_FILES ) {
358 14442 my $file_class = $file;
359 14442 $file_class =~ s/^.*OpenInteract2/OpenInteract2/;
360 14442 $file_class =~ s/\.pm$//;
361 14442 $file_class =~ s/\W/::/g;
362 14442 ▶ $FACTORY_CLASSES{ $file_class } ||= $file;
Condition66%2/3
$FACTORY_CLASSES{$file_class} ||= $file
l!l&&r!l&&!r
722172210
MC/DC0%0/2
$FACTORY_CLASSES{$file_class} ||= $file
$FACTORY_CLASSES{$file_class} $file
Truth table66%2/3
A ||= B
A= $FACTORY_CLASSES{$file_class}B= $file
ABresult
1X1
011
000
363 }
364  
365 # Why 'sort'? It ensures that classes further up the hierarchy
366 # (e.g., 'OI2::Manage::Website') get required before their
367 # children; otherwise we get lots of 'subroutine foo redefined'
368 # messages under '-w', irritating.
369  
370 349 my @included = ();
371 349 foreach my $factory_subclass ( sort keys %FACTORY_CLASSES ) {
372 7221 eval "require $factory_subclass";
373 7221 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 435 6786 7221
374 435 carp "Failed to bring in library '$factory_subclass': $@";
375 }
376 else {
377 6786 push @included, $factory_subclass;
378 }
379 }
380 349 return @included;
381 }
382  
383 1044 ▶sub _find_descend {
Subroutine _find_descend: called CC=5 SCAR=16.1
384 1044 my ( $lib_dir ) = @_;
385 1044 ▶ opendir( FACTORYDIR, $lib_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir FACTORYDIR, $lib_dir or die "Cannot open directory '${lib_dir}': $!" 1044 0 1044
386 || die "Cannot open directory '$lib_dir': $!";
387 1044 my @entries = grep ! /^\./, readdir( FACTORYDIR );
388 1044 foreach my $entry ( @entries ) {
389 14790 my $full_entry_dir = catdir( $lib_dir, $entry );
390 14790 ▶ if ( -d $full_entry_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $full_entry_dir) 348 14442 14790
391 348 _find_descend( $full_entry_dir ); # let this error bubble
392 }
393 14790 ▶ next unless ( $entry =~ /\.pm$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $entry =~ /\.pm$/ 348 14442 14790
394 14442 my $full_filename = catfile( $lib_dir, $entry );
395 14442 $FACTORY_FILES{ $full_filename } = 1;
396 }
397 }
398  
399  
400 87 1;
401  
402 __END__
« OpenInteract2/Error.pm ↑ Summary OpenInteract2/Observer/Controller/GetFiltersFromRequest.pm »