/
App.pm
220 lines (165 loc) · 5.28 KB
/
App.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# See bottom of file for license and copyright information
package Foswiki::App;
use v5.14;
use Cwd;
use Try::Tiny;
use Foswiki::Config;
use Moo;
use namespace::clean;
extends qw(Foswiki::Object);
=begin TML
---+!! package Foswiki::App
The core class of the project responsible for low-level and code glue
functionality.
=cut
has cfg => (
is => 'rw',
lazy => 1,
default => \&_readConfig,
isa => Foswiki::Object::isaCLASS( 'cfg', 'Foswiki::Config', noUndef => 1, ),
);
has env => (
is => 'rw',
required => 1,
);
has engine => (
is => 'rw',
lazy => 1,
default => \&_prepareEngine,
isa =>
Foswiki::Object::isaCLASS( 'engine', 'Foswiki::Engine', noUndef => 1, ),
);
has request => (
is => 'rw',
lazy => 1,
default => \&_prepareRequest,
isa =>
Foswiki::Object::isaCLASS( 'request', 'Foswiki::Request', noUndef => 1, ),
);
=begin TML
---++ ClassMethod new([%parameters])
The following keys could be defined in =%parameters= hash:
|*Key*|*Type*|*Description*|
|=env=|hashref|Environment hash such as shell environment or PSGI env|
=cut
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my %params = @_;
return $orig->( $class, %params );
};
sub BUILD {
my $this = shift;
my $params = shift;
$Foswiki::app = $this;
unless ( defined $this->engine ) {
Foswiki::Exception::Fatal->throw( text => "Cannot initialize engine" );
}
unless ( $this->cfg->data->{isVALID} ) {
$this->cfg->bootstrap;
}
}
=begin TML
---++ StaticMethod run([%parameters])
Starts application, prepares and initiates request processing. The following
keys could be defined in =%parameters= hash:
|*Key*|*Type*|*Description*|
|=env=|hashref|Environment hash such as shell environment or PSGI env|
=cut
sub run {
my $class = shift;
my %params = @_;
# Do nice in shared code environment, localize ALL request-related globals.
local %Foswiki::app;
local %Foswiki::cfg;
local %TWiki::cfg;
my $app;
# We use shell environment by default. PSGI would supply its own env
# hashref.
$params{env} //= \%ENV;
# Use current working dir for fetching the initial setlib.cfg
$params{env}{PWD} //= getcwd;
try {
$app = Foswiki::App->new(%params);
$app->handleRequest;
}
catch {
my $e = $_;
unless ( ref($e) && $e->isa('Foswiki::Exception') ) {
$e = Foswiki::Exception->transmute($e);
}
# Low-level report of errors to user.
if ( defined $app && defined $app->engine ) {
# TODO Send error output to user using the initialized engine.
$app->engine->write( $e->stringify );
}
else {
# Propagade the error using the most primitive way.
die( ref($e) ? $e->stringify : $e );
}
};
}
sub handleRequest {
my $this = shift;
my $res = Foswiki::UI::handleRequest( $this->request );
$this->engine->finalize( $res, $this->request );
}
=begin TML
--++ ObjectMethod create($className, %initArgs)
Similar to =Foswiki::AppObject::create()= method but for the =Foswiki::App=
itself.
=cut
sub create {
my $this = shift;
my $class = shift;
unless ( $class->isa('Foswiki::AppObject') ) {
Foswiki::Exception::Fatal->throw(
text => "Class $class is not a Foswiki::AppObject descendant." );
}
return $class->new( app => $this, @_ );
}
sub _prepareEngine {
my $this = shift;
my $env = $this->env;
my $engine;
# Foswiki::Engine has to determine what environment are we run within and
# return an object of corresponding class.
$engine = Foswiki::Engine::start( env => $env );
return $engine;
}
sub _prepareRequest {
my $this = shift;
my $request = $this->engine->prepare;
return $request;
}
sub _readConfig {
my $this = shift;
my $cfg = $this->create( 'Foswiki::Config', env => $this->env );
return $cfg;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
and TWiki Contributors. All Rights Reserved. TWiki Contributors
are listed in the AUTHORS file in the root of this distribution.
Copyright (C) 2005 Martin at Cleaver.org
Copyright (C) 2005-2007 TWiki Contributors
and also based/inspired on Catalyst framework, whose Author is
Sebastian Riedel. Refer to
http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7010/lib/Catalyst.pm
for more credit and liscence details.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.