Line # Revision Author
1 3 ahitrov@rambler.ru package Contenido::MVC::MasonHandler;
2
3 use strict;
4 use base 'HTML::Mason::ApacheHandler';
5 use Contenido::Globals;
6 use Data::Dumper;
7
8 use Contenido::MVC::Dispatcher;
9 use HTTP::Status;
10 use Apache::Request;
11
12 sub new {
13 my $proto = shift;
14 my %opts = @_;
15 my $class = ref($proto) || $proto;
16
17 my $dispatcher_class = $opts{controller_dispatcher_class} || 'Contenido::MVC::Dispatcher';
18 my $controller_class = $opts{controller_class} || 'Contenido::MVC::Controller';
19 my $controller_main_class = $opts{controller_main_class} || 'Contenido::MVC::Controller';
20
21 # �������� ����� �����������
22 foreach (keys %opts) {
23 delete $opts{$_} if $_ =~ /^controller/i;
24 }
25
26 my $self = $proto->SUPER::new( %opts );
27
28 $self->set_dispatcher( $dispatcher_class->init( controller_class => $controller_class ) );
29 $self->get_dispatcher()->set_main_controller( controller_class => $controller_main_class );
30
31 return $self;
32 }
33
34
35 sub request_args {
36 my $self = shift;
37
38 my %args = $request->get_args();
39
40 if ($request->{'_mason_request_args_'} && ref $request->{'_mason_request_args_'} eq 'HASH') {
41 map { $args{ $_ } = $request->{'_mason_request_args_'}{$_} } keys %{ $request->{'_mason_request_args_'} };
42 }
43
44 return (\%args, $request->r(), undef);
45 }
46
47
48 sub get_dispatcher {
49 my $self = shift;
50 return $self->{dispatcher};
51 }
52
53 sub set_dispatcher {
54 my $self = shift;
55 $self->{dispatcher} = shift;
56 }
57
58 # ���������� �������
59 sub handle_request {
60 my $self = shift;
61 my $r = shift;
62
63 my $apr = $request->r();
64 my ($comp, $response);
65
66 # ���������� action
67 my $action = $self->get_dispatcher()->get_action( $apr );
68
69 # TODO: ������-�� ��� ������� ��������� ��� -
70 # $self->get_dispatcher()->get_main_controller()->prepare_request( $apr ) if $apr->is_initial_req();
71 # ��� �������� ��������� 504 ������ ��� ��������� � http://planeta.rambler.ru/
72 # ��� ������ index.html �������� ������ �� ����������...
73 $self->get_dispatcher()->get_main_controller()->prepare_request( $apr );
74
75 # ���������� begin
76 $self->get_dispatcher()->get_main_controller()->begin( $action );
77
78 if ($self->get_dispatcher()->get_main_controller()->is_response()) {
79 $comp = $self->get_dispatcher()->get_main_controller()->{comp};
80 $response = $self->get_dispatcher()->get_main_controller()->get_response();
81
82 return $self->mason_handle_request( $apr, $response, $comp );
83 }
84
85 if ($action) {
86 # ��������� request
87 $self->get_dispatcher()->get_controller( $action )->prepare_request( $apr );
88
89 # ����������
90 $self->execute($action);
91
92 # ��������� mason %ARGS
93 $self->get_dispatcher()->get_controller( $action )->set_mason_args();
94
95 $comp = $self->get_dispatcher()->get_controller( $action )->{comp};
96 $response = $self->get_dispatcher()->get_controller( $action )->get_response();
97
98 }
99
100 # ���������� end
101 $self->get_dispatcher()->get_main_controller()->end( $action );
102 if ($self->get_dispatcher()->get_main_controller()->is_response()) {
103 $comp = $self->get_dispatcher()->get_main_controller()->{comp};
104 $response = $self->get_dispatcher()->get_main_controller()->get_response();
105
106 return $self->mason_handle_request( $apr , $response, $comp );
107 }
108
109 if ($action && $self->get_dispatcher()->get_controller( $action )->is_response()) {
110 $comp = $self->get_dispatcher()->get_controller( $action )->{comp};
111 $response = $self->get_dispatcher()->get_controller( $action )->get_response();
112 return $self->mason_handle_request( $apr , $response, $comp );
113 }
114
115
116 return $self->mason_handle_request( $apr, $response, $comp );
117 }
118
119 # mason handle_request
120 sub mason_handle_request {
121 my $self = shift;
122 my $apr = shift;
123 my $status = shift;
124 my $comp = shift;
125
126 if (defined $status) {
127 return $status;
128 }
129
130 # ��������� ����������� ����������
131 if ($comp) {
132 my $interp = $self->interp;
133 foreach (map $_->[1], $interp->comp_root_array) {
134 if (-e $_.$comp) {
135 $apr->filename($_.$comp);
136 last;
137 }
138 }
139 }
140
141 return $self->SUPER::handle_request( $apr );
142 }
143
144 # ���������� ��������� �����������, ����������� �� ������ ����
145 sub execute {
146 my $self = shift;
147 my $action = shift;
148
149 return undef unless $action && ref($action) && $action->{'sub'};
150 my $method = $action->{'sub'};
151 my $begin_method = exists $action->{'begin'} ? $action->{'begin'} : 'begin';
152 my $end_method = exists $action->{'end'} ? $action->{'end'} : 'end';
153
154 if ($begin_method) {
155 $self->get_dispatcher()->get_controller( $action )->$begin_method( $action );
156 }
157
158 if ($self->get_dispatcher()->get_controller( $action )->is_response()) {
159 return undef;
160 }
161
162 $self->get_dispatcher()->get_controller( $action )->$method( $action );
163
164 if ($end_method) {
165 $self->get_dispatcher()->get_controller( $action )->$end_method( $action );
166 }
167
168 return undef;
169 }
170
171
172 1;