annotate lib/IMPL/Web/Handler/ViewSelector.pm @ 413:af8d359ee4cc ref20150831

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