-
-
Save anonymous/36ac0cf73d3f17f7e6ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Mojolicious/Renderer.pm b/lib/Mojolicious/Renderer.pm | |
index 81b856f..58a350a 100644 | |
--- a/lib/Mojolicious/Renderer.pm | |
+++ b/lib/Mojolicious/Renderer.pm | |
@@ -1,13 +1,12 @@ | |
package Mojolicious::Renderer; | |
use Mojo::Base -base; | |
-use Carp (); | |
use File::Spec::Functions 'catfile'; | |
use Mojo::Cache; | |
use Mojo::JSON 'encode_json'; | |
use Mojo::Home; | |
use Mojo::Loader; | |
-use Mojo::Util qw(decamelize encode slurp); | |
+use Mojo::Util qw(decamelize encode md5_sum monkey_patch slurp); | |
has cache => sub { Mojo::Cache->new }; | |
has classes => sub { ['main'] }; | |
@@ -70,10 +69,18 @@ sub get_data_template { | |
sub get_helper { | |
my ($self, $name) = @_; | |
+ | |
if (my $h = $self->helpers->{$name} || $self->{proxy}{$name}) { return $h } | |
- return undef unless grep {/^\Q$name\E\./} keys %{$self->helpers}; | |
- return $self->{proxy}{$name} | |
- = sub { bless [shift, $name], 'Mojolicious::Renderer::_Proxy' }; | |
+ return undef unless grep {/^\Q$name\E\./} my @keys = keys %{$self->helpers}; | |
+ | |
+ my $class = 'Mojolicious::Renderer::Helpers::' . md5_sum("$name:$self"); | |
+ for my $key (@keys) { | |
+ next unless $key =~ /^(\Q$name\E\.([^.]+))/; | |
+ my $export = $2; | |
+ my $sub = $self->get_helper($1); | |
+ monkey_patch $class, $export => sub { ${shift()}->$sub(@_) }; | |
+ } | |
+ return $self->{proxy}{$name} = sub { bless \shift, $class }; | |
} | |
sub render { | |
@@ -203,6 +210,7 @@ sub template_path { | |
sub _add { | |
my ($self, $attr, $name, $cb) = @_; | |
$self->$attr->{$name} = $cb; | |
+ delete $self->{proxy}; | |
return $self; | |
} | |
@@ -248,21 +256,6 @@ sub _render_template { | |
return undef; | |
} | |
-package Mojolicious::Renderer::_Proxy; | |
-use Mojo::Base -strict; | |
- | |
-sub AUTOLOAD { | |
- my $self = shift; | |
- | |
- my ($package, $method) = split /::(\w+)$/, our $AUTOLOAD; | |
- my $c = $self->[0]; | |
- Carp::croak qq{Can't locate object method "$method" via package "$package"} | |
- unless my $helper = $c->app->renderer->get_helper("$self->[1].$method"); | |
- return $c->$helper(@_); | |
-} | |
- | |
-sub DESTROY { } | |
- | |
1; | |
=encoding utf8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment