annotate Lib/IMPL/Web/Handler/View.pm @ 357:ec58c47edb52

web security: code cleanup, refactoring
author cin
date Fri, 18 Oct 2013 15:10:10 +0400
parents 9330835535b9
children 833e663796c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
1 package IMPL::Web::Handler::View;
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
2 use strict;
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
3
291
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 285
diff changeset
4 use Carp qw(carp);
204
d63f9a92d6d4 +IMPL::Config::Include - simple way to include external config
sergey
parents: 203
diff changeset
5 use List::Util qw(first);
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
6 use IMPL::lang;
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
7 use IMPL::Const qw(:prop);
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
8 use IMPL::declare {
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
9 require => {
240
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 235
diff changeset
10 Factory => 'IMPL::Web::View::ObjectFactory',
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
11 HttpResponse => 'IMPL::Web::HttpResponse',
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
12 Loader => 'IMPL::Code::Loader',
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
13 ViewResult => '-IMPL::Web::ViewResult'
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
14 },
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
15 base => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
16 'IMPL::Object' => undef,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
17 'IMPL::Object::Autofill' => '@_',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
18 'IMPL::Object::Serializable' => undef
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
19 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
20
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
21 props => [
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
22 contentType => PROP_RO,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
23 contentCharset => PROP_RO,
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
24 view => PROP_RO,
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
25 layout => PROP_RO,
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
26 selectors => PROP_RO,
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
27 defaultDocument => PROP_RW,
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
28 _selectorsCache => PROP_RW
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
29 ]
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
30 };
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
31
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
32 sub CTOR {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
33 my ($this) = @_;
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
34
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
35 $this->_selectorsCache([ map $this->ParseRule($_), @{$this->selectors || []} ]);
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
36 }
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
37
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
38 sub Invoke {
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
39 my ( $this, $action, $next ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
40
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
41 my $result = $next ? $next->($action) : undef;
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
42
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
43 my ($model,$view,$template);
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
44 if( ref $result and eval { $result->isa(ViewResult) } ) {
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
45 $model = $result->model;
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
46 $view = $result;
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
47 $template = $result->template;
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
48 } else {
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
49 $model = $result;
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
50 $view = ViewResult->new(model => $model);
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
51 }
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
52
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
53 my $vars = {
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 240
diff changeset
54 view => $view,
339
97628101b765 refactoring: application now holds a security object factory rather than a security object
cin
parents: 334
diff changeset
55 request => sub { $action },
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
56 app => $action->application,
334
71221d79e6b4 removing web resources contracts
cin
parents: 291
diff changeset
57 context => $action->context,
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
58 env => _cached($action->context->{environment}),
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
59 location => $action->context->{resourceLocation},
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
60 layout => $this->layout
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
61 };
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
62
256
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
63 my %responseParams = (
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
64 type => $this->contentType,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
65 charset => $this->contentCharset,
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
66 body => $this->view->display(
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
67 $model,
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
68 $template || $this->SelectView( $action, ref $model ),
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
69 $vars
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
70 )
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
71 );
256
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
72
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
73 $responseParams{status} = $view->status if $view->status;
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
74 $responseParams{cookies} = $view->cookies if ref $view->cookies eq 'HASH';
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
75 $responseParams{headers} = $view->headers if ref $view->headers eq 'HASH';
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
76
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
77 return HttpResponse->new(
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
78 %responseParams
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 244
diff changeset
79 );
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
80 }
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
81
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
82 sub _cached {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
83 my $arg = shift;
266
89179bb8c388 *corrected TTView to handle plain (and undefined) values
cin
parents: 256
diff changeset
84
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
85 return $arg unless ref $arg eq 'CODE';
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
86
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
87 return sub {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
88 ref $arg eq 'CODE' ? $arg = &$arg() : $arg;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
89 }
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
90 }
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
91
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
92 sub SelectView {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
93 my ($this,$action) = @_;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
94
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
95 my @path;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
96
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
97 for(my $r = $action->context->{resource}; $r ; $r = $r->parent ) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
98 unshift @path, {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
99 name => $r->id,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
100 class => typeof($r->model)
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
101 };
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
102 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
103
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
104 @path = map { name => $_}, split /\/+/, $action->query->path_info()
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
105 unless (@path);
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
106
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
107 return $this->MatchPath(\@path,$this->_selectorsCache) || $this->defaultDocument;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
108 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
109
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
110 sub ParseRule {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
111 my ($this, $rule) = @_;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
112
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
113 my ($selector,$data) = split /\s+=>\s+/, $rule;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
114
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
115 my @parts;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
116 my $first = 1;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
117 my $weight = 0;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
118 foreach my $part ( split /\//, $selector ) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
119 # если первым символом является /
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
120 # значит путь в селекторе абсолютный и не нужно
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
121 # добавлять "любой" элемент в начало
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
122
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
123 if($part) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
124 $weight ++;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
125 push @parts,{ any => 1 } if $first;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
126 } else {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
127 push @parts,{ any => 1 } unless $first;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
128 next;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
129 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
130
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
131 my ($name,$class) = split /@/, $part;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
132
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
133 if ( my ( $varName, $rx ) = ( $name =~ m/^\{(?:(\w+)\:)?(.*)\}$/ ) ) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
134 #this is a regexp
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
135
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
136 push @parts, {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
137 rx => $rx,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
138 var => $varName,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
139 class => $class,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
140 };
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
141 } else {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
142 push @parts, {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
143 name => length($name) ? $name : undef,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
144 class => $class,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
145 };
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
146 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
147 } continue {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
148 $first = 0;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
149 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
150
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
151 return { selector => \@parts, data => $data, weight => $weight };
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
152 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
153
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
154 sub MatchPath {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
155 my ($this,$path,$rules) = @_;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
156
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
157 $path ||= [];
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
158 $rules ||= [];
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
159
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
160 my @next;
266
89179bb8c388 *corrected TTView to handle plain (and undefined) values
cin
parents: 256
diff changeset
161
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
162 foreach my $segment (@$path) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
163 foreach my $rule (@$rules) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
164 my @selector = @{$rule->{selector}};
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
165
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
166 my $part = shift @selector;
291
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 285
diff changeset
167
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 285
diff changeset
168 # if this rule doesn't have a selector
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 285
diff changeset
169 next unless $part;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
170
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
171 if ($part->{any}) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
172 #keep the rule for the next try
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
173 push @next, $rule;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
174
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
175 $part = shift @selector while $part->{any};
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
176 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
177
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
178 my $newRule = {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
179 selector => \@selector,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
180 data => $rule->{data},
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
181 weight => $rule->{weight},
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
182 vars => { %{$rule->{vars} || {}} }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
183 };
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
184
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
185 my $success = 1;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
186 if (my $class = $part->{class}) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
187 $success = isclass($segment->{class},$class);
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
188 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
189
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
190 if($success && (my $name = $part->{name})) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
191 $success = $segment->{name} eq $name;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
192 } elsif ($success && (my $rx = $part->{rx})) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
193 if( my @captures = ($segment->{name} =~ m/($rx)/) ) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
194 $newRule->{vars}->{$part->{var}} = \@captures
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
195 if $part->{var};
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
196 } else {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
197 $success = 0;
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
198 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 206
diff changeset
199 }
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
200
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
201 push @next, $newRule if $success;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
202
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
203 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
204 $rules = [@next];
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
205 undef @next;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
206 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
207
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
208 my $result = (
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
209 sort {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
210 $b->{weight} <=> $a->{weight}
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
211 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
212 grep {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
213 scalar(@{$_->{selector}}) == 0
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
214 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
215 @$rules
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
216 )[0];
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
217
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
218 if($result) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
219 my $data = $result->{data};
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
220 $data =~ s/{(\w+)(?:\:(\d+))?}/
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
221 my ($name,$index) = ($1,$2 || 0);
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
222
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
223 if ($result->{vars}{$name}) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
224 $result->{vars}{$name}[$index];
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
225 } else {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
226 "";
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
227 }
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
228 /gex;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
229
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
230 return $data;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
231 } else {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
232 return;
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
233 }
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
234 }
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
235
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
236 1;
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
237
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
238 __END__
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
239
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
240 =pod
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
241
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
242 =head1 NAME
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
243
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
244 C<IMPL::Web::Handler::TTView> - использует шаблоны для построения представления.
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
245
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
246 =head1 SYNOPSIS
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
247
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
248 =begin code xml
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
249
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
250 <item id="html-view" type="IMPL::Web::Handler::View">
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
251 <contentType>text/html</contentType>
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
252 <view id="tt-loader" type="IMPL::Web::View::TTView">
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
253 <options type="HASH">
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
254 <INCLUDE_PATH type="IMPL::Config::Reference">
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
255 <target>IMPL::Config</target>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
256 <AppBase>view</AppBase>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
257 </INCLUDE_PATH>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
258 <INTERPOLATE>1</INTERPOLATE>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
259 <POST_CHOMP>1</POST_CHOMP>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
260 <ENCODING>utf-8</ENCODING>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
261 </options>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
262 <ext>.tt</ext>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
263 <initializer>global.tt</initializer>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
264 <layoutBase>layouts</layoutBase>
353
feeb3bc4a818 corrected error handling while loading templates
cin
parents: 339
diff changeset
265 </view>
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
266 <defaultDocument>default</defaultDocument>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
267 <selectors type="ARRAY">
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
268 <item>@HASH => dump</item>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
269 <item>@My::Data::Product => product/info</item>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
270 <item>{action:.*} @My::Data::Product => product/{action}</item>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
271 </selectors>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
272 </item>
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
273
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
274 =end code xml
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
275
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
276 =head1 DESCRIPTION
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
277
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
278 Подбирает шаблон для представления результата, полученного при выполнении следующего обработчика. При
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
279 выборе используется принцип похожий на селекторы C<CSS>, основывающийся на именах ресурсов и их типах
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
280 данных.
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
281
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
282 Данный обработчик понимает определенные свойства контекста:
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
283
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
284 =over
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
286 =item * C<resourceLocation>
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
287
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
288 В данном свойстве может быть передана информация о текущем расположении ресурса,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
289 для которого строится представление. Эта информация будет доступна в шаблоне
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
290 через свойство документа C<location>.
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
291
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
292 =item * C<environment>
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
293
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
294 В данном совойстве контекста передается дополнительная информация об окружении
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
295 ресурса, например, которую задали родительские ресурсы. Использование данного
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
296 свойства позволяет не загромождать ресурс реализацией функциональности по
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
297 поддержке окружения. Это свойство может быть ссылкой на функцию, что позволяет
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
298 формировать контекст только по необходимости, при этом указанная функция будет
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
299 выполнена только один раз, при первом обращении.
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
300
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
301 =back
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
302
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
303 =head1 SELECTORS
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
304
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
305 =begin text
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
306
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
307 syntax::= selector => template
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
308
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
309 selector::= ([>]segment-template[@class-name])
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
310
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
311 segment-template::= {'{'name:regular-expr'}'|segment-name}
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
312
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
313 name::= \w+
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
314
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
315 segment-name::= \S+
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
316
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
317 class-name::= name[(::name)]
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
318
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
319 url-template@class => template
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
320
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
321 shoes => product/list
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 281
diff changeset
322 /shop//{action:*.}@My::Data::Product => product/{action}
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
323
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
324 stuff >list => product/list
204
d63f9a92d6d4 +IMPL::Config::Include - simple way to include external config
sergey
parents: 203
diff changeset
325 details => product/details
203
68a59c3358ff Implemented templates selection mechanism
sergey
parents: 200
diff changeset
326
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
327 =end text
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
328
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
329
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
330 =cut
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents:
diff changeset
331