| 1 | package Dancer::Template::TemplateSandbox; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Dancer::FileUtils 'path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use base 'Dancer::Template::Abstract'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | my %_template_config; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub init | ||||||||||||||||||||||||||||
| 15 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=13 SCAR=45.7
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | die "Template::Sandbox is needed by Dancer::Template::TemplateSandbox" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | unless Dancer::ModuleLoader->load( 'Template::Sandbox' ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | %_template_config = ( | ||||||||||||||||||||||||||||
| 22 | open_delimiter => '<%', | ||||||||||||||||||||||||||||
| 23 | close_delimiter => '%>', | ||||||||||||||||||||||||||||
| 24 | template_toolkit_compat => 1, | ||||||||||||||||||||||||||||
| 25 | 1 | %{$self->config}, | |||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | $_template_config{ template_root } = setting( 'views' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | if setting( 'views' ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | if( $_template_config{ cache } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||
| 34 | 0 | my ( $cache_type, $cache_dir ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | $cache_dir = delete $_template_config{ cache_dir } | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if $_template_config{ cache_dir }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | $cache_type = delete $_template_config{ cache_type } | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if $_template_config{ cache_type }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $cache_type ||= 'memory'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$cache_type ||= "memory"
MC/DC0%0/1
$cache_type ||= "memory"
$cache_type
Truth table0%0/2
A
A= $cache_type
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if( $_template_config{ cache } eq 'cache_factory' ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 0 | my ( %options ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | die "Unable to load chosen template caching module: " . | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | "Cache::CacheFactory" | ||||||||||||||||||||||||||||
| 48 | unless Dancer::ModuleLoader->load( 'Cache::CacheFactory' ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if( $cache_type eq 'file' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 0 | %options = ( | |||||||||||||||||||||||||||
| 53 | storage => { 'file' => { cache_root => $cache_dir, }, }, | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else | ||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 0 | %options = ( | |||||||||||||||||||||||||||
| 59 | storage => { $cache_type => {}, }, | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | $_template_config{ cache } = Cache::CacheFactory->new( %options ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif( $_template_config{ cache } eq 'chi' ) | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 0 | my ( %options ); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | die "Unable to load chosen template caching module: CHI" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | unless Dancer::ModuleLoader->load( 'CHI' ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if( $cache_type eq 'file' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 0 | %options = ( | |||||||||||||||||||||||||||
| 75 | driver => 'File', | ||||||||||||||||||||||||||||
| 76 | root_dir => $cache_dir, | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | else | ||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 0 | %options = ( | |||||||||||||||||||||||||||
| 82 | driver => Dancer::ModuleLoader->class_from_setting( | ||||||||||||||||||||||||||||
| 83 | $cache_type ), | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | $_template_config{ cache } = CHI->new( %options ); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else | ||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 0 | die "Unknown template caching module: $_template_config{ cache }"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub render($$$) | ||||||||||||||||||||||||||||
| 97 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 3 | my ( $self, $template, $tokens ) = @_; | |||||||||||||||||||||||||||
| 99 | 3 | my ( $ts ); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | ▶ | die "'$template' is not a regular file" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $template or -f $template
MC/DC100%2/2
ref $template or -f $template
ref $template
-f $template
Truth table100%3/3
A or B
A= ref $templateB= -f $template
| |||||||||||||||||||||||||||||
| 102 | if !ref($template) && (!-f $template); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | $ts = Template::Sandbox->new( %_template_config ); | |||||||||||||||||||||||||||
| 105 | 2 | $ts->add_vars( $tokens ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | if( ref( $template ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | 1 | $ts->set_template_string( ${$template} ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | else | ||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | $template =~ s~^\Q$_template_config{template_root}/\E~~ | ||||||||||||||||||||||||||||
| 114 | 1 | ▶ | if $_template_config{ template_root }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | $ts->set_template( $template ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | my $contentref = $ts->run(); | |||||||||||||||||||||||||||
| 119 | 2 | ▶ | return( $contentref ? ${$contentref} : undef ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | 1; | |||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||