Mercurial > pub > Impl
annotate Lib/IMPL/Web/AutoLocator.pm @ 400:ef37ec102dab
redirection support while fetching child resources
author | cin |
---|---|
date | Wed, 14 May 2014 17:27:32 +0400 |
parents | 793cc7f0a7e7 |
children | 7171a8e2e2ba |
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 | |
326
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
58 sub Sibling { |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
59 my $this = shift; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
60 my $child = shift or die ArgumentException->new("a child resource identifier is required"); |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
61 die ArgumentException->new("a child resource can't be a reference") if ref $child; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
62 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
63 # safe |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
64 $child = uri_escape($child); |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
65 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
66 my %args; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
67 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
68 if($this->base =~ /(.*?)(\/[^\/]*)?$/) { |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
69 $args{base} = join('/',$1,$child); |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
70 } else { |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
71 $args{base} = $child; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
72 } |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
73 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
74 $args{view} = $this->view if $this->view; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
75 $args{hash} = $this->hash if $this->hash; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
76 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
77 if (@_) { |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
78 my $query = shift; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
79 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
80 $args{query} = ref $query eq 'HASH' ? hashMerge($this->query,$query) : $query; |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
81 } |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
82 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
83 return $this->new(%args); |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
84 |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
85 } |
793cc7f0a7e7
IMPL::Web::AutoLocator added Sibling method for locating sibling resources
sergey
parents:
292
diff
changeset
|
86 |
292 | 87 sub Query { |
88 my ($this,$query) = @_; | |
89 | |
90 my %args; | |
91 | |
92 $args{base} = $this->base; | |
93 $args{view} = $this->view if $this->view; | |
94 $args{hash} = $this->hash if $this->hash; | |
95 $args{query} = ref $query eq 'HASH' ? hashMerge($this->query,$query) : $query; | |
96 | |
97 return $this->new(%args); | |
98 } | |
99 | |
211 | 100 sub SetView { |
101 my ($this,$newView) = @_; | |
102 | |
103 $this->view($newView); | |
104 | |
105 return $this; | |
106 } | |
107 | |
108 sub url { | |
109 my ($this) = @_; | |
110 | |
111 my $url = URI->new($this->view ? $this->base . "." . $this->view : $this->base); | |
112 $url->query_form($this->query); | |
113 $url->fragment($this->hash); | |
114 | |
115 return $url; | |
116 } | |
117 | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
118 sub ToAbsolute { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
119 my ($this,$baseUrl) = @_; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
120 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
121 return URI->new_abs( $this->url, $baseUrl ); |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
122 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
244
diff
changeset
|
123 |
244 | 124 sub toString { |
125 shift->url->as_string(); | |
126 } | |
127 | |
211 | 128 sub AUTOLOAD { |
129 our $AUTOLOAD; | |
130 | |
131 (my $method) = ($AUTOLOAD =~ m/(\w+)$/); | |
132 | |
133 return if $method eq 'DESTROY'; | |
134 | |
135 my $this = shift; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
136 return $this->Child($method,@_); |
210 | 137 } |
138 | |
139 | |
140 | |
141 1; | |
142 | |
143 __END__ | |
144 | |
145 =head1 NAME | |
146 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
147 C<IMPL::Web::AutoLocator> - Обертка вокруг адреса ресурса. |
210 | 148 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
149 =head1 SYNOPSIS |
210 | 150 |
151 =begin code | |
152 | |
153 use IMPL::require { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
154 Locator => 'IMPL::Web::AutoLocator' |
210 | 155 }; |
156 | |
212 | 157 my $bugTracker = Locator->new(base => "http://myhost.org/bugzilla")->SetView("cgi"); |
210 | 158 |
159 my $bug = $bugTracker->show_bug({id = 1}); | |
160 | |
212 | 161 my $wikiPages = Locator->new(base => "http://myhost.org/wiki/bin/view"); |
210 | 162 |
163 my $page = $wiki->Main->HowTo; | |
164 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
165 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
|
166 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
167 my $editIco = $images->icons->small->edit; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
168 |
210 | 169 =end code |
170 | |
171 =head1 DESCRIPTION | |
172 | |
229
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 иерархически организованных ресурсов. позволяет гибко работать с параметрами |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
175 запроса и хешем. Для постоты чтения реализует метод C<AUTOLOAD> для доступа |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
176 к дочерним ресурсам. |
211 | 177 |
178 =head1 MEMBERS | |
179 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
180 =head2 C<CTOR(%args)> |
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 Создает новый объект расположение. Позволяет задать путь, расширение, параметры |
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
185 =over |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
186 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
187 =item * C<base> |
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
191 =item * C<view> |
211 | 192 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
193 Задает суфикс, обозначающий представление ресурса, аналогично расширению у |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
194 файлов. Данный суффикс может использоваться контроллером для выбора |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
195 представления ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
196 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
197 =item * C<query> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
198 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
199 Ссылка на хеш с параметрами запроса |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
200 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
201 =item * C<hash> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
202 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
203 Часть C<uri> обозначающая фрагмент документа (все, что идет после символа C<#>). |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
204 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
205 =back |
211 | 206 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
207 =head2 C<Child($child[,$query])> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
208 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
209 Получает расположение дочернего ресурса. При этом cоздается новый объект адреса ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
210 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
211 =head2 C<SetView($view)> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
212 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
213 Позволяет указать представление (расширение) у текущего адреса ресурса. Изменяет |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
214 представление и возвращает измененный адрес ресурса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
215 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
216 =head2 C<[get]base> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
217 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
218 Базовый адрес, относительно которого будут получены дочерние ресурсы. |
211 | 219 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
220 =head2 C<[get,set]view> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
221 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
222 Представление для ресурсов, аналогично расширению у файлов. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
223 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
224 =head2 C<[get,set]query> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
225 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
226 Ссылка на хеш с параметрами для C<GET> запроса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
227 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
228 =head2 C<[get,set]hash> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
229 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
230 Часть адреса ресурса, отвечающая за фрагмент. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
231 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
232 =head2 C<[get]url> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
233 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
234 Объект C<URI> для текущего адреса. |
211 | 235 |
236 =head2 C<AUTLOAD> | |
237 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
212
diff
changeset
|
238 Перенаправляет вызовы методов в метод C<Child> передавая первым параметром имя метода. |
212 | 239 |
240 =cut | |
241 |