blib/lib/Template/Plugin/Directory.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 115 CC 9 SCAR 25.6
CC
9
Cov
0%
new
30.0
scan
20.0
content
6.0
CRAP 13.0
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 use strict;
23 use warnings;
24 use Cwd;
25 use File::Spec;
26 use Template::Plugin::File;
27 use base 'Template::Plugin::File';
28  
29 our $VERSION = '3.105';
30  
31  
32 #------------------------------------------------------------------------
33 # new(\%config)
34 #
35 # Constructor method.
36 #------------------------------------------------------------------------
37  
38 sub new {
39 my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { };
40 my ($class, $context, $path) = @_;
41  
42 return $class->throw('no directory specified')
43 unless defined $path and length $path;
44  
45 my $self = $class->SUPER::new($context, $path, $config);
46 my ($dir, @files, $name, $item, $abs, $rel, $check);
47 $self->{ files } = [ ];
48 $self->{ dirs } = [ ];
49 $self->{ list } = [ ];
50 $self->{ _dir } = { };
51  
52 # don't read directory if 'nostat' or 'noscan' set
53 return $self if $config->{ nostat } || $config->{ noscan };
54  
55 $self->throw("$path: not a directory")
56 unless $self->{ isdir };
57  
58 $self->scan($config);
59  
60 return $self;
61 }
62  
63  
64 #------------------------------------------------------------------------
65 # scan(\%config)
66 #
67 # Scan directory for files and sub-directories.
68 #------------------------------------------------------------------------
69  
70 sub scan {
71 my ($self, $config) = @_;
72 $config ||= { };
73 local *DH;
74 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 $config->{ noscan } = 1 unless $config->{ recurse };
79  
80 $dir = $self->{ abs };
81 opendir(DH, $dir) or return $self->throw("$dir: $!");
82  
83 @files = readdir DH;
84 closedir(DH)
85 or return $self->throw("$dir close: $!");
86  
87 my ($path, $files, $dirs, $list) = @$self{ qw( path files dirs list ) };
88 @$files = @$dirs = @$list = ();
89  
90 foreach $name (sort @files) {
91 next if $name =~ /^\./;
92 $abs = File::Spec->catfile($dir, $name);
93 $rel = File::Spec->catfile($path, $name);
94  
95 if (-d $abs) {
96 $item = Template::Plugin::Directory->new(undef, $rel, $config);
97 push(@$dirs, $item);
98 }
99 else {
100 $item = Template::Plugin::File->new(undef, $rel, $config);
101 push(@$files, $item);
102 }
103 push(@$list, $item);
104 $self->{ _dir }->{ $name } = $item;
105 }
106  
107 return '';
108 }
109  
110  
111 #------------------------------------------------------------------------
112 # file($filename)
113 #
114 # Fetch a named file from this directory.
115 #------------------------------------------------------------------------
116  
117 sub file {
118 my ($self, $name) = @_;
119 return $self->{ _dir }->{ $name };
120 }
121  
122  
123 #------------------------------------------------------------------------
124 # present($view)
125 #
126 # Present self to a Template::View
127 #------------------------------------------------------------------------
128  
129 sub present {
130 my ($self, $view) = @_;
131 $view->view_directory($self);
132 }
133  
134  
135 #------------------------------------------------------------------------
136 # content($view)
137 #
138 # Present directory content to a Template::View.
139 #------------------------------------------------------------------------
140  
141 sub content {
142 my ($self, $view) = @_;
143 return $self->{ list } unless $view;
144 my $output = '';
145 foreach my $file (@{ $self->{ list } }) {
146 $output .= $file->present($view);
147 }
148 return $output;
149 }
150  
151  
152 #------------------------------------------------------------------------
153 # throw($msg)
154 #
155 # Throw a 'Directory' exception.
156 #------------------------------------------------------------------------
157  
158 sub throw {
159 my ($self, $error) = @_;
160 die (Template::Exception->new('Directory', $error));
161 }
162  
163 1;
164  
165 __END__