| 1 | package Text::MicroMason::TemplateDir; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | ###################################################################### | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 35 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 35 | my ( $self, $src_type, $src_data ) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 35 | ▶ | return $self->NEXT('prepare', $src_type, $src_data ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | unless $src_type eq 'file'; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 28 | my $path = $self->resolve_path($src_data); | |||||||||||||||||||||||||||
| 16 | 28 | return $self->NEXT('prepare', 'file' => $path, source_file => $path ); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 24 | ▶ | sub resolve_path { | ||||||||||||||||||||||||||
|
Subroutine
resolve_path: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 24 | my ($self, $src_data) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 24 | my $current = $self->{source_file}; | |||||||||||||||||||||||||||
| 23 | 24 | my $rootdir = $self->template_root(); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 24 | ▶ | my $base = File::Spec->file_name_is_absolute($src_data) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
"File::Spec"->file_name_is_absolute($src_data) or not $current
MC/DC50%1/2
"File::Spec"->file_name_is_absolute($src_data) or not $current
"File::Spec"->file_name_is_absolute($src_data)
not $current
Truth table66%2/3
A or not(B)
A= "File::Spec"->file_name_is_absolute($src_data)B= not $current
| |||||||||||||||||||||||||||||
| 26 | ! $current ? $rootdir : ( File::Spec->splitpath( $current ) )[1]; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 24 | return File::Spec->catfile( $base, $src_data ); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 24 | ▶ | sub template_root { | ||||||||||||||||||||||||||
|
Subroutine
template_root: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 24 | ▶ | return $self->{template_root} || '.' unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{'template_root'} || '.'
MC/DC100%1/1
$self->{'template_root'} || '.'
$self->{'template_root'}
Truth table100%2/2
A
A= $self->{'template_root'}
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | $self->{template_root} = shift; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | sub cache_key { | ||||||||||||||||||||||||||
|
Subroutine
cache_key: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 10 | my ($src_type, $src_data, %options) = @_; | |||||||||||||||||||||||||||
| 41 | 10 | ▶ | return $self->NEXT('cache_key', @_) unless $src_type eq 'file'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 8 | return $self->resolve_path($src_data); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # $contents = $mason->read_file( $filename ); | ||||||||||||||||||||||||||||
| 47 | 28 | ▶ | sub read_file { | ||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=5 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 28 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 28 | ▶ | if ( my $root = $self->{strict_root} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $root = $self->template_root if $root eq '1'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | my $path = Cwd::abs_path($file); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $root_path = Cwd::abs_path($root) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | or $self->croak_msg("Text::MicroMason::TemplateDir: Strict root '$root' doesn't seem to exist"); | ||||||||||||||||||||||||||||
| 55 | # warn "Checking for '$root_path' in '$path' (file $file)\n"; | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | ( $path =~ /\A\Q$root_path\E/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | or $self->croak_msg("Text::MicroMason::TemplateDir: Template '$path' not in required base path '$root_path'"); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 28 | return $self->NEXT('read_file', $file ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ###################################################################### | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 7 | 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | ###################################################################### | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Text::MicroMason::TemplateDir - Use Base Directory and Relative Paths | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Instead of using this class directly, pass its name to be mixed in: | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | use Text::MicroMason; | ||||||||||||||||||||||||||||
| 79 | my $mason = Text::MicroMason->new( -TemplateDir, template_root=>'/foo' ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Use the standard compile and execute methods to parse and evaluate templates: | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | print $mason->compile( file=>$filepath )->( 'name'=>'Dave' ); | ||||||||||||||||||||||||||||
| 84 | print $mason->execute( file=>$filepath, 'name'=>'Dave' ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Templates stored in files are looked up relative to the template root: | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | print $mason->execute( file=>"includes/greeting.msn", 'name'=>'Charles'); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | When including other files into a template you can use relative paths: | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | <& ../includes/greeting.msn, name => 'Alice' &> | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | This module changes the resolution of files passed to compile() and execute() to be relative to a base directory path or to the currently executing template. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 Supported Attributes | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =over 4 | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item template_root | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Base directory from which to find templates. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item strict_root | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Optional directory beyond which not to read files. If set to 1, uses | ||||||||||||||||||||||||||||
| 111 | template_root, Causes read_file to croak if any filename outside of the | ||||||||||||||||||||||||||||
| 112 | root is provided. (Note that this is not a chroot jail and only affects | ||||||||||||||||||||||||||||
| 113 | attempts to load a file as a template; for greater security see the | ||||||||||||||||||||||||||||
| 114 | chroot() builtin and L<Text::MicroMason::Safe>.) | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =back | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 Private Methods | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =over 4 | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item prepare | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Intercepts uses of file templates and applies the base-path adjustment. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item read_file | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Intercepts file access to check for strict_root. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =back | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 EXCEPTIONS | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | The following additional exceptions are generated by | ||||||||||||||||||||||||||||
| 135 | Text::MicroMason::TemplateDir when appropriate: | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =over 4 | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item * | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Text::MicroMason::TemplateDir: Strict root '%s' doesn't seem to exist | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | The strict_root directory (or template_root if strict_root is '1') | ||||||||||||||||||||||||||||
| 144 | doesn't seem to exist. Strict root checking uses Cwd's abs_path(), and | ||||||||||||||||||||||||||||
| 145 | requires the strict_root directory to exist at the time the check is | ||||||||||||||||||||||||||||
| 146 | performed. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item * | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Text::MicroMason::TemplatePath: Template '%s' not in required base path '%s' | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | The template found in the configured template path was not within the | ||||||||||||||||||||||||||||
| 153 | configured strict_root directory. This may be caused by requesting an | ||||||||||||||||||||||||||||
| 154 | absolute template filename not within strict_root, or by specifying a | ||||||||||||||||||||||||||||
| 155 | strict_root which does not match the configured template path. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =back | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | For an overview of this templating framework, see L<Text::MicroMason>. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | This is a mixin class intended for use with L<Text::MicroMason::Base>. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | For distribution, installation, support, copyright and license | ||||||||||||||||||||||||||||
| 166 | information, see L<Text::MicroMason::Docs::ReadMe>. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||