File Coverage

blib/lib/Yeb/Plugin/Static.pm
Criterion Covered Total %
statement 33 34 97.0
branch 7 12 58.3
condition 1 3 33.3
subroutine 12 12 100.0
pod 0 2 0.0
total 53 63 84.1


line stmt bran cond sub pod time code
1             package Yeb::Plugin::Static;
2             BEGIN {
3 2     2   5781 $Yeb::Plugin::Static::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Yeb Plugin for Plack::Middleware::Static
6             $Yeb::Plugin::Static::VERSION = '0.103';
7 2     2   19 use Moo;
  2         5  
  2         18  
8 2     2   1013 use Path::Tiny qw( path );
  2         14  
  2         155  
9 2     2   2065 use Plack::Middleware::Static;
  2         1080  
  2         60  
10 2     2   14 use Carp;
  2         4  
  2         1096  
11              
12             has app => ( is => 'ro', required => 1 );
13             has class => ( is => 'ro', required => 1 );
14              
15 1     1   481 has middleware_statics => (
16             is => 'ro',
17             lazy => 1,
18             builder => sub {[]},
19             );
20              
21             has local_static => (
22             is => 'ro',
23             lazy => 1,
24 1     1   405 builder => sub { 0 },
25             );
26              
27             has content_type => (
28             is => 'ro',
29             predicate => 1,
30             );
31              
32             has default_root => (
33             is => 'ro',
34             predicate => 1,
35             );
36              
37             sub add_middleware_static {
38 2     2 0 4 my ( $self, $from, $root, $pass_through, $content_type ) = @_;
39 2 50 33     10 $root = $self->default_root if !defined $root and $self->has_default_root;
40 2 50       6 croak "Static needs a root directory for a static directory" unless defined $root;
41 2 100       3 unshift @{$self->middleware_statics}, Plack::Middleware::Static->new(
  2 50       26  
    50          
42             path => $from,
43             root => path($root)->absolute,
44             pass_through => $pass_through ? 1 : 0,
45             defined $content_type
46             ? ( content_type => $content_type )
47             : $self->has_content_type
48             ? ( content_type => $self->content_type )
49             : (),
50             );
51             }
52              
53             sub BUILD {
54 1     1 0 1407 my ( $self ) = @_;
55             $self->app->register_function('static',sub {
56 1     1   104 my ( $from, $root, $content_type ) = @_;
57 1         4 $self->add_middleware_static($from,$root,1,$content_type);
58 1         10 });
59             $self->app->register_function('static_404',sub {
60 1     1   33 my ( $from, $root, $content_type ) = @_;
61 1         3 $self->add_middleware_static($from,$root,0,$content_type);
62 1         28 });
63 1 50       20 my $class = $self->local_static
64             ? $self->class
65             : $self->app->y_main;
66             $class->prepend_to_chain("/...",sub {
67 16     16   9381 map { my $mw = $_; sub () { $mw } } @{$self->middleware_statics}
  32         185  
  32         344  
  0         0  
  16         459  
68 1         15 });
69             }
70              
71             1;
72              
73             __END__