annotate Lib/IMPL/Web/Handler/ViewSelector.pm @ 210:6adaeb86945d

added IMPL::Web::AutoLocator
author sergey
date Tue, 29 May 2012 20:07:22 +0400
parents 3d433a977e3b
children d6e2ea24af08
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 => {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
8 NotAcceptable => 'IMPL::Web::NotAcceptableException'
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
9 },
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
10 base => {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
11 'IMPL::Object' => undef,
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
12 'IMPL::Object::Autofill' => '@_',
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
13 'IMPL::Object::Serializable' => undef
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
14 }
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 BEGIN {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
18 public property views => PROP_ALL | PROP_LIST;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
19 public property fallback => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
20 public property types => PROP_ALL;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
21 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
22
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
23 sub Invoke {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
24 my ($this,$action,$next) = @_;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
25
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
26 my $handler;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
27 my $path = $action->query->path_info;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
28
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
29 if ($this->types and $path =~ m/\.(\w+)$/) {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
30 my $forced;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
31 if ($forced = $this->types->{$1} and $action->query->Accept($forced) ) {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
32 ($handler) = grep eval { $_->can('contentType') } && $_->contentType eq $forced, $this->views;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
33 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
34 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
35
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
36 if (not $handler) {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
37
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
38 my @handlers =
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
39 sort {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
40 $b->{preference} <=> $a->{preference}
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
41 } map {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
42 {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
43 handler => $_,
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
44 preference =>
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
45 eval { $_->can('contentType') } ? $action->query->Accept($_->contentType) : 0
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
46 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
47 } $this->views;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
48
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
49 my $info = shift @handlers;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
50 $handler = $info ? $info->{handler} : undef;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
51
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
52 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
53
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
54 die NotAcceptable->new(map { eval {$_->can('contentType') } ? $_->contentType : () } $this->views )
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
55 unless $handler;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
56
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
57 return $handler->Invoke($action,$next);
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
58 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
59
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
60 1;
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
61
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
62 __END__
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
63
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
64 =pod
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
65
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
66 =head1 NAME
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
67
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
68 C<IMPL::Web::Handler::ViewSelector> - Выбор нужного представления на основе заголовка C<Accept>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
69
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
70 =head1 DESCRIPTION
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 Использует заголовок запроса C<Accept> для выбора подходящего представления, если задано свойство C<types>,
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
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
75 =head1 MEMBERS
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
76
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
77 =head2 C<[get,set,list]views>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
78
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
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
81 =head2 C<[get,set]types>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
82
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
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
85 =cut