lib/WWW/Shopify/Liquid/Debugger.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/WWW/Shopify/Liquid/Beautifier.pm ↑ Summary lib/WWW/Shopify/Liquid/Optimizer.pm »
1 #!/usr/bin/perl
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶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 package WWW::Shopify::Liquid::Debugger::Breakpoint;
9  
10 sub new {
11 my ($package) = shift;
12 my $hash = shift;
13 $hash = {} unless $hash;
14 return bless {
15 %$hash
16 }, $package;
17 }
18  
19 sub line { return $_[0]->{line}; }
20 sub file { return $_[0]->{file}; }
21  
22  
23 # Provides a low-level debugging interface to liquid.
24  
25 package WWW::Shopify::Liquid::Debugger;
26  
27 sub new { return bless {
28 renderer => $_[1],
29 breakpoints => [],
30
31 break_line => undef,
32 current_line => undef
33 }, $_[0]; }
34  
35 use Scalar::Util qw(blessed reftype weaken);
36 use List::MoreUtils qw(uniq);
37  
38 my @active_debuggers = ();
39 my $hooked_packages = {};
40  
41 sub dump {
42 my ($self, $item, $level) = @_;
43 $level = 0 unless $level;
44 my $tab = "\t";
45 my $tabs = join("", ($tab x $level));
46 my $accumulator = '';
47 if (reftype($item)) {
48 if (reftype($item) eq 'HASH') {
49 if (blessed($item)) {
50 $accumulator .= do { my $a = ref($item); $a =~ s/WWW::Shopify::Liquid::(Tag|Token|Operator):://; uc($a) } . "\n";
51 } else {
52 $accumulator .= $tabs . ref($item) . "\n";
53 }
54
55 my @keys = grep { $_ ne "line" } keys(%$item);
56 if (int(@keys) > 1) {
57 for (sort(@keys)) {
58 $accumulator.= "$tabs$tab" . $self->dump($item->{$_}, $level+1);
59 }
60 } elsif (int(@keys) > 0) {
61 $accumulator.= "$tabs$tab" . $self->dump($item->{$keys[0]}, $level+1);
62 }
63
64 } elsif (reftype($item) eq 'ARRAY') {
65 $accumulator .= "\n";
66 $accumulator .= "$tabs$tab" . $self->dump($_, $level+1) for (grep { defined $_ } @$item);
67 $accumulator .= "$tabs\n";
68 }
69 } else {
70 return "null" unless defined $item;
71 return "'" . $item . "'\n";
72 }
73 return $accumulator;
74 }
75  
76 sub add_breakpoint {
77 my ($self, $context, $line) = @_;
78 push(@{$self->{breakpoints}}, WWW::Shopify::Liquid::Debugger::Breakpoint->new({ line => $line, file => $context }));
79 }
80  
81 sub remove_breakpoint {
82 my ($self, $context, $line) = @_;
83 $self->{breakpoints} = [grep { $_->line != $line || $_->file ne $context } @{$self->{breakpoints}}];
84
85 }
86  
87  
88 sub render {
89 my ($self, $hash, $ast) = @_;
90 my $debugger = $self;
91 # Only breakpoint on tags.
92 my @tags = grep { $_->isa('WWW::Shopify::Liquid::Tag') } $ast->tokens;
93 # For every tag in here, modify the in-memory package, such that when render is called, before we call render, we call our new breakpoint method.
94 for (uniq(grep { !$hooked_packages->{$_} } map { ref($_) } @tags)) {
95 no strict 'refs';
96 no warnings 'redefine';
97 no warnings 'closure';
98
99 my $qualified = $_ . "::render";
100 my $original = *{$qualified}{CODE};
101 $hooked_packages->{$_} = 1;
102 if ($original) {
103 eval ("package $_;
104 sub render {
105 WWW::Shopify::Liquid::Debugger->package_step(\$_[0]);
106 \$original->(\@_);
107 }");
108 if (my $exp = $@) {
109 die $exp;
110 }
111 } else {
112 eval ("package $_;
113 sub render {
114 WWW::Shopify::Liquid::Debugger->package_step(\$_[0]);
115 shift->SUPER::render(\@_);
116 }");
117 }
118 }
119 push(@active_debuggers, $self);
120 my @results = $self->{renderer}->render($hash, $ast);
121 @active_debuggers = grep { $_ != $self } @active_debuggers;
122 return @results if wantarray;
123 return $results[0];
124
125 }
126  
127 sub package_step {
128 my ($self, $element) = @_;
129 $_->step($element) for (@active_debuggers);
130 }
131  
132  
133 sub step {
134 my ($self, $element) = @_;
135 $self->{current_line} = $element->{line};
136 if (int(grep { $element->{line} && $element->{line}->[3] && $_->file eq $element->{line}->[3] && $element->{line}->[0] == $_->line } @{$self->{breakpoints}}) > 0) {
137 $self->break($element);
138 }
139 if ($self->{break_line} && $element->{line} && $element->{line} >= $self->{break_line}) {
140 $self->{break_line} = undef;
141 $self->break($element);
142 }
143 }
144  
145 sub continue {
146 my ($self) = @_;
147 }
148  
149  
150 sub next_line {
151 my ($self) = @_;
152 $self->{break_line} = $self->{current_line}+1;
153 $self->continue;
154 }
155  
156  
157 sub break {
158 my ($self) = @_;
159 }
160  
161 sub variable {
162 my ($self) = @_;
163
164 }
165  
166  
167 1;
« lib/WWW/Shopify/Liquid/Beautifier.pm ↑ Summary lib/WWW/Shopify/Liquid/Optimizer.pm »