| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | 16 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 16 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use WWW::Shopify::Liquid; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | # Designed to wrap objects in the hash taht shoudln't be cloned. Only works for top level. | ||
| 9 | package WWW::Shopify::Liquid::Renderer::NoClone; | ||
| 10 | sub new { return bless { inner => $_[1] }, $_[0]; } | ||
| 11 | |||
| 12 | package WWW::Shopify::Liquid::Renderer; | ||
| 13 | use base 'WWW::Shopify::Liquid::Pipeline'; | ||
| 14 | use WWW::Shopify::Liquid::Security; | ||
| 15 | use DateTime; | ||
| 16 | use WWW::Shopify::Liquid::Resolver; | ||
| 17 | |||
| 18 | # WATCH THIS CLONING. IT CAN CAUSE SEGFAULTS DEPENDING ON WHAT'S CLONED. | ||
| 19 | sub new { my $package = shift; return bless { clone_hash => 1, silence_exceptions => 1, wrap_exceptions => 0, print_exceptions => 0, default_variables => { | ||
| 20 | now => WWW::Shopify::Liquid::Resolver->new(sub { return DateTime->now; }) | ||
| 21 | }, timeout => undef, max_inclusion_depth => 5, inclusion_context => undef, inclusion_depth => 0, security => WWW::Shopify::Liquid::Security->new, @_ }, $package; } | ||
| 22 | sub clone_hash { $_[0]->{clone_hash} = $_[1] if defined $_[1]; return $_[0]->{clone_hash}; } | ||
| 23 | sub security { $_[0]->{security} = $_[1] if defined $_[1]; return $_[0]->{security}; } | ||
| 24 | sub print_exceptions { $_[0]->{print_exceptions} = $_[1] if defined $_[1]; return $_[0]->{print_exceptions}; } | ||
| 25 | sub silence_exceptions { $_[0]->{silence_exceptions} = $_[1] if defined $_[1]; return $_[0]->{silence_exceptions}; } | ||
| 26 | sub wrap_exceptions { $_[0]->{wrap_exceptions} = $_[1] if defined $_[1]; return $_[0]->{wrap_exceptions}; } | ||
| 27 | sub timeout { $_[0]->{timeout} = $_[1] if defined $_[1]; return $_[0]->{timeout}; } | ||
| 28 | sub max_inclusion_depth { $_[0]->{max_inclusion_depth} = $_[1] if defined $_[1]; return $_[0]->{max_inclusion_depth}; } | ||
| 29 | sub inclusion_context { $_[0]->{inclusion_context} = $_[1] if defined $_[1]; return $_[0]->{inclusion_context}; } | ||
| 30 | sub inclusion_depth { $_[0]->{inclusion_depth} = $_[1] if defined $_[1]; return $_[0]->{inclusion_depth}; } | ||
| 31 | sub default_variables { $_[0]->{default_variants} = $_[1] if @_ > 1; return $_[0]->{default_variables}; } | ||
| 32 | |||
| 33 | use Clone qw(clone); | ||
| 34 | |||
| 35 | sub render { | ||
| 36 | my ($self, $hash, $ast) = @_; | ||
| 37 | return ('', $hash) if !$ast && !wantarray; | ||
| 38 | my $hash_clone = $self->clone_hash && $self->clone_hash == 1 ? clone($hash) : $hash; | ||
| 39 | return ('', $hash_clone) unless $ast; | ||
| 40 | my $default_variables = $self->default_variables; | ||
| 41 | $hash_clone->{$_} = $default_variables->{$_} for (grep { !exists $hash_clone->{$_} } keys(%$default_variables)); | ||
| 42 | my $result; | ||
| 43 | eval { | ||
| 44 | local $SIG{ALRM} = sub { die new WWW::Shopify::Liquid::Exception::Timeout(); }; | ||
| 45 | alarm $self->timeout if $self->timeout; | ||
| 46 | $result = $ast->isa('WWW::Shopify::Liquid::Element') ? $ast->render($self, $hash_clone) : "$ast"; | ||
| 47 | alarm 0; | ||
| 48 | }; | ||
| 49 | if (my $exp = $@) { | ||
| 50 | die $exp; | ||
| 51 | } | ||
| 52 | return $result unless wantarray; | ||
| 53 | return ($result, $hash_clone); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub state { $_[0]->{state} = $_[1] if @_ > 1; return $_[0]->{state}; } | ||
| 57 | |||
| 58 | # Used to resume execution. | ||
| 59 | sub resume { | ||
| 60 | my ($self, $state, $ast) = @_; | ||
| 61 | $self->state($state); | ||
| 62 | return $self->render($state->{hash}, $ast); | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||