| 1 | package Catalyst::Plugin::AutoAssets; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.40'; | ||
| 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 | if (ref $assets eq 'HASH') { | ||
| 31 | $c->_inject_single_asset_controller($_,$assets->{$_}) for (keys %$assets); | ||
| 32 | } | ||
| 33 | elsif (ref $assets eq 'ARRAY') { | ||
| 34 | $c->_inject_single_asset_controller($_) for (@$assets); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | die "'assets' must be a hashref or an arrayref"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _inject_single_asset_controller { | ||
| 42 | my ($c, $controller, $cfg) = @_; | ||
| 43 | |||
| 44 | if(ref($controller)) { | ||
| 45 | $cfg = { %$controller }; | ||
| 46 | $controller = delete $cfg->{controller} | ||
| 47 | or die "Bad asset config; 'controller' class not specified"; | ||
| 48 | } | ||
| 49 | |||
| 50 | unless ($c->can('asset_controllers')) { | ||
| 51 | $c->mk_classdata('asset_controllers'); | ||
| 52 | $c->asset_controllers([]); | ||
| 53 | } | ||
| 54 | my %seen = map {$_=>1} @{$c->asset_controllers}; | ||
| 55 | |||
| 56 | die "Duplicate asset controller '$controller'" if ($seen{$controller}); | ||
| 57 | |||
| 58 | push @{$c->asset_controllers}, $controller; | ||
| 59 | |||
| 60 | $c->config( "Controller::$controller" => $cfg ); | ||
| 61 | |||
| 62 | CatalystX::InjectComponent->inject( | ||
| 63 | into => $c, | ||
| 64 | component => 'Catalyst::Controller::AutoAssets', | ||
| 65 | as => $controller | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | # Convenience method to get all configured html head tags of all the | ||
| 70 | # asset controllers at once: | ||
| 71 | sub all_html_head_tags { | ||
| 72 | my $c = shift; | ||
| 73 | return join("\r\n", map { | ||
| 74 | $c->controller($_)->html_head_tags | ||
| 75 | } @{$c->asset_controllers} ); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ |