Mercurial > pub > Impl
comparison Lib/IMPL/Web/Application.pm @ 285:546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
*IMPL::Web::Application: refactoring
-Removed obsolete IMPL::Text modules
author | cin |
---|---|
date | Mon, 18 Feb 2013 02:55:59 +0400 |
parents | a02b110da931 |
children | 63709a4e6da0 |
comparison
equal
deleted
inserted
replaced
284:f2a6bc5f3184 | 285:546957c50a36 |
---|---|
11 Locator => 'IMPL::Web::AutoLocator', | 11 Locator => 'IMPL::Web::AutoLocator', |
12 TAction => 'IMPL::Web::Application::Action', | 12 TAction => 'IMPL::Web::Application::Action', |
13 HttpResponse => 'IMPL::Web::HttpResponse', | 13 HttpResponse => 'IMPL::Web::HttpResponse', |
14 TFactory => '-IMPL::Object::Factory', | 14 TFactory => '-IMPL::Object::Factory', |
15 Exception => 'IMPL::Exception', | 15 Exception => 'IMPL::Exception', |
16 ArgException => '-IMPL::InvalidArgumentException', | |
16 InvalidOperationException => '-IMPL::InvalidOperationException', | 17 InvalidOperationException => '-IMPL::InvalidOperationException', |
17 Loader => 'IMPL::Code::Loader' | 18 Loader => 'IMPL::Code::Loader' |
18 }, | 19 }, |
19 base => [ | 20 base => [ |
20 'IMPL::Config' => '@_', | 21 'IMPL::Config' => '@_', |
21 'IMPL::Object::Singleton' => '@_' | 22 'IMPL::Object::Singleton' => undef |
22 ], | 23 ], |
23 props => [ | 24 props => [ |
24 baseUrl => PROP_RW, | 25 baseUrl => PROP_RW, |
25 actionFactory => PROP_RW, | 26 actionFactory => PROP_RW, |
26 handlers => PROP_RW | PROP_LIST, | 27 handlers => PROP_RW | PROP_LIST, |
27 security => PROP_RW, | 28 security => PROP_RW, |
28 options => PROP_RW, | 29 options => PROP_RW, |
29 requestCharset => PROP_RW, | 30 requestCharset => PROP_RW, |
30 output => PROP_RW, | 31 output => PROP_RW, |
31 location => PROP_RO | 32 location => PROP_RO, |
33 _handler => PROP_RW | |
32 ] | 34 ] |
33 }; | 35 }; |
34 | 36 |
35 sub CTOR { | 37 sub CTOR { |
36 my ($this) = @_; | 38 my ($this) = @_; |
43 | 45 |
44 $this->actionFactory(TAction) unless $this->actionFactory; | 46 $this->actionFactory(TAction) unless $this->actionFactory; |
45 $this->location(Locator->new(base => $this->baseUrl)); | 47 $this->location(Locator->new(base => $this->baseUrl)); |
46 } | 48 } |
47 | 49 |
48 sub Run { | 50 sub ProcessRequest { |
49 my ($this) = @_; | 51 my ($this,$q) = @_; |
52 | |
53 die ArgException->new(q => 'A query is required') | |
54 unless $q; | |
55 | |
56 my $handler = $this->_handler; | |
57 unless ($handler) { | |
58 $handler = _ChainHandler( $_, $handler ) foreach $this->handlers; | |
59 $this->_handler($handler); | |
60 } | |
61 | |
62 my $action = $this->actionFactory->new( | |
63 query => $q, | |
64 application => $this, | |
65 ); | |
50 | 66 |
51 my $handler; | 67 eval { |
68 my $result = $handler->($action); | |
52 | 69 |
53 $handler = _ChainHandler( $_, $handler ) foreach $this->handlers; | 70 die InvalidOperationException->new("Invalid handlers result. A reference to IMPL::Web::HttpResponse is expexted.") |
71 unless eval { $result->isa(HttpResponse) }; | |
54 | 72 |
55 while ( my $query = $this->FetchRequest() ) { | 73 $result->PrintResponse( $this->output ); |
74 }; | |
75 if ($@) { | |
76 my $e = $@; | |
56 | 77 |
57 my $action = $this->actionFactory->new( | 78 HttpResponse->InternalError( |
58 query => $query, | 79 type => 'text/plain', |
59 application => $this, | 80 charset => 'utf-8', |
60 ); | 81 body => $e |
82 )->PrintResponse( $this->output ); | |
61 | 83 |
62 eval { | 84 } |
63 my $result = $handler->($action); | |
64 | |
65 die InvalidOperationException->new( | |
66 "Invalid handlers result. A reference to IMPL::Web::HttpResponse is expexted." | |
67 ) unless eval { $result->isa(HttpResponse) }; | |
68 | |
69 $result->PrintResponse( $this->output ); | |
70 }; | |
71 if ($@) { | |
72 my $e = $@; | |
73 | |
74 HttpResponse->InternalError( | |
75 type => 'text/plain', | |
76 charset => 'utf-8', | |
77 body => $e | |
78 )->PrintResponse( $this->output ); | |
79 | |
80 } | |
81 } | |
82 } | 85 } |
83 | 86 |
84 sub _ChainHandler { | 87 sub _ChainHandler { |
85 my ( $handler, $next ) = @_; | 88 my ( $handler, $next ) = @_; |
86 | 89 |
125 die new IMPL::InvalidArgumentException( "An invalid handler supplied", | 128 die new IMPL::InvalidArgumentException( "An invalid handler supplied", |
126 $handler ); | 129 $handler ); |
127 } | 130 } |
128 } | 131 } |
129 | 132 |
130 sub FetchRequest { | |
131 | |
132 return; | |
133 } | |
134 | |
135 1; | 133 1; |
136 | 134 |
137 __END__ | 135 __END__ |
138 | 136 |
139 =pod | 137 =pod |
140 | 138 |
141 =head1 NAME | 139 =head1 NAME |
142 | 140 |
143 C<IMPL::Web::Application> Базовай класс для создания экземпляров приложения | 141 C<IMPL::Web::Application> Базовай класс для веб-приложения |
144 | |
145 =head1 SYNOPSIS | |
146 | |
147 =begin code | |
148 | |
149 use IMPL::require { | |
150 App => 'IMPL::Web::Application' | |
151 }; | |
152 | |
153 my $instance = App->spawn(); # will use ./IMPL/Web/Application.xml as configuration | |
154 | |
155 $instance->Run; | |
156 | |
157 =end code | |
158 | |
159 =head1 DESCRIPTION | |
160 | |
161 Создает экземпляр объекта, который получает и обрабатывает C<HTTP> запрос. | |
162 Приложение можно загрузить из C<xml> файла в котором описано состояние свойств, | |
163 для этого используется механизм C<IMPL::Serialization>. | |
164 | |
165 Приложение представлет собой модульную конструкцию, которая состоит из цепочки | |
166 обработчиков. Цепочка обработчиков вызывается снизу вверх, при этом каждый | |
167 обработчик самостоятельно рекурсивно вызывает следующий (более высокого уровня). | |
168 | |
169 См. также C<IMPL::Web::CGIApplication> | |
170 | 142 |
171 =cut | 143 =cut |