| 1 | package Yukki::Web::Context; | ||||||||||||||
| 2 | 8 | $Yukki::Web::Context::VERSION = '0.991_007'; # TRIAL | |||||||||||||
| 3 | 8 | $Yukki::Web::Context::VERSION = '0.991007'; | |||||||||||||
| 4 | 8 | ▶ | use v5.24; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 8 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 8 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 8 | ▶ | use Sub::Name qw( subname ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 8 | ▶ | use Type::Utils; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 8 | ▶ | use Types::Standard qw( ArrayRef HashRef Str ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 8 | ▶ | use Types::URI qw( Uri ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 8 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 8 | ▶ | use Yukki::Web::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 8 | ▶ | use Yukki::Web::Response; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 8 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | # ABSTRACT: request-response context descriptor | ||||||||||||||
| 19 | |||||||||||||||
| 20 | |||||||||||||||
| 21 | 8 | has env => ( | |||||||||||||
| 22 | is => 'ro', | ||||||||||||||
| 23 | isa => HashRef, | ||||||||||||||
| 24 | required => 1, | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | |||||||||||||||
| 28 | has request => ( | ||||||||||||||
| 29 | is => 'ro', | ||||||||||||||
| 30 | isa => class_type('Yukki::Web::Request'), | ||||||||||||||
| 31 | required => 1, | ||||||||||||||
| 32 | lazy => 1, | ||||||||||||||
| 33 | 2 | ▶ | default => sub { Yukki::Web::Request->new(env => shift->env) }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 8 | handles => [ qw( session session_options ) ], | |||||||||||||
| 35 | ); | ||||||||||||||
| 36 | |||||||||||||||
| 37 | |||||||||||||||
| 38 | has response => ( | ||||||||||||||
| 39 | is => 'ro', | ||||||||||||||
| 40 | isa => class_type('Yukki::Web::Response'), | ||||||||||||||
| 41 | required => 1, | ||||||||||||||
| 42 | lazy => 1, | ||||||||||||||
| 43 | 1 | ▶ | default => sub { Yukki::Web::Response->new }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | 8 | ); | |||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | has stash => ( | ||||||||||||||
| 48 | is => 'ro', | ||||||||||||||
| 49 | isa => HashRef, | ||||||||||||||
| 50 | required => 1, | ||||||||||||||
| 51 | 3 | ▶ | default => sub { +{} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 52 | 8 | ); | |||||||||||||
| 53 | |||||||||||||||
| 54 | |||||||||||||||
| 55 | 8 | has base_url => ( | |||||||||||||
| 56 | is => 'rw', | ||||||||||||||
| 57 | isa => Uri, | ||||||||||||||
| 58 | required => 1, | ||||||||||||||
| 59 | coerce => 1, | ||||||||||||||
| 60 | lazy => 1, | ||||||||||||||
| 61 | builder => '_build_base_url', | ||||||||||||||
| 62 | ); | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub _build_base_url { | ||||||||||||
|
Subroutine
_build_base_url: not called CC=4 SCAR=30.0
| |||||||||||||||
| 65 | 0 | my $self = shift; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 0 | my $base_url = $self->env->{'yukki.settings'}->base_url; | |||||||||||||
| 68 | 0 | ▶ | if ($base_url eq 'SCRIPT_NAME') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 69 | 0 | return $self->request->base; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | elsif ($base_url eq 'REWRITE') { | ||||||||||||||
| 73 | 0 | my $path_info = $self->env->{PATH_INFO}; | |||||||||||||
| 74 | 0 | my $request_uri = $self->env->{REQUEST_URI}; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | ▶ | if ($request_uri =~ s/$path_info$//) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 0 | my $base_url = $self->request->uri; | |||||||||||||
| 78 | 0 | $base_url->path($request_uri); | |||||||||||||
| 79 | 0 | return $base_url->canonical; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 0 | return $self->request->base; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | else { | ||||||||||||||
| 86 | 0 | return URI->new($base_url); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | |||||||||||||||
| 91 | # TODO Store these in a flash stash | ||||||||||||||
| 92 | 8 | for my $message_type (qw( errors warnings info )) { | |||||||||||||
| 93 | has $message_type => ( | ||||||||||||||
| 94 | is => 'ro', | ||||||||||||||
| 95 | isa => ArrayRef[Str], | ||||||||||||||
| 96 | required => 1, | ||||||||||||||
| 97 | 9 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 98 | 24 | ); | |||||||||||||
| 99 | |||||||||||||||
| 100 | 8 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | |||||||||||||||
| 102 | 24 | ▶ | *{__PACKAGE__ . "::list_$message_type"} = subname "list_$message_type", sub { | ||||||||||||
|
Subroutine
list_errors: not called CC=1 SCAR=4.9
Subroutine
list_warnings: not called CC=1 SCAR=4.9
Pod
documented
Pod
documented
| |||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||
| 104 | 0 | map { ucfirst "$_." } $self->$message_type->@* | |||||||||||||
| 105 | 24 | }; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 24 | ▶ | *{__PACKAGE__ . "::add_$message_type"} = subname "add_$message_type", sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
Subroutine
add_info: not called CC=1 SCAR=4.1
Pod
documented
| |||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||
| 109 | 0 | push $self->$message_type->@*, @_; | |||||||||||||
| 110 | 24 | }; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 24 | ▶ | *{__PACKAGE__ . "::has_$message_type"} = subname "add_$message_type", sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
Subroutine
add_info: not called CC=1 SCAR=4.1
Pod
documented
| |||||||||||||||
| 113 | 0 | my $self = shift; | |||||||||||||
| 114 | 0 | scalar $self->$message_type->@*; | |||||||||||||
| 115 | 24 | }; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | |||||||||||||||
| 119 | |||||||||||||||
| 120 | 0 | ▶ | sub rebase_url { | ||||||||||||
|
Subroutine
rebase_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 121 | 0 | my ($self, $url) = @_; | |||||||||||||
| 122 | 0 | return URI->new($url)->abs($self->base_url); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | 8 | 1; | |||||||||||||
| 127 | |||||||||||||||
| 128 | __END__ | ||||||||||||||