annotate Lib/IMPL/Web/Handler/ViewSelector.pm @ 213:d6e2ea24af08

sync
author sergey
date Fri, 03 Aug 2012 01:15:15 +0400
parents 3d433a977e3b
children 47f77e6409f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
1 package IMPL::Web::Handler::ViewSelector;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
2 use strict;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
3
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
4 use IMPL::lang qw(:declare :constants);
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
5
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
6 use IMPL::declare {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
7 require => {
213
sergey
parents: 208
diff changeset
8 NotAcceptable => 'IMPL::Web::NotAcceptableException',
sergey
parents: 208
diff changeset
9 ViewResult => 'IMPL::Web::Application::ViewResult'
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
10 },
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
11 base => {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
12 'IMPL::Object' => undef,
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
13 'IMPL::Object::Autofill' => '@_',
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
14 'IMPL::Object::Serializable' => undef
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
15 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
16 };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
17
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
18 BEGIN {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
19 public property views => PROP_ALL | PROP_LIST;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
20 public property fallback => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
21 public property types => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
22 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
23
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
24 sub Invoke {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
25 my ($this,$action,$next) = @_;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
26
213
sergey
parents: 208
diff changeset
27 my $result = $next ? $next->($action) : undef;
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
28
213
sergey
parents: 208
diff changeset
29 my $model;
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
30
213
sergey
parents: 208
diff changeset
31 if( eval { $result->isa(ViewResult) } ) {
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
32
213
sergey
parents: 208
diff changeset
33 my $handler;
sergey
parents: 208
diff changeset
34 my $path = $action->query->path_info;
sergey
parents: 208
diff changeset
35
sergey
parents: 208
diff changeset
36 if ($this->types and $path =~ m/\.(\w+)$/) {
sergey
parents: 208
diff changeset
37 my $forced;
sergey
parents: 208
diff changeset
38 if ($forced = $this->types->{$1} and $action->query->Accept($forced) ) {
sergey
parents: 208
diff changeset
39 ($handler) = grep eval { $_->can('contentType') } && $_->contentType eq $forced, $this->views;
sergey
parents: 208
diff changeset
40 }
sergey
parents: 208
diff changeset
41 }
sergey
parents: 208
diff changeset
42
sergey
parents: 208
diff changeset
43 if (not $handler) {
sergey
parents: 208
diff changeset
44
sergey
parents: 208
diff changeset
45 my @handlers =
sergey
parents: 208
diff changeset
46 sort {
sergey
parents: 208
diff changeset
47 $b->{preference} <=> $a->{preference}
sergey
parents: 208
diff changeset
48 } map {
sergey
parents: 208
diff changeset
49 {
sergey
parents: 208
diff changeset
50 handler => $_,
sergey
parents: 208
diff changeset
51 preference =>
sergey
parents: 208
diff changeset
52 eval { $_->can('contentType') } ? $action->query->Accept($_->contentType) : 0
sergey
parents: 208
diff changeset
53 }
sergey
parents: 208
diff changeset
54 } $this->views;
sergey
parents: 208
diff changeset
55
sergey
parents: 208
diff changeset
56 my $info = shift @handlers;
sergey
parents: 208
diff changeset
57 $handler = $info ? $info->{handler} : undef;
sergey
parents: 208
diff changeset
58
sergey
parents: 208
diff changeset
59 }
sergey
parents: 208
diff changeset
60
sergey
parents: 208
diff changeset
61 die NotAcceptable->new(map { eval {$_->can('contentType') } ? $_->contentType : () } $this->views )
sergey
parents: 208
diff changeset
62 unless $handler;
sergey
parents: 208
diff changeset
63
sergey
parents: 208
diff changeset
64 return $handler->Invoke($action,sub { $result });
sergey
parents: 208
diff changeset
65 } else {
sergey
parents: 208
diff changeset
66 return $result;
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
67 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
68 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
69
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
70 1;
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
71
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
72 __END__
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
73
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
74 =pod
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
75
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
76 =head1 NAME
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
77
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
78 C<IMPL::Web::Handler::ViewSelector> - Выбор нужного представления на основе заголовка C<Accept>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
79
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
80 =head1 DESCRIPTION
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
81
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
82 Использует заголовок запроса C<Accept> для выбора подходящего представления, если задано свойство C<types>,
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
83 пытается в первую очередь по расширению определить, какое представление подходит.
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
84
213
sergey
parents: 208
diff changeset
85 В случаях, когда не требуется строить представление для данных (например, при перенаправлении к другому
sergey
parents: 208
diff changeset
86 ресурсу или если нет данных), нужно, чтобы данному обработчику был возвращен
sergey
parents: 208
diff changeset
87 L<IMPL::Web::Application::ActionResult>, который будет просто передан далее.
sergey
parents: 208
diff changeset
88
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
89 =head1 MEMBERS
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
90
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
91 =head2 C<[get,set,list]views>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
92
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
93 Список представлений, которые могут быть возвращены.
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
94
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
95 =head2 C<[get,set]types>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
96
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
97 Хеш с соотвествием между расширением и типом содержимого, для подсказки при выборе представления.
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
98
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
99 =cut