Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
made all helpers significantly faster
  • Loading branch information
kraih committed Aug 12, 2014
1 parent c1eeadc commit d89e688
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/Mojo/Util.pm
Expand Up @@ -418,6 +418,17 @@ sub _stash {
return $object;
}

sub _teardown {
my $class = shift // '';
return unless my ($first, $last) = ($class =~ /^(.*)::(.*)$/);

# @ISA has to be cleared first because of circular references
no strict 'refs';
@{"${class}::ISA"} = ();
%{"${class}::"} = ();
delete ${"${first}::"}{"${last}::"};
}

1;

=encoding utf8
Expand Down
8 changes: 6 additions & 2 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -4,11 +4,15 @@ use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Template;
use Mojo::Util qw(encode md5_sum monkey_patch);

sub DESTROY { Mojo::Util::_teardown($_[0]->{namespace}) }

sub register {
my ($self, $app, $conf) = @_;

# Auto escape by default to prevent XSS attacks
my $template = {auto_escape => 1, %{$conf->{template} || {}}};
$self->{namespace} = $template->{namespace}
//= 'Mojo::Template::Sandbox::' . md5_sum("$self");
# Add "ep" handler and make it the default
$app->renderer->default_handler('ep')->add_handler(
Expand Down Expand Up @@ -53,8 +57,8 @@ sub register {
sub _helpers {
my ($ns, $helpers) = @_;
for my $name (grep {/^\w+$/} keys %$helpers) {
monkey_patch $ns, $name,
sub { $ns->_C->app->renderer->helpers->{$name}->($ns->_C, @_) };
my $sub = $helpers->{$name};
monkey_patch $ns, $name, sub { $ns->_C->$sub(@_) };
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -29,6 +29,8 @@ $HOME->parse(
$HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
my %TEMPLATES = map { $_ => slurp $HOME->rel_file($_) } @{$HOME->list_files};

sub DESTROY { Mojo::Util::_teardown($_) for @{$_[0]->{namespaces}} }

sub accepts {
my ($self, $c) = (shift, shift);

Expand Down Expand Up @@ -81,6 +83,7 @@ sub get_helper {
}

return undef unless $found;
push @{$self->{namespaces}}, $class;
return $self->{proxy}{$name} = sub { bless \shift, $class };
}

Expand Down
14 changes: 14 additions & 0 deletions t/mojolicious/renderer.t
@@ -1,6 +1,7 @@
use Mojo::Base -strict;

use Test::More;
use Mojo::Util 'decode';
use Mojolicious::Controller;

# Partial rendering
Expand Down Expand Up @@ -95,4 +96,17 @@ eval { $first->app->myapp->missing };
like $@, qr/^Can't locate object method "missing" via package "$class"/,
'right error';

# No leaky namespaces
my $helper_class = ref $second->myapp;
is ref $second->myapp, $helper_class, 'same class';
ok $helper_class->can('defaults'), 'helpers are active';
my $template_class = decode 'UTF-8',
$second->render_to_string(inline => "<%= __PACKAGE__ =%>");
is decode('UTF-8', $second->render_to_string(inline => "<%= __PACKAGE__ =%>")),
$template_class, 'same class';
ok $template_class->can('stash'), 'helpers are active';
undef $second;
ok !$helper_class->can('defaults'), 'helpers have been cleaned up';
ok !$template_class->can('stash'), 'helpers have been cleaned up';

done_testing();
4 changes: 2 additions & 2 deletions t/mojolicious/twinkle_lite_app.t
Expand Up @@ -27,7 +27,7 @@ my $twinkle = {
escape_mark => '*',
expression_mark => '*',
line_start => '.',
namespace => 'TwinkleSandBoxTest',
namespace => 'TwinkleSandBox::Test',
prepend => 'my $prepended = $self->config("foo");',
tag_end => '**',
tag_start => '**',
Expand Down Expand Up @@ -80,7 +80,7 @@ my $t = Test::Mojo->new;

# Basic template with "twinkle" syntax and "ep" layout
$t->get_ok('/')->status_is(200)->header_is('X-Append' => 'bar')
->content_like(qr/testHello <sebastian>!bar TwinkleSandBoxTest123/);
->content_like(qr/testHello <sebastian>!bar TwinkleSandBox::Test123/);

# Advanced template with "twinkle" syntax
$t->get_ok('/advanced')->status_is(200)->header_is('X-Append' => 'bar')
Expand Down

0 comments on commit d89e688

Please sign in to comment.