StaticCompressor/Container.pm

filter: Statementstmt 94.9% 113 / 119 Branchbran 76.4% 26 / 34 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 92.8% 13 / 14 Podpod 100% 5 / 5 totaltotal 89.6% 165 / 184 CC 26 SCAR 16.1
CC
26
Cov
90%
_cache
8.0
new
7.1
_init_from_key
5.2
CRAP 5.0
« StaticCompressor/File.pm ↑ Summary
1 package Mojolicious::Plugin::StaticCompressor::Container;
2 # Complex cache container
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Mojolicious::Plugin::StaticCompressor::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
13 10 my ($class, %hash) = @_;
14 10 my $s = bless({}, $class);
15  
16 # Initialize
17 10 $s->{config} = $hash{config};
18 10 ▶ $s->{mojo_static} = $hash{config}->{mojo_static} || die('Not specified mojo_static');
Condition50%1/2
$hash{'config'}{'mojo_static'} || die('Not specified mojo_static')
l!l
100
MC/DC0%0/1
$hash{'config'}{'mojo_static'} || die('Not specified mojo_static')
$hash{'config'}{'mojo_static'}
Truth table50%1/2
A
A= $hash{'config'}{'mojo_static'}
Aresult
11
00
19 10 ▶ $s->{path_cache_dir} = $hash{config}->{path_cache_dir} || die('Not specified path_cache_dir');
Condition50%1/2
$hash{'config'}{'path_cache_dir'} || die('Not specified path_cache_dir')
l!l
100
MC/DC0%0/1
$hash{'config'}{'path_cache_dir'} || die('Not specified path_cache_dir')
$hash{'config'}{'path_cache_dir'}
Truth table50%1/2
A
A= $hash{'config'}{'path_cache_dir'}
Aresult
11
00
20  
21 10 $s->{key} = undef;
22 10 ▶ $s->{is_minify} = $hash{is_minify} || 0;
Condition100%2/2
$hash{'is_minify'} || 0
l!l
37
MC/DC100%1/1
$hash{'is_minify'} || 0
$hash{'is_minify'}
Truth table100%2/2
A
A= $hash{'is_minify'}
Aresult
11
00
23 10 ▶ $s->{extension} = $hash{extension} || undef;
Condition100%2/2
$hash{'extension'} || undef
l!l
55
MC/DC100%1/1
$hash{'extension'} || undef
$hash{'extension'}
Truth table100%2/2
A
A= $hash{'extension'}
Aresult
11
00
24 10 $s->{files} = undef;
25 10 $s->{num_of_files} = undef;
26 10 $s->{content} = undef;
27  
28 10 ▶ if(defined $hash{key}){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hash{'key'}) { } 5 5 10
elsif (defined $hash{'path_files_ref'}) { } 5 0 5
29 # Initialize from the key (with load the path of files, from cached file)
30 5 $s->{key} = $hash{key};
31 5 $s->_init_from_key();
32 } elsif(defined $hash{path_files_ref}) {
33 # Initialize from the path of files
34 5 $s->_init_from_source_files( $hash{path_files_ref} );
35 }
36  
37 10 return $s;
38 }
39  
40 # Get (or Generate) the key
41 30 ▶sub get_key {
Subroutine get_key: called CC=5 SCAR=16.1
Pod documented
42 30 my $s = shift;
43  
44 30 ▶ if(!defined $s->{key}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s->{"key"}) 5 25 30
45  
46 5 my $key = "";
47 5 foreach my $file (@{$s->{files}}){
48 7 ▶ if($key ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "") 2 5 7
49 2 $key .= ",";
50 }
51 7 $key .= $file->get_raw_path();
52 }
53  
54 5 $key = Mojo::Util::sha1_sum($key);
55  
56 5 ▶ if($s->{is_minify} == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"is_minify"} == 0) 2 3 5
57 2 $key = "nomin-".$key;
58 }
59 5 $s->{key} = $key.'.'.$s->{extension};
60 }
61  
62 30 return $s->{key};
63 }
64  
65 # Get the extension
66 5 ▶sub get_extension {
Subroutine get_extension: called CC=1 SCAR=0.0
Pod documented
67 5 my $s = shift;
68 5 return $s->{extension};
69 }
70  
71 # Get the processed content
72 5 ▶sub get_content {
Subroutine get_content: called CC=1 SCAR=0.0
Pod documented
73 5 my $s = shift;
74 5 return $s->{content};
75 }
76  
77 # Update with check for update of files. (Return: Not updated = 0 / Updated = 1)
78 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
79 0 my $s = shift;
80 0 return $s->_cache();
81 }
82  
83 # Initialize from the key
84 5 ▶sub _init_from_key {
Subroutine _init_from_key: called CC=5 SCAR=16.6
85 5 my $s = shift;
86  
87 # Load parameters from the key
88 5 ▶ if(defined $s->{key}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"key"}) 5 0 5
89 5 ▶ if($s->{key} =~ /^(nomin\-|)(\w+).(\w+)$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"key"} =~ /^(nomin\-|)(\w+).(\w+)$/) 5 0 5
90 # Minify
91 5 ▶ if($1 eq 'nomin-'){
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'nomin-') { } 2 3 5
92 2 $s->{is_minify} = 0;
93 } else {
94 3 $s->{is_minify} = 1;
95 }
96 # Extension
97 5 $s->{extension} = $3;
98 }
99 }
100  
101 # Load the list from cached file
102 5 my @paths_files = $s->_load_from_cache();
103  
104 # Process and (re)cache
105 5 ▶ if(@paths_files){
Branch: 1 branch
BranchTrueFalseTotal
if (@paths_files) { } 5 0 5
106 5 $s->_init_from_source_files( \@paths_files );
107 } else {
108 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 5 ▶sub _load_from_cache {
Subroutine _load_from_cache: called CC=4 SCAR=14.5
114 5 my $s = shift;
115 5 my $path_cache_file = $s->{path_cache_dir}.$s->{key};
116  
117 5 ▶ if(-f $path_cache_file){ # If exist cached file
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path_cache_file) 5 0 5
118 5 my $content;
119 # Load the file and check the update_at
120 5 eval {
121 5 my $cache = Mojo::Asset::File->new( path => $path_cache_file );
122 5 $content = Encode::decode_utf8($cache->slurp());
123 5 my $updated_at = (stat( $path_cache_file ))[9];
124 };
125 0 ▶ if($@){ die("Can't read the cache file:". $path_cache_file); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
126  
127 # Parse the file
128 5 ▶ if($content =~ /^\/\*-{5}StaticCompressor-{5}\n((.+\n)+?)-{10}\*\/\n/){
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[^/\*-{5}StaticCompressor-{5}\n((.+\n)+?)-{10}\*/\n]) 5 0 5
129 5 my @paths = split("\n", $1);
130 5 $content =~ s/^\/\*-{5}StaticCompressor-{5}\n((.+\n)+?)-{10}\*\/\n//;
131 5 $s->{content} = $content;
132 5 return @paths;
133 }
134  
135 0 $s->{content} = $content;
136 }
137  
138 0 return;
139 }
140  
141 # Initialize from the source files
142 10 ▶sub _init_from_source_files {
Subroutine _init_from_source_files: called CC=2 SCAR=6.9
143 10 my ($s, $path_files_ref) = @_;
144  
145 10 $s->{files} = ();
146 10 $s->{num_of_files} = 0;
147  
148 10 foreach my $path (@{$path_files_ref}){
149 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 14 );
155 14 push(@{$s->{files}}, $file);
156 14 $s->{num_of_files} += 1;
157 }
158  
159 # Process and cache
160 10 $s->_cache();
161 }
162  
163 # Process and cache the container file
164 10 ▶sub _cache {
Subroutine _cache: called CC=8 SCAR=20.8
165 10 my $s = shift;
166 10 my $path_cache_file = $s->{path_cache_dir}.$s->get_key();
167  
168 10 ▶ if(-f $path_cache_file){ # If exist cached file
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path_cache_file) 5 5 10
169 # Check for update of container cache
170 5 my $updated_at;
171 5 eval {
172 5 my $cache = Mojo::Asset::File->new( path => $path_cache_file );
173 5 $updated_at = (stat( $path_cache_file ))[9];
174 };
175 5 ▶ unless(@$){
Branch: 1 branch
BranchTrueFalseTotal
unless (@$) 5 0 5
176 # Check for update of single files / caches
177 5 my $is_need_update = 0;
178 5 foreach my $file (@{$s->{files}}){
179 6 ▶ if($updated_at <= $file->get_updated_at()){
Branch: 1 branch
BranchTrueFalseTotal
if ($updated_at <= $file->get_updated_at) 3 3 6
180 3 $is_need_update = 1;
181 3 last;
182 }
183 }
184  
185 5 ▶ if($is_need_update == 0){ # Latest already
Branch: 1 branch
BranchTrueFalseTotal
if ($is_need_update == 0) 2 3 5
186 2 return 0;
187 }
188 }
189 }
190  
191 8 my $content = "";
192 8 my $paths_text = "";
193 # Process the files
194 8 foreach my $file (@{$s->{files}}){
195 # Process of the file (Minify), and Combine it
196 11 $content .= $file->get_content();
197  
198 11 ▶ if($paths_text ne ""){ $paths_text .= "\n"};
Branch: 1 branch
BranchTrueFalseTotal
if ($paths_text ne "") 3 8 11
199 11 $paths_text .= $file->get_raw_path();
200 }
201 8 $s->{content} = $content;
202
203 # Save to container cache
204 8 my $cache = Mojo::Asset::File->new();
205 8 my $save_header = <<EOF;
206 /*-----StaticCompressor-----
207 $paths_text
208 ----------*/
209 EOF
210 8 $cache->add_chunk( Encode::encode_utf8($save_header.$content) );
211 8 $cache->move_to( $path_cache_file );
212  
213 8 return 1;
214 }
215  
216 4 1;
217 __END__
« StaticCompressor/File.pm ↑ Summary