line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DTL::Fast::Tag::Url; |
2
|
2
|
|
|
2
|
|
1300
|
use strict; use utf8; use warnings FATAL => 'all'; |
|
2
|
|
|
2
|
|
4
|
|
|
2
|
|
|
2
|
|
49
|
|
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
48
|
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
69
|
|
3
|
2
|
|
|
2
|
|
10
|
use parent 'DTL::Fast::Tag::Simple'; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
12
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
$DTL::Fast::TAG_HANDLERS{'url'} = __PACKAGE__; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
127
|
use DTL::Fast::Utils; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
2184
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#@Override |
10
|
|
|
|
|
|
|
sub parse_parameters |
11
|
|
|
|
|
|
|
{ |
12
|
4
|
|
|
4
|
0
|
5
|
my( $self ) = @_; |
13
|
|
|
|
|
|
|
|
14
|
4
|
50
|
|
|
|
63
|
if( $self->{'parameter'} =~ /^\s*(.+?)(?:\s+as\s+([^\s]+))?\s*$/s ) |
15
|
|
|
|
|
|
|
{ |
16
|
4
|
|
|
|
|
11
|
$self->{'target_name'} = $2; |
17
|
4
|
|
|
|
|
18
|
my @params = split /\s+/, $self->backup_strings($1); |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
|
|
|
18
|
$self->{'model_path'} = $self->get_backup_or_variable(shift @params); |
20
|
4
|
100
|
|
|
|
12
|
if( scalar @params ) |
21
|
|
|
|
|
|
|
{ |
22
|
3
|
100
|
|
|
|
11
|
if( $params[0] =~ /\=/ ) |
23
|
|
|
|
|
|
|
{ |
24
|
2
|
|
|
|
|
6
|
$self->parse_named_parameters(\@params); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
else |
27
|
|
|
|
|
|
|
{ |
28
|
1
|
|
|
|
|
4
|
$self->parse_positional_parameters(\@params); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
else |
33
|
|
|
|
|
|
|
{ |
34
|
0
|
|
|
|
|
0
|
die $self->get_parse_error("unable to parse url parameters: $self->{'parameter'}"); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
4
|
|
|
|
|
10
|
return $self; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#@Override |
41
|
|
|
|
|
|
|
sub render |
42
|
|
|
|
|
|
|
{ |
43
|
4
|
|
|
4
|
0
|
6
|
my( $self, $context ) = @_; |
44
|
|
|
|
|
|
|
|
45
|
4
|
|
|
|
|
4
|
my $result = ''; |
46
|
|
|
|
|
|
|
|
47
|
4
|
|
|
|
|
8
|
my $url_source = $context->{'ns'}->[-1]->{'_dtl_url_source'}; |
48
|
|
|
|
|
|
|
|
49
|
4
|
50
|
33
|
|
|
20
|
if( |
50
|
|
|
|
|
|
|
defined $url_source |
51
|
|
|
|
|
|
|
and ref $url_source eq 'CODE' |
52
|
|
|
|
|
|
|
) |
53
|
|
|
|
|
|
|
{ |
54
|
4
|
|
|
|
|
13
|
my $model_path = $self->{'model_path'}->render($context); |
55
|
4
|
|
|
|
|
10
|
my $arguments = $self->render_arguments($context); |
56
|
4
|
|
|
|
|
12
|
my $url_template = $url_source->($model_path, $arguments); |
57
|
|
|
|
|
|
|
|
58
|
4
|
50
|
|
|
|
38
|
if( $url_template ) |
59
|
|
|
|
|
|
|
{ |
60
|
4
|
|
|
|
|
10
|
$result = $self->restore_url($url_template, $arguments); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else |
63
|
|
|
|
|
|
|
{ |
64
|
0
|
|
|
|
|
0
|
die $self->get_render_error("url source returned false value by model path: $model_path"); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
|
|
0
|
die $self->get_render_error("in order to render url's you must provide `url_source` argument to the template constructor"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
4
|
|
|
|
|
18
|
return $result; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub restore_url |
76
|
|
|
|
|
|
|
{ |
77
|
4
|
|
|
4
|
0
|
8
|
my( $self, $template, $arguments ) = @_; |
78
|
|
|
|
|
|
|
|
79
|
4
|
100
|
|
|
|
10
|
if( ref $arguments eq 'ARRAY' ) |
80
|
|
|
|
|
|
|
{ |
81
|
|
|
|
|
|
|
my $replacer = sub{ |
82
|
4
|
|
100
|
4
|
|
32
|
return DTL::Fast::Utils::escape((shift @$arguments) // ''); |
83
|
2
|
|
|
|
|
7
|
}; |
84
|
2
|
|
|
|
|
12
|
$template =~ s/ |
85
|
|
|
|
|
|
|
\( |
86
|
|
|
|
|
|
|
[^)(]+ |
87
|
|
|
|
|
|
|
\) |
88
|
|
|
|
|
|
|
\?? |
89
|
4
|
|
|
|
|
12
|
/$replacer->()/xge; # @todo: this one is dumb, need improve |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
else # MUST be a hash |
92
|
|
|
|
|
|
|
{ |
93
|
|
|
|
|
|
|
my $replacer = sub{ |
94
|
4
|
|
|
4
|
|
8
|
my( $key ) = @_; |
95
|
4
|
|
|
|
|
26
|
return DTL::Fast::Utils::escape($arguments->{$key}); |
96
|
2
|
|
|
|
|
8
|
}; |
97
|
2
|
|
|
|
|
13
|
$template =~ s/ |
98
|
|
|
|
|
|
|
\(\?<(.+?)> |
99
|
|
|
|
|
|
|
[^)(]+ |
100
|
|
|
|
|
|
|
\) |
101
|
|
|
|
|
|
|
\?? |
102
|
4
|
|
|
|
|
9
|
/$replacer->($1)/xge; # @todo: this one is dumb, need improve |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# removing regexp remains |
106
|
4
|
|
|
|
|
52
|
$template =~ s/( |
107
|
|
|
|
|
|
|
^\^ |
108
|
|
|
|
|
|
|
|\$$ |
109
|
|
|
|
|
|
|
|\(\?\: |
110
|
|
|
|
|
|
|
|\( |
111
|
|
|
|
|
|
|
|\) |
112
|
|
|
|
|
|
|
)//xgs; |
113
|
|
|
|
|
|
|
|
114
|
4
|
|
|
|
|
17
|
return '/'.$template; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub render_arguments |
119
|
|
|
|
|
|
|
{ |
120
|
4
|
|
|
4
|
0
|
7
|
my( $self, $context ) = @_; |
121
|
|
|
|
|
|
|
|
122
|
4
|
|
|
|
|
5
|
my $result = []; |
123
|
|
|
|
|
|
|
|
124
|
4
|
100
|
|
|
|
10
|
if( $self->{'arguments'} ) |
125
|
|
|
|
|
|
|
{ |
126
|
3
|
100
|
|
|
|
9
|
if( ref $self->{'arguments'} eq 'ARRAY' ) |
127
|
|
|
|
|
|
|
{ |
128
|
1
|
|
|
|
|
2
|
$result = []; |
129
|
|
|
|
|
|
|
|
130
|
1
|
|
|
|
|
2
|
foreach my $argument (@{$self->{'arguments'}}) |
|
1
|
|
|
|
|
2
|
|
131
|
|
|
|
|
|
|
{ |
132
|
2
|
|
|
|
|
7
|
push @$result, $argument->render($context); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else # MUST be a HASH |
136
|
|
|
|
|
|
|
{ |
137
|
2
|
|
|
|
|
4
|
$result = {}; |
138
|
2
|
|
|
|
|
43
|
foreach my $key (keys( %{$self->{'arguments'}})) |
|
2
|
|
|
|
|
8
|
|
139
|
|
|
|
|
|
|
{ |
140
|
4
|
|
|
|
|
15
|
$result->{$key} = $self->{'arguments'}->{$key}->render($context); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
4
|
|
|
|
|
8
|
return $result; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub parse_named_parameters |
149
|
|
|
|
|
|
|
{ |
150
|
2
|
|
|
2
|
0
|
4
|
my( $self, $params ) = @_; |
151
|
|
|
|
|
|
|
|
152
|
2
|
|
|
|
|
3
|
my $result = {}; |
153
|
2
|
|
|
|
|
4
|
foreach my $param (@$params) |
154
|
|
|
|
|
|
|
{ |
155
|
4
|
50
|
|
|
|
17
|
if( $param =~ /^(.+)\=(.+)$/ ) |
156
|
|
|
|
|
|
|
{ |
157
|
4
|
|
|
|
|
11
|
$result->{$1} = $self->get_backup_or_variable($2); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
else |
160
|
|
|
|
|
|
|
{ |
161
|
0
|
|
|
|
|
0
|
die $self->get_parse_error("you can't mix positional and named arguments in url tag: $self->{'parameter'}"); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
2
|
|
|
|
|
5
|
$self->{'arguments'} = $result; |
165
|
2
|
|
|
|
|
4
|
return $self; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub parse_positional_parameters |
169
|
|
|
|
|
|
|
{ |
170
|
1
|
|
|
1
|
0
|
2
|
my( $self, $params ) = @_; |
171
|
|
|
|
|
|
|
|
172
|
1
|
|
|
|
|
2
|
my $result = []; |
173
|
1
|
|
|
|
|
2
|
foreach my $param (@$params) |
174
|
|
|
|
|
|
|
{ |
175
|
2
|
50
|
|
|
|
6
|
die $self->get_parse_error("you can't mix positional and named arguments in url tag: $self->{'parameter'}") |
176
|
|
|
|
|
|
|
if $param =~ /\=/; |
177
|
|
|
|
|
|
|
|
178
|
2
|
|
|
|
|
6
|
push @$result, $self->get_backup_or_variable($param); |
179
|
|
|
|
|
|
|
} |
180
|
1
|
|
|
|
|
3
|
$self->{'arguments'} = $result; |
181
|
1
|
|
|
|
|
3
|
return $self; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |