blib/lib/Kelp.pm

filter: Statementstmt 96.8% 184 / 190 Branchbran 78.8% 41 / 52 Conditioncond 63.6% 21 / 33 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 94.2% 49 / 52 Podpod 91.6% 22 / 24 totaltotal 87.5% 323 / 369 CC 38 SCAR 13.1
CC
38
Cov
89%
_psgi_internal
10.0
new_anon
8.6
__ANON__
7.5
CRAP 3.7
« blib/lib/Kelp/Middleware.pm ↑ Summary blib/lib/Kelp/Module/Logger/Simple.pm »
1 package Kelp;
2  
3 39 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 ▶use Carp qw/ longmess croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 39 our $VERSION = '2.22';
15  
16 # Basic attributes
17 39 attr -host => hostname;
18 39 ▶attr mode => $ENV{KELP_ENV} // $ENV{PLACK_ENV} // 'development';
Condition80%4/5
($ENV{"KELP_ENV"} // $ENV{"PLACK_ENV"}) // "development"
l!l
138
$ENV{"KELP_ENV"} // $ENV{"PLACK_ENV"}
l!l&&r!l&&!r
0138
MC/DC50%1/2
($ENV{"KELP_ENV"} // $ENV{"PLACK_ENV"}) // "development"
$ENV{"KELP_ENV"} $ENV{"PLACK_ENV"}
Truth table66%2/3
A // B
A= $ENV{"KELP_ENV"}B= $ENV{"PLACK_ENV"}
ABresult
1X1
011
000
19 39 attr -path => $FindBin::Bin;
20 0 ▶attr -name => sub { (ref($_[0]) =~ /(\w+)$/) ? $1 : 'Noname' };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
ref($_[0]) =~ /(\w+)$/ ? : 0 0 0
21 39 attr request_obj => 'Kelp::Request';
22 39 attr response_obj => 'Kelp::Response';
23 39 attr context_obj => 'Kelp::Context';
24 39 attr middleware_obj => 'Kelp::Middleware';
25  
26 # Debug
27 39 ▶attr long_error => $ENV{KELP_LONG_ERROR} // 0;
Condition50%1/2
$ENV{"KELP_LONG_ERROR"} // 0
l!l
039
MC/DC0%0/1
$ENV{"KELP_LONG_ERROR"} // 0
$ENV{"KELP_LONG_ERROR"}
Truth table50%1/2
A
A= $ENV{"KELP_LONG_ERROR"}
Aresult
11
00
28  
29 # The charset is set to UTF-8 by default in config module.
30 # No default here because we want to support 'undef' charset
31 29 ▶attr charset => sub { $_[0]->config('charset') };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 30 ▶attr request_charset => sub { $_[0]->config('request_charset') };
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 # Name the config module
35 39 attr config_module => 'Config';
36  
37 # Undocumented.
38 # Used to unlock the undocumented features of the Config module.
39 39 attr __config => undef;
40  
41 52 ▶attr -loaded_modules => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 # Current context of the application - tracks the state application is in,
44 # especially when it comes to managing controller instances.
45 39 attr context => \&build_context;
46  
47 # registered application encoder modules
48 45 ▶attr encoder_modules => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 # Initialization
51 sub new
52 52 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
53 52 my $self = shift->SUPER::new(@_);
54  
55 52 Kelp::Util::_DEBUG(1 => 'Loading essential modules...');
56  
57 # Always load these modules, but allow client to override
58 52 $self->_load_config();
59 51 $self->_load_routes();
60  
61 51 Kelp::Util::_DEBUG(1 => 'Loading modules from config...');
62  
63 # Load the modules from the config
64 51 ▶ if (defined(my $modules = $self->config('modules'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $modules = $self->config("modules"))) 49 2 51
65 49 $self->load_module($_) for (@$modules);
66 }
67  
68 51 Kelp::Util::_DEBUG(1 => 'Calling build method...');
69  
70 51 $self->build();
71 51 return $self;
72 }
73  
74 sub new_anon
75 6 ▶{
Subroutine new_anon: called CC=8 SCAR=21.5
Pod documented
76 6 state $last_anon = 0;
77 6 my $class = shift;
78  
79 # make sure we don't eval something dodgy
80 6 ▶ die "invalid class for new_anon"
Branch: 1 branch
BranchTrueFalseTotal
if ref $class or not $class or not "Class::Inspector"->loaded($class) or not $class->isa(__PACKAGE__) 2 4 6
Condition77%7/9
ref $class or not $class or not "Class::Inspector"->loaded($class) or not $class->isa(__PACKAGE__)
l!l&&r!l&&!r
204
ref $class or not $class or not "Class::Inspector"->loaded($class)
l!l&&r!l&&!r
114
ref $class or not $class
l!l&&r!l&&!r
015
MC/DC50%2/4
ref $class or not $class or not "Class::Inspector"->loaded($class) or not $class->isa(__PACKAGE__)
ref $class not $class not "Class::Inspector"->loaded($class) not $class->isa(__PACKAGE__)
Truth table60%3/5
A or not(B) or not(C) or not(D)
A= ref $classB= not $classC= not "Class::Inspector"->loaded($class)D= not $class->isa(__PACKAGE__)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
81 if ref $class # not a string
82 || !$class # not an empty string, undef or 0
83 || !Class::Inspector->loaded($class) # not a loaded class
84 || !$class->isa(__PACKAGE__) # not a correct class
85 ;
86  
87 4 my $anon_class = "Kelp::Anonymous::$class" . ++$last_anon;
88 4 my $err = do {
89 4 local $@;
90 4 ▶ my $eval_status = eval qq[
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
91 {
92 package $anon_class;
93 use parent -norequire, '$class';
94  
95 sub _real_class { '$class' }
96 }
97 1;
98 ];
99 4 ▶ $@ || !$eval_status;
Condition33%1/3
$@ || !$eval_status
l!l&&r!l&&!r
004
MC/DC0%0/2
$@ || !$eval_status
$@ !$eval_status
Truth table33%1/3
A || not(B)
A= $@B= !$eval_status
ABresult
1X1
011
000
100 };
101  
102 4 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 4 4
103 0 ▶ die "Couldn't create anonymous Kelp instance: " .
Branch: 1 branch
BranchTrueFalseTotal
length $err > 1 ? : 0 0 0
104 (length $err > 1 ? $err : 'unknown error');
105 }
106  
107 4 return $anon_class->new(@_);
108 }
109  
110 sub _load_config
111 54 ▶{
Subroutine _load_config: called CC=1 SCAR=0.0
112 54 my $self = shift;
113 53 $self->load_module($self->config_module, extra => $self->__config);
114  
115 52 Kelp::Util::_DEBUG(config => 'Merged configuration: ', $self->config_hash);
116 }
117  
118 sub _load_routes
119 52 ▶{
Subroutine _load_routes: called CC=1 SCAR=0.0
120 51 my $self = shift;
121 51 $self->load_module('Routes');
122 }
123  
124 # Create a shallow copy of the app, optionally blessed into a
125 # different subclass.
126 sub _clone
127 18 ▶{
Subroutine _clone: called CC=3 SCAR=12.0
128 17 my $self = shift;
129 17 ▶ my $subclass = shift || ref($self);
Condition33%1/3
shift() || ref $self
l!l&&r!l&&!r
1700
MC/DC0%0/2
shift() || ref $self
shift() ref $self
Truth table33%1/3
A || B
A= shift()B= ref $self
ABresult
1X1
011
000
130  
131 17 ▶ ref $self or croak '_clone requires instance';
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("_clone requires instance") 17 0 17
132 17 return bless {%$self}, $subclass;
133 }
134  
135 sub load_module
136 203 ▶{
Subroutine load_module: called CC=5 SCAR=16.1
Pod documented
137 203 my ($self, $name, %args) = @_;
138  
139 # A module name with a leading + indicates it's already fully
140 # qualified (i.e., it does not need the Kelp::Module:: prefix).
141 203 ▶ my $prefix = $name =~ s/^\+// ? undef : 'Kelp::Module';
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^\+// ? : 1 202 203
142  
143 # Make sure the module was not already loaded
144 203 ▶ return if $self->loaded_modules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->loaded_modules->{$name} 0 203 203
145  
146 203 my $class = Plack::Util::load_class($name, $prefix);
147 202 my $module = $self->loaded_modules->{$name} = $class->new(app => $self);
148  
149 # When loading the Config module itself, we don't have
150 # access to $self->config yet. This is why we check if
151 # config is available, and if it is, then we pull the
152 # initialization hash.
153 202 my $args_from_config = {};
154 202 ▶ if ($self->can('config')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("config")) 163 39 202
155 163 ▶ $args_from_config = $self->config("modules_init.$name") // {};
Condition100%2/2
$self->config("modules_init.$name") // {}
l!l
14221
MC/DC100%1/1
$self->config("modules_init.$name") // {}
$self->config("modules_init.$name")
Truth table100%2/2
A
A= $self->config("modules_init.$name")
Aresult
11
00
156 }
157  
158 202 Kelp::Util::_DEBUG(modules => "Loading $class module with args: ", {%$args_from_config, %args});
159  
160 202 $module->build(%$args_from_config, %args);
161 201 return $module;
162 }
163  
164 # Override this one to add custom initializations
165 sub build
166 42 ▶{
Subroutine build: called CC=1 SCAR=0.0
Pod documented
167 }
168  
169 # Override to use a custom context object
170 sub build_context
171 30 ▶{
Subroutine build_context: called CC=1 SCAR=0.0
Pod documented
172 30 return Kelp::Util::load_package($_[0]->context_obj)->new(
173 app => $_[0],
174 );
175 }
176  
177 # Override to use a custom request object
178 sub build_request
179 252 ▶{
Subroutine build_request: called CC=1 SCAR=0.0
Pod documented
180 252 return Kelp::Util::load_package($_[0]->request_obj)->new(
181 app => $_[0],
182 env => $_[1],
183 );
184 }
185  
186 sub req
187 644 ▶{
Subroutine req: called CC=1 SCAR=0.0
Pod documented
188 644 my $self = shift;
189 644 return $self->context->req(@_);
190 }
191  
192 # Override to use a custom response object
193 sub build_response
194 246 ▶{
Subroutine build_response: called CC=1 SCAR=0.0
Pod documented
195 246 return Kelp::Util::load_package($_[0]->response_obj)->new(
196 app => $_[0],
197 );
198 }
199  
200 sub res
201 1068 ▶{
Subroutine res: called CC=1 SCAR=0.0
Pod documented
202 1068 my $self = shift;
203 1068 return $self->context->res(@_);
204 }
205  
206 # Override to change what happens before the route is handled
207 sub before_dispatch
208 261 ▶{
Subroutine before_dispatch: called CC=2 SCAR=6.9
Pod documented
209 261 my ($self, $destination) = @_;
210  
211 # Log info about the route
212 261 ▶ if ($self->can('logger')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("logger")) 1 260 261
213 1 my $req = $self->req;
214  
215 1 $self->info(
216 sprintf "%s: %s - %s %s - %s",
217 ref $self,
218 $req->address, $req->method,
219 $req->path, $destination
220 );
221 }
222 }
223  
224 # Override to change what happens when nothing gets rendered
225 sub after_unrendered
226 8 ▶{
Subroutine after_unrendered: called CC=2 SCAR=6.9
Pod documented
227 8 my ($self, $match) = @_;
228  
229 # render 404 if only briges matched
230 8 ▶ if ($match->[-1]->bridge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match->[-1]->bridge) { } 5 3 8
231 5 $self->res->render_404;
232 }
233  
234 # or die with error
235 else {
236 3 die $match->[-1]->to
237 . " did not render for method "
238 . $self->req->method;
239 }
240 }
241  
242 # Override to manipulate the end response
243 sub before_finalize
244 228 ▶{
Subroutine before_finalize: called CC=1 SCAR=0.0
Pod documented
245 228 my $self = shift;
246 228 $self->res->header('X-Framework' => 'Perl Kelp');
247 }
248  
249 # Override this to wrap more middleware around the app
250 sub run
251 251 ▶{
Subroutine run: called CC=1 SCAR=0.0
Pod documented
252 251 my $self = shift;
253 251 ▶ my $app = sub { $self->psgi(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
254  
255 251 Kelp::Util::_DEBUG(1 => 'Running the application...');
256  
257 251 my $middleware = Kelp::Util::load_package($self->middleware_obj)->new(
258 app => $self,
259 );
260  
261 251 return $middleware->wrap($app);
262 }
263  
264 sub _psgi_internal
265 243 ▶{
Subroutine _psgi_internal: called CC=10 SCAR=23.0
266 243 my ($self, $match) = @_;
267 243 my $req = $self->req;
268 243 my $res = $self->res;
269  
270 # Go over the entire route chain
271 243 for my $route (@$match) {
272  
273 # Dispatch
274 266 $req->named($route->named);
275 266 $req->route_name($route->name);
276 266 my $data = $self->routes->dispatch($self, $route);
277  
278 238 ▶ if ($route->bridge) {
Branch: 2 branches
BranchTrueFalseTotal
if ($route->bridge) { } 33 205 238
elsif (defined $data) { } 189 16 205
279  
280 # Is it a bridge? Bridges must return a true value to allow the
281 # rest of the routes to run. They may also have rendered
282 # something, in which case trust that and don't render 403 (but
283 # still end the execution chain)
284  
285 33 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 4 29 33
286 4 ▶ $res->render_403 unless $res->rendered;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->rendered 2 2 4
287 }
288 }
289 elsif (defined $data) {
290  
291 # If the non-bridge route returned something, then analyze it and render it
292  
293 # Handle delayed response if CODE
294 189 ▶ return $data if ref $data eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "CODE" 1 188 189
295 188 ▶ $res->render($data) unless $res->rendered;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->rendered 148 40 188
296 }
297  
298 # Do not go any further if we got a render
299 233 ▶ last if $res->rendered;
Branch: 1 branch
BranchTrueFalseTotal
if $res->rendered 201 32 233
300 }
301  
302 # If nothing got rendered
303 210 ▶ if (!$res->rendered) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->rendered) 9 201 210
304 9 $self->context->run_method(after_unrendered => ($match));
305 }
306  
307 207 return $self->finalize;
308 }
309  
310 sub NEXT_APP
311 260 ▶{
Subroutine NEXT_APP: called CC=1 SCAR=0.0
Pod documented
312 261 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
313 261 (shift @{$_[0]->{'kelp.execution_chain'}})->($_[0]);
314 260 };
315 }
316  
317 sub psgi
318 251 ▶{
Subroutine psgi: called CC=2 SCAR=7.0
Pod undocumented
319 251 my ($self, $env) = @_;
320  
321 # Initialize the app object state
322 251 $self->context->clear;
323 251 my $req = $self->req($self->build_request($env));
324 251 my $res = $self->res($self->build_response);
325  
326 # Get route matches
327 251 my $match = $self->routes->match($req->path, $req->method);
328  
329 # None found? Short-circuit and show 404
330 251 ▶ if (!@$match) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$match) 8 243 251
331 8 $res->render_404;
332 8 return $self->finalize;
333 }
334  
335 243 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
336 $env->{'kelp.execution_chain'} = [
337 271 (grep { defined } map { $_->psgi_middleware } @$match),
338 243 ▶ sub { $self->_psgi_internal($match) },
Subroutine __ANON__: called CC=1 SCAR=0.0
339 243 ];
340  
341 243 return Kelp->NEXT_APP->($env);
342 }
343 35 ▶ catch {
Subroutine __ANON__: called CC=7 SCAR=20.2
344 35 my $exception = $_;
345  
346 # CONTEXT CHANGE: we need to clear response state because anything that
347 # was rendered beforehand is no longer valid. Body and code will be
348 # replaced automatically with render methods, but headers must be
349 # cleared explicitly
350 35 $res->headers->clear;
351  
352 35 ▶ if (blessed $exception && $exception->isa('Kelp::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($exception) and $exception->isa('Kelp::Exception')) { } 10 25 35
Condition100%3/3
builtin::blessed($exception) and $exception->isa("Kelp::Exception")
!ll&&!rl&&r
17810
MC/DC100%2/2
builtin::blessed($exception) and $exception->isa("Kelp::Exception")
builtin::blessed($exception) $exception->isa("Kelp::Exception")
Truth table100%3/3
A and B
A= builtin::blessed($exception)B= $exception->isa("Kelp::Exception")
ABresult
0X0
100
111
353  
354 # only log it as an error if the body is present
355 10 ▶ $self->logger('error', $exception->body)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("logger") and defined $exception->body 0 10 10
Condition33%1/3
$self->can("logger") and defined $exception->body
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->can("logger") and defined $exception->body
$self->can("logger") defined $exception->body
Truth table33%1/3
A and B
A= $self->can("logger")B= defined $exception->body
ABresult
0X0
100
111
356 if $self->can('logger') && defined $exception->body;
357  
358 10 $res->render_exception($exception);
359 }
360 else {
361 25 ▶ my $message = $self->long_error ? longmess($exception) : $exception;
Branch: 1 branch
BranchTrueFalseTotal
$self->long_error ? : 0 25 25
362  
363 # Log error
364 25 ▶ $self->logger('critical', $message) if $self->can('logger');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("logger") 0 25 25
365  
366 # Render an application erorr (hides details on production)
367 25 $res->render_500($exception);
368 }
369  
370 35 return $self->finalize;
371 243 };
372 }
373  
374 sub finalize
375 250 ▶{
Subroutine finalize: called CC=1 SCAR=0.0
Pod undocumented
376 250 my $self = shift;
377  
378 # call it with current context, so that it will get controller's hook if
379 # possible
380 250 $self->context->run_method(before_finalize => ());
381  
382 250 return $self->res->finalize;
383 }
384  
385 #----------------------------------------------------------------
386 # Request and Response shortcuts
387 #----------------------------------------------------------------
388 sub param
389 71 ▶{
Subroutine param: called CC=1 SCAR=0.0
Pod documented
390 71 my $self = shift;
391 71 unshift @_, $self->req;
392  
393 # goto will allow carp show the correct caller
394 71 goto $_[0]->can('param');
395 }
396  
397 0 ▶sub session { shift->req->session(@_) }
Subroutine session: not called CC=1 SCAR=6.9
Pod documented
398  
399 sub stash
400 2 ▶{
Subroutine stash: called CC=2 SCAR=6.9
Pod documented
401 2 my $self = shift;
402 2 ▶ @_ ? $self->req->stash->{$_[0]} : $self->req->stash;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
403 }
404  
405 sub named
406 21 ▶{
Subroutine named: called CC=2 SCAR=7.2
Pod documented
407 21 my $self = shift;
408 21 ▶ @_ ? $self->req->named->{$_[0]} : $self->req->named;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 21 0 21
409 }
410  
411 #----------------------------------------------------------------
412 # Utility
413 #----------------------------------------------------------------
414  
415 sub is_production
416 36 ▶{
Subroutine is_production: called CC=1 SCAR=0.0
Pod documented
417 36 my $self = shift;
418 36 ▶ return any { lc $self->mode eq $_ } qw(deployment production);
Subroutine __ANON__: called CC=1 SCAR=0.0
419 }
420  
421 sub url_for
422 7 ▶{
Subroutine url_for: called CC=1 SCAR=0.0
Pod documented
423 7 my ($self, $name, @args) = @_;
424 7 my $result = $name;
425 7 ▶ try { $result = $self->routes->url($name, @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
426 7 return $result;
427 }
428  
429 sub abs_url
430 0 ▶{
Subroutine abs_url: not called CC=1 SCAR=6.9
Pod documented
431 0 my ($self, $name, @args) = @_;
432 0 my $url = $self->url_for($name, @args);
433 0 return URI->new_abs($url, $self->config('app_url'))->as_string;
434 }
435  
436 sub get_encoder
437 69 ▶{
Subroutine get_encoder: called CC=2 SCAR=7.6
Pod documented
438 69 my ($self, $type, $name) = @_;
439  
440 69 ▶ my $encoder = $self->encoder_modules->{$type} //
Condition33%1/3
$self->encoder_modules->{$type} // croak("No $type encoder")
l!l&&r!l&&!r
6900
MC/DC0%0/2
$self->encoder_modules->{$type} // croak("No $type encoder")
$self->encoder_modules->{$type} croak("No $type encoder")
Truth table33%1/3
A // B
A= $self->encoder_modules->{$type}B= croak("No $type encoder")
ABresult
1X1
011
000
441 croak "No $type encoder";
442  
443 69 return $encoder->get_encoder($name);
444 }
445  
446 39 1;
447  
448 __END__
449  
450 =pod
451  
452 =head1 NAME
453  
454 Kelp - A web framework light, yet rich in nutrients.
455  
456 =head1 SYNOPSIS
457  
458 package MyApp;
459 use parent 'Kelp';
460  
461 # bootstrap your application
462 sub build {
463 my ($self) = @_;
464  
465 my $r = $self->routes;
466  
467 $r->add('/simple/route', 'route_handler');
468 $r->add('/route/:name', {
469 to => 'namespace::controller::action',
470 ... # other options, see Kelp::Routes
471 });
472 }
473  
474 # example route handler
475 sub route_handler {
476 my ($kelp_instance, @route_parameters) = @_;
477  
478 return 'text to be rendered';
479 }
480  
481 1;
482  
483 =head1 DESCRIPTION
484  
485 Kelp is a light, modular web framework built on top of Plack.
486  
487 This document lists all the methods and attributes available in the main
488 instance of a Kelp application, passed as a first argument to route handling
489 routines. If you're just getting started, you may be more interested in the
490 following documentation pages:
491  
492 See L<Kelp::Manual> for a complete reference.
493  
494 See L<Kelp::Manual::Cookbook> for solutions to common problems.
495  
496 =head1 REASONS TO USE KELP
497  
498 =over
499  
500 =item
501  
502 B<Plack ecosystem>. Kelp isn't just compatible with L<Plack>, it's built on top
503 of it. Your application can be supported by a collection of already available Plack
504 components.
505  
506 =item
507  
508 B<Advanced Routing>. Create intricate, yet simple ways to capture HTTP requests
509 and route them to their designated code. Use explicit and optional named
510 placeholders, wildcards, or just regular expressions.
511  
512 =item
513  
514 B<Flexible Configuration>. Use different configuration file for each
515 environment, e.g. development, deployment, etc. Merge a temporary configuration
516 into your current one for testing and debugging purposes.
517  
518 =item
519  
520 B<Enhanced Logging>. Log messages at different levels of emergency. Log to a
521 file, screen, or anything supported by L<Log::Dispatch>.
522  
523 =item
524  
525 B<Powerful Rendering>. Use the built-in auto-rendering logic, or the template
526 module of your choice to return rich text, html and JSON responses.
527  
528 =item
529  
530 B<JSON encoder/decoder>. Kelp can handle JSON-formatted requests and responses
531 automatically, making working with JSON much more enjoyable. On top of that, it
532 uses L<JSON::MaybeXS> to choose the best (fastest, most secure) backend
533 available.
534  
535 =item
536  
537 B<Extendable Core>. Kelp has straightforward code and uses pluggable modules
538 for everything. This allows anyone to extend it or add a module for a custom
539 interface. Writing Kelp modules is easy.
540  
541 =item
542  
543 B<Sleek Testing>. Kelp takes Plack::Test and wraps it in an object oriented
544 class of convenience methods. Testing is done via sending requests to your
545 routes, then analyzing the response.
546  
547 =back
548  
549 =head1 ATTRIBUTES
550  
551 =head2 host
552  
553 Gets the current hostname.
554  
555 sub some_route {
556 my $self = shift;
557 if ( $self->host eq 'prod-host' ) {
558 ...
559 }
560 }
561  
562 =head2 mode
563  
564 Sets or gets the current mode. The mode is important for the app to know what
565 configuration file to merge into the main configuration. See
566 L<Kelp::Module::Config> for more information.
567  
568 my $app = MyApp->new( mode => 'development' );
569 # conf/config.pl and conf/development.pl are merged with priority
570 # given to the second one.
571  
572 =head2 context_obj
573  
574 Provide a custom package name to define the ::Context object. Defaults to
575 L<Kelp::Context>.
576  
577 =head2 middleware_obj
578  
579 Provide a custom package name to define the middleware object. Defaults to
580 L<Kelp::Middleware>.
581  
582 =head2 request_obj
583  
584 Provide a custom package name to define the ::Request object. Defaults to
585 L<Kelp::Request>.
586  
587 =head2 response_obj
588  
589 Provide a custom package name to define the ::Response object. Defaults to
590 L<Kelp::Response>.
591  
592 =head2 config_module
593  
594 Sets of gets the class of the configuration module to be loaded on startup. The
595 default value is C<Config>, which will cause the C<Kelp::Module::Config> to get
596 loaded. See the documentation for L<Kelp::Module::Config> for more information
597 and for an example of how to create and use other config modules.
598  
599 =head2 loaded_modules
600  
601 A hashref containing the names and instances of all loaded modules. For example,
602 if you have these two modules loaded: Template and JSON, then a dump of
603 the C<loaded_modules> hash will look like this:
604  
605 {
606 Template => Kelp::Module::Template=HASH(0x208f6e8),
607 JSON => Kelp::Module::JSON=HASH(0x209d454)
608 }
609  
610 This can come in handy if your module does more than just registering a new method
611 into the application. Then, you can use its object instance to access that
612 additional functionality.
613  
614  
615 =head2 path
616  
617 Gets the current path of the application. That would be the path to C<app.psgi>
618  
619 =head2 name
620  
621 Gets or sets the name of the application. If not set, the name of the main
622 class will be used.
623  
624 my $app = MyApp->new( name => 'Twittar' );
625  
626 =head2 charset
627  
628 Gets or sets the output encoding charset of the app. It will be C<UTF-8>, if
629 not set to anything else. The charset can also changed in the config files.
630  
631 If the charset is explicitly configured to be C<undef> or false, the
632 application won't do any automatic encoding of responses, unless you set it by
633 explicitly calling L<Kelp::Response/charset>.
634  
635 =head2 request_charset
636  
637 Same as L</charset>, but only applies to the input. Request data will be
638 decoded using this charset or charset which came with the request.
639  
640 If the request charset is explicitly configured to be C<undef> or false, the
641 application won't do any automatic decoding of requests, B<even if message came
642 with a charset>.
643  
644 For details, see L<Kelp::Request/ENCODING>.
645  
646 =head2 long_error
647  
648 When a route dies, Kelp will by default display a short error message. Set this
649 attribute to a true value if you need to see a full stack trace of the error.
650 The C<KELP_LONG_ERROR> environment variable can also set this attribute.
651  
652 =head2 req
653  
654 This attribute only makes sense if called within a route definition. It will
655 contain a reference to the current L<Kelp::Request> instance.
656  
657 sub some_route {
658 my $self = shift;
659 if ( $self->req->is_json ) {
660 ...
661 }
662 }
663  
664 This attribute is a proxy to the same attribute in L</context>.
665  
666 =head2 res
667  
668 This attribute only makes sense if called within a route definition. It will
669 contain a reference to the current L<Kelp::Response> instance.
670  
671 sub some_route {
672 my $self = shift;
673 $self->res->json->render( { success => 1 } );
674 }
675  
676 This attribute is a proxy to the same attribute in L</context>.
677  
678 =head2 context
679  
680 This holds application's context. Its usage is advanced and only useful for
681 controller logic, but may allow for some introspection into Kelp.
682  
683 For example, if you have a route in a controller and need to get the original
684 Kelp app object, you may call this:
685  
686 sub some_route {
687 my $controller = shift;
688 my $app = $controller->context->app;
689 }
690  
691 =head2 encoder_modules
692  
693 A hash reference of registered encoder modules. Should only be interacted with
694 through L</get_encoder> or inside encoder module's code.
695  
696 =head1 METHODS
697  
698 =head2 new
699  
700 my $the_only_kelp = KelpApp->new;
701  
702 A standard constructor. B<Cannot> be called multiple times: see L</new_anon>.
703  
704 =head2 new_anon
705  
706 my $kelp1 = KelpApp->new_anon(config => 'conf1');
707 my $kelp2 = KelpApp->new_anon(config => 'conf2');
708  
709 B<Deprecated>. This only solves the problem in a basic scenario and has a
710 critical bug when it comes to subclassing and reblessing the app. The
711 C<new_anon> constructor itself will not be removed, but its internals will be
712 modified to use a more robust implementation method. It is not guaranteed to
713 work exactly the same for your use case with the new method. It's usually
714 better to treat every Kelp app like a singleton since that's how it was
715 designed.
716  
717 A constructor that can be called repeatedly. Cannot be mixed with L</new>.
718  
719 It works by creating a new anonymous class extending the class of your
720 application and running I<new> on it. C<ref $kelp> will return I<something
721 else> than the name of your Kelp class, but C<< $kelp->isa('KelpApp') >> will
722 be true. This will likely be useful during testing or when running multiple
723 instances of the same application with different configurations.
724  
725 =head2 build
726  
727 On its own, the C<build> method doesn't do anything. It is called by the
728 constructor, so it can be overridden to add route destinations and
729 initializations.
730  
731 package MyApp;
732  
733 sub build {
734 my $self = shift;
735 my $r = $self->routes;
736  
737 # Load some modules
738 $self->load_module("MongoDB");
739 $self->load_module("Validate");
740  
741 # Add all route destinations
742 $r->add("/one", "one");
743 ...
744  
745 }
746  
747 =head2 load_module
748  
749 C<load_module($name, %options)>
750  
751 Used to load a module. All modules should be under the C<Kelp::Module::>
752 namespace. If they are not, their class name must be prepended with C<+>.
753  
754 $self->load_module("Redis", server => '127.0.0.1');
755 # Will look for and load Kelp::Module::Redis
756  
757 Options for the module may be specified after its name, or in the
758 C<modules_init> hash in the config. Precedence is given to the
759 inline options.
760 See L<Kelp::Module> for more information on making and using modules.
761  
762 =head2 build_context
763  
764 This method is used to build the context. By default, it's used lazily by
765 L</context_obj> attribute. It can be overridden to modify how context is built.
766  
767 =head2 build_request
768  
769 This method is used to create the request object for each HTTP request. It
770 returns an instance of the class defined in the request_obj attribute (defaults to
771 L<Kelp::Request>), initialized with the current request's environment. You can
772 override this method to use a custom request module if you need to do something
773 interesting. Though there is a provided attribute that can be used to overide
774 the class of the object used.
775  
776 package MyApp;
777 use MyApp::Request;
778  
779 sub build_request {
780 my ( $self, $env ) = @_;
781 return MyApp::Request->new( app => $app, env => $env );
782 }
783  
784 # Now each request will be handled by MyApp::Request
785  
786 =head2 build_response
787  
788 This method creates the response object, e.g. what an HTTP request will return.
789 By default the object created is L<Kelp::Response> though this can be
790 overwritten via the respone_obj attribute. Much like L</build_request>, the
791 response can also be overridden to use a custom response object if you need
792 something completely custom.
793  
794 =head2 before_dispatch
795  
796 Override this method to modify the behavior before a route is handled. The
797 default behavior is to log access (if C<logger> is available).
798  
799 package MyApp;
800  
801 sub before_dispatch {
802 my ( $self, $destination ) = @_;
803  
804 # default access logging is disabled
805 }
806  
807 The C<$destination> param will depend on the routes implementation used. The
808 default router will pass the unchanged L<Kelp::Routes::Pattern/to>. If
809 possible, it will be run on the controller object (allowing overriding
810 C<before_dispatch> on controller classes).
811  
812 =head2 after_unrendered
813  
814 Override this method to control what's going to happen when a route has been
815 found but it did not render anything. The default behavior is to render page
816 404 (if only bridges are found) or throw an error.
817  
818 This hook will get passed an array reference to all matched routes, so you can
819 inspect them at will to decide what to do. It's strongly recommended to still
820 render 404 if the last match is a bridge (as is default).
821  
822 sub after_unrendered {
823 my ( $self, $matches ) = @_;
824  
825 if ($matches->[-1]->bridge) {
826 $self->res->render_404;
827 }
828 else {
829 # do something custom
830 }
831 }
832  
833 =head2 before_finalize
834  
835 Override this method to modify the response object just before it gets
836 finalized.
837  
838 package MyApp;
839  
840 sub before_finalize {
841 my $self = shift;
842 $self->res->set_header("X-App-Name", "MyApp");
843 }
844  
845 ...
846  
847 The above is an example of how to insert a custom header into the response of
848 every route.
849  
850  
851 =head2 run
852  
853 This method builds and returns the PSGI app. You can override it to get more
854 control over PSGI representation of the app.
855  
856 =head2 param
857  
858 A shortcut to C<$self-E<gt>req-E<gt>param>:
859  
860 sub some_route {
861 my $self = shift;
862 if ( $self->param('age') > 18 ) {
863 $self->can_watch_south_path(1);
864 }
865 }
866  
867 This function can be tricky to use because of context sensivity. See
868 L<Kelp::Request/param> for more information and examples.
869  
870 =head2 session
871  
872 A shortcut to C<$self-E<gt>req-E<gt>session>. Take a look at L<Kelp::Request/session>
873 for more information and examples.
874  
875 =head2 stash
876  
877 Provides safe access to C<$self-E<gt>req-E<gt>stash>. When called without
878 arguments, it will return the stash hash. If called with a single argument, it
879 will return the value of the corresponding key in the stash.
880 See L<Kelp::Request/stash> for more information and examples.
881  
882 =head2 named
883  
884 Provides safe access to C<$self-E<gt>req-E<gt>named>. When called without
885 arguments, it will return the named hash. If called with a single argument, it
886 will return the value of the corresponding key in the named hash.
887 See L<Kelp::Request/named> for more information and examples.
888  
889 =head2 url_for
890  
891 A safe shortcut to C<$self-E<gt>routes-E<gt>url>. Builds a URL from path and
892 arguments.
893  
894 sub build {
895 my $self = shift;
896 $self->routes->add("/:name/:id", { name => 'name', to => sub {
897 ...
898 }});
899 }
900  
901 sub check {
902 my $self = shift;
903 my $url_for_name = $self->url_for('name', name => 'jake', id => 1003);
904 $self->res->redirect_to( $url_for_name );
905 }
906  
907 =head2 abs_url
908  
909 Same as L</url_for>, but returns the full absolute URI for the current
910 application (based on configuration).
911  
912 =head2 is_production
913  
914 Returns whether the application is in production mode. Checks if L</mode> is
915 either C<deployment> or C<production>.
916  
917 =head2 get_encoder
918  
919 my $json_encoder = $self->get_encoder('json');
920  
921 Gets an instance of a given encoder. It takes two arguments:
922  
923 =over
924  
925 =item * type of the encoder module (eg. C<json>)
926  
927 =item * optional name of the encoder (default is C<default>)
928  
929 =back
930  
931 It will get extra config (if available) from C<encoders.TYPE.NAME>
932 configuration hash. Will instantiate the encoder just once and then reuse it.
933 Croaks when there is no such encoder type.
934  
935 Example new JSON encoder type defined in config:
936  
937 encoders => {
938 json => {
939 not_very_pretty => {
940 pretty => 0,
941 },
942 },
943 },
944  
945 =head2 NEXT_APP
946  
947 Helper method for giving Kelp back the control over PSGI application. It must
948 be used when declaring route-level middleware. It is context-independent and
949 can be called from C<Kelp> package.
950  
951 use Plack::Builder;
952  
953 builder {
954 enable 'SomeMiddleware';
955 Kelp->NEXT_APP;
956 }
957  
958 Internally, it uses C<kelp.execution_chain> PSGI environment to dynamically
959 construct a wrapped PSGI app without too much overhead.
960  
961 =head1 AUTHOR
962  
963 Stefan Geneshky - minimal <at> cpan.org
964  
965 =head1 LICENSE
966  
967 This module and all the modules in this package are governed by the same license
968 as Perl itself.
969  
970 =cut
971  
« blib/lib/Kelp/Middleware.pm ↑ Summary blib/lib/Kelp/Module/Logger/Simple.pm »