Plugin/AutoAssets.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 78 SCAR 30.0
CC
4
Cov
0%
inject_asset_controllers
20.0
all_html_head_tags
2.0
BEGIN
2.0
CRAP 20.0
1 package Catalyst::Plugin::AutoAssets;
2 use strict;
3 use warnings;
4  
5 our $VERSION = 0.32;
6  
7 use Moose::Role;
8 use namespace::autoclean;
9  
10 use CatalystX::InjectComponent;
11 use Catalyst::Controller::AutoAssets;
12  
13 after setup_finalize => sub {
14 my $c = shift;
15  
16 # New: Turn off new 'autoflush' flag in logger (see Catalyst::Log).
17 # This is needed to surpress output of debug log messages for
18 # static requests (see no_log opt in Catalyst::Plugin::AutoAssets)
19 $c->log->autoflush(0) if $c->log->can('autoflush');
20 };
21  
22 after 'setup_components' => sub { (shift)->inject_asset_controllers(@_) };
23  
24 sub inject_asset_controllers {
25 my $c = shift;
26  
27 my $config = $c->config->{'Plugin::AutoAssets'} or return;
28 my $assets = $config->{assets} or die "No 'assets' defined in 'Plugin::AutoAssets' config!";
29  
30 # Apply the Controller configs first:
31  
32 my @controllers = ();
33 if (ref $assets eq 'HASH') {
34 $c->config( "Controller::$_" => $assets->{$_} ) and push @controllers, $_
35 for (keys %$assets);
36 }
37 elsif (ref $assets eq 'ARRAY') {
38 my %seen = ();
39 for my $orig (@$assets) {
40 my $cnf = { %$orig };
41 my $controller = delete $cnf->{controller}
42 or die "Bad asset config; 'controller' class not specified";
43 die "Duplicate asset controller '$controller'" if ($seen{$controller}++);
44 $c->config( "Controller::$controller" => $cnf ) and push @controllers, $controller;
45 }
46 }
47 else {
48 die "'assets' must be a hashref or an arrayref";
49 }
50  
51 # Now inject the new Controllers:
52 CatalystX::InjectComponent->inject(
53 into => $c,
54 component => 'Catalyst::Controller::AutoAssets',
55 as => $_
56 ) for (@controllers);
57  
58 # Record the list in a class attr
59 $c->mk_classdata('asset_controllers');
60 $c->asset_controllers(\@controllers);
61 }
62  
63  
64 # Convenience method to get all configured html head tags of all the
65 # asset controllers at once:
66 sub all_html_head_tags {
67 my $c = shift;
68 return join("\r\n", map {
69 $c->controller($_)->html_head_tags
70 } @{$c->asset_controllers} );
71 }
72  
73  
74 1;
75  
76 __END__
77  
78 =pod
79  
80 =head1 NAME
81  
82 Catalyst::Plugin::AutoAssets - Plugin interface to L<Catalyst::Controller::AutoAssets>
83  
84 =head1 SYNOPSIS
85  
86 use Catalyst;
87 with 'Catalyst::Plugin::AutoAssets';
88
89 # Inject/setup AutoAssets controllers:
90 # * MyApp::Controller::Assets::ExtJS (/assets/extjs)
91 # * MyApp::Controller::Assets::MyCSS (/assets/mycss)
92 __PACKAGE__->config(
93 name => 'MyApp',
94 'Plugin::AutoAssets' => {
95 assets => {
96 'Assets::ExtJS' => {
97 type => 'Directory',
98 include => 'ext-3.4.0',
99 persist_state => 1,
100 sha1_string_length => 15
101 },
102 'Assets::MyCSS' => {
103 type => 'CSS',
104 include => '/path/to/css',
105 minify => 1
106 }
107 }
108 }
109 );
110
111 # Or, using arrayref syntax (if order is important):
112 __PACKAGE__->config(
113 name => 'MyApp',
114 'Plugin::AutoAssets' => {
115 assets => [
116 {
117 controller => 'Assets::ExtJS',
118 type => 'Directory',
119 include => 'ext-3.4.0',
120 persist_state => 1,
121 sha1_string_length => 15
122 },
123 {
124 controller => 'Assets::MyCSS',
125 type => 'CSS',
126 include => '/path/to/css',
127 minify => 1
128 },
129 {
130 controller => 'Assets::Icons',
131 type => 'IconSet',
132 include => 'root/static/icons'
133 }
134 ]
135 }
136 );
137  
138 Optionally, within .tt files:
139  
140 <head>
141 <!-- all html includes from all assets at once -->
142 [% c.all_html_head_tags %]
143 </head>
144  
145 =head1 DESCRIPTION
146  
147 This class provides a simple Catalyst Plugin interface to L<Catalyst::Controller::AutoAssets> for easy
148 setup of multiple AutoAssets controllers via config. To use, simply pass a hashref (or arrayref) of 'assets' into the
149 config key 'Plugin::AutoAssets' in your Catalyst application config. This hash should contain controller
150 class names in the keys and Catalyst::Controller::AutoAssets hash configs in the values. Each controller
151 will be injected into your application at runtime.
152  
153 This is just a faster setup than creating the controller classes manually. See L<Catalyst::Controller::AutoAssets>
154 for details and supported config params.
155  
156 =head1 CONFIG PARAMS
157  
158 =head2 assets
159  
160 HashRef or ArrayRef of L<Catalyst::Controller::AutoAssets> configs. Defines the name of each controller to create
161 and the config to use. In HashRef form, the Controller name is specified in the keys with hashref config values.
162 ArrayRef form is a list of hashref configs with an extra key 'controller' to set the Controller name (removed from
163 the config before being passed into the Controller).
164  
165 See the SYNOPSIS above for examples of both.
166  
167 =head1 METHODS
168  
169 =head2 all_html_head_tags
170  
171 Convenience method concats the output of C<html_head_tags()> from all the AutoAssets controllers at once.
172  
173 =head1 SEE ALSO
174  
175 =over
176  
177 =item L<Catalyst::Controller::AutoAssets>
178  
179 =back
180  
181  
182 =head1 AUTHOR
183  
184 Henry Van Styn <vanstyn@cpan.org>
185  
186 =head1 COPYRIGHT AND LICENSE
187  
188 This software is copyright (c) 2013 by IntelliTree Solutions llc.
189  
190 This is free software; you can redistribute it and/or modify it under
191 the same terms as the Perl 5 programming language system itself.
192  
193 =cut