ASP/GlobalASA.pm

filter: Statementstmt 89.2% 58 / 65 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 84.2% 16 / 19 Podpod 81.8% 9 / 11 totaltotal 84.6% 88 / 104 CC 5 SCAR 4.5
CC
5
Cov
86%
BUILD
3.0
_build_package
2.0
execute_event
2.0
CRAP 1.6
« ASP/Application.pm ↑ Summary ASP/Exception/End.pm »
1 package CatalystX::ASP::GlobalASA;
2  
3 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use MooseX::Types::Path::Tiny qw(Path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our @Routines = qw(
10 Application_OnStart
11 Application_OnEnd
12 Session_OnStart
13 Session_OnEnd
14 Script_OnStart
15 Script_OnEnd
16 Script_OnParse
17 Script_OnFlush
18 );
19  
20 9 has 'asp' => (
21 is => 'ro',
22 isa => 'CatalystX::ASP',
23 required => 1,
24 weak_ref => 1,
25 );
26  
27 =head1 NAME
28  
29 CatalystX::ASP::GlobalASA - global.asa
30  
31 =head1 SYNOPSIS
32  
33 ### in global.asa
34 sub Script_OnStart {
35 printf STDERR "Executing script: %s\n", $Request->ServerVariables('SCRIPT_NAME');
36 }
37  
38 =head1 DESCRIPTION
39  
40 The ASP platform allows developers to create Web Applications. In fulfillment of
41 real software requirements, ASP allows event-triggered actions to be taken,
42 which are defined in a F<global.asa> file. The global.asa file resides in the
43 C<Global> directory, defined as a config option, and may define the following actions:
44  
45 Action Event
46 ------ ------
47 Script_OnStart * - Beginning of Script execution
48 Script_OnEnd * - End of Script execution
49 Script_OnFlush * - Before $Response being flushed to client.
50 Script_OnParse * - Before script compilation
51 Application_OnStart - Beginning of Application
52 Application_OnEnd - End of Application
53 Session_OnStart - Beginning of user Session.
54 Session_OnEnd - End of user Session.
55  
56 * These are API extensions that are not portable, but were
57 added because they are incredibly useful
58  
59 These actions must be defined in the C<< "$self->Global/global.asa" >> file as
60 subroutines, for example:
61  
62 sub Session_OnStart {
63 $Application->{$Session->SessionID()} = started;
64 }
65  
66 Sessions are easy to understand. When visiting a page in a web application, each
67 user has one unique C<$Session>. This session expires, after which the user will
68 have a new C<$Session> upon revisiting.
69  
70 A web application starts when the user visits a page in that application, and
71 has a new C<$Session> created. Right before the first C<$Session> is created,
72 the C<$Application> is created. When the last user C<$Session> expires, that
73 C<$Application> expires also. For some web applications that are always busy,
74 the C<Application_OnEnd> event may never occur.
75  
76 =cut
77  
78 has 'filename' => (
79 is => 'ro',
80 isa => Path,
81 6 ▶ default => sub { shift->asp->Global->child( 'global.asa' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 9 );
83  
84 9 has 'package' => (
85 is => 'ro',
86 isa => 'Str',
87 lazy_build => 1,
88 );
89  
90 6 ▶sub _build_package {
Subroutine _build_package: called CC=2 SCAR=7.0
91 6 my ( $self ) = @_;
92 6 my $asp = $self->asp;
93 6 my $id = $asp->file_id( $asp->Global, 1 );
94 6 ▶ return $asp->GlobalPackage || "CatalystX::ASP::Compiles::$id";
Condition50%1/2
$asp->GlobalPackage || "CatalystX::ASP::Compiles::$id"
l!l
60
MC/DC0%0/1
$asp->GlobalPackage || "CatalystX::ASP::Compiles::$id"
$asp->GlobalPackage
Truth table50%1/2
A
A= $asp->GlobalPackage
Aresult
11
00
95 }
96  
97 6 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
98 6 my ( $self ) = @_;
99 6 my $asp = $self->asp;
100 6 my $c = $asp->c;
101  
102 6 ▶ return unless $self->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists 0 6 6
103  
104 6 my $package = $self->package;
105 6 my $filename = $self->filename;
106 6 my $global = $asp->Global;
107 6 my $code = read_file( $filename );
108 6 my $match_events = join '|', @Routines;
109 6 $code =~ s/\<script[^>]*\>((.*)\s+sub\s+($match_events).*)\<\/script\>/$1/isg;
110 $code = join( '',
111 "\n#line 1 $filename\n",
112 join( ' ;; ',
113 "package $package;",
114 'no strict;',
115 6 'use vars qw(' . join( ' ', map {"\$$_"} @CatalystX::ASP::Objects ) . ');',
116 "use lib qw($global);",
117 $code,
118 'sub exit { $main::Response->End(); }',
119 "no lib qw($global);",
120 '1;',
121 )
122 );
123 6 $code =~ /^(.*)$/s; # Realized this is for untainting
124 6 $code = $1;
125  
126 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 6 eval $code; ## no critic (BuiltinFunctions::ProhibitStringyEval)
128 6 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
129 0 $c->error( "Error on compilation of global.asa: $@" ); # don't throw error, so we can throw die later
130 }
131 }
132  
133 6 ▶sub exists { shift->filename->exists }
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
134  
135 =head1 METHODS
136  
137 =over
138  
139 =item $self->execute_event($event);
140  
141 Execute the event defined in F<global.asa>
142  
143 =cut
144  
145 45 ▶sub execute_event {
Subroutine execute_event: called CC=2 SCAR=6.9
Pod documented
146 45 my ( $self, $event ) = @_;
147 45 my $asp = $self->asp;
148 45 ▶ $asp->execute( $asp->c, $event ) if "$self->package"->can( $event );
Branch: 1 branch
BranchTrueFalseTotal
if ("$self->package")->can($event) 15 30 45
149 }
150  
151 =item Application_OnStart
152  
153 This event marks the beginning of an ASP application, and is run just before the
154 C<Session_OnStart> of the first Session of an application. This event is useful
155 to load up C<$Application> with data that will be used in all user sessions.
156  
157 =cut
158  
159 6 ▶sub Application_OnStart {
Subroutine Application_OnStart: called CC=1 SCAR=0.0
Pod documented
160 6 my ( $self ) = @_;
161 6 $self->execute_event( join( '::', $self->package, 'Application_OnStart' ) );
162 }
163  
164 =item Application_OnEnd
165  
166 The end of the application is marked by this event, which is run after the last
167 user session has timed out for a given ASP application.
168  
169 =cut
170  
171 0 ▶sub Application_OnEnd {
Subroutine Application_OnEnd: not called CC=1 SCAR=6.9
Pod documented
172 0 my ( $self ) = @_;
173 0 $self->execute_event( join( '::', $self->package, 'Application_OnEnd' ) );
174 }
175  
176 =item Session_OnStart
177  
178 Triggered by the beginning of a user's session, C<Session_OnStart> gets run
179 before the user's executing script, and if the same session recently timed out,
180 after the session's triggered C<Session_OnEnd>.
181  
182 The C<Session_OnStart> is particularly useful for caching database data, and
183 avoids having the caching handled by clumsy code inserted into each script being
184 executed.
185  
186 =cut
187  
188 0 ▶sub Session_OnStart {
Subroutine Session_OnStart: not called CC=1 SCAR=6.9
Pod documented
189 0 my ( $self ) = @_;
190 0 $self->execute_event( join( '::', $self->package, 'Session_OnStart' ) );
191 }
192  
193 =item Session_OnEnd
194  
195 Triggered by a user session ending, C<Session_OnEnd> can be useful for cleaning
196 up and analyzing user data accumulated during a session.
197  
198 Sessions end when the session timeout expires, and the C<StateManager> performs
199 session cleanup. The timing of the C<Session_OnEnd> does not occur immediately
200 after the session times out, but when the first script runs after the session
201 expires, and the C<StateManager> allows for that session to be cleaned up.
202  
203 So on a busy site with default C<SessionTimeout> (20 minutes) and
204 C<StateManager> (10 times) settings, the C<Session_OnEnd> for a particular
205 session should be run near 22 minutes past the last activity that Session saw.
206 A site infrequently visited will only have the C<Session_OnEnd> run when a
207 subsequent visit occurs, and theoretically the last session of an application
208 ever run will never have its C<Session_OnEnd> run.
209  
210 Thus I would not put anything mission-critical in the C<Session_OnEnd>, just
211 stuff that would be nice to run whenever it gets run.
212  
213 =cut
214  
215 0 ▶sub Session_OnEnd {
Subroutine Session_OnEnd: not called CC=1 SCAR=6.9
Pod documented
216 0 my ( $self ) = @_;
217 0 $self->execute_event( join( '::', $self->package, 'Session_OnEnd' ) );
218 }
219  
220 =item Script_OnStart
221  
222 The script events are used to run any code for all scripts in an application
223 defined by a F<global.asa>. Often, you would like to run the same code for every
224 script, which you would otherwise have to add by hand, or add with a file
225 include, but with these events, just add your code to the F<global.asa>, and it
226 will be run. This runs before a script is executed.
227  
228 =cut
229  
230 9 ▶sub Script_OnStart {
Subroutine Script_OnStart: called CC=1 SCAR=0.0
Pod documented
231 9 my ( $self ) = @_;
232 9 $self->execute_event( join( '::', $self->package, 'Script_OnStart' ) );
233 }
234  
235 =item Script_OnEnd
236  
237 Like C<Script_OnStart> except at the end.
238  
239 There is one caveat. Code in C<Script_OnEnd> is not guaranteed to be run when
240 C<< $Response->End() >> is called, since the program execution ends immediately
241 at this event. To always run critical code, use the API extension:
242  
243 $Server->RegisterCleanup()
244  
245 =cut
246  
247 9 ▶sub Script_OnEnd {
Subroutine Script_OnEnd: called CC=1 SCAR=0.0
Pod documented
248 9 my ( $self ) = @_;
249 9 $self->execute_event( join( '::', $self->package, 'Script_OnEnd' ) );
250 }
251  
252 =item Script_OnParse
253  
254 This event allows one to set up a source filter on the script text, allowing one
255 to change the script on the fly before the compilation stage occurs. The script
256 text is available in the C<< $Server->{ScriptRef} >> scalar reference, and can
257 be accessed like so:
258  
259 sub Script_OnParse {
260 my $code = $Server->{ScriptRef}
261 $$code .= " ADDED SOMETHING ";
262 }
263  
264 =cut
265  
266 12 ▶sub Script_OnParse {
Subroutine Script_OnParse: called CC=1 SCAR=0.0
Pod documented
267 12 my ( $self ) = @_;
268 12 $self->execute_event( join( '::', $self->package, 'Script_OnParse' ) );
269 }
270  
271 =item Script_OnFlush
272  
273 API extension. This event will be called prior to flushing the C<$Response>
274 buffer to the web client. At this time, the C<< $Response->{BinaryRef} >> buffer
275 reference may be used to modify the buffered output at runtime to apply global
276 changes to scripts output without having to modify all the scripts.
277  
278 sub Script_OnFlush {
279 my $ref = $Response->{BinaryRef};
280 $$ref =~ s/\s+/ /sg; # to strip extra white space
281 }
282  
283 =cut
284  
285 9 ▶sub Script_OnFlush {
Subroutine Script_OnFlush: called CC=1 SCAR=0.0
Pod documented
286 9 my ( $self ) = @_;
287 9 $self->execute_event( join( '::', $self->package, 'Script_OnFlush' ) );
288 }
289  
290  
291 9 __PACKAGE__->meta->make_immutable;
292  
293 =back
294  
295 =head1 SEE ALSO
296  
297 =over
298  
299 =item * L<CatalystX::ASP>
300  
301 =back
« ASP/Application.pm ↑ Summary ASP/Exception/End.pm »