| 1 | # | ||||||||||||||||||||||||||||||||||
| 2 | # This file is part of Template-Plugin-Next | ||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2017 by Alexander Kühne. | ||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||
| 9 | package Template::Plugin::Next; | ||||||||||||||||||||||||||||||||||
| 10 | # ABSTRACT: include the 'next' template file with identical relative path | ||||||||||||||||||||||||||||||||||
| 11 | 2 | $Template::Plugin::Next::VERSION = '0.04'; | |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use base qw( Template::Plugin ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Template::Plugin (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Template::Exception (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 23 | # called as Template::Plugin::Next->new($context) | ||||||||||||||||||||||||||||||||||
| 24 | 4 | my ($class, $context, %params) = @_; | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 4 | my $self = | |||||||||||||||||||||||||||||||||
| 27 | bless { | ||||||||||||||||||||||||||||||||||
| 28 | _CONTEXT => $context | ||||||||||||||||||||||||||||||||||
| 29 | }, $class; | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 4 | $self; | |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 35 | 0 | my $proto = shift; | |||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | die( ref( $_[0] ) ? @_ : do { $proto->SUPER::error(@_); Template::Exception->new( 'Next', $proto->SUPER::error ) } ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | sub process { | ||||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=16 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 41 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 42 | 2 | my $params = shift; | |||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | my $localise = shift || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 44 | 2 | my $context = $self->{_CONTEXT}; | |||||||||||||||||||||||||||||||||
| 45 | 2 | my $stash = $context->stash(); | |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | 2 | my $name = $stash->get( 'component.name' ); # template file path not file name | |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | my $providers = $context->{ PREFIX_MAP }->{ default } || $context->{ LOAD_TEMPLATES }; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$context->{'PREFIX_MAP'}{'default'} || $context->{'LOAD_TEMPLATES'}
MC/DC0%0/2
$context->{'PREFIX_MAP'}{'default'} || $context->{'LOAD_TEMPLATES'}
$context->{'PREFIX_MAP'}{'default'}
$context->{'LOAD_TEMPLATES'}
Truth table33%1/3
A || B
A= $context->{'PREFIX_MAP'}{'default'}B= $context->{'LOAD_TEMPLATES'}
| |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 2 | foreach my $provider ( grep { ref( $_ ) eq 'Template::Provider' } @$providers ) { | |||||||||||||||||||||||||||||||||
| 52 | # we know only how to handle the standard behaviour of providers | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 2 | my $rel_path = $name; | |||||||||||||||||||||||||||||||||
| 55 | 2 | my @include_paths = @{$provider->paths}; | |||||||||||||||||||||||||||||||||
| 56 | # include paths are returned in a list even if | ||||||||||||||||||||||||||||||||||
| 57 | # multiple include paths have been specified via a colon separated scalar | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $self->error( 'Not applicable. There is no second include path!' ) if scalar(@include_paths) == 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if ( File::Spec->file_name_is_absolute( my $abs_path = $rel_path ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | # this is for subsequent calls to NEXT | ||||||||||||||||||||||||||||||||||
| 63 | ( my( $include_path ), $rel_path ) = | ||||||||||||||||||||||||||||||||||
| 64 | @{ | ||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | ( ( $stash->get( 'Next_' ) || $self->error( 'Could not find Next_ stash entry!' ) )->{$abs_path} | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
+($stash->get('Next_') || $self->error('Could not find Next_ stash entry!'))->{$abs_path} || $self->error("Could not find abs path $abs_path in Next_ map!")
$stash->get('Next_') || $self->error('Could not find Next_ stash entry!')
MC/DC0%0/3
+($stash->get('Next_') || $self->error('Could not find Next_ stash entry!'))->{$abs_path} || $self->error("Could not find abs path $abs_path in Next_ map!")
$stash->get('Next_')
$self->error('Could not find Next_ stash entry!')
$self->error("Could not find abs path $abs_path in Next_ map!")
Truth table25%1/4
A || B || C
A= $stash->get('Next_')B= $self->error('Could not find Next_ stash entry!')C= $self->error("Could not find abs path $abs_path in Next_ map!")
| |||||||||||||||||||||||||||||||||||
| 66 | || | ||||||||||||||||||||||||||||||||||
| 67 | $self->error( "Could not find abs path $abs_path in Next_ map!" ) | ||||||||||||||||||||||||||||||||||
| 68 | ) | ||||||||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | while ( @include_paths ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | last if ( shift @include_paths ) eq $include_path; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | 1 | $rel_path =~ s/^\///; | |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||||
| 77 | # this is for the initial call to NEXT | ||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | while ( @include_paths ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | 2 | ▶ | last if scalar stat( _concat_path( (shift @include_paths ), $rel_path ) ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 2 | foreach my $include_path ( @include_paths ) { | |||||||||||||||||||||||||||||||||
| 84 | 2 | my $path = _concat_path( $include_path, $rel_path ); | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if( scalar stat( $path ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | 2 | my $template; | |||||||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||||||||
| 89 | # make abs. template paths local to this template retrieval | ||||||||||||||||||||||||||||||||||
| 90 | 2 | local $provider->{ABSOLUTE} = 1; | |||||||||||||||||||||||||||||||||
| 91 | 2 | $template = $context->template($path); | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | defined $template || $self->error( $context->error ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | 2 | ▶ | my $map = $stash->get( 'Next_' ) || {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$stash->get('Next_') || {}
MC/DC100%1/1
$stash->get('Next_') || {}
$stash->get('Next_')
Truth table100%2/2
A
A= $stash->get('Next_')
| |||||||||||||||||||||||||||||||||||
| 95 | 2 | $map->{$path} = [ $include_path, $rel_path ]; | |||||||||||||||||||||||||||||||||
| 96 | 2 | $stash->set( 'Next_' => $map ); | |||||||||||||||||||||||||||||||||
| 97 | 2 | return $context->process( $template, $params, $localise ); | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 0 | $self->error( "No 'Next' template!" ); | |||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub include { | ||||||||||||||||||||||||||||||||
|
Subroutine
include: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 108 | 0 | $self->process( @_, 1 ); | |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | sub _concat_path { | ||||||||||||||||||||||||||||||||
|
Subroutine
_concat_path: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||
| 112 | 4 | my ( $base_path, $append_dirs ) = @_; | |||||||||||||||||||||||||||||||||
| 113 | # $base_dir: base path (no filename) as string | ||||||||||||||||||||||||||||||||||
| 114 | # $append_dirs: directories to append as string or an array reference | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 4 | my ($base_volume, $base_directories, $base_file) = File::Spec->splitpath( $base_path, 1 ); | |||||||||||||||||||||||||||||||||
| 117 | File::Spec->catpath( | ||||||||||||||||||||||||||||||||||
| 118 | $base_volume, | ||||||||||||||||||||||||||||||||||
| 119 | File::Spec->catdir( | ||||||||||||||||||||||||||||||||||
| 120 | File::Spec->splitdir( $base_directories ), | ||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | ( ref($append_dirs) ? @{$append_dirs} : File::Spec->splitdir( $append_dirs ) ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 122 | ) | ||||||||||||||||||||||||||||||||||
| 123 | , | ||||||||||||||||||||||||||||||||||
| 124 | $base_file | ||||||||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 2 | 1; # End of Template::Plugin::Next | |||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||