Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/Action.pm @ 65:2840c4c85db8
Application configuration improvements
Documentation
| author | wizard |
|---|---|
| date | Tue, 16 Mar 2010 17:36:13 +0300 |
| parents | 76b878ad6596 |
| children | 9f5795a10939 |
| rev | line source |
|---|---|
| 52 | 1 package IMPL::Web::Application::Action; |
| 55 | 2 use strict; |
| 52 | 3 |
| 62 | 4 use base qw(IMPL::Object IMPL::Object::Autofill); |
| 52 | 5 |
|
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
6 __PACKAGE__->PassThroughArgs; |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
7 |
| 52 | 8 use IMPL::Class::Property; |
| 9 | |
| 10 BEGIN { | |
| 11 public property application => prop_get | owner_set; | |
| 62 | 12 public property query => prop_get | owner_set; |
| 52 | 13 public property response => prop_get | owner_set; |
| 65 | 14 public property responseFactory => prop_get | owner_set; |
| 55 | 15 |
| 16 private property _entryPoint => prop_all; | |
| 17 } | |
| 18 | |
| 65 | 19 sub CTOR { |
| 20 my ($this) = @_; | |
| 21 | |
| 22 $this->responseFactory('IMPL::Web::Application::Response') unless $this->responseFactory; | |
| 23 $this->response( $this->responseFactory->new(query => $this->query) ); | |
| 24 } | |
|
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
25 |
| 55 | 26 sub Invoke { |
| 27 my ($this) = @_; | |
| 28 | |
| 29 if ($this->_entryPoint) { | |
| 30 $this->_entryPoint->(); | |
| 31 } else { | |
| 32 die new IMPL::InvalidOperationException("At least one handler is required"); | |
| 33 } | |
| 34 } | |
| 35 | |
| 65 | 36 sub ReinitResponse { |
| 37 my ($this) = @_; | |
| 38 | |
| 39 die new IMPL::InvalidOperationException("Response already sent") if $this->response->isHeaderPrinted; | |
| 40 | |
| 41 $this->response->Discard; | |
| 42 $this->response($this->responseFactory->new(query => $this->query)); | |
| 43 } | |
| 44 | |
| 55 | 45 sub ChainHandler { |
| 46 my ($this,$handler) = @_; | |
| 47 | |
| 48 my $delegateNext = $this->_entryPoint(); | |
| 49 | |
| 50 if (ref $handler eq 'CODE') { | |
| 56 | 51 $this->_entryPoint( sub { |
| 55 | 52 $handler->($this,$delegateNext); |
| 56 | 53 } ); |
|
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
54 } elsif (ref $handler and UNIVERSAL::isa($handler,'IMPL::Web::QueryHandler')) { |
| 56 | 55 $this->_entryPoint( sub { |
| 55 | 56 $handler->Invoke($this,$delegateNext); |
| 56 | 57 } ); |
| 58 } elsif ($handler and not ref $handler) { | |
| 59 | |
|
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
60 if (my $method = $this->can($handler) ) { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
61 $this->_entryPoint( sub { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
62 $method->($this,$delegateNext); |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
63 } ); |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
64 } else { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
65 { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
66 no strict 'refs'; |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
67 eval "require $handler; 1;" or die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler,"Failed to load module") unless keys %{"${handler}::"}; |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
68 } |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
69 |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
70 if (UNIVERSAL::isa($handler,'IMPL::Web::QueryHandler')) { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
71 $this->_entryPoint( sub { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
72 $handler->Invoke($this,$delegateNext); |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
73 } ); |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
74 } else { |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
75 die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler); |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
76 } |
|
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
77 } |
| 55 | 78 } else { |
|
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
79 die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler); |
| 55 | 80 } |
| 81 | |
| 52 | 82 } |
| 83 | |
| 84 1; | |
| 85 | |
| 86 __END__ | |
| 87 | |
| 88 =pod | |
| 89 | |
| 90 =head1 DESCRIPTION | |
| 91 | |
| 57 | 92 Определяет порядок выполнения запроса. |
| 52 | 93 |
| 57 | 94 Запрос выполняется последовательным вызовом цепочки обработчиков, при этом обработчики |
| 95 сами вызывают следующие. | |
| 52 | 96 |
| 97 Типичная цепочка может быть такой, в порядке добавления | |
| 98 | |
| 57 | 99 SecCallToMethod($target,$method) |
| 100 AuthenticateMethod | |
| 101 TDocumentOut($file) | |
| 52 | 102 |
| 103 что приведет к следующей последовательности | |
| 104 | |
| 105 Action->Invoke() { | |
| 106 TDocumentOut->Invoke($Action,$nextHandler) { | |
| 107 my $result = $nextHandler() { | |
| 108 $AuthenticateMethod($Action,$nextHandler) { | |
| 62 | 109 my $context = $Action->application->security->Authenticate($Action->query,$Action->response); |
| 52 | 110 return $context->Impersonate($nextHandler) { |
| 111 $objSecCallToMethod->Invoke($Action,undef) { | |
| 112 IMPL::Security->AccessCheck($target,$method); | |
| 113 return $target->$method(); | |
| 114 } | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 $this->format($result,$Action->response->streamBody); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 или как альтернатива может быть еще | |
| 123 | |
| 124 $objSecCallToMethod($target,$method) | |
| 125 $AuthenticateMethod | |
| 126 $TransfromToSimpleData | |
| 127 $JSONOut | |
| 128 | |
| 129 В данной цепочке также происходит вызов метода, но его результат потом преобразуется | |
| 130 в простые структуры и передается JSON преобразователю. Таким образом модулю логики | |
| 131 не требуется знать о выходном формате, всю работу проделают дополнительные фильтры. | |
| 132 | |
| 56 | 133 =head1 HANDLERS |
| 134 | |
| 135 =head2 subroutines | |
| 136 | |
| 137 =over | |
| 138 | |
| 139 =item CODE ref | |
| 140 | |
| 141 Ссылка на процедуру может являться обработчиком, при этом функция будет вызвана с | |
| 142 двумя параметрами: ссылкой на action объект, и точкой входа следующего обработчика. | |
| 143 | |
| 144 =item Method Name | |
| 145 | |
| 146 Имя метода, передается в виде строки. У текущего объекта action ищется метод с | |
| 147 указанным именем, после чего используется ссылка на этот метод для вызова с двумя | |
| 148 параметрами: ссылкой на action объект, и точкой входа следующего обработчика. | |
| 149 | |
| 150 Получается вызов идентичный следующему C<< $action->MethodName($nextHandler) >>; | |
| 151 | |
| 152 =back | |
| 153 | |
| 57 | 154 =head2 C< IMPL::Web::Application::QueryHandler > |
| 155 | |
| 156 Любой объект наследованный от C< IMPL::Web::Application::QueryHandler > может быть | |
| 157 использован в качестве обработчика запроса | |
| 158 | |
| 52 | 159 =cut |
