Compare commits

...
Author SHA1 Message Date
Graham Ollis 20d795cb51 Compatibility with mojo 3.33 2012-08-23 22:20:08 -04:00
Graham Ollis 7455ae1be8 v1.23 2012-08-23 15:27:07 -04:00
Graham Ollis 870d195761 use die instead of render_exception 2012-08-23 15:26:38 -04:00
Graham Ollis b21db5cf22 v1.22 2012-08-21 20:39:02 -04:00
Graham Ollis 24990483e5 avoid deep recursion
when using a tt template for exceptions, and there is
an exception in the template, don't go into infinite
recursion.
2012-08-21 16:30:19 -04:00
Graham Ollis 4f5d3a0951 update Changes 2012-08-21 15:47:33 -04:00
Graham Ollis a72ec0e750 ignore cycles that are taken care of in Template::Provider#DESTROY 2012-08-21 15:44:33 -04:00
Graham Ollis 2956335d9e use Test::Memory::Cycle instead of find_cycle 2012-08-21 14:55:28 -04:00
Graham Ollis 7d1ac4eecd tt is private. 2012-08-21 14:49:58 -04:00
Graham Ollis 28ae3d62f8 return 1 on failure 2012-08-21 15:44:10 -03:00
6 changed files with 109 additions and 23 deletions
+11
View File
@@ -1,6 +1,17 @@
Revision history for Mojolicious::Plugin::TtRenderer
{{$NEXT}}
- Compatibility with Mojolicious 3.33+
1.23 August 23, 2012
- Use die instead of render_exception (GH#29)
1.22 August 21, 2012
- Compatibility with Mojolicious 3.05+ (GH#24)
- Avoid deep recursion when exception template dies (GH#25,GH26)
- Fixed test failures (GH#27,GH#28)
1.21 June 5, 2012
- Fixed double TT rendering on error (Matthias Bethke (GH#21))
- Cache templates in tmpdir by default (Marcus Ramberg)
- Require Mojolicious 2.51 to avoid memory leaks (GH#19)
+1 -2
View File
@@ -6,7 +6,7 @@ copyright_holder = Ask Bjørn Hansen
# copyright_year = 2009
[Prereqs]
Mojolicious = 2.51
Mojolicious = 3.33
Template = 2.18
[Prereqs / TestRequires ]
@@ -39,7 +39,6 @@ version_regexp = ^release/(.*)
[HasVersionTests]
[MetaTests]
[ReadmeFromPod]
[PodCoverageTests]
[Manifest]
[NextRelease]
+25 -14
View File
@@ -2,6 +2,7 @@ package Mojolicious::Plugin::TtRenderer::Engine;
use warnings;
use strict;
use v5.10;
use base 'Mojo::Base';
@@ -30,7 +31,7 @@ sub _init {
my $dir;
my $app = delete $args{mojo} || delete $args{app};
if($dir=$args{cache_dir}) {
if($app && substr($dir,0,1) ne '/') {
$dir=$app->home->rel_dir('tmp/ctpl');
}
@@ -81,20 +82,14 @@ sub _render {
$$output = '';
my @params = ({%{$c->stash}, c => $c, h => $helper}, $output, {binmode => ':utf8'});
$self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0]->ctx($c);
my $provider = $self->tt->{SERVICE}->{CONTEXT}->{LOAD_TEMPLATES}->[0];
$provider->options($options);
$provider->ctx($c);
my $ok = $self->tt->process(defined $inline ? \$inline : $t, @params);
# Error
unless ($ok) {
my $e = Mojo::Exception->new($self->tt->error.'');
$$output = '';
$c->app->log->error(qq/Template error in "$t": $e/);
$c->render_exception($e);
$self->tt->error('');
return 0;
}
die $self->tt->error unless $ok;
return 1;
}
@@ -154,6 +149,7 @@ sub new {
sub renderer { @_ > 1 ? $_[0]->{renderer} = $_[1] : $_[0]->{renderer} }
sub ctx { @_ > 1 ? $_[0]->{ctx} = $_[1] : $_[0]->{ctx} }
sub options { @_ > 1 ? $_[0]->{options} = $_[1] : $_[0]->{options} }
sub _template_modified {1}
@@ -161,17 +157,32 @@ sub _template_content {
my $self = shift;
my ($path) = @_;
my $options = delete $self->{options};
# Convert backslashes to forward slashes to make inline templates work on Windows
$path =~ s/\\/\//g;
my ($t) = ($path =~ m{templates\/(.*)$});
if (-r $path) {
return $self->SUPER::_template_content(@_);
}
# Try DATA section
elsif (my $d = $self->renderer->get_data_template($self->ctx, $t)) {
return wantarray ? ($d, '', time) : $d;
if(defined $options)
{
my $d = $self->renderer->get_data_template($options);
return wantarray ? ($d, '', time) : $d
if $d;
}
else
{
my $loader = Mojo::Loader->new;
foreach my $class (@{ $self->renderer->classes })
{
my $d = $loader->data($class, $t);
return wantarray ? ($d, '', time) : $d
if $d;
}
}
my $data = '';
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env perl
use strict;
use warnings;
#BEGIN { $ENV{MOJO_MODE}='testing'; };
use utf8;
use Test::More tests => 3;
use Mojolicious::Lite;
use Test::Mojo;
# Silence
app->log->level('fatal');
plugin 'tt_renderer';
get '/exception' => sub { die };
#say app->mode;
#app->start;
#exit;
my $t = Test::Mojo->new;
$t->app->renderer->default_handler('tt');
my $deep_recursion = 0;
do {
local $SIG{__WARN__} = sub {
my $warning = shift;
if($warning =~ /Deep recursion/) {
$deep_recursion = 1;
die $warning;
}
};
$t->get_ok('/exception')
->status_is(500);
};
ok !$deep_recursion, 'no deep recursion';
__DATA__
@@ exception.development.html.tt
[% 1 + % %]
@@ exception.html.tt
[% 1 + % %]
@@ exception.testing.html.tt
[% 1 + % %]
+4 -1
View File
@@ -15,4 +15,7 @@ eval "use Pod::Coverage $min_pc";
plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage"
if $@;
all_pod_coverage_ok();
plan tests => 2;
pod_coverage_ok('Mojolicious::Plugin::TtRenderer');
pod_coverage_ok('Mojolicious::Plugin::TtRenderer::Engine', { also_private => [qr{^tt$}] } );
+13 -6
View File
@@ -43,10 +43,17 @@ $t->get_ok('/')->status_is(200)
->content_like(qr/test123456/);
$t->get_ok('/blow')->status_is(500)->content_like(qr/file error - doesnotexist.tt: No such file or directory/);
eval "
use Devel::Cycle 'find_cycle';
find_cycle(app, sub {
ok(0, 'Cycle found');
if(eval q{ use Devel::Cycle; 1 })
{
Devel::Cycle::find_cycle(app, sub {
my $arg = shift;
# Template::Provider (from which M::P::T::Provider inherits) has some cycles which are freed manaully by
# its DESTROY method, so we skip reporting those cycles.
unless(scalar(scalar(grep { ref($_->[2]) eq 'Mojolicious::Plugin::TtRenderer::Provider' && $_->[1] =~ /^(HEAD|TAIL|LOOKUP)$/ } @$arg)) > 0)
{
#use YAML ();
#diag YAML::Dump([ map { [ $_->[0], $_->[1], ref($_->[2]), ref($_->[3]) ] } @$arg ]);
fail('Cycle found')
}
});
";
};