StaticCompressor.pm

filter: Statementstmt 89.6% 113 / 126 Branchbran 72.2% 26 / 36 Conditioncond 60.0% 18 / 30 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 87.5% 21 / 24 Podpod 100% 1 / 1 totaltotal 78.5% 183 / 233 CC 35 SCAR 18.4
CC
35
Cov
82%
_load_options
15.3
__ANON__
12.0
__ANON__
5.5
CRAP 6.3
↑ Summary StaticCompressor/File.pm »
1 package Mojolicious::Plugin::StaticCompressor;
2 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 our $VERSION = '1.0.0';
8  
9 4 ▶use Encode qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::Find qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Mojolicious::Plugin::StaticCompressor::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $static; # Instance of Mojo::Asset
17 4 our %containers; # Hash of Containers
18 4 our $config; # Hash-ref (Configuration items)
19  
20 3 ▶sub register {
Subroutine register: called CC=4 SCAR=14.1
Pod documented
21 3 my ($self, $app, $conf) = @_;
22
23 # Initilaize
24 3 %containers = ();
25 3 $static = $app->static;
26 3 $config = _load_options( $app, $conf );
27  
28 # Add "js" helper
29 4 ▶ $app->helper(js => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 4 my $self = shift;
31 4 my @file_paths = _generate_list( (@_) );;
32 4 return _generate_import('js', 1, \@file_paths);
33 3 });
34  
35 # Add "css" helper
36 2 ▶ $app->helper(css => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 my $self = shift;
38 2 my @file_paths = _generate_list( (@_) );;
39 2 return _generate_import('css', 1, \@file_paths);
40 3 });
41  
42 # Add "js_nominify" helper
43 2 ▶ $app->helper(js_nominify => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 2 my $self = shift;
45 2 my @file_paths = _generate_list( (@_) );;
46 2 return _generate_import('js', 0, \@file_paths);
47 3 });
48  
49 # Add "css_nominify" helper
50 0 ▶ $app->helper(css_nominify => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 my $self = shift;
52 0 my @file_paths = _generate_list( (@_) );;
53 0 return _generate_import('css', 0, \@file_paths);
54 3 });
55  
56 3 ▶ unless($config->{is_disable}){ # Enable
Branch: 1 branch
BranchTrueFalseTotal
unless ($Mojolicious::Plugin::StaticCompressor::config->{"is_disable"}) 2 1 3
57  
58 # Check the cache directory
59 2 ▶ if(!-d $config->{path_cache_dir}){
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $Mojolicious::Plugin::StaticCompressor::config->{"path_cache_dir"}) 1 1 2
60 1 mkdir $config->{path_cache_dir};
61 }
62
63 # Add hook
64 $app->hook(
65 10 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=5 SCAR=17.0
66 10 my $self = shift;
67 10 ▶ if($self->req->url->path->contains('/'.$config->{url_path_prefix})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->req->url->path->contains('/' . $Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}) and $self->req->url->path =~ m[/$Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}/(.+)$]u) 5 5 10
Condition66%2/3
$self->req->url->path->contains('/' . $Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}) and $self->req->url->path =~ m[/$Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}/(.+)$]u
!ll&&!rl&&r
505
MC/DC50%1/2
$self->req->url->path->contains('/' . $Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}) and $self->req->url->path =~ m[/$Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}/(.+)$]u
$self->req->url->path->contains('/' . $Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}) $self->req->url->path =~ m[/$Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}/(.+)$]u
Truth table66%2/3
A and B
A= $self->req->url->path->contains('/' . $Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"})B= $self->req->url->path =~ m[/$Mojolicious::Plugin::StaticCompressor::config->{"url_path_prefix"}/(.+)$]u
ABresult
0X0
100
111
68 && $self->req->url->path =~ /\/$config->{url_path_prefix}\/(.+)$/){
69 5 my $container_key = $1;
70
71 5 eval {
72 5 my $cont = Mojolicious::Plugin::StaticCompressor::Container->new(
73 key => $container_key,
74 config => $config,
75 );
76  
77 5 ▶ if(!defined $containers{$cont->get_key()}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Mojolicious::Plugin::StaticCompressor::containers{$cont->get_key}) 0 5 5
78 0 $containers{$cont->get_key()} = $cont;
79 }
80
81 5 $self->render( text => $cont->get_content(), format => $cont->get_extension() );
82 };
83  
84 5 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
85 0 $self->render( text => $@, status => 400 );
86 }
87 }
88 }
89 2 );
90  
91 # Automatic cleanup
92 2 _cleanup_old_files();
93  
94 # Start background loop
95 2 ▶ if($config->{is_background}){
Branch: 1 branch
BranchTrueFalseTotal
if ($Mojolicious::Plugin::StaticCompressor::config->{"is_background"}) 0 2 2
96 0 _start_background_loop();
97 }
98 }
99 }
100  
101 # Load the options
102 3 ▶sub _load_options {
Subroutine _load_options: called CC=13 SCAR=27.3
103 3 my ($app, $option) = @_;
104 3 my $config = {};
105  
106 # Disable
107 3 ▶ my $disable = $option->{disable} || 0;
Condition50%1/2
$option->{'disable'} || 0
l!l
03
MC/DC0%0/1
$option->{'disable'} || 0
$option->{'disable'}
Truth table50%1/2
A
A= $option->{'disable'}
Aresult
11
00
108 3 ▶ my $disable_on_devmode = $option->{disable_on_devmode} || 0;
Condition100%2/2
$option->{'disable_on_devmode'} || 0
l!l
12
MC/DC100%1/1
$option->{'disable_on_devmode'} || 0
$option->{'disable_on_devmode'}
Truth table100%2/2
A
A= $option->{'disable_on_devmode'}
Aresult
11
00
109 3 ▶ $config->{is_disable} = ($disable eq 1 || ($disable_on_devmode eq 1 && $app->mode eq 'development')) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$disable eq 1 || $disable_on_devmode eq 1 && $app->mode eq 'development' ? : 1 2 3
Condition66%4/6
$disable eq 1 or $disable_on_devmode eq 1 and $app->mode eq "development"
l!l&&r!l&&!r
012
$disable_on_devmode eq 1 and $app->mode eq "development"
!ll&&!rl&&r
201
MC/DC33%1/3
$disable eq 1 or $disable_on_devmode eq 1 and $app->mode eq "development"
$disable eq 1 $disable_on_devmode eq 1 $app->mode eq "development"
Truth table50%2/4
A or B and C
A= $disable eq 1B= $disable_on_devmode eq 1C= $app->mode eq "development"
ABCresult
1XX1
0111
00X0
0100
110  
111 # Debug
112 3 ▶ $config->{is_debug} = $option->{is_debug} || 0;
Condition50%1/2
$option->{'is_debug'} || 0
l!l
03
MC/DC0%0/1
$option->{'is_debug'} || 0
$option->{'is_debug'}
Truth table50%1/2
A
A= $option->{'is_debug'}
Aresult
11
00
113  
114 # Prefix
115 3 ▶ my $prefix = $option->{url_path_prefix} || 'auto_compressed';
Condition50%1/2
$option->{'url_path_prefix'} || 'auto_compressed'
l!l
03
MC/DC0%0/1
$option->{'url_path_prefix'} || 'auto_compressed'
$option->{'url_path_prefix'}
Truth table50%1/2
A
A= $option->{'url_path_prefix'}
Aresult
11
00
116 3 $config->{url_path_prefix} = $prefix;
117  
118 # Path of cache directory
119 3 ▶ $config->{path_cache_dir} = $option->{file_cache_path} || $FindBin::Bin.'/'.$prefix.'/';
Condition50%1/2
$option->{'file_cache_path'} || $FindBin::Bin . '/' . $prefix . '/'
l!l
03
MC/DC0%0/1
$option->{'file_cache_path'} || $FindBin::Bin . '/' . $prefix . '/'
$option->{'file_cache_path'}
Truth table50%1/2
A
A= $option->{'file_cache_path'}
Aresult
11
00
120 3 $config->{path_single_cache_dir} = $config->{path_cache_dir}.'single/';
121  
122 # Background processing
123 3 ▶ $config->{is_background} = $option->{background} || 0;
Condition50%1/2
$option->{'background'} || 0
l!l
03
MC/DC0%0/1
$option->{'background'} || 0
$option->{'background'}
Truth table50%1/2
A
A= $option->{'background'}
Aresult
11
00
124 3 ▶ $config->{background_interval_sec} = $option->{background_interval_sec} || 5;
Condition50%1/2
$option->{'background_interval_sec'} || 5
l!l
03
MC/DC0%0/1
$option->{'background_interval_sec'} || 5
$option->{'background_interval_sec'}
Truth table50%1/2
A
A= $option->{'background_interval_sec'}
Aresult
11
00
125  
126 # Automatic cleanup
127 3 ▶ $config->{is_auto_cleanup} = $option->{auto_cleanup} || 1;
Condition50%1/2
$option->{'auto_cleanup'} || 1
l!l
03
MC/DC0%0/1
$option->{'auto_cleanup'} || 1
$option->{'auto_cleanup'}
Truth table50%1/2
A
A= $option->{'auto_cleanup'}
Aresult
11
00
128  
129 # Expires seconds for automatic cleanup
130 3 ▶ $config->{auto_cleanup_expires_sec} = $option->{auto_cleanup_expires_sec} || 60 * 60 * 24 * 7; # 7days
Condition50%1/2
$option->{'auto_cleanup_expires_sec'} || 604800
l!l
03
MC/DC0%0/1
$option->{'auto_cleanup_expires_sec'} || 604800
$option->{'auto_cleanup_expires_sec'}
Truth table50%1/2
A
A= $option->{'auto_cleanup_expires_sec'}
Aresult
11
00
131  
132 # Others
133 3 $config->{mojo_static} = $static;
134  
135 3 return $config;
136 }
137  
138 8 ▶sub _generate_import {
Subroutine _generate_import: called CC=3 SCAR=11.1
139 8 my ($extension, $is_minify, $path_files_ref) = @_;
140  
141 8 ▶ if($config->{is_disable}){
Branch: 1 branch
BranchTrueFalseTotal
if ($Mojolicious::Plugin::StaticCompressor::config->{"is_disable"}) 3 5 8
142 3 return Mojo::ByteStream->new( _generate_import_raw_tag( $extension, $path_files_ref ) );
143 }
144  
145 5 my $cont = Mojolicious::Plugin::StaticCompressor::Container->new(
146 extension => $extension,
147 is_minify => $is_minify,
148 path_files_ref => $path_files_ref,
149 config => $config,
150 );
151  
152 5 ▶ if(defined $containers{$cont->get_key()}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Mojolicious::Plugin::StaticCompressor::containers{$cont->get_key}) { } 0 5 5
153 0 $containers{$cont->get_key()}->update();
154 } else {
155 5 $containers{$cont->get_key()} = $cont;
156 }
157  
158 5 return Mojo::ByteStream->new( _generate_import_processed_tag( $extension, "/".$config->{url_path_prefix}."/".$cont->get_key() ) );
159 }
160  
161 # Generate of import HTML-tag for processed
162 5 ▶sub _generate_import_processed_tag {
Subroutine _generate_import_processed_tag: called CC=3 SCAR=11.0
163 5 my ($extension, $url) = @_;
164 5 ▶ if ($extension eq 'js'){
Branch: 2 branches
BranchTrueFalseTotal
if ($extension eq 'js') { } 4 1 5
elsif ($extension eq 'css') { } 1 0 1
165 4 return "<script src=\"$url\"></script>\n";
166 } elsif ($extension eq 'css'){
167 1 return "<link rel=\"stylesheet\" href=\"$url\">\n";
168 }
169 }
170  
171 # Generate of import HTML-tag for raw
172 3 ▶sub _generate_import_raw_tag {
Subroutine _generate_import_raw_tag: called CC=5 SCAR=16.1
173 3 my ($extension, $urls_ref) = @_;
174 3 my $tag = "";
175 3 ▶ if ($extension eq 'js'){
Branch: 2 branches
BranchTrueFalseTotal
if ($extension eq 'js') { } 2 1 3
elsif ($extension eq 'css') { } 1 0 1
176 2 foreach(@{$urls_ref}){
177 3 $tag .= "<script src=\"$_\"></script>\n";
178 }
179 } elsif ($extension eq 'css'){
180 1 foreach(@{$urls_ref}){
181 1 $tag .= "<link rel=\"stylesheet\" href=\"$_\">\n";
182 }
183 }
184 3 return $tag;
185 }
186  
187 # Start background process loop
188 0 ▶sub _start_background_loop {
Subroutine _start_background_loop: not called CC=1 SCAR=6.9
189 0 ▶ my $id = Mojo::IOLoop->recurring( $config->{background_interval_sec} => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
190 0 foreach my $key (keys %containers){
191 0 ▶ if( $containers{$key}->update() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($Mojolicious::Plugin::StaticCompressor::containers{$key}->update) 0 0 0
192 0 warn "[StaticCompressor] Cache updated in background - $key";
193 }
194 }
195 0 });
196 }
197  
198 # Cleanup
199 2 ▶sub _cleanup_old_files {
Subroutine _cleanup_old_files: called CC=1 SCAR=0.1
200 9 ▶ File::Find::find(sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
201 9 my $path = $File::Find::name;
202 9 my $now = time();
203 9 ▶ if( -f $path && $path =~ /^(.*)\.(js|css)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path and $path =~ /^(.*)\.(js|css)$/u) 6 3 9
Condition66%2/3
-f $path and $path =~ /^(.*)\.(js|css)$/u
!ll&&!rl&&r
306
MC/DC50%1/2
-f $path and $path =~ /^(.*)\.(js|css)$/u
-f $path $path =~ /^(.*)\.(js|css)$/u
Truth table66%2/3
A and B
A= -f $pathB= $path =~ /^(.*)\.(js|css)$/u
ABresult
0X0
100
111
204 6 my $updated_at = (stat($path))[9];
205 6 ▶ if($config->{auto_cleanup_expires_sec} < ($now - $updated_at)){
Branch: 1 branch
BranchTrueFalseTotal
if ($Mojolicious::Plugin::StaticCompressor::config->{"auto_cleanup_expires_sec"} < $now - $updated_at) 0 6 6
206 0 warn "DELETE: $path";
207 #unlink($config->{path_cache_dir}) || die("Can't delete old file: $path");
208 }
209 }
210 2 }, $config->{path_cache_dir});
211 }
212  
213 #Generate one dimensional array
214 8 ▶sub _generate_list{
Subroutine _generate_list: called CC=3 SCAR=11.1
215 8 my @temp = @_;
216 8 my @file_paths;
217 8 ▶ while (@temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@temp) 11 8 19
218 11 my $next = shift @temp;
219 11 ▶ if (ref($next) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next eq 'ARRAY') { } 0 11 11
220 0 unshift @file_paths, @$next;
221 }
222 else {
223 11 push @file_paths, $next;
224 }
225 }
226 8 return @file_paths;
227 }
228  
229 4 1;
230 __END__
↑ Summary StaticCompressor/File.pm »