blib/lib/Mojolicious/Plugin/Mason2Renderer.pm

filter: Statementstmt 41.2% 26 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 80.0% 8 / 10 Podpod 100% 1 / 1 totaltotal 32.7% 35 / 107 CC 15 SCAR 42.1
CC
15
Cov
29%
register
132.0
__ANON__
30.0
BEGIN
1.0
CRAP 67.1
↑ Summary
1 package Mojolicious::Plugin::Mason2Renderer;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Mason;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mason::Interp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Mason::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Mason::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Mojolicious::Plugin::Mason2Renderer - Mason 2 (aka Mason 2.x) Renderer Plugin.
17  
18 =head1 VERSION
19  
20 Version 0.03
21  
22 =cut
23  
24 2 our $VERSION = '0.03';
25  
26 =head1 SYNOPSIS
27  
28 ## Mojolicious::Lite
29  
30 # example -1-
31 use Mojolicious::Lite;
32 plugin 'mason2_renderer';
33 get '/' => sub {
34 my $self = shift;
35 $self->render('/index', handler => "mason" );
36 };
37 app->start;
38  
39 # template: MOJO_HOME/mason/index.mc
40 <html>
41 <body>Welcome</body>
42 </html>
43  
44 # example -2-
45 use Mojolicious::Lite;
46 plugin 'mason2_renderer' => { preload_regexps => [ '.mc$', '/path/to/comps/to/preload', ... ],
47 interp_params => { comp_root => "/path/to/mason/comps",
48 ... (other parameters to the new() Mason::Interp constructor)
49 },
50 request_params => { ... (other parameters to the new() Mason::Request constructor)
51 },
52 };
53 get '/' => sub {
54 my $self = shift;
55 $self->render('/index', handler => "mason", mytext => "Hello world" );
56 };
57 app->start;
58  
59 # template: /path/to/mason/comps/index.mc
60 <%args>
61 $mytext => undef
62 </%args>
63 <html>
64 <body>Welcome : <% $mytext %></body>
65 </html>
66  
67  
68 ## Mojolicious
69  
70 # example -1-
71 package MyApp;
72 use Mojo::Base 'Mojolicious';
73  
74 sub startup {
75 my $self = shift;
76 $self->plugin('mason2_renderer');
77 $self->routes->get('/' => sub {
78 my $self = shift;
79 $self->render('/index', handler => "mason" );
80 }
81 );
82 }
83 1;
84  
85 # template: MOJO_HOME/mason/index.mc
86 <html>
87 <body>Welcome</body>
88 </html>
89  
90 # example -2-
91 package MyApp;
92 use Mojo::Base 'Mojolicious';
93  
94 sub startup {
95 my $self = shift;
96 $self->plugin('mason2_renderer', { preload_regexps => [ '.mc$', '/path/to/comps/to/preload', ... ],
97 interp_params => { comp_root => "/path/to/mason/comps",
98 ... (other parameters to the new() Mason::Interp constructor)
99 },
100 request_params => { ... (other parameters to the new() Mason::Request constructor)
101 },
102 }
103 );
104 $self->routes->get('/' => sub {
105 my $self = shift;
106 $self->render('/index', handler => "mason", mytext => "Hello World" );
107 }
108 );
109 }
110 1;
111  
112 # template: /path/to/mason/comps/index.mc
113 <%args>
114 $mytext => undef
115 </%args>
116 <html>
117 <body>
118 Welcome : <% $mytext %><br/>
119 Mason root_comp is <% $c->app->home %><br/>
120 </body>
121 </html>
122  
123 =head1 DESCRIPTION
124  
125 L<Mojolicous::Plugin::Mason2Renderer> is a renderer for L<Mason> 2 (aka L<Mason> 2.x) template system.
126  
127 =head2 Mojolicious::Controller object aka. $c
128  
129 Mason templates have access to the L<Mojolicious::Controller> object as global $c.
130  
131 =head2 Mason comp_root
132  
133 C<comp_root> is set to default "MOJO_HOME/mason"
134  
135  
136 =head1 METHODS
137  
138 L<Mojolicious::Plugin::Mason2Renderer> inherits all methods from L<Mojolicious::Plugin> and implements the following new ones.
139  
140 =head2 register
141  
142 $plugin->register;
143  
144 Register renderer in L<Mojolicious> application.
145  
146 =cut
147  
148 0 ▶sub register {
Subroutine register: not called CC=11 SCAR=48.8
Pod documented
149 0 my ($self, $app, $conf) = @_;
150  
151 # Config
152 0 ▶ $conf ||= {};
Condition0%0/2
$conf ||= {}
l!l
00
MC/DC0%0/1
$conf ||= {}
$conf
Truth table0%0/2
A
A= $conf
Aresult
11
00
153  
154 # Mason::Interp params
155 0 ▶ $conf->{interp_params}->{comp_root} ||= $app->home->rel_dir('mason');
Condition0%0/3
$conf->{"interp_params"}{"comp_root"} ||= $app->home->rel_dir("mason")
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{"interp_params"}{"comp_root"} ||= $app->home->rel_dir("mason")
$conf->{"interp_params"}{"comp_root"} $app->home->rel_dir("mason")
Truth table0%0/3
A ||= B
A= $conf->{"interp_params"}{"comp_root"}B= $app->home->rel_dir("mason")
ABresult
1X1
011
000
156
157 0 ▶ my @allow_globals = ('$c', @{$conf->{interp_params}->{allow_globals} || []});
Condition0%0/2
$conf->{'interp_params'}{'allow_globals'} || []
l!l
00
MC/DC0%0/1
$conf->{'interp_params'}{'allow_globals'} || []
$conf->{'interp_params'}{'allow_globals'}
Truth table0%0/2
A
A= $conf->{'interp_params'}{'allow_globals'}
Aresult
11
00
158 0 $conf->{interp_params}->{allow_globals} = \@allow_globals;
159  
160  
161 # make Mason::Interp
162 0 my $interp = Mason->new(%{$conf->{interp_params}});
163  
164  
165 # preload comps ?
166 0 ▶ if( (defined $conf->{preload_regexps}) && (ref($conf->{preload_regexps}) eq "ARRAY") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"preload_regexps"} and ref $conf->{"preload_regexps"} eq "ARRAY") 0 0 0
Condition0%0/3
defined $conf->{"preload_regexps"} and ref $conf->{"preload_regexps"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conf->{"preload_regexps"} and ref $conf->{"preload_regexps"} eq "ARRAY"
defined $conf->{"preload_regexps"} ref $conf->{"preload_regexps"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $conf->{"preload_regexps"}B= ref $conf->{"preload_regexps"} eq "ARRAY"
ABresult
0X0
100
111
167 0 foreach my $regexp (@{$conf->{preload_regexps}}) {
168 0 foreach my $comp ($interp->all_paths) {
169 0 ▶ next if($comp=~/.+~$/);
Branch: 1 branch
BranchTrueFalseTotal
if $comp =~ /.+~$/u 0 0 0
170 0 ▶ $interp->load($comp) if($comp =~ /$regexp/);
Branch: 1 branch
BranchTrueFalseTotal
if $comp =~ /$regexp/u 0 0 0
171 }
172 }
173 }
174  
175  
176 # Mason::Request params
177 0 ▶ $conf->{request_params} ||= {};
Condition0%0/2
$conf->{"request_params"} ||= {}
l!l
00
MC/DC0%0/1
$conf->{"request_params"} ||= {}
$conf->{"request_params"}
Truth table0%0/2
A
A= $conf->{"request_params"}
Aresult
11
00
178  
179 0 my $request_params = $conf->{request_params};
180  
181  
182 # Add "mason" handler
183 $app->renderer->add_handler(
184 0 ▶ mason => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
185 0 my ($r, $c, $output, $options) = @_;
186  
187 # check $interp object
188 0 ▶ if(not $interp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($interp) 0 0 0
189 0 $c->app->log->error("Mason::Interp not initialized");
190 0 $c->render_exception("Mason::Interp not initialized");
191 0 $$output = "";
192 0 return 0;
193 }
194  
195 # stash contains args to pass to Mason
196 0 my $stash = $c->stash;
197  
198 # template name
199 0 ▶ return 0 unless my $template = $options->{template};
Branch: 1 branch
BranchTrueFalseTotal
unless my $template = $options->{"template"} 0 0 0
200 0 $template =~ s,^/*,/,; # Mason component must start with /
201  
202 # set global "$c" in Mason environment
203 0 $interp->set_global('c' => $c);
204  
205  
206 # call Mason interpreter
207 0 $request_params->{out_method} = $output;
208 0 my $result = $interp->run( $request_params, $template, %{$stash});
209  
210 0 ▶ if(not $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
211 0 $c->app->log->error("Mason::Request not initialized");
212 0 $c->render_exception("Mason::Request not initialized");
213 0 $$output = "";
214 0 return 0;
215 }
216
217  
218 # Encoding
219 0 ▶ $$output = decode($r->encoding, $$output) if $r->encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $r->encoding 0 0 0
220  
221  
222 # All seems OK
223 0 return 1;
224 }
225 0 );
226  
227 }
228  
229 =head1 SEE ALSO
230  
231 L<Mojolicious>, L<Mojolicious::Plugin>, L<http://mojolicio.us>.
232  
233 Mason 1, L<HTML::Mason>, L<http://www.masonhq.com>.
234  
235 Mason 2, L<Mason>.
236  
237 Mason 1 Mojolicious Plugin, L<Mojolicious::Plugin::Mason1Renderer>
238  
239 =head1 AUTHOR
240  
241 Alexandre SIMON, C<< <asimon at cpan.org> >>
242  
243 =head1 BUGS and SUPPORT
244  
245 You can find documentation for this module with the perldoc command.
246  
247 perldoc Mojolicious::Plugin::Mason2Renderer
248  
249  
250 Bugs and feature requests will be tracked at RT:
251  
252 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-Mason2Renderer
253 bug-mojolicious-plugin-mason2renderer at rt.cpan.org
254  
255 The latest source code can be browsed and fetched at:
256  
257 https://github.com/igit/Mojolicious-Plugin-Mason2Renderer
258 git clone git://github.com/igit/Mojolicious-Plugin-Mason2Renderer.git
259  
260  
261 You can also look for information at:
262  
263 =over 4
264  
265 =item * RT: CPAN's request tracker
266  
267 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-Mason2Renderer>
268  
269 =item * AnnoCPAN: Annotated CPAN documentation
270  
271 L<http://annocpan.org/dist/Mojolicious-Plugin-Mason2Renderer>
272  
273 =item * CPAN Ratings
274  
275 L<http://cpanratings.perl.org/d/Mojolicious-Plugin-Mason2Renderer>
276  
277 =item * Search CPAN
278  
279 L<http://search.cpan.org/dist/Mojolicious-Plugin-Mason2Renderer/>
280  
281 =back
282  
283  
284 =head1 ACKNOWLEDGEMENTS
285  
286 Original idea was taken from Graham BARR (L<http://search.cpan.org/~gbarr/>)
287 MojoX::Renderer::Mason module. This module was not longer adapted to Mojolicious new
288 Plugin philosophy.
289  
290 Many, many thanks to Sebastian RIEDEL for developping Mojolicious and Jonathan SWARTZ
291 for developping HTML::Mason and Mason (2).
292  
293 =head1 LICENSE AND COPYRIGHT
294  
295 Copyright 2011 Alexandre SIMON.
296  
297 This program is free software; you can redistribute it and/or modify it
298 under the terms of either: the GNU General Public License as published
299 by the Free Software Foundation; or the Artistic License.
300  
301 See http://dev.perl.org/licenses/ for more information.
302  
303  
304 =cut
305  
306 2 1; # End of Mojolicious::Plugin::Mason2Renderer
↑ Summary