lib/CatalystX/CMS/View.pm

filter: Statementstmt 93.5% 101 / 108 Branchbran 52.6% 20 / 38 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 79.0% 147 / 186 CC 29 SCAR 23.9
CC
29
Cov
80%
template_vars
18.0
process
15.1
new
6.3
CRAP 11.0
« lib/CatalystX/CMS/Action.pm ↑ Summary lib/CatalystX/CMS/Controller.pm »
1 package CatalystX::CMS::View;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw( Catalyst::View::TT );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use CatalystX::CMS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Template::Plugin::Handy 'install';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.011';
16  
17 2 my $DEBUG = 0;
18  
19 2 __PACKAGE__->config(
20 WRAPPER => 'cms/wrapper.tt',
21  
22 # turn .tt caching off so that we force read on every req.
23 # with caching on (default) occasional errors if the
24 # .tt is saved to disk and then immediately requested,
25 # because the cached version is being read instead of from disk.
26 CACHE_SIZE => 0,
27 );
28  
29 2 __PACKAGE__->mk_accessors(qw( cms_template_base ));
30  
31 =head1 NAME
32  
33 CatalystX::CMS::View - base View class
34  
35 =head1 SYNOPSIS
36  
37 package MyApp::View::CMS;
38 use strict;
39 use base qw( CatalystX::CMS::View );
40 1;
41
42 =head1 DESCRIPTION
43  
44 CatalystX::CMS::View isa Catalyst::View::TT class that handles
45 dynamic include path generation and other CMS-related features.
46  
47 =head1 VIRTUAL METHODS
48  
49 CatalystX::CMS::View uses the Template::Plugin::Handy module to install
50 additional vmethods in the global Template namespace.
51
52 =head1 METHODS
53  
54 Only new or overridden method are documented here.
55  
56 =cut
57  
58 =head2 new
59  
60 Overrides new() method to set INCLUDE_PATH to include
61 all the paths in main app cms B<root> config.
62  
63 =cut
64  
65 1 ▶sub new {
Subroutine new: called CC=6 SCAR=18.4
Pod documented
66 1 my ( $class, $c, $arg ) = @_;
67  
68 1 my $template_base = Class::Inspector->loaded_filename('CatalystX::CMS');
69 1 $template_base =~ s/\.pm$//;
70  
71 1 my $default_tt = Path::Class::dir( $template_base, 'tt' );
72  
73 # put the CMS app installed templates last so they can be
74 # locally overridden
75 # and then whatever is already set.
76 # *AND* no duplicates.
77 my @paths = (
78 Path::Class::dir( $c->config->{root} ),
79 1 @{ $c->config->{cms}->{root}->{rw} },
80 1 @{ $c->config->{cms}->{root}->{r} }, $default_tt,
81 );
82 1 my ( %uniq, @inc_path );
83 1 ▶ for ( @paths, @{ $class->config->{INCLUDE_PATH} || [] } ) {
Condition50%1/2
$class->config->{'INCLUDE_PATH'} || []
l!l
01
MC/DC0%0/1
$class->config->{'INCLUDE_PATH'} || []
$class->config->{'INCLUDE_PATH'}
Truth table50%1/2
A
A= $class->config->{'INCLUDE_PATH'}
Aresult
11
00
84 4 ▶ push( @inc_path, $_ ) unless $uniq{"$_"}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $uniq{"$_"}++ 3 1 4
85 }
86  
87 1 ▶ if ( $c->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug) 0 1 1
88 0 my $t = Text::SimpleTable->new(74);
89 0 $t->row("$_") for @inc_path;
90 0 $c->log->debug( "CMS View inc path:\n" . $t->draw . "\n" );
91 }
92  
93 1 $class->config( { INCLUDE_PATH => \@inc_path } );
94  
95 1 my $self = $class->next::method( $c, $arg );
96  
97 1 $self->cms_template_base($default_tt);
98  
99 1 return $self;
100 }
101  
102 =head2 process
103  
104 Overrides base method to test cmspage in stash and mangle
105 the C<template> value if it isa CatalystX::CMS::Page object.
106  
107 =cut
108  
109 2 ▶sub process {
Subroutine process: called CC=10 SCAR=27.2
Pod documented
110 2 my ( $self, $c ) = @_;
111 2 my $t = $c->stash->{template};
112 2 ▶ $c->log->debug("template = $t") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 2 2
113 2 ▶ if ( blessed($t)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($t) and $t->isa("CatalystX::CMS::Page")) 2 0 2
Condition33%1/3
builtin::blessed($t) and $t->isa("CatalystX::CMS::Page")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($t) and $t->isa("CatalystX::CMS::Page")
builtin::blessed($t) $t->isa("CatalystX::CMS::Page")
Truth table33%1/3
A and B
A= builtin::blessed($t)B= $t->isa("CatalystX::CMS::Page")
ABresult
0X0
100
111
114 and $t->isa('CatalystX::CMS::Page') )
115 {
116 2 my $file = $t->file . $t->ext;
117  
118 2 ▶ if ( -s $t->delegate ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $t->delegate) { } 2 0 2
119 2 ▶ $c->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 2 2
120 "resetting template to $file [" . $t->delegate . ']' )
121 if $c->debug;
122 2 $c->stash( template => $file );
123 2 ▶ unless ( exists $c->stash->{cmspage} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $c->stash->{"cmspage"}) 2 0 2
124 2 $c->stash( cmspage => $t );
125 }
126 }
127 else {
128 0 ▶ $c->log->debug( "setting new_file = " . $t->file ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
129 0 $c->stash( new_file => $t->file, template => 'cms/new_file.tt' );
130 }
131 }
132  
133 2 ▶ $DEBUG and warn "View -> process next::method";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "View -> process next::method" 0 2 2
134  
135 2 my $ret = $self->next::method($c);
136  
137 2 ▶ $DEBUG and warn "View -> process complete";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "View -> process complete" 0 2 2
138  
139 2 return $ret;
140 }
141  
142 =head2 template_vars
143  
144 Override base method to set additional vars for render(), including:
145  
146 =over
147  
148 =item
149  
150 CMS
151  
152 =item
153  
154 additional_template_paths
155  
156 =back
157  
158 =cut
159  
160 3 ▶sub template_vars {
Subroutine template_vars: called CC=15 SCAR=28.9
Pod documented
161 3 my ( $self, $c ) = @_;
162  
163 3 my $cvar = $self->config->{CATALYST_VAR};
164 3 ▶ my $cms_mode = $c->stash->{cms_mode} || 0;
Condition100%2/2
$c->stash->{'cms_mode'} || 0
l!l
12
MC/DC100%1/1
$c->stash->{'cms_mode'} || 0
$c->stash->{'cms_mode'}
Truth table100%2/2
A
A= $c->stash->{'cms_mode'}
Aresult
11
00
165 3 my $conf = $c->config->{cms};
166 3 my $cmspage = $c->stash->{cmspage};
167 3 my @inc;
168  
169 # There are two parts to setting up the wrapper paths.
170 # (1) the cmspage.type, which defaults to 'html' (think MIME)
171 # (2) the cmspage.flavour, which defaults to 'default' (think skins)
172 3 ▶ my $type = $conf->{default_type} || 'html';
Condition50%1/2
$conf->{'default_type'} || 'html'
l!l
03
MC/DC0%0/1
$conf->{'default_type'} || 'html'
$conf->{'default_type'}
Truth table50%1/2
A
A= $conf->{'default_type'}
Aresult
11
00
173 3 ▶ my $flavour = $conf->{default_flavour} || 'default';
Condition50%1/2
$conf->{'default_flavour'} || 'default'
l!l
03
MC/DC0%0/1
$conf->{'default_flavour'} || 'default'
$conf->{'default_flavour'}
Truth table50%1/2
A
A= $conf->{'default_flavour'}
Aresult
11
00
174 3 ▶ if ($cmspage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmspage) 3 0 3
175  
176 #warn dump $cmspage;
177  
178 3 my $attrs = $cmspage->attrs;
179 3 ▶ $type = exists $attrs->{type} ? $attrs->{type} : $type;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'type'} ? : 0 3 3
180 3 ▶ $flavour = exists $attrs->{flavour} ? $attrs->{flavour} : $flavour;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'flavour'} ? : 0 3 3
181  
182 3 my %uniq;
183 3 for my $dir ( @{ $conf->{root}->{rw} }, @{ $conf->{root}->{r} } ) {
184 6 my $path = Path::Class::dir( $dir, $type, $flavour ) . '';
185 6 $uniq{$path}++;
186 6 push( @inc, $path );
187 }
188 3 my $cmspage_dir = $cmspage->dir . '';
189 3 ▶ push( @inc, $cmspage_dir ) unless exists $uniq{$cmspage_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $uniq{$cmspage_dir} 0 3 3
190 }
191  
192 # in cms mode, always use that flavour for wrappers
193 3 my $wrapper_flav = $flavour;
194 3 ▶ if ($cms_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cms_mode) 1 2 3
195 1 $wrapper_flav = 'cms';
196 }
197  
198 3 my @path = ( 'cms', 'wrappers', $type, $wrapper_flav );
199 my $CMS = {
200 css => [],
201 js => [],
202 mode => $cms_mode,
203 3 ▶ static => ( $c->config->{static_url} || $c->uri_for('/static') ),
Condition33%1/3
$c->config->{'static_url'} || $c->uri_for('/static')
l!l&&r!l&&!r
030
MC/DC0%0/2
$c->config->{'static_url'} || $c->uri_for('/static')
$c->config->{'static_url'} $c->uri_for('/static')
Truth table33%1/3
A || B
A= $c->config->{'static_url'}B= $c->uri_for('/static')
ABresult
1X1
011
000
204 wrappers => {
205 type => $type,
206 flavour => $flavour,
207 header => Path::Class::file( @path, 'header.tt' ),
208 footer => Path::Class::file( @path, 'footer.tt' ),
209 body => Path::Class::file( @path, 'body.tt' ),
210 wrapper => Path::Class::file( @path, 'wrapper.tt' ),
211 base => Path::Class::dir(@path),
212 },
213 };
214  
215 3 ▶ if ( $c->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug) 0 3 3
216 0 $c->log->debug( "View extra inc path: " . dump \@inc );
217 0 $c->log->debug("cms_mode = $cms_mode");
218  
219 #$c->log->debug( "CMS var = " . dump $CMS );
220 }
221  
222 3 ▶ $DEBUG and warn "View -> template_vars set";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "View -> template_vars set" 0 3 3
223 3 ▶ $DEBUG and warn "View: " . dump($CMS);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn 'View: ' . CatalystX::CMS::View::dump($CMS) 0 3 3
224  
225 defined $cvar
226 ? ( $cvar => $c )
227 : (
228 c => $c,
229 base => $c->req->base,
230 name => $c->config->{name},
231 3 ▶ CMS => $CMS,
Branch: 1 branch
BranchTrueFalseTotal
defined $cvar ? : 0 3 3
232 additional_template_paths => \@inc,
233 );
234 }
235  
236 2 1;
237  
238 __END__
« lib/CatalystX/CMS/Action.pm ↑ Summary lib/CatalystX/CMS/Controller.pm »