lib/OpenFrame/AppKit/App.pm

filter: Statementstmt 87.5% 119 / 136 Branchbran 61.3% 27 / 44 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.0% 18 / 20 Podpod 10.0% 1 / 10 totaltotal 77.6% 170 / 219 CC 30 SCAR 24.6
CC
30
Cov
81%
_match_hash_arguments
29.5
_copy_app_from_namespace
4.0
dispatch_on_uri
3.0
CRAP 11.7
↑ Summary blib/lib/OpenFrame/AppKit/Segment/SessionLoader.pm »
1 package OpenFrame::AppKit::App;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION=3.03;
7  
8 2 ▶use Storable qw ( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use OpenFrame::AppKit::Segment::DispatchOnURI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base qw ( OpenFrame::AppKit::Segment::DispatchOnURI );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
13 1 my $self = shift;
14  
15 1 $self->{'::appkit'} = undef;
16  
17 1 $self->config( {} ); ## initialize the configuration
18  
19 1 $self->uri( '/' ); ## create a default place for it to execute on
20 1 $self->namespace( 'default' ); ## create a default namespace for it to execute with
21  
22 1 $self->SUPER::init( @_ );
23 }
24  
25 20 ▶sub request {
Subroutine request: called CC=2 SCAR=7.2
Pod undocumented
26 20 my $self = shift;
27 20 my $req = shift;
28 20 ▶ if (defined($req)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req) { } 20 0 20
29 20 $self->{'::appkit'}->{request} = $req;
30 20 return $self;
31 } else {
32 0 return $self->{'::appkit'}{request};
33 }
34 }
35  
36 #sub uri {
37 # my $self = shift;
38 # my $pattern = shift;
39 # if ( defined( $pattern ) ) {
40 # if( ref($pattern) ) {
41 # $self->{'::appkit'}->{ execute_on_uris_matching } = $pattern;
42 # return $self;
43 # } else {
44 # return $self->uri( qr/$pattern/ );
45 # }
46 # } else {
47 # return $self->{'::appkit'}->{ execute_on_uris_matching };
48 # }
49 #}
50  
51 21 ▶sub namespace {
Subroutine namespace: called CC=2 SCAR=6.9
Pod undocumented
52 21 my $self = shift;
53 21 my $ns = shift;
54 21 ▶ if (defined( $ns )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) { } 1 20 21
55 1 $self->{'::appkit'}{ ns } = $ns;
56 1 return $self;
57 } else {
58 20 return $self->{'::appkit'}->{ns};
59 }
60 }
61  
62 10 ▶sub _copy_app_from_namespace {
Subroutine _copy_app_from_namespace: called CC=4 SCAR=13.9
63 10 my $self = shift;
64 10 my $session = shift;
65 10 ▶ my $namespace = $self->namespace || '';
Condition50%1/2
$self->namespace || ''
l!l
100
MC/DC0%0/1
$self->namespace || ''
$self->namespace
Truth table50%1/2
A
A= $self->namespace
Aresult
11
00
66  
67 10 $self->emit("namespace is $namespace");
68
69 10 ▶ if ($session->{application}->{ $namespace }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"application"}{$namespace}) 6 4 10
70 6 my $copy = dclone($session->{application}->{ $namespace });
71 6 foreach my $key (keys %$copy) {
72 24 $self->{$key} = $copy->{ $key };
73 }
74 }
75 }
76  
77 10 ▶sub dispatch_on_uri {
Subroutine dispatch_on_uri: called CC=3 SCAR=11.1
Pod undocumented
78 10 my $self = shift;
79 10 my $pipe = shift;
80  
81 10 my $store = $pipe->store();
82 10 my $request = $store->get('OpenFrame::Request');
83  
84 0 ▶ if (!$request) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($request) 0 10 10
85  
86 10 my $session = $self->get_session( $store );
87 10 delete $session->{ app };
88  
89 10 $self->_copy_app_from_namespace( $session );
90  
91 10 $self->request( $request ); ## set the request
92  
93 10 my @results = $self->_enter( $store );
94  
95 10 $self->request(''); ## clear the request
96  
97 10 ▶ my $namespace = $self->namespace || '';
Condition50%1/2
$self->namespace || ''
l!l
100
MC/DC0%0/1
$self->namespace || ''
$self->namespace
Truth table50%1/2
A
A= $self->namespace
Aresult
11
00
98 10 my %hashcopy = %{$self};
99 10 $session->{app} = \%hashcopy;
100 10 $session->{application}->{ $namespace } = \%hashcopy;
101  
102 10 return @results;
103 }
104  
105 10 ▶sub get_session {
Subroutine get_session: called CC=1 SCAR=0.0
Pod undocumented
106 10 my $self = shift;
107 10 my $store = shift;
108 10 my $session = $store->get('OpenFrame::AppKit::Session');
109 10 return $session;
110 }
111  
112 10 ▶sub get_entry {
Subroutine get_entry: called CC=3 SCAR=11.1
Pod undocumented
113 10 my $self = shift;
114 10 my $store = shift;
115  
116 10 my $args = $store->get('OpenFrame::Request')->arguments();
117 10 my $epnt = $self->_get_entry_points();
118  
119 10 my $dispatch = "get_entry_" . ref($epnt);
120 10 my $method;
121 10 eval {
122 10 $method = $self->$dispatch( $args, $epnt );
123 };
124 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
125 0 $self->emit("could not call $dispatch");
126 }
127  
128 10 ▶ return $method || 'default';
Condition100%2/2
$method || 'default'
l!l
64
MC/DC100%1/1
$method || 'default'
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
129 }
130  
131 10 ▶sub get_entry_HASH {
Subroutine get_entry_HASH: called CC=3 SCAR=11.0
Pod undocumented
132 10 my $self = shift;
133 10 my $args = shift;
134 10 my $epnt = shift;
135 10 my $method;
136 10 foreach my $point ( keys %$epnt ) {
137 ## we have a hash
138 32 ▶ if ($self->_match_hash_arguments( $args, $epnt->{ $point } )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match_hash_arguments($args, $epnt->{$point})) 6 26 32
139 6 $method = $point;
140 6 last;
141 }
142 }
143 10 return $method;
144 }
145  
146 10 ▶sub _enter {
Subroutine _enter: called CC=2 SCAR=7.0
147 10 my $self = shift;
148 10 my $store = shift;
149  
150 10 my $method = $self->get_entry( $store );
151  
152 10 my $sub = $self->can($method);
153 10 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) { } 10 0 10
154 10 return ($sub->($self, $store));
155 } else {
156 ## can't do anything
157 }
158 }
159  
160 0 ▶sub default {}
Subroutine default: not called CC=1 SCAR=0.0
Pod undocumented
161  
162 32 ▶sub _match_hash_arguments {
Subroutine _match_hash_arguments: called CC=17 SCAR=33.8
163 32 my $self = shift;
164 32 my $args = shift;
165 32 my $against = shift;
166  
167 32 my $count = scalar(@$against);
168 32 my $match = 0;
169 32 foreach my $wanted (@$against) {
170 96 ▶ if (ref $wanted eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $wanted eq 'ARRAY') { } 64 32 96
elsif (substr($wanted, 0, 1) eq '!') { } 0 32 32
elsif (exists $args->{$wanted}) { } 32 0 32
171 64 my $wantarg = $wanted->[0];
172 64 my $lnot = 0;
173 64 ▶ if (substr($wantarg,0,1) eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($wantarg, 0, 1) eq "!") 0 64 64
174 0 $wantarg = substr($wantarg, 1);
175 0 $lnot = 1;
176 }
177 64 ▶ if (exists $args->{ $wantarg }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$wantarg}) 64 0 64
178 64 my $argvalue = $args->{ $wantarg };
179 64 foreach my $wantvalue (@$wanted[1..$#$wanted]) {
180 64 ▶ if (ref $wantvalue eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wantvalue eq 'Regexp') { } 25 39 64
181 25 ▶ if ($lnot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lnot) { } 0 25 25
182 0 ▶ $match--, last if $argvalue =~ $wantvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $argvalue =~ /$wantvalue/ 0 0 0
183 } else {
184 25 ▶ $match++, last if $argvalue =~ $wantvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $argvalue =~ /$wantvalue/ 15 10 25
185 }
186 }
187 else {
188 39 ▶ if ($lnot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lnot) { } 0 39 39
189 0 ▶ $match--, last if $argvalue eq $wantvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $argvalue eq $wantvalue 0 0 0
190 } else {
191 39 ▶ $match++, last if $argvalue eq $wantvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $argvalue eq $wantvalue 19 20 39
192 }
193 }
194 }
195 }
196 }
197 elsif ( substr($wanted,0,1) eq '!') {
198 0 my $realwanted = substr($wanted, 1);
199 0 ▶ if (exists $args->{ $realwanted }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$realwanted}) { } 0 0 0
200 0 return 0;
201 } else {
202 0 $match++;
203 }
204 } elsif (exists $args->{ $wanted }) {
205 32 $match++;
206 } else {
207 ## skip
208 }
209 }
210 32 ▶ return 1 if $match == $count;
Branch: 1 branch
BranchTrueFalseTotal
if $match == $count 6 26 32
211 26 return 0;
212 }
213  
214 10 ▶sub _get_entry_points {
Subroutine _get_entry_points: called CC=1 SCAR=0.0
215 10 my $self = shift;
216 10 return $self->entry_points();
217 }
218  
219 0 ▶sub entry_points {
Subroutine entry_points: not called CC=1 SCAR=2.2
Pod undocumented
220 0 my $self = shift;
221 0 my $class = ref($self);
222 {
223 0 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.2
224 0 return $ {$class . '::epoints'};
225 }
226 }
227  
228 1 ▶sub config {
Subroutine config: called CC=2 SCAR=7.2
Pod undocumented
229 1 my $self = shift;
230 1 my $conf = shift;
231 1 ▶ if (defined( $conf )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf) { } 1 0 1
232 1 $self->{'::appkit'}{config} = $conf;
233 1 return $self;
234 } else {
235 0 return $self->{'::appkit'}->{config};
236 }
237 }
238  
239 2 1;
240  
241 =head1 NAME
242  
243 OpenFrame::AppKit::App - The OpenFrame AppKit application class
244  
245 =head1 SYNOPSIS
246  
247 package MyApplication;
248  
249 use strict;
250  
251 use OpenFrame::AppKit::App;
252 use base qw ( OpenFrame::AppKit::App );
253  
254 =head1 DESCRIPTION
255  
256 The C<OpenFrame::AppKit::App> class is designed to be inherited from.
257 It provides all the basic functionality of a pipeline segment, as well
258 as basic functionality that applications will need to start running.
259  
260 To create an application, all you need to do to get started is
261 subclass OpenFrame::AppKit::App.
262  
263 package MyApplication;
264  
265 use strict;
266  
267 use OpenFrame::AppKit::App;
268 use base qw ( OpenFrame::AppKit::App );
269  
270 In your server code you can now instantiate your application:
271  
272 my $app = MyApplication->new();
273  
274 However, applications require a little more information to act in the
275 manner we have come to expect. Applications in common web
276 applications act when a url is requested that they listen to. Your
277 new application is capable of that, but you need to tell it which URIs
278 to match against. You do this by using the C<uri()> method of that
279 OpenFrame::AppKit::App helpfully provides. If for instance you wanted
280 your application to execute whenever you went to '/myapp.html' URL
281 then simply use the URI method to specify a regular expression to
282 match:
283  
284 $app->uri( qr!/myapp\.html! );
285  
286 OpenFrame::AppKit::App uses the concept of namespaces to keep your
287 application's data seperate from other application's data in the
288 global session. You can specify the namespace of your application by
289 using the C<namespace()> method, that once again,
290 OpenFrame::AppKit::App provides:
291  
292 $app->namespace( 'myapplication' );
293  
294 As you have probably noticed, the work needed to set up your
295 applications initialization is performed through method calls to your
296 application. All methods that have been demonstrated here are capable
297 of being chained:
298  
299 my $app = MyApplication->new()
300 ->uri( qr!/myapp\.html! )
301 ->namespace( 'myapplication' );
302  
303 All this is very useful, but so far the application still does nothing
304 at all. This will change. C<OpenFrame::AppKit::App> applications act
305 by default as state machines. These states are specified by
306 parameters sent to the OpenFrame server. In the case of an HTTP GET
307 message you can see them on the end of a URL:
308  
309 http://some.server.com/test.cgi?name=value
310  
311 In this case there is one parameter, C<name> and one value C<value>.
312 The application's state machine looks at the parameters, your
313 application acts on values. To set up your state machine you create a
314 method in your application called C<entry_points()>. This method
315 should return a hash of arrays. In the hash, the keys represent
316 methods in your module, and then elements in the array represent
317 parameters that have to exist in order for your application to be run:
318  
319 sub entry_points {
320 return {
321 form_filled => [ 'name', 'age' ]
322 };
323 }
324  
325 Each of the keys in your hash is an entry point, and needs a
326 subroutine in your module to perform the work.
327  
328 sub form_filled {
329  
330 }
331  
332 In the case that you want a method to be called even if there are no
333 parameters matched, OpenFrame automatically calls a method called
334 C<default()> for you.
335  
336 Whenever C<OpenFrame::AppKit> calls an entry point in your application
337 it calls it with two parameters. The first of the two parameters is
338 the Application object itself. The second is the Pipeline store (but
339 I'll talk about that in a little while, its not important right now).
340 A method that your application will use nearly every time it is in an
341 entry point is the C<request()> method. It returns the
342 C<OpenFrame::Request> object that you can use to find out the exact
343 uri that has been called as well as the paramaters and values supplied
344 to it.
345  
346 sub form_filled {
347 my $self = shift;
348 my $store = shift;
349  
350 my $request = $self->request();
351 my $uri = $request->uri();
352 my $args = $request->arguments();
353 }
354  
355 For more information about the request object and what it does, you
356 can see the C<OpenFrame::Request> documentation. For now we'll talk
357 only about the $args variable, which is a hash reference. Lets assume
358 that your application is up and running, and receiving requests. If
359 you were to receive a request that was represented in URI form as:
360  
361 http://some.server.com/myapp.html?name=Bob&age=34
362  
363 Then you could expect to find that your $args hash would look like:
364  
365 $args = {
366 name => 'Bob',
367 age => 34
368 }
369  
370 my $name = $args->{name};
371  
372 The $name variable would hold the value C<Bob>.
373  
374 When you write an application any data that you want to provide to the
375 template writer (which may be yourself) should be placed inside the
376 $self object. $self is a hash, and provided you don't use the
377 C<::appkit> key you can place whatever you'd like in there.
378  
379 =head1 AUTHOR
380  
381 James A. Duncan <jduncan@fotango.com>
382  
383 =head1 COPYRIGHT
384  
385 Copyright 2002 Fotango Ltd.
386  
387 This code is released under the same terms as perl itself.
388  
389 http://opensource.fotango.com/
390  
391 =cut
↑ Summary blib/lib/OpenFrame/AppKit/Segment/SessionLoader.pm »