lib/Template/Plugin/Directory.pm

filter: Statementstmt 100% 69 / 69 Branchbran 80.0% 16 / 20 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 12 / 12 Podpod 16.6% 1 / 6 totaltotal 90.8% 109 / 120 CC 16 SCAR 15.8
CC
16
Cov
95%
scan
8.0
new
7.0
content
3.0
CRAP 4.9
« lib/Template/Constants.pm ↑ Summary lib/Template/Config.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Directory
4 #
5 # DESCRIPTION
6 # Plugin for encapsulating information about a file system directory.
7 #
8 # AUTHORS
9 # Michael Stevens <michael@etla.org>, with some mutilations from
10 # Andy Wardley <abw@wardley.org>.
11 #
12 # COPYRIGHT
13 # Copyright (C) 2000-2022 Michael Stevens, Andy Wardley.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Plugin::Directory;
21  
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Template::Plugin::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use base 'Template::Plugin::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our $VERSION = '3.106';
30  
31  
32 #------------------------------------------------------------------------
33 # new(\%config)
34 #
35 # Constructor method.
36 #------------------------------------------------------------------------
37  
38 36 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
39 36 ▶ my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 28 8 36
40 36 my ($class, $context, $path) = @_;
41  
42 36 ▶ return $class->throw('no directory specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 1 35 36
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
1035
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
43 unless defined $path and length $path;
44  
45 35 my $self = $class->SUPER::new($context, $path, $config);
46 34 my ($dir, @files, $name, $item, $abs, $rel, $check);
47 34 $self->{ files } = [ ];
48 34 $self->{ dirs } = [ ];
49 34 $self->{ list } = [ ];
50 34 $self->{ _dir } = { };
51  
52 # don't read directory if 'nostat' or 'noscan' set
53 34 ▶ return $self if $config->{ nostat } || $config->{ noscan };
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"nostat"} or $config->{"noscan"} 15 19 34
Condition100%3/3
$config->{"nostat"} or $config->{"noscan"}
l!l&&r!l&&!r
11419
MC/DC100%2/2
$config->{"nostat"} or $config->{"noscan"}
$config->{"nostat"} $config->{"noscan"}
Truth table100%3/3
A or B
A= $config->{"nostat"}B= $config->{"noscan"}
ABresult
1X1
011
000
54  
55 $self->throw("$path: not a directory")
56 19 ▶ unless $self->{ isdir };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"isdir"} 0 19 19
57  
58 19 $self->scan($config);
59  
60 19 return $self;
61 }
62  
63  
64 #------------------------------------------------------------------------
65 # scan(\%config)
66 #
67 # Scan directory for files and sub-directories.
68 #------------------------------------------------------------------------
69  
70 23 ▶sub scan {
Subroutine scan: called CC=8 SCAR=20.8
Pod undocumented
71 23 my ($self, $config) = @_;
72 23 ▶ $config ||= { };
Condition100%2/2
$config ||= {}
l!l
194
MC/DC100%1/1
$config ||= {}
$config
Truth table100%2/2
A
A= $config
Aresult
11
00
73 23 local *DH;
74 23 my ($dir, @files, $name, $abs, $rel, $item);
75  
76 # set 'noscan' in config if recurse isn't set, to ensure Directories
77 # created don't try to scan deeper
78 23 ▶ $config->{ noscan } = 1 unless $config->{ recurse };
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"recurse"} 11 12 23
79  
80 23 $dir = $self->{ abs };
81 23 ▶ opendir(DH, $dir) or return $self->throw("$dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
opendir DH, $dir or return $self->throw("${dir}: $!") 23 0 23
82  
83 23 @files = readdir DH;
84 23 ▶ closedir(DH)
Branch: 1 branch
BranchTrueFalseTotal
closedir DH or return $self->throw("$dir close: $!") 23 0 23
85 or return $self->throw("$dir close: $!");
86  
87 23 my ($path, $files, $dirs, $list) = @$self{ qw( path files dirs list ) };
88 23 @$files = @$dirs = @$list = ();
89  
90 23 foreach $name (sort @files) {
91 125 ▶ next if $name =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\./ 46 79 125
92 79 $abs = File::Spec->catfile($dir, $name);
93 79 $rel = File::Spec->catfile($path, $name);
94  
95 79 ▶ if (-d $abs) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $abs) { } 22 57 79
96 22 $item = Template::Plugin::Directory->new(undef, $rel, $config);
97 22 push(@$dirs, $item);
98 }
99 else {
100 57 $item = Template::Plugin::File->new(undef, $rel, $config);
101 57 push(@$files, $item);
102 }
103 79 push(@$list, $item);
104 79 $self->{ _dir }->{ $name } = $item;
105 }
106  
107 23 return '';
108 }
109  
110  
111 #------------------------------------------------------------------------
112 # file($filename)
113 #
114 # Fetch a named file from this directory.
115 #------------------------------------------------------------------------
116  
117 1 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod undocumented
118 1 my ($self, $name) = @_;
119 1 return $self->{ _dir }->{ $name };
120 }
121  
122  
123 #------------------------------------------------------------------------
124 # present($view)
125 #
126 # Present self to a Template::View
127 #------------------------------------------------------------------------
128  
129 3 ▶sub present {
Subroutine present: called CC=1 SCAR=0.0
Pod undocumented
130 3 my ($self, $view) = @_;
131 3 $view->view_directory($self);
132 }
133  
134  
135 #------------------------------------------------------------------------
136 # content($view)
137 #
138 # Present directory content to a Template::View.
139 #------------------------------------------------------------------------
140  
141 3 ▶sub content {
Subroutine content: called CC=3 SCAR=11.0
Pod undocumented
142 3 my ($self, $view) = @_;
143 3 ▶ return $self->{ list } unless $view;
Branch: 1 branch
BranchTrueFalseTotal
unless $view 0 3 3
144 3 my $output = '';
145 3 foreach my $file (@{ $self->{ list } }) {
146 9 $output .= $file->present($view);
147 }
148 3 return $output;
149 }
150  
151  
152 #------------------------------------------------------------------------
153 # throw($msg)
154 #
155 # Throw a 'Directory' exception.
156 #------------------------------------------------------------------------
157  
158 2 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
Pod undocumented
159 2 my ($self, $error) = @_;
160 2 die (Template::Exception->new('Directory', $error));
161 }
162  
163 1 1;
164  
165 __END__
« lib/Template/Constants.pm ↑ Summary lib/Template/Config.pm »