Catalyst-Controller-AutoAssets

 view release on metacpan or  search on metacpan

lib/Catalyst/Controller/AutoAssets.pm  view on Meta::CPAN

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' => (

lib/Catalyst/Controller/AutoAssets/Handler.pm  view on Meta::CPAN

require MIME::Types;
require Module::Runtime;

has 'Controller' => (
  is => 'ro', required => 1,
  isa => 'Catalyst::Controller::AutoAssets',
  handles => [qw(type _app action_namespace unknown_asset _build_params _module_version)],
);

# Directories to include
has 'include', is => 'ro', isa => 'ScalarRef|Str|ArrayRef[ScalarRef|Str]', required => 1;

# Optional regex to require files to match to be included
has 'include_regex', is => 'ro', isa => 'Maybe[Str]', default => undef;

# Optional regex to exclude files
has 'exclude_regex', is => 'ro', isa => 'Maybe[Str]', default => undef;

# Whether or not to use qr/$regex/i or qr/$regex/
has 'regex_ignore_case', is => 'ro', isa => 'Bool', default => 0;

lib/Catalyst/Controller/AutoAssets/Handler.pm  view on Meta::CPAN

has 'current_alias', is => 'ro', isa => 'Str', default => 'current';

# Whether or not to make the current asset available via a static path
# with no benefit of caching
has 'allow_static_requests', is => 'ro', isa => 'Bool', default => 0;

# What string to use for the 'static' path
has 'static_alias', is => 'ro', isa => 'Str', default => 'static';

# Extra custom response headers for current/static requests 
has 'current_response_headers', is => 'ro', isa => 'HashRef', default => sub {{}};
has 'static_response_headers', is => 'ro', isa => 'HashRef', default => sub {{}};

# Whether or not to set 'Etag' response headers and check 'If-None-Match' request headers
# Very useful when using 'static' paths
has 'use_etags', is => 'ro', isa => 'Bool', default => 0;

# Max number of seconds before recalculating the fingerprint (sha1 checksum)
# regardless of whether or not the mtime has changed. 0 means infinite/disabled
has 'max_fingerprint_calc_age', is => 'ro', isa => 'Int', default => sub {0};

# Max number of seconds to wait to obtain a lock (to be thread safe)

lib/Catalyst/Controller/AutoAssets/Handler.pm  view on Meta::CPAN

  my $self = shift;
  return file($self->work_dir,'fingerprint');
};

has 'lock_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
  my $self = shift;
  return file($self->work_dir,'lockfile');
};


has 'includes', is => 'ro', isa => 'ArrayRef', lazy => 1, default => sub {
  my $self = shift;
  my $rel = $self->include_relative_dir;

  my @list = ((ref $self->include)||'') eq 'ARRAY' ? @{$self->include} : $self->include;
  my $i = 0;
  return [ map {
    my $inc; $i++;
    if((ref($_)||'') eq 'SCALAR') {
      # New support for ScalarRef includes ... we pre-dump them to a temp file
      $inc = file( $self->scratch_dir, join('','_generated_include_file_',$i) );

lib/Catalyst/Controller/AutoAssets/Handler.pm  view on Meta::CPAN

  return 0;
}

# -----
# Quick and dirty state persistence for faster startup
has 'persist_state_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
  my $self = shift;
  return file($self->work_dir,'state.dat');
};

has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
 built_mtime
 inc_mtimes
 last_fingerprint_calculated
)]};

sub _persist_state {
  my $self = shift;
  return undef unless ($self->persist_state);
  my $data = { map { $_ => $self->$_ } @{$self->_persist_attrs} };
  $data->{_module_version} = $self->_module_version;

lib/Catalyst/Controller/AutoAssets/Handler/CSS.pm  view on Meta::CPAN

use namespace::autoclean;

with 'Catalyst::Controller::AutoAssets::Handler';

use Path::Class 0.32 qw( dir file );
use Module::Runtime;
use CSS::Scopifier 0.04;
use CSS::Scopifier::Group;

has 'minify', is => 'ro', isa => 'Bool', default => sub{0};
has 'scopify', is => 'ro', isa => 'Maybe[ArrayRef]', default => sub{undef};

sub BUILD {
  my $self = shift;
  
  Catalyst::Exception->throw("No minifier available")
    if($self->minify && ! $self->minifier);
}

has 'minifier', is => 'ro', isa => 'Maybe[CodeRef]', lazy => 1, default => sub {
  my $self = shift;

lib/Catalyst/Controller/AutoAssets/Handler/Directory.pm  view on Meta::CPAN

  my $list = shift;
  $self->subfile_meta({
    map { join('/', grep { $_ ne '.' } $_->relative($self->dir_root)->components) => {
      file => $_,
      mtime => $_->stat->mtime,
      content_type => $self->content_type_resolver->($self,$_)
    } } @$list
  });
}

has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
 built_mtime
 inc_mtimes
 last_fingerprint_calculated
 subfile_meta
 _excluded_paths
)]};


has 'dir_root', is => 'ro', isa => 'Path::Class::Dir', lazy => 1, default => sub {
  my $self = shift;

lib/Catalyst/Controller/AutoAssets/Handler/Directory.pm  view on Meta::CPAN

sub write_built_file {
  my ($self, $fd, $files) = @_;
  # The built file is just a placeholder in the case of 'directory' type 
  # asset whose data is served from the original files
  my @relative = map { join('/', grep { $_ ne '.' } file($_)->relative($self->dir_root)->components) } @$files;
  $fd->write(join("\r\n",@relative) . "\r\n");
}


# These apply only to 'directory' asset type
has 'html_head_css_subfiles', is => 'ro', isa => 'ArrayRef', default => sub {[]};
has 'html_head_js_subfiles', is => 'ro', isa => 'ArrayRef', default => sub {[]};

# --------------------
# html_head_tags()
#
# Convenience method to generate a set of CSS <link> and JS <script> tags
# suitable to drop into the <head> section of an HTML document. 
#
# For 'css' and 'js' assets this will be a single tag pointing at the current
# valid asset path. For 'directory' asset types this will be a listing of
# css and/or js tags pointing at subfile asset paths supplied in the attrs:

lib/Catalyst/Controller/AutoAssets/Handler/IconSet.pm  view on Meta::CPAN

    unless (scalar @path > 0);
  
  # Will prepare the asset if needed:
  $self->asset_path(@path);
  
  my $name = join('/',@path);
  my $data = $self->manifest->{$name} or die "No such image/icon '$name'";
  return $data->{icon_name};
}

has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
 built_mtime
 inc_mtimes
 last_fingerprint_calculated
 subfile_meta
 _excluded_paths
 manifest
 icon_manifest
)]};


lib/Catalyst/Controller/AutoAssets/Handler/ImageSet.pm  view on Meta::CPAN

};

around asset_path => sub {
  my ($orig, $self, @subpath) = @_;
  return $self->flatten_paths 
    ?  join('/',$self->base_path,$self->asset_name,@subpath)
    : $self->$orig(@subpath);
};


has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
 built_mtime
 inc_mtimes
 last_fingerprint_calculated
 subfile_meta
 _excluded_paths
 manifest
)]};


sub img_tag {



( run in 0.722 second using v1.01-cache-2.11-cpan-5f2e87ce722 )