blib/lib/Catalyst/Plugin/Assets.pm

filter: Statementstmt 91.6% 44 / 48 Branchbran 61.1% 11 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 8 / 8 Podpod 25.0% 1 / 4 totaltotal 77.2% 68 / 88 CC 12 SCAR 15.4
CC
12
Cov
82%
make_assets
8.3
setup
4.2
assets
3.0
CRAP 4.7
↑ Summary
1 package Catalyst::Plugin::Assets;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Catalyst::Plugin::Assets - Manage and minify .css and .js assets in a Catalyst application
9  
10 =head1 VERSION
11  
12 Version 0.036
13  
14 =cut
15  
16 4 our $VERSION = '0.036';
17  
18 =head1 SYNOPSIS
19  
20 # In your Catalyst application...
21  
22 use Catalyst qw/-Debug Assets Static::Simple/;
23 # Static::Simple is not *required*, but C::P::Assets does not serve files by itself!
24
25 # This is all you need. Now your $catalyst object will now have an ->assets method.
26  
27 # Sometime during the request ...
28  
29 sub some_action : Local {
30 my ($self, $catalyst) = @_;
31
32 ...
33  
34 $catalyst->assets->include("stylesheet.css");
35  
36 ...
37 }
38  
39 # Then, in your .tt (or whatever you're using for view processing):
40  
41 <html>
42 <head><title>[% title %]</title>
43  
44 [% catalyst.assets.export %]
45  
46 </head>
47 <body>
48  
49 ...
50
51 =head1 DESCRIPTION
52  
53 Catalyst::Plugin::Assets integrates L<File::Assets> into your Catalyst application. Essentially, it provides a unified way to include .css and .js assets from different parts of your program. When you're done processing a request, you can use $catalyst->assets->export() to generate HTML or $catalyst->assets->exports() to get a list of assets.
54  
55 C::P::Assets will also handle .css files of different media types properly.
56  
57 In addition, C::P::Assets includes support for minification via YUI compressor, L<JavaScript::Minifier>, L<CSS::Minifier>, L<JavaScript::Minifier::XS>, and L<CSS::Minifier::XS>
58  
59 Note that Catalyst::Plugin::Assets does not serve files directly, it will work with Static::Simple or whatever static-file-serving mechanism you're using.
60  
61 =head2 A brief description of L<File::Assets>
62  
63 L<File::Assets> is a tool for managing JavaScript and CSS assets in a (web) application. It allows you to "publish" assests in one place after having specified them in different parts of the application (e.g. throughout request and template processing phases).
64  
65 =head1 USAGE
66  
67 For usage hints and tips, see L<File::Assets>
68  
69 =head1 CONFIGURATION
70  
71 You can configure C::P::Assets by manipulating the $catalyst->config->{'Plugin::Assets'} hash.
72  
73 Note, in previous versions, the configuration location was $catalyst->config->{assets}
74  
75 The following settings are available:
76  
77 path # A path to automatically look for assets under (e.g. "/static" or "/assets")
78  
79 # This path will be automatically prepended to includes, so that instead of
80 # doing ->include("/static/stylesheet.css") you can just do ->include("stylesheet.css")
81
82  
83 output_path # The path to output the results of minification under (if any).
84 # For example, if output is "built/" (the trailing slash is important), then minified assets will be
85 # written to "root/<assets-path>/built/..."
86  
87  
88 minify # '1' to use JavaScript::Minifier and CSS::Minifier for minification
89 # 'yuicompressor:<path-to-yuicompressor-jar>' to use YUI Compressor
90  
91  
92 stash_var # The name of the key in the stash that provides the assets object (accessible via $catalyst->stash->{<stash_var}.
93 # By default, the <stash_var> is "assets".
94 # To disable the setting of the stash variable, set <stash_var> to undef
95  
96 =head2 Example configuration
97  
98 Here is an example configuration:
99  
100 # Under the configuration below, the assets object will automatically
101 # look for assets (.css and .js files) under <home>/root/static/*
102 # If it needs to generate a minified asset, it will deposit the generated asset under <home>/root/static/built/*
103  
104 # To turn off minification, set minify to 0
105  
106 # Finally, the assets object is also available via $catalyst->stash->{assets} (This is actually the default setting)
107  
108 __PACKAGE__->config(
109
110 name => 'Example',
111  
112 'Plugin::Assets' => {
113  
114 path => "/static",
115 output_path => "built/",
116 minify => 1,
117  
118 stash_var => "assets", # This is the default setting
119 },
120  
121 );
122  
123 # Later, to include "http://localhost/static/example.css", do:
124  
125 $catalyst->assets->include("example.css");
126  
127 # To include "http://localhost/static/example.js", do:
128  
129 $catalyst->assets->include("example.js");
130  
131 =cut
132  
133 4 ▶use File::Assets;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 4 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 4 __PACKAGE__->mk_accessors(qw/_assets/);
137  
138 =head1 METHODS
139  
140 =cut
141  
142 4 ▶sub setup {
Subroutine setup: called CC=4 SCAR=14.2
Pod undocumented
143 4 my $catalyst = shift;
144
145 4 $catalyst->maybe::next::method(@_);
146
147 4 my $config;
148 4 ▶ if ($config = $catalyst->config->{'Plugin::Assets'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config = $catalyst->config->{'Plugin::Assets'}) { } 1 3 4
elsif ($config = $catalyst->config->{'assets'}) { } 3 0 3
149 }
150 elsif ($config = $catalyst->config->{assets}) {
151 3 warn
152 "\n*** Setting ${catalyst}->config->{assets} has been deprecated!\n" .
153 "*** Please update your configuration to use ${catalyst}->config->{'Plugin::Assets'} instead...\n" .
154 "*** I'm copying 'assets' into 'Plugin::Assets' ...\n";
155 3 $catalyst->config->{'Plugin::Assets'} = $config;
156 }
157 else {
158 0 $config = {};
159 }
160  
161
162 4 ▶ $config->{stash_var} = "assets" unless exists $config->{stash_var};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"stash_var"} 4 0 4
163 }
164  
165 11 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod undocumented
166 11 my $self = shift;
167
168 11 my $catalyst = $self->maybe::next::method(@_);
169  
170 11 $catalyst->assets; # Instantiate some new assets to use for this request
171  
172 11 return $catalyst;
173 }
174  
175 =head2 assets
176  
177 Return the L<File::Assets> object that exists throughout the lifetime of the request
178  
179 =cut
180  
181 61 ▶sub assets {
Subroutine assets: called CC=3 SCAR=11.1
Pod documented
182 61 my $self = shift;
183 61 ▶ return $self->{_assets} ||= do {
Condition66%2/3
$self->{"_assets"} ||= do { my $assets = $self->make_assets; my $config = $self->config->{"Plugin::Assets"}; if (defined(my $stash_var = $config->{"stash_var"})) { $self->stash->{$stash_var} = $assets; } ; $assets }
l!l&&r!l&&!r
50110
MC/DC0%0/2
$self->{"_assets"} ||= do { my $assets = $self->make_assets; my $config = $self->config->{"Plugin::Assets"}; if (defined(my $stash_var = $config->{"stash_var"})) { $self->stash->{$stash_var} = $assets; } ; $assets }
$self->{"_assets"} do { my $assets = $self->make_assets; my $config = $self->config->{"Plugin::Assets"}; if (defined(my $stash_var = $config->{"stash_var"})) { $self->stash->{$stash_var} = $assets; } ; $assets }
Truth table66%2/3
A ||= B
A= $self->{"_assets"}B= do { my $assets = $self->make_assets; my $config = $self->config->{"Plugin::Assets"}; if (defined(my $stash_var = $config->{"stash_var"})) { $self->stash->{$stash_var} = $assets; } ; $assets }
ABresult
1X1
011
000
184 11 my $assets = $self->make_assets;
185 11 my $config = $self->config->{'Plugin::Assets'};
186 11 ▶ if (defined (my $stash_var = $config->{stash_var})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $stash_var = $config->{"stash_var"})) 11 0 11
187 11 $self->stash->{$stash_var} = $assets;
188 }
189 11 $assets;
190 }
191 }
192  
193 11 ▶sub make_assets {
Subroutine make_assets: called CC=7 SCAR=21.1
Pod undocumented
194 11 my $self = shift;
195  
196 11 my $config = $self->config->{'Plugin::Assets'};
197 11 my $path = $config->{path};
198 11 ▶ my $output_path = $config->{output_path} || $config->{output};
Condition66%2/3
$config->{'output_path'} || $config->{'output'}
l!l&&r!l&&!r
920
MC/DC0%0/2
$config->{'output_path'} || $config->{'output'}
$config->{'output_path'} $config->{'output'}
Truth table66%2/3
A || B
A= $config->{'output_path'}B= $config->{'output'}
ABresult
1X1
011
000
199  
200 11 my %assets;
201 # Different from previous version, KISS
202 11 ▶ if (my $minify = $config->{minify}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $minify = $config->{"minify"}) 8 3 11
203 8 ▶ if ($minify =~ m/^\s*(?:on|yes|true)\s*$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($minify =~ /^\s*(?:on|yes|true)\s*$/i) { } 0 8 8
elsif ($minify =~ /^\s*(?:off|no|false|0)\s*$/i) { } 0 8 8
elsif (ref $minify eq '') { } 8 0 8
204 0 $assets{minify} = 1;
205 }
206 elsif ($minify =~ m/^\s*(?:off|no|false|0)\s*$/i) {
207 }
208 elsif (ref $minify eq "") { # yuicompressor:... etc.
209 8 $assets{minify} = $minify;
210 }
211 else {
212 0 die "Don't understand minify option: $minify";
213 }
214 }
215  
216 11 my $assets = File::Assets->new(
217 base => { uri => $self->uri_for("/"), dir => $self->path_to("root"), path => $path },
218 output_path => $output_path,
219 %assets,
220 );
221  
222 11 ▶ if (my $customize = $config->{customize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $customize = $config->{"customize"}) 0 11 11
223 0 $customize->($assets, $self);
224 }
225  
226 11 return $assets;
227 }
228  
229 =head1 AUTHOR
230  
231 Robert Krimen, C<< <rkrimen at cpan.org> >>
232  
233 =head1 BUGS
234  
235 Please report any bugs or feature requests to C<bug-catalyst-plugin-assets at rt.cpan.org>, or through
236 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Assets>. I will be notified, and then you'll
237 automatically be notified of progress on your bug as I make changes.
238  
239  
240  
241  
242 =head1 SUPPORT
243  
244 You can find documentation for this module with the perldoc command.
245  
246 perldoc Catalyst::Plugin::Assets
247  
248  
249 You can also look for information at:
250  
251 =over 4
252  
253 =item * RT: CPAN's request tracker
254  
255 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Assets>
256  
257 =item * AnnoCPAN: Annotated CPAN documentation
258  
259 L<http://annocpan.org/dist/Catalyst-Plugin-Assets>
260  
261 =item * CPAN Ratings
262  
263 L<http://cpanratings.perl.org/d/Catalyst-Plugin-Assets>
264  
265 =item * Search CPAN
266  
267 L<http://search.cpan.org/dist/Catalyst-Plugin-Assets>
268  
269 =back
270  
271 =head1 SEE ALSO
272  
273 L<File::Assets>
274  
275 L<Catalyst>
276  
277 L<http://developer.yahoo.com/yui/compressor/>
278  
279 L<JavaScript::Minifier::XS>
280  
281 L<CSS::Minifier::XS>
282  
283 L<JavaScript::Minifier>
284  
285 L<CSS::Minifier>
286  
287 =head1 ACKNOWLEDGEMENTS
288  
289 =head1 COPYRIGHT & LICENSE
290  
291 Copyright 2008 Robert Krimen
292  
293 This program is free software; you can redistribute it and/or modify it
294 under the same terms as Perl itself.
295  
296  
297 =cut
298  
299 4 1; # End of Catalyst::Plugin::Assets
↑ Summary