blib/lib/Mojolicious/Plugin/Mason1Renderer.pm

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