| 1 | package Kelp::Context; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 27 | ▶ | use Kelp::Base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 27 | ▶ | use Kelp::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 27 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 27 | attr req => undef; | |||||||||||||||||||
| 8 | 27 | attr res => undef; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 0 | ▶ | attr -app => sub { croak 'app is required' }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 11 | 33 | ▶ | attr -_controllers => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 32 | ▶ | attr persistent_controllers => sub { $_[0]->app->config('persistent_controllers') }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 0 | ▶ | attr current => sub { shift->app }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | sub build_controller | ||||||||||||||||||||
| 16 | 17 | ▶ | { | ||||||||||||||||||
|
Subroutine
build_controller: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 17 | 17 | my ($self, $controller_class) = @_; | |||||||||||||||||||
| 18 | 17 | return $self->app->_clone($controller_class); | |||||||||||||||||||
| 19 | } | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # loads the class, reblesses and returns - can be used to get controller on | ||||||||||||||||||||
| 22 | # demand with partial or unloaded class name | ||||||||||||||||||||
| 23 | sub controller | ||||||||||||||||||||
| 24 | 7 | ▶ | { | ||||||||||||||||||
|
Subroutine
controller: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 25 | 7 | my ($self, $controller) = @_; | |||||||||||||||||||
| 26 | 7 | my $base = $self->app->routes->base; | |||||||||||||||||||
| 27 | 7 | ▶ | $controller = '+' . $base | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | if !defined $controller; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 7 | $controller = Kelp::Util::camelize($controller, $base, 1); | |||||||||||||||||||
| 31 | 7 | Kelp::Util::load_package($controller); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 7 | ▶ | croak "Invalid controller, not subclassing $base" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | unless $controller->isa($base); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 7 | ▶ | return $self->_controllers->{$controller} //= | ||||||||||||||||||
|
Condition33%1/3
$self->_controllers->{$controller} //= $self->build_controller($controller)
MC/DC0%0/2
$self->_controllers->{$controller} //= $self->build_controller($controller)
$self->_controllers->{$controller}
$self->build_controller($controller)
Truth table33%1/3
A //= B
A= $self->_controllers->{$controller}B= $self->build_controller($controller)
| |||||||||||||||||||||
| 37 | $self->build_controller($controller); | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # reblesses, remembers and sets the current controller - used internally | ||||||||||||||||||||
| 41 | sub set_controller | ||||||||||||||||||||
| 42 | 275 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_controller: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 43 | 275 | my ($self, $controller) = @_; | |||||||||||||||||||
| 44 | 275 | ▶ | return $self->current($self->app) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | unless $controller; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # the controller class should already be loaded by the router | ||||||||||||||||||||
| 48 | 20 | ▶ | my $current = $self->_controllers->{$controller} //= | ||||||||||||||||||
|
Condition66%2/3
$self->_controllers->{$controller} //= $self->build_controller($controller)
MC/DC0%0/2
$self->_controllers->{$controller} //= $self->build_controller($controller)
$self->_controllers->{$controller}
$self->build_controller($controller)
Truth table66%2/3
A //= B
A= $self->_controllers->{$controller}B= $self->build_controller($controller)
| |||||||||||||||||||||
| 49 | $self->build_controller($controller); | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 20 | $self->current($current); | |||||||||||||||||||
| 52 | 20 | return $current; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # clears the object for the next route - used internally | ||||||||||||||||||||
| 56 | sub clear | ||||||||||||||||||||
| 57 | 260 | ▶ | { | ||||||||||||||||||
|
Subroutine
clear: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 58 | 260 | my $self = shift; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 260 | ▶ | %{$self->_controllers} = () | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | unless $self->persistent_controllers; | ||||||||||||||||||||
| 62 | 260 | $self->current($self->app); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | # run method in current context. If current context does not provide this | ||||||||||||||||||||
| 66 | # method, run it in app instead. | ||||||||||||||||||||
| 67 | sub run_method | ||||||||||||||||||||
| 68 | 656 | ▶ | { | ||||||||||||||||||
|
Subroutine
run_method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 69 | 656 | my $self = shift; | |||||||||||||||||||
| 70 | 656 | my $method = shift; | |||||||||||||||||||
| 71 | 656 | my $c = $self->current; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 656 | ▶ | if ($c->can($method)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 649 | $c->$method(@_); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||
| 77 | 7 | $self->app->$method(@_); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 27 | 1; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||