Mercurial > pub > Impl
annotate Lib/IMPL/Web/AutoLocator.pm @ 256:32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
Dirty hacks to handle binary data
RestController doesn't deal with file extensions anymore.
author | sergey |
---|---|
date | Wed, 12 Dec 2012 04:29:50 +0400 |
parents | a02b110da931 |
children | 6dc1c369eb71 |
rev | line source |
---|---|
210 | 1 package IMPL::Web::AutoLocator; |
2 use strict; | |
3 | |
244 | 4 use overload '""' => 'toString'; |
5 | |
232 | 6 use IMPL::Const qw(:prop); |
244 | 7 use IMPL::lang qw(:hash); |
8 use IMPL::clone qw(clone); | |
210 | 9 use URI; |
10 use URI::Escape; | |
11 use IMPL::declare { | |
12 require => { | |
13 Exception => 'IMPL::Exception', | |
14 ArgumentException => '-IMPL::InvalidArgumentException' | |
15 }, | |
16 base => [ | |
17 'IMPL::Object' => undef, | |
18 'IMPL::Object::Autofill' => '@_', | |
19 'IMPL::Object::Serializable' => '@_' | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
20 ], |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
21 props => [ |
232 | 22 base => PROP_RO, |
23 view => PROP_RW, | |
24 query => PROP_RW, | |
25 hash => PROP_RW | |
210 | 26 ] |
27 }; | |
28 | |
244 | 29 sub Clone { |
30 my $this = shift; | |
31 | |
32 return clone($this); | |
33 } | |
34 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
35 sub Child { |
210 | 36 my $this = shift; |
211 | 37 my $child = shift or die ArgumentException->new("a child resource identifier is required"); |
38 die ArgumentException->new("a child resource can't be a reference") if ref $child; | |
210 | 39 |
40 # safe | |
41 $child = uri_escape($child); | |
42 | |
43 my %args; | |
44 | |
45 $args{base} = $this->base =~ /\/$/ ? $this->base . $child : $this->base . '/' . $child; | |
46 $args{view} = $this->view if $this->view; | |
47 $args{hash} = $this->hash if $this->hash; | |
48 | |
49 if (@_) { | |
50 my $query = shift; | |
51 | |
52 $args{query} = ref $query eq 'HASH' ? hashMerge($this->query,$query) : $query; | |
53 } | |
54 | |
212 | 55 return $this->new(%args); |
211 | 56 } |
57 | |
58 sub SetView { | |
59 my ($this,$newView) = @_; | |
60 | |
61 $this->view($newView); | |
62 | |
63 return $this; | |
64 } | |
65 | |
66 sub url { | |
67 my ($this) = @_; | |
68 | |
69 my $url = URI->new($this->view ? $this->base . "." . $this->view : $this->base); | |
70 $url->query_form($this->query); | |
71 $url->fragment($this->hash); | |
72 | |
73 return $url; | |
74 } | |
75 | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
76 sub ToAbsolute { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
77 my ($this,$baseUrl) = @_; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
78 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
79 return URI->new_abs( $this->url, $baseUrl ); |
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 |
244 | 82 sub toString { |
83 shift->url->as_string(); | |
84 } | |
85 | |
211 | 86 sub AUTOLOAD { |
87 our $AUTOLOAD; | |
88 | |
89 (my $method) = ($AUTOLOAD =~ m/(\w+)$/); | |
90 | |
91 return if $method eq 'DESTROY'; | |
92 | |
93 my $this = shift; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
94 return $this->Child($method,@_); |
210 | 95 } |
96 | |
97 | |
98 | |
99 1; | |
100 | |
101 __END__ | |
102 | |
103 =head1 NAME | |
104 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
105 C<IMPL::Web::AutoLocator> - Обертка вокруг адреса ресурса. |
210 | 106 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
107 =head1 SYNOPSIS |
210 | 108 |
109 =begin code | |
110 | |
111 use IMPL::require { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
112 Locator => 'IMPL::Web::AutoLocator' |
210 | 113 }; |
114 | |
212 | 115 my $bugTracker = Locator->new(base => "http://myhost.org/bugzilla")->SetView("cgi"); |
210 | 116 |
117 my $bug = $bugTracker->show_bug({id = 1}); | |
118 | |
212 | 119 my $wikiPages = Locator->new(base => "http://myhost.org/wiki/bin/view"); |
210 | 120 |
121 my $page = $wiki->Main->HowTo; | |
122 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
123 my $images = Locator->new(base => "http://static.myhost.org/images", view => "png"); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
124 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
125 my $editIco = $images->icons->small->edit; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
126 |
210 | 127 =end code |
128 | |
129 =head1 DESCRIPTION | |
130 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
131 Для удобстав навигации по ресурсам, полностью отражает классическую структуру |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
132 иерархически организованных ресурсов. позволяет гибко работать с параметрами |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
133 запроса и хешем. Для постоты чтения реализует метод C<AUTOLOAD> для доступа |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
134 к дочерним ресурсам. |
211 | 135 |
136 =head1 MEMBERS | |
137 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
138 =head2 C<CTOR(%args)> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
139 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
140 Создает новый объект расположение. Позволяет задать путь, расширение, параметры |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
141 запроса и фрагмент ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
142 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
143 =over |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
144 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
145 =item * C<base> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
146 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
147 Строка с базовым адресом для дочерних ресурсов. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
148 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
149 =item * C<view> |
211 | 150 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
151 Задает суфикс, обозначающий представление ресурса, аналогично расширению у |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
152 файлов. Данный суффикс может использоваться контроллером для выбора |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
153 представления ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
154 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
155 =item * C<query> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
156 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
157 Ссылка на хеш с параметрами запроса |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
158 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
159 =item * C<hash> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
160 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
161 Часть C<uri> обозначающая фрагмент документа (все, что идет после символа C<#>). |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
162 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
163 =back |
211 | 164 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
165 =head2 C<Child($child[,$query])> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
166 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
167 Получает расположение дочернего ресурса. При этом cоздается новый объект адреса ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
168 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
169 =head2 C<SetView($view)> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
170 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
171 Позволяет указать представление (расширение) у текущего адреса ресурса. Изменяет |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
172 представление и возвращает измененный адрес ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
173 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
174 =head2 C<[get]base> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
175 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
176 Базовый адрес, относительно которого будут получены дочерние ресурсы. |
211 | 177 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
178 =head2 C<[get,set]view> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
179 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
180 Представление для ресурсов, аналогично расширению у файлов. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
181 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
182 =head2 C<[get,set]query> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
183 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
184 Ссылка на хеш с параметрами для C<GET> запроса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
185 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
186 =head2 C<[get,set]hash> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
187 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
188 Часть адреса ресурса, отвечающая за фрагмент. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
189 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
190 =head2 C<[get]url> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
191 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
192 Объект C<URI> для текущего адреса. |
211 | 193 |
194 =head2 C<AUTLOAD> | |
195 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
196 Перенаправляет вызовы методов в метод C<Child> передавая первым параметром имя метода. |
212 | 197 |
198 =cut | |
199 |