AssetPack/Store.pm

filter: Statementstmt 92.8% 209 / 225 Branchbran 64.4% 58 / 90 Conditioncond 51.4% 18 / 35 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 100% 30 / 30 Podpod 100% 6 / 6 totaltotal 79.8% 325 / 407 CC 72 SCAR 24.1
CC
72
Cov
81%
asset
33.8
_download
18.3
__ANON__
8.0
CRAP 11.2
« AssetPack/Pipe/CoffeeScript.pm ↑ Summary AssetPack/Pipe/Riotjs.pm »
1 package Mojolicious::Plugin::AssetPack::Store;
2 15 ▶use Mojo::Base 'Mojolicious::Static';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 15 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Mojo::Loader 'data_section';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Mojolicious::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Mojolicious::Plugin::AssetPack::Asset;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Mojolicious::Plugin::AssetPack::Util qw(diag checksum has_ro DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶use constant CACHE_DIR => 'cache';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # MOJO_ASSETPACK_DB_FILE is used in tests
16 15 ▶use constant DB_FILE => $ENV{MOJO_ASSETPACK_DB_FILE} || 'assetpack.db';
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MOJO_ASSETPACK_DB_FILE'} || 'assetpack.db'
l!l
141
MC/DC100%1/1
$ENV{'MOJO_ASSETPACK_DB_FILE'} || 'assetpack.db'
$ENV{'MOJO_ASSETPACK_DB_FILE'}
Truth table100%2/2
A
A= $ENV{'MOJO_ASSETPACK_DB_FILE'}
Aresult
11
00
17 15 our %DB_KEYS = map { $_ => 1 } qw(checksum format minified rel);
18 15 our %FALLBACK_TEMPLATES = %{data_section(__PACKAGE__)};
19  
20 15 for my $name (keys %FALLBACK_TEMPLATES) {
21 30 my $text = delete $FALLBACK_TEMPLATES{$name};
22 30 $name =~ m!(\w+)\.ep$!;
23 30 $FALLBACK_TEMPLATES{$1} = Mojo::Template->new->parse($text)->prepend('my ($c, $assets) = @_;');
24 }
25  
26 15 has asset_class => 'Mojolicious::Plugin::AssetPack::Asset';
27 15 ▶has default_headers => sub { +{"Cache-Control" => "max-age=31536000"} };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 ▶has fallback_headers => sub { +{"Cache-Control" => "max-age=60"} };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 1 ▶has fallback_templates => sub { +{%FALLBACK_TEMPLATES} };
Subroutine __ANON__: called CC=1 SCAR=0.0
30 15 has retry_delay => 3;
31 15 has retries => 0;
32  
33 15 ▶has _types => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
34 15 my $t = Mojolicious::Types->new;
35 15 $t->type(eot => 'application/vnd.ms-fontobject');
36 15 $t->type(otf => 'application/font-otf');
37 15 $t->type(ttf => 'application/font-ttf');
38 15 $t->type(woff2 => 'application/font-woff2');
39 15 delete $t->mapping->{$_} for qw(atom bin htm html txt xml zip);
40 15 $t;
41 15 };
42  
43 15 has_ro 'ua';
44  
45 18 ▶has_ro _db => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
46 18 my $self = shift;
47 18 my ($db, $key, $url) = ({});
48 18 for my $path (reverse map { path($_, DB_FILE) } @{$self->paths}) {
49 18 ▶ open my $DB, '<', $path or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $DB, "<", $path or next 4 14 18
50 4 ▶ while (my $line = <$DB>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $DB)) 16 4 20
51 16 ▶ ($key, $url) = ($1, $2) if $line =~ /^\[([\w-]+):(.+)\]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\[([\w-]+):(.+)\]$/u 4 12 16
52 16 ▶ $db->{$url}{$key}{$1} = $2 if $key and $line =~ /^(\w+)=(.*)/ and $DB_KEYS{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $key and $line =~ /^(\w+)=(.*)/u and $Mojolicious::Plugin::AssetPack::Store::DB_KEYS{$1} 12 4 16
Condition66%4/6
$key and $line =~ /^(\w+)=(.*)/u and $Mojolicious::Plugin::AssetPack::Store::DB_KEYS{$1}
!ll&&!rl&&r
4012
$key and $line =~ /^(\w+)=(.*)/u
!ll&&!rl&&r
0412
MC/DC33%1/3
$key and $line =~ /^(\w+)=(.*)/u and $Mojolicious::Plugin::AssetPack::Store::DB_KEYS{$1}
$key $line =~ /^(\w+)=(.*)/u $Mojolicious::Plugin::AssetPack::Store::DB_KEYS{$1}
Truth table50%2/4
A and B and C
A= $keyB= $line =~ /^(\w+)=(.*)/uC= $Mojolicious::Plugin::AssetPack::Store::DB_KEYS{$1}
ABCresult
0XX0
10X0
1100
1111
53 }
54 }
55 18 return $db;
56 15 };
57  
58 39 ▶sub asset {
Subroutine asset: called CC=17 SCAR=35.2
Pod documented
59 39 my ($self, $urls, $paths) = @_;
60 39 my $asset;
61  
62 39 ▶ for my $url (ref $urls eq 'ARRAY' ? @$urls : ($urls)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $urls eq 'ARRAY' ? : 0 39 39
63 39 ▶ return $self->_asset_from_helper(Mojo::URL->new($url)) if $url =~ m!^helper://!;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[^helper://]u 3 36 39
64 36 ▶ for my $path (@{$paths || $self->paths}) {
Condition33%1/3
$paths || $self->paths
l!l&&r!l&&!r
0360
MC/DC0%0/2
$paths || $self->paths
$paths $self->paths
Truth table33%1/3
A || B
A= $pathsB= $self->paths
ABresult
1X1
011
000
65 36 ▶ next unless $path =~ m!^https?://!;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^https?://]u 36 0 36
66 0 my $abs = Mojo::URL->new($path);
67 0 $abs->path->merge($url);
68 0 ▶ return $asset if $asset = $self->_already_downloaded($abs);
Branch: 1 branch
BranchTrueFalseTotal
if $asset = $self->_already_downloaded($abs) 0 0 0
69 }
70 }
71  
72 36 ▶ for my $url (ref $urls eq 'ARRAY' ? @$urls : ($urls)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $urls eq 'ARRAY' ? : 0 36 36
73 36 ▶ return $asset if $url =~ m!^https?://! and $asset = $self->_download(Mojo::URL->new($url));
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[^https?://]u and $asset = $self->_download("Mojo::URL"->new($url)) 1 35 36
Condition66%2/3
$url =~ m[^https?://]u and $asset = $self->_download("Mojo::URL"->new($url))
!ll&&!rl&&r
3501
MC/DC50%1/2
$url =~ m[^https?://]u and $asset = $self->_download("Mojo::URL"->new($url))
$url =~ m[^https?://]u $asset = $self->_download("Mojo::URL"->new($url))
Truth table66%2/3
A and B
A= $url =~ m[^https?://]uB= $asset = $self->_download("Mojo::URL"->new($url))
ABresult
0X0
100
111
74  
75 35 ▶ for my $path (@{$paths || $self->paths}) {
Condition33%1/3
$paths || $self->paths
l!l&&r!l&&!r
0350
MC/DC0%0/2
$paths || $self->paths
$paths $self->paths
Truth table33%1/3
A || B
A= $pathsB= $self->paths
ABresult
1X1
011
000
76 35 ▶ if ($path =~ m!^https?://!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^https?://]u) { } 0 35 35
77 0 my $abs = Mojo::URL->new($path);
78 0 $abs->path->merge($url);
79 0 ▶ return $asset if $asset = $self->_download($abs);
Branch: 1 branch
BranchTrueFalseTotal
if $asset = $self->_download($abs) 0 0 0
80 }
81 else {
82 35 local $self->{paths} = [$path];
83 35 ▶ next unless $asset = $self->file($url);
Branch: 1 branch
BranchTrueFalseTotal
unless $asset = $self->file($url) 2 33 35
84 33 return $self->asset_class->new(url => $url, content => $asset);
85 }
86 }
87 }
88  
89 2 return undef;
90 }
91  
92 6 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod documented
93 6 my ($self, $attrs) = @_;
94 6 ▶ my $db_attr = $self->_db_get($attrs) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $db_attr = $self->_db_get($attrs) or return undef 4 2 6
95 4 my @rel = $self->_cache_path($attrs);
96 4 my $asset = $self->asset(join '/', @rel);
97  
98 4 ▶ return undef unless $asset;
Branch: 1 branch
BranchTrueFalseTotal
unless $asset 0 4 4
99 4 ▶ return undef unless $db_attr->{checksum} eq $attrs->{checksum};
Branch: 1 branch
BranchTrueFalseTotal
unless $db_attr->{"checksum"} eq $attrs->{"checksum"} 1 3 4
100 3 diag 'Load "%s" = 1', $asset->path || $asset->url if DEBUG;
101 3 return $asset;
102 }
103  
104 19 ▶sub persist {
Subroutine persist: called CC=7 SCAR=19.5
Pod documented
105 19 my $self = shift;
106 19 my $db = $self->_db;
107 19 my $path = path($self->paths->[0], DB_FILE);
108 19 my @db_keys = sort keys %DB_KEYS;
109 19 my $DB;
110  
111 19 ▶ unless (open $DB, '>', $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $DB, ">", $path) 2 17 19
112 2 diag 'Save "%s" = 0 (%s)', $path, $! if DEBUG;
113 2 return $self;
114 }
115  
116 17 diag 'Save "%s" = 1', $path if DEBUG;
117 17 for my $url (sort keys %$db) {
118 6 for my $key (sort keys %{$db->{$url}}) {
119 6 ▶ Carp::confess("Invalid key '$key'. Need to be [a-z-].") unless $key =~ /^[\w-]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^[\w-]+$/u 0 6 6
120 6 printf $DB "[%s:%s]\n", $key, $url;
121 6 for my $attr (@db_keys) {
122 24 ▶ next unless defined $db->{$url}{$key}{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db->{$url}{$key}{$attr} 6 18 24
123 18 printf $DB "%s=%s\n", $attr, $db->{$url}{$key}{$attr};
124 }
125 }
126 }
127  
128 17 return $self;
129 }
130  
131 3 ▶sub save {
Subroutine save: called CC=4 SCAR=14.5
Pod documented
132 3 my ($self, $ref, $attrs) = @_;
133 3 my $path = path($self->paths->[0], $self->_cache_path($attrs));
134 3 my $dir = $path->dirname;
135  
136 # Do not care if this fail. Can fallback to temp files.
137 3 ▶ mkdir $dir if !-d $dir and -w $dir->dirname;
Branch: 1 branch
BranchTrueFalseTotal
if not -d $dir and -w $dir->dirname 0 3 3
Condition33%1/3
not -d $dir and -w $dir->dirname
!ll&&!rl&&r
300
MC/DC0%0/2
not -d $dir and -w $dir->dirname
not -d $dir -w $dir->dirname
Truth table33%1/3
not(A) and B
A= not -d $dirB= -w $dir->dirname
ABresult
0X0
100
111
138 3 diag 'Save "%s" = %s', $path, -d $dir ? 1 : 0 if DEBUG;
139  
140 3 ▶ return $self->asset_class->new(%$attrs, content => $$ref) unless -w $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $dir 0 3 3
141  
142 3 $path->spew($$ref);
143 3 $self->_db_set(%$attrs);
144 3 return $self->asset_class->new(%$attrs, path => $path);
145 }
146  
147 25 ▶sub serve_asset {
Subroutine serve_asset: called CC=5 SCAR=16.5
Pod documented
148 25 my ($self, $c, $asset) = @_;
149 25 my $dh = $self->default_headers;
150 25 my $h = $c->res->headers;
151  
152 25 $h->header($_ => $dh->{$_}) for keys %$dh;
153 25 ▶ $h->content_type($self->_types->type($asset->format) || 'application/octet-stream');
Condition100%2/2
$self->_types->type($asset->format) || 'application/octet-stream'
l!l
241
MC/DC100%1/1
$self->_types->type($asset->format) || 'application/octet-stream'
$self->_types->type($asset->format)
Truth table100%2/2
A
A= $self->_types->type($asset->format)
Aresult
11
00
154  
155 25 ▶ if (my $renderer = $asset->renderer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $renderer = $asset->renderer) { } 0 25 25
156 0 $renderer->($asset, $c);
157 }
158 else {
159 25 ▶ $self->SUPER::serve_asset($c, $asset->can('asset') ? $asset->asset : $asset);
Branch: 1 branch
BranchTrueFalseTotal
$asset->can('asset') ? : 25 0 25
160 }
161  
162 25 return $self;
163 }
164  
165 2 ▶sub serve_fallback_for_assets {
Subroutine serve_fallback_for_assets: called CC=6 SCAR=18.3
Pod documented
166 2 my ($self, $c, $topic, $assets) = @_;
167 2 my $fh = $self->fallback_headers;
168 2 ▶ my $format = $topic =~ m!\.(\w+)$! ? $1 : 'css';
Branch: 1 branch
BranchTrueFalseTotal
$topic =~ /\.(\w+)$/u ? : 2 0 2
169 2 my $h = $c->res->headers;
170  
171 2 $h->header($_ => $fh->{$_}) for keys %$fh;
172 2 ▶ $h->content_type($self->_types->type($format) || 'application/octet-stream');
Condition50%1/2
$self->_types->type($format) || 'application/octet-stream'
l!l
20
MC/DC0%0/1
$self->_types->type($format) || 'application/octet-stream'
$self->_types->type($format)
Truth table50%1/2
A
A= $self->_types->type($format)
Aresult
11
00
173  
174 2 ▶ if (my $template = $self->fallback_templates->{$format}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $template = $self->fallback_templates->{$format}) { } 1 1 2
elsif (@$assets == 1) { } 1 0 1
175 1 $c->render(data => $template->process($c, $assets));
176 }
177 elsif (@$assets == 1) {
178 1 my $url = $assets->[0]->url_for($c);
179 1 $url->path->[-1] = $topic;
180 1 $c->redirect_to($url);
181 }
182 else {
183 0 $c->render(text => "// Invalid checksum for topic '$topic'\n", status => 404);
184 }
185  
186 2 return $self;
187 }
188  
189 1 ▶sub _already_downloaded {
Subroutine _already_downloaded: called CC=6 SCAR=20.2
190 1 my ($self, $url) = @_;
191 1 my $asset = $self->asset_class->new(url => "$url");
192 1 my @dirname = $self->_url2path($url, '');
193 1 my $basename = pop @dirname;
194  
195 1 for my $path (map { path $_, @dirname } @{$self->paths}) {
196  
197 # URL with extension
198 1 my $file = $path->child($basename);
199 1 ▶ return $asset->format($1)->path($file) if -e $file and $file =~ m!\.(\w+)$!;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file and $file =~ /\.(\w+)$/u 0 1 1
Condition33%1/3
-e $file and $file =~ /\.(\w+)$/u
!ll&&!rl&&r
100
MC/DC0%0/2
-e $file and $file =~ /\.(\w+)$/u
-e $file $file =~ /\.(\w+)$/u
Truth table33%1/3
A and B
A= -e $fileB= $file =~ /\.(\w+)$/u
ABresult
0X0
100
111
200  
201 # URL without extension - https://fonts.googleapis.com/css?family=Roboto
202 1 for my $file ($path->list->each) {
203 0 ▶ next unless $file->basename =~ /^$basename(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->basename =~ /^$basename(\w+)$/u 0 0 0
204 0 return $asset->format($1)->path($file);
205 }
206 }
207  
208 1 return undef;
209 }
210  
211 3 ▶sub _asset_from_helper {
Subroutine _asset_from_helper: called CC=4 SCAR=13.9
212 3 my ($self, $url) = @_;
213 3 my $app = $self->ua->server->app;
214 3 my $args = $url->query->to_hash;
215 3 my $helper = $app->renderer->helpers->{$url->host};
216 3 my $output = $app->build_controller->$helper($url->path->[0], $args);
217  
218 0 ▶ die "[AssetPack] Unknown helper @{[$url->host]}" unless $helper;
Branch: 1 branch
BranchTrueFalseTotal
unless $helper 0 3 3
219 3 ▶ my $asset = $self->asset_class->new(url => $url, ref $output ? %$output : (content => $output));
Branch: 1 branch
BranchTrueFalseTotal
ref $output ? : 1 2 3
220  
221 3 ▶ $asset->format($args->{format}) if $args->{format};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"format"} 2 1 3
222 3 $asset;
223 }
224  
225 7 ▶sub _cache_path {
Subroutine _cache_path: called CC=2 SCAR=7.2
226 7 my ($self, $attrs) = @_;
227 return (
228 CACHE_DIR, sprintf '%s-%s.%s%s',
229 $attrs->{name},
230 checksum($attrs->{url}),
231 $attrs->{minified} ? 'min.' : '',
232 $attrs->{format}
233 7 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'minified'} ? : 7 0 7
234 }
235  
236 6 ▶sub _db_get {
Subroutine _db_get: called CC=3 SCAR=11.0
237 6 my ($self, $attrs) = @_;
238 6 my $db = $self->_db;
239 6 ▶ return undef unless my $data = $db->{$attrs->{url}};
Branch: 1 branch
BranchTrueFalseTotal
unless my $data = $$db{$attrs->{"url"}} 2 4 6
240 4 ▶ return undef unless $data = $data->{$attrs->{key}};
Branch: 1 branch
BranchTrueFalseTotal
unless $data = $$data{$attrs->{"key"}} 0 4 4
241 4 return {%$attrs, %$data};
242 }
243  
244 3 ▶sub _db_set {
Subroutine _db_set: called CC=2 SCAR=7.0
245 3 ▶ return if $ENV{MOJO_ASSETPACK_LAZY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJO_ASSETPACK_LAZY"} 0 3 3
246 3 my ($self, %attrs) = @_;
247 3 my ($key, $url) = @attrs{qw(key url)};
248 3 $self->_db->{$url}{$key} = {%attrs};
249 }
250  
251 1 ▶sub _download {
Subroutine _download: called CC=13 SCAR=29.1
252 1 my ($self, $url) = @_;
253 1 my %attrs = (url => $url->clone);
254 1 my ($asset, $path);
255  
256 1 ▶ if ($attrs{url}->host eq 'local') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs{"url"}->host eq "local") 0 1 1
257 0 my $base = $self->ua->server->url;
258 0 $url = $url->clone->scheme($base->scheme)->host_port($base->host_port);
259 }
260  
261 1 ▶ return $asset if $attrs{url}->host ne 'local' and $asset = $self->_already_downloaded($url);
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"url"}->host ne "local" and $asset = $self->_already_downloaded($url) 0 1 1
Condition33%1/3
$attrs{"url"}->host ne "local" and $asset = $self->_already_downloaded($url)
!ll&&!rl&&r
010
MC/DC0%0/2
$attrs{"url"}->host ne "local" and $asset = $self->_already_downloaded($url)
$attrs{"url"}->host ne "local" $asset = $self->_already_downloaded($url)
Truth table33%1/3
A and B
A= $attrs{"url"}->host ne "local"B= $asset = $self->_already_downloaded($url)
ABresult
0X0
100
111
262  
263 1 my $tx;
264 1 my $retries = $self->retries;
265 1 while (1) {
266 4 $tx = $self->ua->get($url);
267 4 ▶ last unless my $err = $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
unless my $err = $tx->error 1 3 4
268  
269 3 ▶ if ($retries-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries-- > 0) 3 0 3
270 3 sleep $self->retry_delay;
271 3 next;
272 }
273  
274 0 $self->_log->warn("[AssetPack] Unable to download $url: $err->{message}");
275 0 return undef;
276 }
277  
278 1 my $h = $tx->res->headers;
279 1 ▶ my $ct = $h->content_type || '';
Condition50%1/2
$h->content_type || ''
l!l
10
MC/DC0%0/1
$h->content_type || ''
$h->content_type
Truth table50%1/2
A
A= $h->content_type
Aresult
11
00
280 1 ▶ if ($ct ne 'text/plain') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct ne "text/plain") 1 0 1
281 1 $ct =~ s!;.*$!!;
282 1 $attrs{format} = $self->_types->detect($ct)->[0];
283 }
284  
285 1 ▶ $attrs{format} ||= $tx->req->url->path->[-1] =~ /\.(\w+)$/ ? $1 : 'bin';
Branch: 1 branch
BranchTrueFalseTotal
$tx->req->url->path->[-1] =~ /\.(\w+)$/u ? : 1 0 1
Condition33%1/3
$attrs{"format"} ||= $tx->req->url->path->[-1] =~ /\.(\w+)$/u ? $1 : "bin"
l!l&&r!l&&!r
010
MC/DC0%0/2
$attrs{"format"} ||= $tx->req->url->path->[-1] =~ /\.(\w+)$/u ? $1 : "bin"
$attrs{"format"} $tx->req->url->path->[-1] =~ /\.(\w+)$/u ? $1 : "bin"
Truth table33%1/3
A ||= B
A= $attrs{"format"}B= $tx->req->url->path->[-1] =~ /\.(\w+)$/u ? $1 : "bin"
ABresult
1X1
011
000
286  
287 1 ▶ if ($attrs{url}->host ne 'local') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs{"url"}->host ne "local") 1 0 1
288 1 $path = path($self->paths->[0], $self->_url2path($attrs{url}, $attrs{format}));
289 1 $self->_log->info(qq(Caching "$url" to "$path".));
290 1 ▶ $path->dirname->make_path unless -d $path->dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path->dirname 1 0 1
291 1 $path->spew($tx->res->body);
292 }
293  
294 1 $attrs{url} = "$attrs{url}";
295 1 ▶ return $self->asset_class->new(%attrs, path => $path) if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 1 0 1
296 0 return $self->asset_class->new(%attrs)->content($tx->res->body);
297 }
298  
299 1 ▶sub _log { shift->ua->server->app->log }
Subroutine _log: called CC=1 SCAR=0.0
300  
301 2 ▶sub _url2path {
Subroutine _url2path: called CC=2 SCAR=6.9
302 2 my ($self, $url, $format) = @_;
303 2 my $query = $url->query->to_string;
304 2 my @path;
305  
306 2 push @path, $url->host;
307 2 push @path, @{$url->path};
308  
309 2 $query =~ s!\W!_!g;
310 2 ▶ $path[-1] .= "_$query.$format" if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 2 2
311  
312 2 return CACHE_DIR, @path;
313 }
314  
315 15 1;
316  
317 =encoding utf8
318  
319 =head1 NAME
320  
321 Mojolicious::Plugin::AssetPack::Store - Storage for assets
322  
323 =head1 SYNOPSIS
324  
325 use Mojolicious::Lite;
326  
327 # Load plugin and pipes in the right order
328 plugin AssetPack => {pipes => \@pipes};
329  
330 # Change where assets can be found
331 app->asset->store->paths([
332 app->home->rel_file("some/directory"),
333 "/some/other/directory",
334 ]);
335  
336 # Change where assets are stored
337 app->asset->store->paths->[0] = app->home->rel_file("some/directory");
338  
339 # Define asset
340 app->asset->process($moniker => @assets);
341  
342 # Retrieve a Mojolicious::Plugin::AssetPack::Asset object
343 my $asset = app->asset->store->asset("some/file.js");
344  
345 =head1 DESCRIPTION
346  
347 L<Mojolicious::Plugin::AssetPack::Store> is an object to manage cached
348 assets on disk.
349  
350 The idea is that a L<Mojolicious::Plugin::AssetPack::Pipe> object can store
351 an asset after it is processed. This will speed up development, since only
352 changed assets will be processed and it will also allow processing tools to
353 be optional in production environment.
354  
355 This module will document meta data about each asset which is saved to disk, so
356 it can be looked up later as a unique item using L</load>.
357  
358 =head1 ATTRIBUTES
359  
360 L<Mojolicious::Plugin::AssetPack::Store> inherits all attributes from
361 L<Mojolicious::Static> implements the following new ones.
362  
363 =head2 asset_class
364  
365 $str = $self->asset_class;
366 $self = $self->asset_class("Mojolicious::Plugin::AssetPack::Asset");
367  
368 Holds the classname of which new assets will be constructed from.
369  
370 =head2 default_headers
371  
372 $hash_ref = $self->default_headers;
373 $self = $self->default_headers({"Cache-Control" => "max-age=31536000"});
374  
375 Used to set headers used by L</serve_asset>.
376  
377 =head2 fallback_headers
378  
379 $hash_ref = $self->fallback_headers;
380 $self = $self->fallback_headers({"Cache-Control" => "max-age=300"});
381  
382 Used to set headers used by L</serve_fallback_for_assets>.
383  
384 This is currently an EXPERIMENTAL feature.
385  
386 =head2 fallback_templates
387  
388 $hash_ref = $self->fallback_templates;
389 $self = $self->fallback_templates->{"css"} = Mojo::Template->new;
390  
391 Used to set up templates used by L</serve_fallback_for_assets>.
392  
393 This is currently an EXPERIMENTAL feature.
394  
395 =head2 paths
396  
397 $paths = $self->paths;
398 $self = $self->paths([$app->home->rel_file("assets")]);
399  
400 See L<Mojolicious::Static/paths> for details.
401  
402 =head2 retry_delay
403  
404 my $delay = $self->retry_delay;
405 $self = $self->retry_delay(0.5);
406  
407 Delay in seconds between download attempts for assets that need to be fetched, defaults to C<3>.
408  
409 =head2 retries
410  
411 my $retries = $self->retries;
412 $self = $self->retries(5);
413  
414 Number of times asset downloads will be attempted for assets that need to be fetched, defaults to C<0>.
415  
416 =head2 ua
417  
418 $ua = $self->ua;
419  
420 See L<Mojolicious::Plugin::AssetPack/ua>.
421  
422 =head1 METHODS
423  
424 L<Mojolicious::Plugin::AssetPack::Store> inherits all attributes from
425 L<Mojolicious::Static> implements the following new ones.
426  
427 =head2 asset
428  
429 $asset = $self->asset($url, $paths);
430  
431 Returns a L<Mojolicious::Plugin::AssetPack::Asset> object or undef unless
432 C<$url> can be found in C<$paths>. C<$paths> default to
433 L<Mojolicious::Static/paths>. C<$paths> and C<$url> can be...
434  
435 =over 2
436  
437 =item * helper://some.mojo.helper/some_identifier?format=css
438  
439 Will call a helper registered under the name C<csome.mojo.helper>, with the
440 query parameters as arguments. Example:
441  
442 $output = $c->some->mojo->helper(some_identifier => {format => "css"});
443  
444 C<$output> can be a scalar containing the asset content or a hash-ref with
445 arguments passed on to L<Mojolicious::Plugin::AssetPack::Asset>. Note that
446 C<format> need to be present in the URL or the returning hash-ref for this
447 to work.
448  
449 This feature is currently EXPERIMENTAL. Let me know if you use it/find it
450 interesting.
451  
452 =item * http://example.com/foo/bar
453  
454 An absolute URL will be downloaded from web, unless the host is "local":
455 "local" is a special host which will run the request through the current
456 L<Mojolicious> application.
457  
458 =item * foo/bar
459  
460 An relative URL will be looked up using L<Mojolicious::Static/file>.
461  
462 =back
463  
464 Note that assets from web will be cached locally, which means that you need to
465 delete the files on disk to download a new version.
466  
467 =head2 load
468  
469 $bool = $self->load($asset, \%attr);
470  
471 Used to load an existing asset from disk. C<%attr> will override the
472 way an asset is looked up. The example below will ignore
473 L<minified|Mojolicious::Plugin::AssetPack::Asset/minified> and rather use
474 the value from C<%attr>:
475  
476 $bool = $self->load($asset, {minified => $bool});
477  
478 =head2 persist
479  
480 $self = $self->persist;
481  
482 Used to save the internal state of the store to disk.
483  
484 This method is EXPERIMENTAL, and may change without warning.
485  
486 =head2 save
487  
488 $bool = $self->save($asset, \%attr);
489  
490 Used to save an asset to disk. C<%attr> are usually the same as
491 L<Mojolicious::Plugin::AssetPack::Asset/TO_JSON> and used to document metadata
492 about the C<$asset> so it can be looked up using L</load>.
493  
494 =head2 serve_asset
495  
496 $self = $self->serve_asset($c, $asset);
497  
498 Override L<Mojolicious::Static/serve_asset> with the functionality to set
499 response headers first, from L</default_headers>.
500  
501 Will call L<Mojolicious::Plugin::AssetPack::Asset/render> if available, after
502 setting Content-Type header and other L</default_headers>.
503  
504 =head2 serve_fallback_for_assets
505  
506 $self = $self->serve_fallback_for_assets($c, $topic, $assets);
507  
508 Used to serve a fallback response for given C<$topic> and a
509 L<Mojo::Collection> of C<Mojolicious::Plugin::AssetPack::Asset> objects.
510  
511 Will set the headers in L</fallback_headers> and then either render either a
512 template matching the extension from C<$topic> from L</fallback_templates>, a
513 302 redirect to the actual asset, or a 404 Not Found.
514  
515 This is currently an EXPERIMENTAL feature.
516  
517 =head1 SEE ALSO
518  
519 L<Mojolicious::Plugin::AssetPack>.
520  
521 =cut
522  
523 __DATA__
« AssetPack/Pipe/CoffeeScript.pm ↑ Summary AssetPack/Pipe/Riotjs.pm »