blib/lib/DBIx/DR/PerlishTemplate.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 302 SCAR 67.0
CC
28
Cov
0%
_parse_ep
72.0
_render
42.0
_parse
30.0
CRAP 812.0
1 use utf8;
2 use strict;
3 use warnings;
4  
5 package DBIx::DR::PerlishTemplate;
6 use Mouse;
7 use Carp;
8 use Scalar::Util;
9 use DBIx::DR::ByteStream;
10  
11 has line_tag => (is => 'rw', isa => 'Str', default => '%');
12 has open_tag => (is => 'rw', isa => 'Str', default => '<%');
13 has close_tag => (is => 'rw', isa => 'Str', default => '%>');
14 has quote_mark => (is => 'rw', isa => 'Str', default => '=');
15 has immediate_mark => (is => 'rw', isa => 'Str', default => '==');
16  
17 has sql => (is => 'ro', isa => 'Str', default => '');
18 has variables => (is => 'ro', isa => 'ArrayRef');
19  
20 has template => (is => 'rw', isa => 'Str', default => '');
21 has template_file => (is => 'rw', isa => 'Str', default => '');
22  
23 has stashes => (is => 'ro', isa => 'ArrayRef');
24 has pretokens => (is => 'ro', isa => 'ArrayRef');
25 has prepretokens => (is => 'ro', isa => 'ArrayRef');
26 has parsed_template => (is => 'ro', isa => 'Str', default => '');
27 has namespace => (is => 'rw', isa => 'Str',
28 default => 'DBIx::DR::PerlishTemplate::Sandbox');
29  
30  
31 has sql_utf8 => (is => 'ro', isa => 'Bool', default => 1);
32 sub _render {
33 my ($_PTPL) = @_;
34 my $_PTSUB;
35  
36 unless ($_PTPL->parsed_template) {
37 $_PTSUB = $_PTPL->{parsed_template} = $_PTPL->_parse;
38 } else {
39 $_PTSUB = $_PTPL->parsed_template;
40 }
41  
42 $_PTPL->{parsed_template} = $_PTSUB;
43  
44 my $esub = eval $_PTSUB;
45 if (my $e = $@) {
46 my $do_croak;
47 my $template;
48 if ($_PTPL->template_file) {
49 $template = $_PTPL->template_file;
50 } else {
51 $do_croak = 1;
52 $template = 'inline template';
53 };
54 $e =~ s{ at .*?line (\d+)(\.\s*|,\s+.*?)?$}
55 [" at $template line " . ( $1 - $_PTPL->pre_lines )]gsme;
56  
57 if ($1) {
58 $e =~ s/\s*$/\n/g;
59 die $e unless $do_croak;
60 croak $e;
61 }
62  
63 croak "$e at $template";
64 }
65  
66 $_PTPL->{sql} = '';
67 $_PTPL->{variables} = [];
68  
69 $esub->( @{ $_PTPL->stashes } );
70 1;
71 }
72  
73 sub render {
74 my ($self, $tpl, @args) = @_;
75 $self->{parsed_template} = '';
76 $self->template($tpl);
77 $self->template_file('');
78 $self->{stashes} = \@args;
79 $self->clean_namespace;
80 return $self->_render;
81 }
82  
83 sub render_file {
84 my ($self, $file, @args) = @_;
85 croak "File '@{[ $file // 'undef' ]}' not found or readable"
86 unless -r $file;
87 open my $fh, '<:raw', $file;
88 my $data;
89  
90 { local $/; $data = <$fh> }
91  
92 utf8::decode $data if $self->sql_utf8;
93  
94 $self->{parsed_template} = '';
95 $self->template_file($file);
96 $self->template($data);
97 $self->{stashes} = \@args;
98 $self->clean_namespace;
99 return $self->_render;
100 }
101  
102 sub clean_prepends {
103 my ($self) = @_;
104 $self->{pretokens} = [];
105 $self;
106 }
107  
108 sub clean_preprepends {
109 my ($self) = @_;
110 $self->{prepretokens} = [];
111 $self;
112 }
113  
114  
115 sub immediate {
116 my ($self, $str) = @_;
117 if (Scalar::Util::blessed $str) {
118 if ('DBIx::DR::ByteStream' eq Scalar::Util::blessed $str) {
119 $self->{sql} .= $str->content;
120 } elsif ($str->can('content')) {
121 $self->{sql} .= $str->content;
122 } else {
123 croak "Can't extract content from " . Scalar::Util::blessed $str;
124 }
125 } else {
126 $self->{sql} .= $str;
127 }
128 return DBIx::DR::ByteStream->new('');
129 }
130  
131 sub add_bind_value {
132 my ($self, @values) = @_;
133 push @{ $self->variables } => @values;
134 }
135  
136  
137 sub quote {
138 my ($self, $variable) = @_;
139  
140 if (Scalar::Util::blessed $variable) {
141 return $self->immediate($variable)
142 if 'DBIx::DR::ByteStream' eq Scalar::Util::blessed $variable;
143 }
144  
145 $self->{sql} .= '?';
146 $self->add_bind_value($variable);
147 return DBIx::DR::ByteStream->new('');
148 }
149  
150 sub _parse {
151 my ($self) = @_;
152  
153 my $result = '';
154  
155 my $immediate_mark = $self->immediate_mark;
156 my $quote_mark = $self->quote_mark;
157  
158 my $code_cb = sub {
159 my ($t) = @_;
160 return unless defined $t and length $t;
161  
162 if ($t =~ /^\Q$immediate_mark\E/) {
163 $result .= join '',
164 'immediate(',
165 substr($t, length($immediate_mark)),
166 ');';
167 return;
168 }
169  
170 if ($t =~ /^\Q$quote_mark\E/) {
171 $result .= join '',
172 'quote(',
173 substr($t, length($quote_mark)),
174 ');';
175 return;
176 }
177  
178 $result .= "$t;"; # always place ';' at end of code.
179 };
180  
181 my $text_cb = sub {
182 my ($content) = @_;
183 return unless defined $content and length $content;
184 $content =~ s/'/\\'/g;
185 $result .= "immediate('" . $content . "');";
186 };
187  
188 $self->_parse_ep($self->template, $text_cb, $code_cb);
189  
190 $result = join '',
191 'package ', $self->namespace, ';',
192 'BEGIN { ',
193 '*quote = sub { $_PTPL->quote(@_) };',
194 '*immediate = sub { $_PTPL->immediate(@_) };',
195 '};',
196 $self->preprepend,
197 'sub {', $self->prepend, $result, "\n}";
198  
199 return $result;
200 }
201  
202 sub _parse_ep {
203  
204 my ($self, $tpl, $text_cb, $code_cb) = @_;
205  
206 #---------------------------------------------------------
207 # единственные три переменные из self
208 my $line_tag = $self->line_tag;
209 my $open_tag = $self->open_tag;
210 my $close_tag = $self->close_tag;
211 # по идее это можно было оформить в виде независимого кода
212 #---------------------------------------------------------
213  
214 my @lines = split /\n/, $tpl;
215  
216 my $st = 'text';
217 my $code_text;
218  
219 for (my $i = 0; $i < @lines; $i++) {
220 local $_ = $lines[$i];
221  
222 CODE:
223 if ($st eq 'code') {
224 if (/^(.*?)\Q$close_tag\E(.*)/) {
225 $_ = $2;
226 $code_cb->($code_text . $1);
227 $code_text = undef;
228 $st = 'text';
229 goto ANYTEXT;
230 } else {
231 $code_text .= $_;
232 $code_text .= "\n";
233 next;
234 }
235 }
236  
237 TEXT_BEGIN:
238 if (/^(\s*)\Q$line_tag\E(.*)/) {
239 $text_cb->($1);
240 if ($i < $#lines) {
241 $code_cb->("$2\n");
242 } else {
243 $code_cb->($2);
244 }
245 next;
246 }
247  
248 ANYTEXT:
249 if (/^(.*?)\Q$open_tag\E(.*)/) {
250 $_ = $2;
251 $text_cb->($1);
252 $code_text = '';
253 $st = 'code';
254 goto CODE;
255 } else {
256 $text_cb->($_);
257 $text_cb->("\n") if $i < $#lines;
258 next;
259 }
260 }
261 $text_cb->("<%" . $code_text) if defined $code_text and length $code_text;
262 }
263  
264  
265 sub preprepend {
266 my ($self, @tokens) = @_;
267 $self->{prepretokens} ||= [];
268 push @{ $self->prepretokens } => map "$_;\n", @tokens if @tokens;
269 return join '' => @{ $self->prepretokens } if defined wantarray;
270 }
271  
272 sub prepend {
273 my ($self, @tokens) = @_;
274 $self->{pretokens} ||= [];
275 push @{ $self->pretokens } => map "$_;", @tokens if @tokens;
276 return join '' => @{ $self->pretokens } if defined wantarray;
277 }
278  
279  
280 sub pre_lines {
281 my ($self) = @_;
282 my $lines = 0;
283 $lines += @{[ /\n/g ]} for ($self->preprepend, $self->prepend);
284 return $lines;
285 }
286  
287 sub clean_prepend {
288 my ($self) = shift;
289 $self->{pretokens} = [];
290 }
291  
292 sub clean_namespace {
293 my ($self) = @_;
294 my $sb = $self->namespace;
295  
296 no strict 'refs';
297 undef *{$sb . '::' . $_} for keys %{ $sb . '::' };
298 }
299  
300 1;
301  
302 =head1 NAME
303  
304 DBIx::DR::PerlishTemplate - template engine for L<DBIx::DR>.
305  
306 =head1 COPYRIGHT
307  
308 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
309 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
310  
311 This program is free software, you can redistribute it and/or
312 modify it under the terms of the Artistic License.
313  
314 =cut
315  
316  
317  
318  
319