line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::StaticCompressor::Container; |
2
|
|
|
|
|
|
|
# Complex cache container |
3
|
|
|
|
|
|
|
|
4
|
4
|
|
|
4
|
|
22
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
150
|
|
5
|
4
|
|
|
4
|
|
22
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
97
|
|
6
|
4
|
|
|
4
|
|
21
|
use utf8; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
22
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
1096
|
use Mojo::Asset::File; |
|
4
|
|
|
|
|
13357
|
|
|
4
|
|
|
|
|
55
|
|
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
3250
|
use Mojolicious::Plugin::StaticCompressor::File; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
5445
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
10
|
|
|
10
|
1
|
64
|
my ($class, %hash) = @_; |
14
|
10
|
|
|
|
|
39
|
my $s = bless({}, $class); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Initialize |
17
|
10
|
|
|
|
|
51
|
$s->{config} = $hash{config}; |
18
|
10
|
|
50
|
|
|
68
|
$s->{mojo_static} = $hash{config}->{mojo_static} || die('Not specified mojo_static'); |
19
|
10
|
|
50
|
|
|
61
|
$s->{path_cache_dir} = $hash{config}->{path_cache_dir} || die('Not specified path_cache_dir'); |
20
|
|
|
|
|
|
|
|
21
|
10
|
|
|
|
|
30
|
$s->{key} = undef; |
22
|
10
|
|
100
|
|
|
62
|
$s->{is_minify} = $hash{is_minify} || 0; |
23
|
10
|
|
100
|
|
|
55
|
$s->{extension} = $hash{extension} || undef; |
24
|
10
|
|
|
|
|
35
|
$s->{files} = undef; |
25
|
10
|
|
|
|
|
32
|
$s->{num_of_files} = undef; |
26
|
10
|
|
|
|
|
25
|
$s->{content} = undef; |
27
|
|
|
|
|
|
|
|
28
|
10
|
100
|
|
|
|
83
|
if(defined $hash{key}){ |
|
|
50
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Initialize from the key (with load the path of files, from cached file) |
30
|
5
|
|
|
|
|
20
|
$s->{key} = $hash{key}; |
31
|
5
|
|
|
|
|
21
|
$s->_init_from_key(); |
32
|
|
|
|
|
|
|
} elsif(defined $hash{path_files_ref}) { |
33
|
|
|
|
|
|
|
# Initialize from the path of files |
34
|
5
|
|
|
|
|
32
|
$s->_init_from_source_files( $hash{path_files_ref} ); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
10
|
|
|
|
|
281
|
return $s; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Get (or Generate) the key |
41
|
|
|
|
|
|
|
sub get_key { |
42
|
30
|
|
|
30
|
1
|
44
|
my $s = shift; |
43
|
|
|
|
|
|
|
|
44
|
30
|
100
|
|
|
|
92
|
if(!defined $s->{key}){ |
45
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
10
|
my $key = ""; |
47
|
5
|
|
|
|
|
11
|
foreach my $file (@{$s->{files}}){ |
|
5
|
|
|
|
|
20
|
|
48
|
7
|
100
|
|
|
|
34
|
if($key ne ""){ |
49
|
2
|
|
|
|
|
4
|
$key .= ","; |
50
|
|
|
|
|
|
|
} |
51
|
7
|
|
|
|
|
32
|
$key .= $file->get_raw_path(); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
29
|
$key = Mojo::Util::sha1_sum($key); |
55
|
|
|
|
|
|
|
|
56
|
5
|
100
|
|
|
|
86
|
if($s->{is_minify} == 0){ |
57
|
2
|
|
|
|
|
6
|
$key = "nomin-".$key; |
58
|
|
|
|
|
|
|
} |
59
|
5
|
|
|
|
|
22
|
$s->{key} = $key.'.'.$s->{extension}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
30
|
|
|
|
|
144
|
return $s->{key}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Get the extension |
66
|
|
|
|
|
|
|
sub get_extension { |
67
|
5
|
|
|
5
|
1
|
10
|
my $s = shift; |
68
|
5
|
|
|
|
|
36
|
return $s->{extension}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Get the processed content |
72
|
|
|
|
|
|
|
sub get_content { |
73
|
5
|
|
|
5
|
1
|
10
|
my $s = shift; |
74
|
5
|
|
|
|
|
23
|
return $s->{content}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Update with check for update of files. (Return: Not updated = 0 / Updated = 1) |
78
|
|
|
|
|
|
|
sub update { |
79
|
0
|
|
|
0
|
1
|
0
|
my $s = shift; |
80
|
0
|
|
|
|
|
0
|
return $s->_cache(); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Initialize from the key |
84
|
|
|
|
|
|
|
sub _init_from_key { |
85
|
5
|
|
|
5
|
|
11
|
my $s = shift; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Load parameters from the key |
88
|
5
|
50
|
|
|
|
18
|
if(defined $s->{key}){ |
89
|
5
|
50
|
|
|
|
42
|
if($s->{key} =~ /^(nomin\-|)(\w+).(\w+)$/){ |
90
|
|
|
|
|
|
|
# Minify |
91
|
5
|
100
|
|
|
|
21
|
if($1 eq 'nomin-'){ |
92
|
2
|
|
|
|
|
4
|
$s->{is_minify} = 0; |
93
|
|
|
|
|
|
|
} else { |
94
|
3
|
|
|
|
|
8
|
$s->{is_minify} = 1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
# Extension |
97
|
5
|
|
|
|
|
23
|
$s->{extension} = $3; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Load the list from cached file |
102
|
5
|
|
|
|
|
23
|
my @paths_files = $s->_load_from_cache(); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Process and (re)cache |
105
|
5
|
50
|
|
|
|
21
|
if(@paths_files){ |
106
|
5
|
|
|
|
|
31
|
$s->_init_from_source_files( \@paths_files ); |
107
|
|
|
|
|
|
|
} else { |
108
|
0
|
|
|
|
|
0
|
die("Can't load the information from cached file. You must access to page of import origin."); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Load from the cached file and return the list of file |
113
|
|
|
|
|
|
|
sub _load_from_cache { |
114
|
5
|
|
|
5
|
|
10
|
my $s = shift; |
115
|
5
|
|
|
|
|
34
|
my $path_cache_file = $s->{path_cache_dir}.$s->{key}; |
116
|
|
|
|
|
|
|
|
117
|
5
|
50
|
|
|
|
275
|
if(-f $path_cache_file){ # If exist cached file |
118
|
5
|
|
|
|
|
9
|
my $content; |
119
|
|
|
|
|
|
|
# Load the file and check the update_at |
120
|
5
|
|
|
|
|
17
|
eval { |
121
|
5
|
|
|
|
|
121
|
my $cache = Mojo::Asset::File->new( path => $path_cache_file ); |
122
|
5
|
|
|
|
|
98
|
$content = Encode::decode_utf8($cache->slurp()); |
123
|
5
|
|
|
|
|
1094
|
my $updated_at = (stat( $path_cache_file ))[9]; |
124
|
|
|
|
|
|
|
}; |
125
|
5
|
50
|
|
|
|
218
|
if($@){ die("Can't read the cache file:". $path_cache_file); } |
|
0
|
|
|
|
|
0
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Parse the file |
128
|
5
|
50
|
|
|
|
44
|
if($content =~ /^\/\*-{5}StaticCompressor-{5}\n((.+\n)+?)-{10}\*\/\n/){ |
129
|
5
|
|
|
|
|
30
|
my @paths = split("\n", $1); |
130
|
5
|
|
|
|
|
41
|
$content =~ s/^\/\*-{5}StaticCompressor-{5}\n((.+\n)+?)-{10}\*\/\n//; |
131
|
5
|
|
|
|
|
14
|
$s->{content} = $content; |
132
|
5
|
|
|
|
|
1166
|
return @paths; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
0
|
$s->{content} = $content; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
0
|
return; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Initialize from the source files |
142
|
|
|
|
|
|
|
sub _init_from_source_files { |
143
|
10
|
|
|
10
|
|
19
|
my ($s, $path_files_ref) = @_; |
144
|
|
|
|
|
|
|
|
145
|
10
|
|
|
|
|
28
|
$s->{files} = (); |
146
|
10
|
|
|
|
|
38
|
$s->{num_of_files} = 0; |
147
|
|
|
|
|
|
|
|
148
|
10
|
|
|
|
|
18
|
foreach my $path (@{$path_files_ref}){ |
|
10
|
|
|
|
|
29
|
|
149
|
14
|
|
|
|
|
148
|
my $file = Mojolicious::Plugin::StaticCompressor::File->new( |
150
|
|
|
|
|
|
|
path_file => $path, |
151
|
|
|
|
|
|
|
extension => $s->{extension}, |
152
|
|
|
|
|
|
|
is_minify => $s->{is_minify}, |
153
|
|
|
|
|
|
|
config => $s->{config}, |
154
|
|
|
|
|
|
|
); |
155
|
14
|
|
|
|
|
25
|
push(@{$s->{files}}, $file); |
|
14
|
|
|
|
|
38
|
|
156
|
14
|
|
|
|
|
53
|
$s->{num_of_files} += 1; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# Process and cache |
160
|
10
|
|
|
|
|
43
|
$s->_cache(); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Process and cache the container file |
164
|
|
|
|
|
|
|
sub _cache { |
165
|
10
|
|
|
10
|
|
21
|
my $s = shift; |
166
|
10
|
|
|
|
|
97
|
my $path_cache_file = $s->{path_cache_dir}.$s->get_key(); |
167
|
|
|
|
|
|
|
|
168
|
10
|
50
|
|
|
|
463
|
if(-f $path_cache_file){ # If exist cached file |
169
|
|
|
|
|
|
|
# Check for update of container cache |
170
|
10
|
|
|
|
|
19
|
my $updated_at; |
171
|
10
|
|
|
|
|
17
|
eval { |
172
|
10
|
|
|
|
|
84
|
my $cache = Mojo::Asset::File->new( path => $path_cache_file ); |
173
|
10
|
|
|
|
|
331
|
$updated_at = (stat( $path_cache_file ))[9]; |
174
|
|
|
|
|
|
|
}; |
175
|
10
|
50
|
|
|
|
388
|
unless(@$){ |
176
|
|
|
|
|
|
|
# Check for update of single files / caches |
177
|
10
|
|
|
|
|
15
|
my $is_need_update = 0; |
178
|
10
|
|
|
|
|
17
|
foreach my $file (@{$s->{files}}){ |
|
10
|
|
|
|
|
27
|
|
179
|
13
|
100
|
|
|
|
97
|
if($updated_at <= $file->get_updated_at()){ |
180
|
3
|
|
|
|
|
6
|
$is_need_update = 1; |
181
|
3
|
|
|
|
|
5
|
last; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
10
|
100
|
|
|
|
33
|
if($is_need_update == 0){ # Latest already |
186
|
7
|
|
|
|
|
25
|
return 0; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
3
|
|
|
|
|
5
|
my $content = ""; |
192
|
3
|
|
|
|
|
4
|
my $paths_text = ""; |
193
|
|
|
|
|
|
|
# Process the files |
194
|
3
|
|
|
|
|
5
|
foreach my $file (@{$s->{files}}){ |
|
3
|
|
|
|
|
7
|
|
195
|
|
|
|
|
|
|
# Process of the file (Minify), and Combine it |
196
|
4
|
|
|
|
|
16
|
$content .= $file->get_content(); |
197
|
|
|
|
|
|
|
|
198
|
4
|
100
|
|
|
|
12
|
if($paths_text ne ""){ $paths_text .= "\n"}; |
|
1
|
|
|
|
|
2
|
|
199
|
4
|
|
|
|
|
16
|
$paths_text .= $file->get_raw_path(); |
200
|
|
|
|
|
|
|
} |
201
|
3
|
|
|
|
|
6
|
$s->{content} = $content; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
# Save to container cache |
204
|
3
|
|
|
|
|
15
|
my $cache = Mojo::Asset::File->new(); |
205
|
3
|
|
|
|
|
22
|
my $save_header = <<EOF; |
206
|
|
|
|
|
|
|
/*-----StaticCompressor----- |
207
|
|
|
|
|
|
|
$paths_text |
208
|
|
|
|
|
|
|
----------*/ |
209
|
|
|
|
|
|
|
EOF |
210
|
3
|
|
|
|
|
14
|
$cache->add_chunk( Encode::encode_utf8($save_header.$content) ); |
211
|
3
|
|
|
|
|
1514
|
$cache->move_to( $path_cache_file ); |
212
|
|
|
|
|
|
|
|
213
|
3
|
|
|
|
|
1038
|
return 1; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1; |
217
|
|
|
|
|
|
|
__END__ |
218
|
|
|
|
|
|
|
=head1 NAME |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Mojolicious::Plugin::StaticCompressor::Container |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 SYNOPSIS |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
This is internal package. |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Please see POD for L<Mojolicious::Plugin::StaticCompressor>. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
L<https://github.com/mugifly/p5-Mojolicious-Plugin-StaticCompressor/blob/master/README.pod> |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 METHODS |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 new ( ... ) |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Initialize a instance of cache container. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head2 get_key ( ) |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Get a cache key of the file. (If necessary, generate it.) |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head2 get_extension ( ) |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Get the extension of the file. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 get_content ( ) |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Get the processed content of the file. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head2 update ( ) |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Check for updates of source files. And if necessary, update cache. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head1 SEE ALSO |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
L<Mojolicious::Plugin::StaticCompressor> ( L<https://github.com/mugifly/p5-Mojolicious-Plugin-StaticCompressor> ) |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Please see POD for L<Mojolicious::Plugin::StaticCompressor>. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
L<https://github.com/mugifly/p5-Mojolicious-Plugin-StaticCompressor/blob/master/README.pod> |