line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 , with some mutilations from |
10
|
|
|
|
|
|
|
# Andy Wardley . |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# COPYRIGHT |
13
|
|
|
|
|
|
|
# Copyright (C) 2000-2007 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
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
23
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
22
|
|
24
|
1
|
|
|
1
|
|
2
|
use Cwd; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
25
|
1
|
|
|
1
|
|
3
|
use File::Spec; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
26
|
1
|
|
|
1
|
|
266
|
use Template::Plugin::File; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
27
|
1
|
|
|
1
|
|
4
|
use base 'Template::Plugin::File'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
516
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = 2.70; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
33
|
|
|
|
|
|
|
# new(\%config) |
34
|
|
|
|
|
|
|
# |
35
|
|
|
|
|
|
|
# Constructor method. |
36
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub new { |
39
|
36
|
100
|
|
36
|
1
|
70
|
my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { }; |
40
|
36
|
|
|
|
|
40
|
my ($class, $context, $path) = @_; |
41
|
|
|
|
|
|
|
|
42
|
36
|
100
|
66
|
|
|
120
|
return $class->throw('no directory specified') |
43
|
|
|
|
|
|
|
unless defined $path and length $path; |
44
|
|
|
|
|
|
|
|
45
|
35
|
|
|
|
|
82
|
my $self = $class->SUPER::new($context, $path, $config); |
46
|
34
|
|
|
|
|
34
|
my ($dir, @files, $name, $item, $abs, $rel, $check); |
47
|
34
|
|
|
|
|
53
|
$self->{ files } = [ ]; |
48
|
34
|
|
|
|
|
40
|
$self->{ dirs } = [ ]; |
49
|
34
|
|
|
|
|
48
|
$self->{ list } = [ ]; |
50
|
34
|
|
|
|
|
39
|
$self->{ _dir } = { }; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# don't read directory if 'nostat' or 'noscan' set |
53
|
34
|
100
|
66
|
|
|
120
|
return $self if $config->{ nostat } || $config->{ noscan }; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$self->throw("$path: not a directory") |
56
|
19
|
50
|
|
|
|
33
|
unless $self->{ isdir }; |
57
|
|
|
|
|
|
|
|
58
|
19
|
|
|
|
|
38
|
$self->scan($config); |
59
|
|
|
|
|
|
|
|
60
|
19
|
|
|
|
|
48
|
return $self; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
65
|
|
|
|
|
|
|
# scan(\%config) |
66
|
|
|
|
|
|
|
# |
67
|
|
|
|
|
|
|
# Scan directory for files and sub-directories. |
68
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub scan { |
71
|
23
|
|
|
23
|
0
|
134
|
my ($self, $config) = @_; |
72
|
23
|
|
100
|
|
|
43
|
$config ||= { }; |
73
|
23
|
|
|
|
|
47
|
local *DH; |
74
|
23
|
|
|
|
|
14
|
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
|
100
|
|
|
|
45
|
$config->{ noscan } = 1 unless $config->{ recurse }; |
79
|
|
|
|
|
|
|
|
80
|
23
|
|
|
|
|
20
|
$dir = $self->{ abs }; |
81
|
23
|
50
|
|
|
|
445
|
opendir(DH, $dir) or return $self->throw("$dir: $!"); |
82
|
|
|
|
|
|
|
|
83
|
23
|
|
|
|
|
213
|
@files = readdir DH; |
84
|
23
|
50
|
|
|
|
117
|
closedir(DH) |
85
|
|
|
|
|
|
|
or return $self->throw("$dir close: $!"); |
86
|
|
|
|
|
|
|
|
87
|
23
|
|
|
|
|
45
|
my ($path, $files, $dirs, $list) = @$self{ qw( path files dirs list ) }; |
88
|
23
|
|
|
|
|
35
|
@$files = @$dirs = @$list = (); |
89
|
|
|
|
|
|
|
|
90
|
23
|
|
|
|
|
78
|
foreach $name (sort @files) { |
91
|
125
|
100
|
|
|
|
261
|
next if $name =~ /^\./; |
92
|
79
|
|
|
|
|
611
|
$abs = File::Spec->catfile($dir, $name); |
93
|
79
|
|
|
|
|
301
|
$rel = File::Spec->catfile($path, $name); |
94
|
|
|
|
|
|
|
|
95
|
79
|
100
|
|
|
|
786
|
if (-d $abs) { |
96
|
22
|
|
|
|
|
89
|
$item = Template::Plugin::Directory->new(undef, $rel, $config); |
97
|
22
|
|
|
|
|
26
|
push(@$dirs, $item); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
57
|
|
|
|
|
159
|
$item = Template::Plugin::File->new(undef, $rel, $config); |
101
|
57
|
|
|
|
|
83
|
push(@$files, $item); |
102
|
|
|
|
|
|
|
} |
103
|
79
|
|
|
|
|
66
|
push(@$list, $item); |
104
|
79
|
|
|
|
|
233
|
$self->{ _dir }->{ $name } = $item; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
23
|
|
|
|
|
77
|
return ''; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
112
|
|
|
|
|
|
|
# file($filename) |
113
|
|
|
|
|
|
|
# |
114
|
|
|
|
|
|
|
# Fetch a named file from this directory. |
115
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub file { |
118
|
1
|
|
|
1
|
0
|
1
|
my ($self, $name) = @_; |
119
|
1
|
|
|
|
|
8
|
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
|
3
|
|
|
3
|
0
|
4
|
my ($self, $view) = @_; |
131
|
3
|
|
|
|
|
15
|
$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
|
3
|
|
|
3
|
0
|
42
|
my ($self, $view) = @_; |
143
|
3
|
50
|
|
|
|
5
|
return $self->{ list } unless $view; |
144
|
3
|
|
|
|
|
4
|
my $output = ''; |
145
|
3
|
|
|
|
|
3
|
foreach my $file (@{ $self->{ list } }) { |
|
3
|
|
|
|
|
9
|
|
146
|
9
|
|
|
|
|
20
|
$output .= $file->present($view); |
147
|
|
|
|
|
|
|
} |
148
|
3
|
|
|
|
|
8
|
return $output; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
153
|
|
|
|
|
|
|
# throw($msg) |
154
|
|
|
|
|
|
|
# |
155
|
|
|
|
|
|
|
# Throw a 'Directory' exception. |
156
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub throw { |
159
|
2
|
|
|
2
|
0
|
2
|
my ($self, $error) = @_; |
160
|
2
|
|
|
|
|
8
|
die (Template::Exception->new('Directory', $error)); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
__END__ |