line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MVC::Neaf::Util::Container; |
2
|
|
|
|
|
|
|
|
3
|
82
|
|
|
82
|
|
69123
|
use strict; |
|
82
|
|
|
|
|
188
|
|
|
82
|
|
|
|
|
2571
|
|
4
|
82
|
|
|
82
|
|
426
|
use warnings; |
|
82
|
|
|
|
|
167
|
|
|
82
|
|
|
|
|
3529
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.2800_01'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
MVC::Neaf::Util::Container - path & method based container for Not Even A Framework |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 DESCRIPTION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
This is utility class. |
14
|
|
|
|
|
|
|
Nothing to see here unless one intends to work on L itself. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
This class can hold multiple entities addressed by paths and methods |
17
|
|
|
|
|
|
|
and extract them in the needed order. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $c = MVC::Neaf::Util::Container->new; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$c->store( "foo", path => '/foo', method => 'GET' ); |
24
|
|
|
|
|
|
|
$c->store( "bar", path => '/foo/bar', exclude => '/foo/bar/baz' ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$c->fetch( path => "/foo", method => 'GET' ); # foo |
27
|
|
|
|
|
|
|
$c->fetch( path => "/foo/bar", method => 'GET' ); # foo bar |
28
|
|
|
|
|
|
|
$c->fetch( path => "/foo/bar", method => 'POST' ); |
29
|
|
|
|
|
|
|
# qw(bar) - 'foo' limited to GET only |
30
|
|
|
|
|
|
|
$c->fetch( path => "/foo/bar/baz", method => 'GET' ); |
31
|
|
|
|
|
|
|
# qw(foo) - 'bar' excluded |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
82
|
|
|
82
|
|
510
|
use Carp; |
|
82
|
|
|
|
|
178
|
|
|
82
|
|
|
|
|
4661
|
|
36
|
|
|
|
|
|
|
|
37
|
82
|
|
|
82
|
|
975
|
use parent qw(MVC::Neaf::Util::Base); |
|
82
|
|
|
|
|
566
|
|
|
82
|
|
|
|
|
531
|
|
38
|
82
|
|
|
82
|
|
6941
|
use MVC::Neaf::Util qw( maybe_list canonize_path path_prefixes supported_methods check_path ); |
|
82
|
|
|
|
|
210
|
|
|
82
|
|
|
|
|
90471
|
|
39
|
|
|
|
|
|
|
our @CARP_NOT = qw(MVC::Neaf::Route); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 exclusive |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Only store one item per (path,method) pair, and fail loudly in case of conflicts. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 store |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
store( $data, %spec ) |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Store $data in container. Spec may include: |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=over |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item path - single path or list of paths, '/' assumed if none. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item method - name of method or array of methods. |
60
|
|
|
|
|
|
|
By default, all methods supported by Neaf. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item exclude - single path or list of paths. None by default. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item prepend - if true, prepend to the list instead of appending. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item tentative (exclusive container only) - if true, don't override existing |
67
|
|
|
|
|
|
|
declarations, and don't complain when overridden. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item override (exclusive container only) - if true, override |
70
|
|
|
|
|
|
|
any preexisting content. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=back |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub store { |
77
|
165
|
|
|
165
|
1
|
536
|
my ($self, $data, %opt) = @_; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$self->my_croak( "'tentative' and 'override' are useless for non-exclusive container" ) |
80
|
165
|
50
|
33
|
|
|
1371
|
if !$self->{exclusive} and ( $opt{tentative} or $opt{override} ); |
|
|
|
66
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$self->my_croak( "'tentative' and 'override' are mutually exclusive" ) |
83
|
165
|
50
|
66
|
|
|
618
|
if $opt{tentative} and $opt{override}; |
84
|
|
|
|
|
|
|
|
85
|
165
|
|
|
|
|
463
|
$opt{data} = $data; |
86
|
|
|
|
|
|
|
|
87
|
165
|
|
|
|
|
860
|
my @methods = map { uc $_ } maybe_list( $opt{method}, supported_methods() ); |
|
967
|
|
|
|
|
2025
|
|
88
|
|
|
|
|
|
|
|
89
|
165
|
|
|
|
|
1537
|
my @todo = check_path map { canonize_path( $_ ) } maybe_list( $opt{path}, '' ); |
|
169
|
|
|
|
|
663
|
|
90
|
165
|
100
|
|
|
|
722
|
if ($opt{exclude}) { |
91
|
3
|
|
|
|
|
9
|
my $rex = join '|', map { quotemeta(canonize_path($_)) } |
92
|
3
|
|
|
|
|
13
|
check_path maybe_list( $opt{exclude} ); |
93
|
3
|
|
|
|
|
82
|
$opt{exclude} = qr(^(?:$rex)(?:[/?]|$)); |
94
|
3
|
|
|
|
|
24
|
@todo = grep { $_ !~ $opt{exclude} } @todo |
|
3
|
|
|
|
|
26
|
|
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
165
|
100
|
|
|
|
529
|
if ($self->{exclusive}) { |
98
|
19
|
|
|
|
|
100
|
my @list = $self->store_check_conflict( %opt, method => \@methods, path => \@todo ); |
99
|
19
|
100
|
|
|
|
73
|
$self->my_croak( "Conflicting path spec: ".join ", ", @list ) |
100
|
|
|
|
|
|
|
if @list; |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
164
|
|
|
|
|
429
|
foreach my $method ( @methods ) { |
104
|
965
|
|
|
|
|
1613
|
foreach my $path ( @todo ) { |
105
|
979
|
|
100
|
|
|
7399
|
my $array = $self->{data}{$method}{$path} ||= []; |
106
|
979
|
100
|
|
|
|
4822
|
if ( $self->{exclusive} ) { |
|
|
100
|
|
|
|
|
|
107
|
|
|
|
|
|
|
@$array = (\%opt) |
108
|
113
|
100
|
100
|
|
|
344
|
unless $array->[0] and $opt{tentative} and !$array->[0]{tentative}; |
|
|
|
66
|
|
|
|
|
109
|
|
|
|
|
|
|
} elsif ( $opt{prepend} ) { |
110
|
12
|
|
|
|
|
23
|
unshift @$array, \%opt; |
111
|
|
|
|
|
|
|
} else { |
112
|
854
|
|
|
|
|
2195
|
push @$array, \%opt; |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
}; |
115
|
|
|
|
|
|
|
}; |
116
|
|
|
|
|
|
|
|
117
|
164
|
|
|
|
|
606
|
$self; |
118
|
|
|
|
|
|
|
}; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 store_check_conflict |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
store_check_conflict( path => ..., method => ... ) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Check that no previous declarations conflict with the new one. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
This is only if exclusive was specified. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub store_check_conflict { |
131
|
19
|
|
|
19
|
1
|
80
|
my ($self, %opt) = @_; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$self->my_croak( "useless call for non-exclusive container" ) |
134
|
19
|
50
|
|
|
|
120
|
unless $self->{exclusive}; |
135
|
|
|
|
|
|
|
|
136
|
19
|
100
|
100
|
|
|
114
|
if (!$opt{tentative} and !$opt{override}) { |
137
|
|
|
|
|
|
|
# Check for conflicts before changing anything |
138
|
12
|
|
|
|
|
21
|
my %conflict; |
139
|
12
|
|
|
|
|
20
|
foreach my $method ( @{ $opt{method} } ) { |
|
12
|
|
|
|
|
36
|
|
140
|
64
|
|
|
|
|
81
|
foreach my $path ( @{ $opt{path} } ) { |
|
64
|
|
|
|
|
103
|
|
141
|
68
|
|
|
|
|
120
|
my $existing = $self->{data}{$method}{$path}; |
142
|
68
|
100
|
66
|
|
|
156
|
next unless $existing && $existing->[0]; |
143
|
7
|
100
|
|
|
|
16
|
next if $existing->[0]->{tentative}; |
144
|
1
|
|
|
|
|
2
|
push @{ $conflict{$path} }, $method; |
|
1
|
|
|
|
|
4
|
|
145
|
|
|
|
|
|
|
}; |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my @list = |
149
|
12
|
|
|
|
|
41
|
map { $_."[".(join ",", sort @{ $conflict{$_} })."]" } |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
150
|
|
|
|
|
|
|
sort keys %conflict; |
151
|
12
|
|
|
|
|
41
|
return @list; |
152
|
|
|
|
|
|
|
}; |
153
|
|
|
|
|
|
|
|
154
|
7
|
|
|
|
|
23
|
return (); |
155
|
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 list_methods |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Returns methods currently in the storage. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub list_methods { |
164
|
3
|
|
|
3
|
1
|
7
|
my $self = shift; |
165
|
|
|
|
|
|
|
|
166
|
3
|
|
|
|
|
6
|
return keys %{ $self->{data} }; |
|
3
|
|
|
|
|
22
|
|
167
|
|
|
|
|
|
|
}; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 list_paths |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Returns paths for given method, or all if no method given. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub list_paths { |
176
|
2
|
|
|
2
|
1
|
7
|
my ($self, @methods) = @_; |
177
|
|
|
|
|
|
|
|
178
|
2
|
100
|
|
|
|
8
|
@methods = $self->list_methods |
179
|
|
|
|
|
|
|
unless @methods; |
180
|
|
|
|
|
|
|
|
181
|
2
|
|
|
|
|
3
|
my %uniq; |
182
|
2
|
|
|
|
|
5
|
foreach my $method (@methods) { |
183
|
8
|
|
|
|
|
14
|
$uniq{$_}++ for keys %{ $self->{data}{$method} }; |
|
8
|
|
|
|
|
25
|
|
184
|
|
|
|
|
|
|
}; |
185
|
2
|
|
|
|
|
16
|
return keys %uniq; |
186
|
|
|
|
|
|
|
}; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 fetch |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
fetch( %spec ) |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Return all matching previously stored objects, |
193
|
|
|
|
|
|
|
from shorter to longer paths, in order of addition. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Spec may include: |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=over |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item path - a single path to match against |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item method - method to match against |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=back |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=cut |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub fetch { |
208
|
292
|
|
|
292
|
1
|
1474
|
my $self = shift; |
209
|
292
|
|
|
|
|
759
|
return map { $_->{data} } $self->fetch_raw(@_); |
|
339
|
|
|
|
|
1268
|
|
210
|
|
|
|
|
|
|
}; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 fetch_last |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Same as fetch(), but only return the last (last added & longest path) element. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub fetch_last { |
219
|
10
|
|
|
10
|
1
|
99
|
my $self = shift; |
220
|
10
|
|
|
|
|
41
|
my ($bucket) = reverse $self->fetch_raw(@_); |
221
|
10
|
|
|
|
|
57
|
return $bucket->{data}; |
222
|
|
|
|
|
|
|
}; |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 fetch_raw |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Same as fetch(), but return additional info instead of just stored item: |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
{ |
229
|
|
|
|
|
|
|
data => $your_item_here, |
230
|
|
|
|
|
|
|
path => $all_the_paths, |
231
|
|
|
|
|
|
|
method => $list_of_methods, |
232
|
|
|
|
|
|
|
... |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub fetch_raw { |
238
|
302
|
|
|
302
|
1
|
1018
|
my ($self, %opt) = @_; |
239
|
|
|
|
|
|
|
|
240
|
302
|
|
|
|
|
675
|
my @missing = grep { !defined $opt{$_} } qw(path method); |
|
604
|
|
|
|
|
1661
|
|
241
|
302
|
50
|
|
|
|
807
|
croak __PACKAGE__."->fetch: required fields missing: @missing" |
242
|
|
|
|
|
|
|
if @missing; |
243
|
|
|
|
|
|
|
|
244
|
302
|
|
|
|
|
927
|
my $path = canonize_path( $opt{path} ); |
245
|
|
|
|
|
|
|
|
246
|
302
|
|
|
|
|
537
|
my @ret; |
247
|
302
|
|
|
|
|
926
|
my $tree = $self->{data}{ $opt{method} }; |
248
|
|
|
|
|
|
|
|
249
|
302
|
|
100
|
|
|
1264
|
foreach my $prefix ( path_prefixes( $opt{path} || '' ) ) { |
250
|
560
|
|
|
|
|
1021
|
my $list = $tree->{$prefix}; |
251
|
560
|
100
|
|
|
|
1303
|
next unless $list; |
252
|
320
|
|
|
|
|
707
|
foreach my $node( @$list ) { |
253
|
353
|
100
|
100
|
|
|
1058
|
next if $node->{exclude} and $opt{path} =~ $node->{exclude}; |
254
|
348
|
|
|
|
|
841
|
push @ret, $node; |
255
|
|
|
|
|
|
|
}; |
256
|
|
|
|
|
|
|
}; |
257
|
|
|
|
|
|
|
|
258
|
302
|
|
|
|
|
968
|
return @ret; |
259
|
|
|
|
|
|
|
}; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
This module is part of L suite. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
Copyright 2016-2023 Konstantin S. Uvarin C. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
268
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
269
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
See L for more information. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=cut |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
1; |