annotate Lib/IMPL/Web/Handler/TTView.pm @ 290:7b0dad6117d5

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