Action.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Action/State.pm ↑ Summary
1 package Apache::Action;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw($VERSION @ISA %ACTIONS);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Apache::Constants qw(:response);
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 $VERSION = 0.02;
11 @ISA = qw(Exporter);
12  
13 # Class methods
14  
15 sub register {
16 my $self = shift;
17 my $app = shift;
18 my $module = shift;
19 my $actions = ($#_ == 0) ? { %{ (shift) } } : { @_ };
20  
21 my ($package) = caller;
22  
23 foreach (keys %$actions) {
24 my $val = $actions->{$_};
25 my $subref;
26  
27 if (ref($val) eq 'CODE') {
28 # val is a coderef
29 $subref = $val;
30 }
31 elsif (ref($val)) {
32 die "Cannot use reference to " . ref($val) .
33 " as an action routine.";
34 }
35 else {
36 no strict qw(refs);
37 my $subref = ($val =~ /::/)
38 ? \&{"$val"}
39 : \&{"$package\::$val"};
40 die "No such subroutine " . $val
41 unless $subref;
42 }
43  
44 $ACTIONS{$app}->{$module}->{$_} = $subref;
45 }
46 }
47  
48 # Instance methods
49  
50 sub new {
51 my $class = shift;
52 my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
53 die "No Request in Apache::Action" unless $self->{Request};
54 die "No Session in Apache::Action" unless $self->{Session};
55 die "No State in Apache::Action" unless $self->{State};
56 $self->{Errors} = [ ];
57 return bless $self, $class;
58 }
59  
60 sub run {
61 my ($self) = @_;
62  
63 my $httparg = $self->param('action');
64 $self->param('action', undef);
65 return OK unless length($httparg);
66  
67 my ($app, $module, $action) = split("/", $httparg);
68  
69 return OK unless
70 length($app) &&
71 length($module) &&
72 length($action);
73  
74 unless (exists $ACTIONS{$app}) {
75 $self->error("No such application $app.");
76 $self->{State}->error($self->errors);
77 return NOT_FOUND;
78 }
79  
80 unless (exists $ACTIONS{$app}->{$module}) {
81 $self->error("No such module $module in application $app.");
82 $self->{State}->error($self->errors);
83 return NOT_FOUND;
84 }
85  
86 unless (exists $ACTIONS{$app}->{$module}->{$action}) {
87 $self->error("No such action $action in module $module " .
88 "of application $app.");
89 $self->{State}->error($self->errors);
90 return NOT_FOUND;
91 }
92  
93 print STDERR "Running $module -> $action\n";
94 my $status = $ACTIONS{$app}->{$module}->{$action}->($self);
95  
96 $self->{State}->error($self->errors);
97  
98 return $status;
99 }
100  
101 sub error {
102 my $self = shift;
103 push(@{ $self->{Errors} }, @_);
104 }
105  
106 sub errors {
107 return @{ $_[0]->{Errors} };
108 }
109  
110 sub param {
111 my $self = shift;
112 my $key = shift;
113  
114 my $t = $self->{Request}->parms; # Apache::Table
115  
116 if ($#_ >= 0) {
117 my $value = shift;
118 if (!defined($value)) {
119 # print STDERR "Module: Modified parameter $key (UNSET)\n";
120 confess "Invalid key " . Dumper($key) if ref $key;
121 $t->unset($key);
122 }
123 elsif (ref($value) eq 'ARRAY') {
124 # print STDERR "Module: Modified parameter $key => $value\n";
125 # We convert list refs into separate table entries.
126 $t->unset($key);
127 foreach my $el (@$value) {
128 $t->add($key, $el);
129 }
130 }
131 else {
132 # print STDERR "Module: Modified parameter $key => $value\n";
133 $t->set($key, $value);
134 }
135 }
136  
137 # scalar/array context is passed on to this request.
138 return $self->{Request}->param($key);
139 }
140  
141 sub session {
142 my $self = shift;
143 my $key = shift;
144 if ($#_ >= 0) {
145 my $value = shift;
146 $self->{Session}->{$key} = $value;
147 }
148 return $self->{Session}->{$key};
149 }
150  
151 sub state {
152 my $self = shift;
153 return $self->{State};
154 }
155  
156 sub params {
157 my $self = shift;
158 # Tied hash to Apache::Table
159 my $parms = $self->{Request}->parms;
160 # Make a copy
161 return { %{ $parms } };
162 }
163  
164 sub upload {
165 my $self = shift;
166 # scalar/array context is passed on to this request.
167 return $self->{Request}->upload(@_);
168 }
169  
170 =head1 NAME
171  
172 Apache::Action - A method dispatch mechanism for Apache
173  
174 =head1 SYNOPSIS
175  
176 # An Apache handler to manage the cycle.
177 package My::Apache::Handler;
178 my $ah = new HTML::Mason::ApacheHandler...);
179 sub handler {
180 my ($r) = @_;
181 ...
182 tie %SESSION, 'Apache::Session::....';
183 my $state = new Apache::Action::State( # Or custom state class
184 Request => $r,
185 Session => \%SESSION,
186 );
187 my $action = new Apache::Action(
188 Request => $r,
189 Session => \%SESSION,
190 State => $state,
191 );
192 my $status = eval { $action->run; };
193 if ($@) { $state->error($@); $status = SERVER_ERROR; }
194 unless ($status == OK) {
195 my $subreq = $r->lookup_uri('/error.html');
196 $r->filename($subreq->filename);
197 }
198 return $ah->handle_request($r);
199 }
200  
201 # A set of action handlers
202 package My::Apache::Actions;
203 use base 'Apache::Action';
204 __PACKAGE__->register('AppName', 'ObjectName',
205 action0 => \&handler0,
206 action1 => \&handler1,
207 ...
208 );
209 sub handler0 {
210 my ($self) = @_;
211 # my $user = $self->state->user; # If user defined.
212 }
213  
214 =head1 DESCRIPTION
215  
216 This module reads values out of the HTTP submission and dispatches
217 to code as appropriate. The architecture requires four elements:
218  
219 =over 4
220  
221 =item The apache request
222  
223 This is normally a singleton instance of Apache::Request.
224  
225 =item The persistent session
226  
227 This is usually an Apache::Session, but anything which provides a
228 hashref will do. The session stores the persistent data, and may be
229 serialised by any method desired.
230  
231 =item A request state
232  
233 This is usually a subclass of Apache::Action::State and stores
234 nonserialisable and per-request data.
235  
236 =item An action dispatcher.
237  
238 This is an Apache::Action instance.
239  
240 =back
241  
242 It is normal to write a class which inherits Apache::Action::State,
243 which generates and caches nonserialisable or non-normalised data
244 on demand. Things like user id may be stored in the session, and
245 the state may then provide a 'user' method which reads the user-id
246 from the session and retrieves the user from the database, caching
247 the object for the duration of the request. See eg/State.pm in this
248 distribution for an example.
249  
250 Loaded modules may register actions with Apache::Action using the
251 'register' call, as described above. When an Apache::Action is 'run',
252 it looks for the field 'action' in the HTTP request parameters. This
253 field is of the form "application/module/action". It will then call the
254 appropriate subref, passing itself as the one and only parameter.
255  
256 When using this module with HTML::Mason, it is normal to exoprt the
257 state and the session into the HTML::Mason::Commands namespace so
258 that they can be accessed by pages.
259  
260 =head1 METHODS
261  
262 =over 4
263  
264 =item Apache::Action->register($app, $module, $action)
265  
266 Register a new action with Apache::Action. This is a class method and is
267 designed to be called from the top level of any loaded Perl module. See
268 eg/Feedback.pm for an example.
269  
270 =item Apache::Action->new(...)
271  
272 Construct a new Action object. This reqires three parameters: Request,
273 Session and State. The Request is an Apache::Request instance. The
274 Session is usually an Apache::Session instance but may be any session
275 hash. The State is an instance of Apache::Action::State;
276  
277 =item $action->run()
278  
279 Search the HTTP arguments in the Request, and run an action, if
280 appropriate.
281  
282 =item $action->param($name)
283  
284 Return the HTTP parameter named.
285  
286 =item $action->params($name)
287  
288 Return a hashref of all HTTP parameters, copying the data.
289  
290 =item $action->upload
291  
292 Return an Apache::Upload object as named.
293  
294 =item $action->session($name)
295  
296 Return data from the session hash, as named.
297  
298 =item $action->session($name, $value)
299  
300 Store data in the session hash, as named.
301  
302 =item $action->error($error)
303  
304 Record that an error happened during this execution. The action object
305 will add the errors to the state object at the end of the run. It is
306 the responsibility of the Apache handler writer to check whether any
307 errors were recorded in the action object before continuing. This method
308 merely provides a log.
309  
310 =item $action->errors()
311  
312 Return a list of errors recorded in this execution.
313  
314 =back
315  
316 =head1 BUGS
317  
318 Mostly documentation. This code has been pulled out of a running
319 system and patched up for CPAN, so patches welcome if it doesn't run
320 as smoothly as expected outside of that system.
321  
322 This module is quite hard to test outside Apache.
323  
324 =head1 SUPPORT
325  
326 Mail the author at <cpan@anarres.org>
327  
328 =head1 AUTHOR
329  
330 Shevek
331 CPAN ID: SHEVEK
332 cpan@anarres.org
333 http://www.anarres.org/projects/
334  
335 =head1 COPYRIGHT
336  
337 Copyright (c) 2004 Shevek. All rights reserved.
338  
339 This program is free software; you can redistribute it and/or modify
340 it under the same terms as Perl itself.
341  
342 =head1 SEE ALSO
343  
344 Apache::Action::State
345 Apache::Session
346 HTML::Mason
347  
348 =cut
349  
350 1;
351 __END__
« Action/State.pm ↑ Summary