Catalyst-Controller-AutoAssets
view release on metacpan or search on metacpan
lib/Catalyst/Controller/AutoAssets.pm view on Meta::CPAN
package Catalyst::Controller::AutoAssets;
use strict;
use warnings;
our $VERSION = '0.40';
use Moose;
use namespace::autoclean;
require Module::Runtime;
BEGIN { extends 'Catalyst::Controller' }
has 'type', is => 'ro', isa => 'Str', required => 1;
has 'no_logs', is => 'rw', isa => 'Bool', default => sub {1};
has '_module_version', is => 'ro', isa => 'Str', default => $VERSION;
# Save the build params (passed to constructor)
has '_build_params', is => 'ro', isa => 'HashRef', required => 1;
around BUILDARGS => sub {
my ($orig, $class, $c, @args) = @_;
my %params = (ref($args[0]) eq 'HASH') ? %{ $args[0] } : @args; # <-- arg as hash or hashref
$params{_build_params} = {%params};
return $class->$orig($c,\%params);
};
# The Handler (which is determined by the asset type) is
# where most of the actual work gets done:
has '_Handler' => (
is => 'ro', init_arg => undef, lazy => 1,
does => 'Catalyst::Controller::AutoAssets::Handler',
handles => [qw(request asset_path html_head_tags)],
default => sub {
my $self = shift;
my $class = $self->_resolve_handler_class($self->type);
return $class->new({
%{$self->_build_params},
Controller => $self
});
}
);
# Delegate all other function calls to the Handler to support future
# Handler classes and new methods
our $AUTOLOAD;
sub AUTOLOAD {
$AUTOLOAD =~ /([^:]+)$/;
eval "sub $1 { (shift)->_Handler->$1(\@_); }";
goto $_[0]->can($1);
}
sub _resolve_handler_class {
my $self = shift;
my $class = shift;
# legacy, original, lower-case, built-in type names:
my %type_aliases = ( css => 'CSS', js => 'JS', directory => 'Directory' );
$class = $type_aliases{$class} if (exists $type_aliases{$class});
# Allow absolute class names using '+' prefix:
$class = $class =~ /^\+(.*)$/ ? $1
: "Catalyst::Controller::AutoAssets::Handler::$class";
Module::Runtime::require_module($class);
return $class;
}
sub BUILD {
my $self = shift;
# init type handler:
$self->_Handler;
}
sub index :Chained :PathPrefix {
my ($self, $c, @args) = @_;
# New: set 'abort' just like Static::Simple to suppress log messages:
if ( $self->no_logs && $c->log->can('abort') ) {
$c->log->abort( 1 );
( run in 0.578 second using v1.01-cache-2.11-cpan-39bf76dae61 )