File Coverage

blib/lib/Web/AssetLib/OutputEngine/LocalFile.pm
Criterion Covered Total %
statement 60 62 96.7
branch 9 14 64.2
condition n/a
subroutine 11 11 100.0
pod n/a
total 80 87 91.9


line stmt bran cond sub pod time code
1             package Web::AssetLib::OutputEngine::LocalFile;
2              
3 6     6   12194725 use Method::Signatures;
  6         48646  
  6         49  
4 6     6   2695 use Moose;
  6         281156  
  6         35  
5 6     6   25926 use Carp;
  6         9  
  6         377  
6              
7 6     6   560 use Web::AssetLib::Util;
  6         10  
  6         90  
8 6     6   2385 use Web::AssetLib::Output::Link;
  6         20  
  6         145  
9              
10 6     6   752 use Path::Tiny;
  6         7807  
  6         265  
11              
12 6     6   66 use v5.14;
  6         15  
13 6     6   25 no if $] >= 5.018, warnings => "experimental";
  6         31  
  6         48  
14              
15             extends 'Web::AssetLib::OutputEngine';
16              
17             has 'output_path' => (
18             is => 'rw',
19             isa => 'Str',
20             required => 1
21             );
22              
23             # should correspond with the root of output_path
24             has 'link_path' => (
25             is => 'rw',
26             isa => 'Str',
27             required => 1
28             );
29              
30 6 50   6   18817 method export (:$assets!, :$minifier?) {
  4 50   4   9  
  4 50       21  
  4         19  
  4         7  
  4         6  
  4         9  
  4         8  
  4         16  
  4         180  
31 4         9 my $types = {};
32 4         9 my $output = [];
33              
34             # categorize into type groups, and seperate concatenated
35             # assets from those that stand alone
36              
37 4         29 foreach my $asset ( sort { $a->rank <=> $b->rank } @$assets ) {
  6         265  
38 9 100       351 if ( $asset->isPassthru ) {
39 1         23 push @$output,
40             Web::AssetLib::Output::Link->new(
41             src => $asset->link_path,
42             type => $asset->type,
43             default_html_attrs => $asset->default_html_attrs
44             );
45             }
46             else {
47 8         38 for ( $asset->type ) {
48 8         291 when (/css|js/) {
49              
50             # should concatenate
51             $$types{ $asset->type }{_CONCAT_}
52 8         30 .= $asset->contents . "\n\r\n\r";
53             }
54 0         0 default {
55 0         0 $$types{ $asset->type }{ $asset->digest }
56             = $asset->contents;
57             }
58             }
59             }
60             }
61              
62 4         2439 foreach my $type ( keys %$types ) {
63 8         192 foreach my $id ( keys %{ $$types{$type} } ) {
  8         30  
64 8         15 my $output_contents = $$types{$type}{$id};
65              
66 8 50       68 my $digest
67             = $id eq '_CONCAT_'
68             ? $self->generateDigest($output_contents)
69             : $id;
70              
71 8         41 my $filename = "$digest.$type";
72 8         373 my $output_path = path( $self->output_path )->child($filename);
73 8         821 my $link_path = path( $self->link_path )->child($filename);
74              
75             # # output pre-minify
76             # my $output_path_debug = path( $self->output_path )->child($filename.".orig.$type");
77             # unless($output_path_debug->exists){
78             # $output_path_debug->touchpath;
79             # $output_path_debug->spew_utf8($output_contents);
80             # }
81              
82 8 100       280 unless ( $output_path->exists ) {
83 4         216 $output_path->touchpath;
84              
85 4 50       2138 if ($minifier) {
86 4         28 $output_contents = $minifier->minify(
87             contents => $output_contents,
88             type => $type
89             );
90             }
91              
92 4         55 $output_path->spew_utf8($output_contents);
93             }
94              
95 8         4505 push @$output,
96             Web::AssetLib::Output::Link->new(
97             src => "$link_path",
98             type => $type
99             );
100             }
101             }
102              
103 4         313 return $output;
104             }
105              
106 6     6   2934 no Moose;
  6         9  
  6         48  
107             1;
108              
109             =pod
110            
111             =encoding UTF-8
112            
113             =head1 NAME
114              
115             Web::AssetLib::OutputEngine::LocalFile - allows exporting an asset or bundle to your local filesystem
116              
117             =head1 SYNOPSIS
118              
119             my $library = My::AssetLib::Library->new(
120             output_engines => [
121             Web::AssetLib::OutputEngine::LocalFile->new(
122             output_path => '/my/local/output/path',
123             link_path => '/output/path/relative/to/webserver'
124             )
125             ]
126             );
127              
128             =head1 USAGE
129              
130             Instantiate with C<< output_path >> and C<< link_path >> parameters, and include in your library's
131             output engine list.
132              
133             =head1 ATTRIBUTES
134            
135             =head2 output_path
136            
137             String; the absolute path that the compiled assets should be exported to
138              
139             =head2 link_path
140            
141             String; the path relative to your webserver root, which points to the L<< /output_path >>.
142             Used in generating HTML tags.
143              
144             =head1 SEE ALSO
145              
146             L<Web::AssetLib::OutputEngine>
147              
148             =head1 AUTHOR
149            
150             Ryan Lang <rlang@cpan.org>
151              
152             =cut