| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Ark::Context; | 
| 2 | 61 |  |  | 61 |  | 396 | use Mouse; | 
|  | 61 |  |  |  |  | 113 |  | 
|  | 61 |  |  |  |  | 322 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 61 |  |  | 61 |  | 20297 | use Scalar::Util (); | 
|  | 61 |  |  |  |  | 120 |  | 
|  | 61 |  |  |  |  | 1681 |  | 
| 5 | 61 |  |  | 61 |  | 29513 | use Try::Tiny; | 
|  | 61 |  |  |  |  | 122748 |  | 
|  | 61 |  |  |  |  | 3415 |  | 
| 6 | 61 |  |  | 61 |  | 26480 | use URI::WithBase; | 
|  | 61 |  |  |  |  | 398758 |  | 
|  | 61 |  |  |  |  | 13722 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $DETACH    = 'ARK_DETACH'; | 
| 9 |  |  |  |  |  |  | our $DEFERRED  = 'ARK_DEFERRED'; | 
| 10 |  |  |  |  |  |  | our $STREAMING = 'ARK_STREAMING'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | extends 'Ark::Component'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | has request => ( | 
| 15 |  |  |  |  |  |  | is       => 'rw', | 
| 16 |  |  |  |  |  |  | isa      => 'Object', | 
| 17 |  |  |  |  |  |  | required => 1, | 
| 18 |  |  |  |  |  |  | ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | has response => ( | 
| 21 |  |  |  |  |  |  | is      => 'rw', | 
| 22 |  |  |  |  |  |  | isa     => 'Ark::Response', | 
| 23 |  |  |  |  |  |  | lazy    => 1, | 
| 24 |  |  |  |  |  |  | default => sub { | 
| 25 |  |  |  |  |  |  | Ark::Response->new; | 
| 26 |  |  |  |  |  |  | }, | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | has app => ( | 
| 30 |  |  |  |  |  |  | is       => 'rw', | 
| 31 |  |  |  |  |  |  | isa      => 'Ark::Core', | 
| 32 |  |  |  |  |  |  | required => 1, | 
| 33 |  |  |  |  |  |  | weak_ref => 1, | 
| 34 |  |  |  |  |  |  | handles  => ['debug', 'log', 'get_actions', 'get_action', 'ensure_class_loaded', | 
| 35 |  |  |  |  |  |  | 'component', 'controller', 'view', 'model', 'path_to', 'config', | 
| 36 |  |  |  |  |  |  | 'router',], | 
| 37 |  |  |  |  |  |  | ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | has stash => ( | 
| 40 |  |  |  |  |  |  | is      => 'rw', | 
| 41 |  |  |  |  |  |  | isa     => 'HashRef', | 
| 42 |  |  |  |  |  |  | lazy    => 1, | 
| 43 |  |  |  |  |  |  | default => sub { {} }, | 
| 44 |  |  |  |  |  |  | ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | has stack => ( | 
| 47 |  |  |  |  |  |  | is      => 'rw', | 
| 48 |  |  |  |  |  |  | isa     => 'ArrayRef', | 
| 49 |  |  |  |  |  |  | lazy    => 1, | 
| 50 |  |  |  |  |  |  | default => sub { [] }, | 
| 51 |  |  |  |  |  |  | ); | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | has state => ( | 
| 54 |  |  |  |  |  |  | is      => 'rw', | 
| 55 |  |  |  |  |  |  | default => 0, | 
| 56 |  |  |  |  |  |  | ); | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | has error => ( | 
| 59 |  |  |  |  |  |  | is      => 'rw', | 
| 60 |  |  |  |  |  |  | isa     => 'ArrayRef', | 
| 61 |  |  |  |  |  |  | lazy    => 1, | 
| 62 |  |  |  |  |  |  | default => sub { [] }, | 
| 63 |  |  |  |  |  |  | ); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | has [qw/detached finalized/] => ( | 
| 66 |  |  |  |  |  |  | is      => 'rw', | 
| 67 |  |  |  |  |  |  | isa     => 'Bool', | 
| 68 |  |  |  |  |  |  | default => 0, | 
| 69 |  |  |  |  |  |  | ); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | {   # alias | 
| 72 | 61 |  |  | 61 |  | 499 | no warnings 'once'; | 
|  | 61 |  |  |  |  | 147 |  | 
|  | 61 |  |  |  |  | 102780 |  | 
| 73 |  |  |  |  |  |  | *req = \&request; | 
| 74 |  |  |  |  |  |  | *res = \&response; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub process { | 
| 78 | 281 |  |  | 281 | 0 | 5296 | my $self = shift; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 281 |  |  |  |  | 1181 | $self->prepare; | 
| 81 | 281 |  |  |  |  | 1490 | $self->dispatch; | 
| 82 | 281 | 50 |  |  |  | 2661 | $self->finalize unless $self->response->is_deferred; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub prepare { | 
| 86 | 281 |  |  | 281 | 0 | 548 | my $self = shift; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 281 |  |  |  |  | 2065 | $self->prepare_action; | 
| 89 | 281 |  |  |  |  | 71751 | $self->prepare_encoding; | 
| 90 | 281 |  |  |  |  | 1186 | $self->prepare_headers; | 
| 91 | 281 |  |  |  |  | 1042 | $self->prepare_body; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub prepare_action { | 
| 95 | 281 |  |  | 281 | 0 | 573 | my $self = shift; | 
| 96 | 281 |  |  |  |  | 833 | my $req  = $self->request; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 281 |  |  |  |  | 1771 | $req->match( $self->router->match($req->path) ); | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  | 281 | 0 |  | sub prepare_headers {} | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  | 281 | 0 |  | sub prepare_body {} | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub forward { | 
| 106 | 142 |  |  | 142 | 0 | 9176 | my ($self, $target, @args) = @_; | 
| 107 | 142 | 100 |  |  |  | 470 | return 0 unless $target; | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 136 | 100 |  |  |  | 445 | unless (@args) { | 
| 110 | 121 |  |  |  |  | 711 | @args = @{ $self->req->captures } ? @{ $self->req->captures } | 
|  | 10 |  |  |  |  | 159 |  | 
| 111 | 121 | 100 |  |  |  | 195 | : @{ $self->req->args }; | 
|  | 111 |  |  |  |  | 1937 |  | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 136 | 100 |  |  |  | 1714 | if (Scalar::Util::blessed($target)) { | 
| 115 | 25 | 50 |  |  |  | 579 | if ($target->isa('Ark::Action')) { | 
|  |  | 50 |  |  |  |  |  | 
| 116 | 0 |  |  |  |  | 0 | $target->dispatch($self, @args); | 
| 117 | 0 |  |  |  |  | 0 | return $self->state; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | elsif ($target->can('process')) { | 
| 120 | 25 |  |  |  |  | 157 | $self->execute($target, 'process', @args); | 
| 121 | 25 |  |  |  |  | 109 | return $self->state; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  | else { | 
| 125 | 111 | 100 |  |  |  | 641 | if ($target =~ m!^/.+!) { | 
| 126 | 18 |  |  |  |  | 202 | my ($namespace, $name) = $target =~ m!^(.*/)([^/]+)$!; | 
| 127 | 18 |  |  |  |  | 129 | $namespace =~ s!(^/|/$)!!g; | 
| 128 | 18 | 50 | 100 |  |  | 120 | if (my $action = $self->get_action($name, $namespace || '')) { | 
| 129 | 18 |  |  |  |  | 897 | $action->dispatch($self, @args); | 
| 130 | 18 |  |  |  |  | 124 | return $self->state; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | else { | 
| 134 | 93 |  |  |  |  | 323 | my $last = $self->stack->[-1]; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 93 | 50 | 33 |  |  | 1369 | if ($last | 
|  |  |  | 33 |  |  |  |  | 
| 137 |  |  |  |  |  |  | and $last->{obj}->isa('Ark::Controller') | 
| 138 |  |  |  |  |  |  | and my $action = $self->get_action($target, $last->{obj}->namespace)) { | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 93 |  |  |  |  | 4566 | $action->dispatch($self, @args); | 
| 141 | 91 |  |  |  |  | 344 | return $self->state; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  | 0 | my $error = qq/Couldn't forward to $target, Invalid action or component/; | 
| 147 | 0 |  |  |  |  | 0 | $self->log( error => $error ); | 
| 148 | 0 |  |  |  |  | 0 | push @{ $self->error }, $error; | 
|  | 0 |  |  |  |  | 0 |  | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 0 |  |  |  |  | 0 | return 0; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | sub detach { | 
| 154 | 10 |  |  | 10 | 0 | 251 | shift->forward(@_); | 
| 155 | 10 |  |  |  |  | 87 | die $DETACH; | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | sub dispatch { | 
| 159 | 272 |  |  | 272 | 0 | 596 | my $self = shift; | 
| 160 |  |  |  |  |  |  |  | 
| 161 | 272 |  |  |  |  | 1028 | my $match = $self->request->match; | 
| 162 | 272 | 100 |  |  |  | 896 | if ($match) { | 
| 163 | 271 | 100 | 66 |  |  | 1153 | $self->dispatch_private_action('begin') | 
| 164 |  |  |  |  |  |  | and $self->dispatch_auto_action | 
| 165 |  |  |  |  |  |  | and $match->dispatch($self); | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 271 |  |  |  |  | 912 | $self->detached(0); | 
| 168 | 271 | 50 | 33 |  |  | 2806 | $self->dispatch_private_action('end') | 
| 169 |  |  |  |  |  |  | unless $self->res->is_deferred or $self->res->is_streaming; | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  | else { | 
| 172 | 1 |  |  |  |  | 8 | $self->log( error => 'no action found' ); | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | sub dispatch_action { | 
| 177 | 0 |  |  | 0 | 0 | 0 | my ($self, $name) = @_; | 
| 178 |  |  |  |  |  |  |  | 
| 179 | 0 | 0 |  |  |  | 0 | my $action = ($self->router->get_actions($name, $self->req->action->namespace))[-1] | 
| 180 |  |  |  |  |  |  | or return 1; | 
| 181 | 0 |  |  |  |  | 0 | $action->dispatch($self); | 
| 182 |  |  |  |  |  |  |  | 
| 183 | 0 |  |  |  |  | 0 | !@{ $self->error }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | sub dispatch_private_action { | 
| 187 | 542 |  |  | 542 | 0 | 2575 | my ($self, $name) = @_; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 542 |  |  |  |  | 1616 | my $action = ($self->router->get_actions($name, $self->req->action->namespace))[-1]; | 
| 190 | 542 | 100 | 100 |  |  | 28989 | return 1 unless ($action and $action->attributes->{Private}); | 
| 191 |  |  |  |  |  |  |  | 
| 192 | 8 |  |  |  |  | 35 | $action->dispatch($self); | 
| 193 |  |  |  |  |  |  |  | 
| 194 | 8 |  |  |  |  | 11 | !@{ $self->error }; | 
|  | 8 |  |  |  |  | 34 |  | 
| 195 |  |  |  |  |  |  | } | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | sub dispatch_auto_action { | 
| 198 | 271 |  |  | 271 | 0 | 530 | my $self = shift; | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 271 |  |  |  |  | 852 | for my $auto ($self->router->get_actions('auto', $self->req->action->namespace)) { | 
| 201 | 4 | 50 |  |  |  | 176 | next unless $auto->attributes->{Private}; | 
| 202 | 4 |  |  |  |  | 12 | $auto->dispatch($self); | 
| 203 | 4 | 100 |  |  |  | 16 | return 0 unless $self->state; | 
| 204 |  |  |  |  |  |  | } | 
| 205 |  |  |  |  |  |  |  | 
| 206 | 270 |  |  |  |  | 11865 | 1; | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | sub depth { | 
| 210 | 12 |  |  | 12 | 0 | 18 | scalar @{ shift->stack }; | 
|  | 12 |  |  |  |  | 62 |  | 
| 211 |  |  |  |  |  |  | } | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | sub execute { | 
| 214 | 488 |  |  | 488 | 0 | 1181 | my ($self, $obj, $method, @args) = @_; | 
| 215 | 488 |  |  |  |  | 848 | my $class = ref $obj; | 
| 216 |  |  |  |  |  |  |  | 
| 217 | 488 |  |  |  |  | 1431 | $self->state(0); | 
| 218 | 488 |  |  |  |  | 745 | push @{ $self->stack }, { | 
|  | 488 |  |  |  |  | 2846 |  | 
| 219 |  |  |  |  |  |  | obj       => $obj, | 
| 220 |  |  |  |  |  |  | method    => $method, | 
| 221 |  |  |  |  |  |  | args      => \@args, | 
| 222 |  |  |  |  |  |  | as_string => "${class}->${method}" | 
| 223 |  |  |  |  |  |  | }; | 
| 224 |  |  |  |  |  |  |  | 
| 225 | 488 |  |  |  |  | 963 | my $error; | 
| 226 |  |  |  |  |  |  | try { | 
| 227 | 488 |  |  | 488 |  | 21418 | $self->execute_action($obj, $method, @args); | 
| 228 |  |  |  |  |  |  | } catch { | 
| 229 | 13 |  |  | 13 |  | 219 | $error = $_; | 
| 230 | 488 |  |  |  |  | 3867 | }; | 
| 231 |  |  |  |  |  |  |  | 
| 232 | 488 |  |  |  |  | 6270 | pop @{ $self->stack }; | 
|  | 488 |  |  |  |  | 1134 |  | 
| 233 |  |  |  |  |  |  |  | 
| 234 | 488 | 100 |  |  |  | 1445 | if ($error) { | 
| 235 | 13 | 100 |  |  |  | 166 | if ($error =~ /^${DETACH} at /) { | 
| 236 | 12 | 100 |  |  |  | 62 | die $DETACH if ($self->depth >= 1); | 
| 237 | 10 |  |  |  |  | 41 | $self->detached(1); | 
| 238 |  |  |  |  |  |  | } | 
| 239 |  |  |  |  |  |  | else { | 
| 240 | 1 |  |  |  |  | 2 | push @{ $self->error }, $error; | 
|  | 1 |  |  |  |  | 6 |  | 
| 241 | 1 |  |  |  |  | 3 | $self->state(0); | 
| 242 |  |  |  |  |  |  | } | 
| 243 |  |  |  |  |  |  | } | 
| 244 |  |  |  |  |  |  |  | 
| 245 | 486 |  |  |  |  | 1741 | $self->state; | 
| 246 |  |  |  |  |  |  | } | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | sub execute_action { | 
| 249 | 488 |  |  | 488 | 0 | 1466 | my ($self, $obj, $method, @args) = @_; | 
| 250 |  |  |  |  |  |  |  | 
| 251 | 488 |  |  |  |  | 2616 | my $state = $obj->$method($self, @args); | 
| 252 | 475 | 100 |  |  |  | 70470 | $self->state( defined $state ? $state : undef ); | 
| 253 |  |  |  |  |  |  | } | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | sub redirect { | 
| 256 | 2 |  |  | 2 | 0 | 5 | my ($self, $uri, $status) = @_; | 
| 257 |  |  |  |  |  |  |  | 
| 258 | 2 |  | 100 |  |  | 10 | $status ||= '302'; | 
| 259 |  |  |  |  |  |  |  | 
| 260 | 2 |  |  |  |  | 14 | $self->res->status($status); | 
| 261 | 2 |  |  |  |  | 117 | $self->res->header( Location => $uri ); | 
| 262 |  |  |  |  |  |  | } | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | sub redirect_and_detach { | 
| 265 | 2 |  |  | 2 | 0 | 924 | my $self = shift; | 
| 266 | 2 |  |  |  |  | 9 | $self->redirect(@_); | 
| 267 | 2 |  |  |  |  | 148 | $self->detach; | 
| 268 |  |  |  |  |  |  | } | 
| 269 |  |  |  |  |  |  |  | 
| 270 |  |  |  |  |  |  | sub uri_for { | 
| 271 | 10 |  |  | 10 | 0 | 128 | my ($self, @path) = @_; | 
| 272 | 10 | 50 |  |  |  | 46 | my $params = ref $path[-1] eq 'HASH' ? pop @path : {}; | 
| 273 |  |  |  |  |  |  |  | 
| 274 | 10 |  |  |  |  | 47 | my $base = $self->req->base; | 
| 275 | 10 |  |  |  |  | 69 | $base =~ s!/*$!!; | 
| 276 |  |  |  |  |  |  |  | 
| 277 | 10 |  |  |  |  | 170 | (my $path = join '/', @path) =~ s!/{2,}!/!g; | 
| 278 | 10 |  |  |  |  | 31 | $path =~ s!^/+!!; | 
| 279 | 10 |  |  |  |  | 66 | my $uri = URI::WithBase->new($path, $base . '/'); | 
| 280 | 10 |  |  |  |  | 852 | $uri->query_form($params); | 
| 281 |  |  |  |  |  |  |  | 
| 282 | 10 |  |  |  |  | 665 | $uri->abs; | 
| 283 |  |  |  |  |  |  | } | 
| 284 |  |  |  |  |  |  |  | 
| 285 |  |  |  |  |  |  | sub finalize { | 
| 286 | 281 |  |  | 281 | 0 | 1064 | my $self = shift; | 
| 287 |  |  |  |  |  |  |  | 
| 288 | 281 |  |  |  |  | 1002 | my $is_deferred = $self->response->is_deferred; | 
| 289 |  |  |  |  |  |  |  | 
| 290 | 281 | 50 |  |  |  | 626 | if ($is_deferred) { | 
| 291 | 0 |  |  |  |  | 0 | my $action = $self->request->action; | 
| 292 | 0 | 0 |  |  |  | 0 | if ($action) { | 
| 293 | 0 |  |  |  |  | 0 | $self->dispatch_private_action('end'); | 
| 294 |  |  |  |  |  |  | } | 
| 295 |  |  |  |  |  |  | } | 
| 296 |  |  |  |  |  |  |  | 
| 297 | 281 |  |  |  |  | 1010 | $self->finalize_headers; | 
| 298 | 281 |  |  |  |  | 951 | $self->finalize_body; | 
| 299 | 281 |  |  |  |  | 1069 | $self->finalize_encoding; | 
| 300 | 281 | 50 |  |  |  | 1295 | $self->response->finalize if $self->response->is_deferred; | 
| 301 | 281 |  |  |  |  | 1364 | $self->finalized(1); | 
| 302 |  |  |  |  |  |  | } | 
| 303 |  |  |  |  |  |  |  | 
| 304 |  |  |  | 281 | 0 |  | sub finalize_headers {} | 
| 305 |  |  |  | 281 | 0 |  | sub finalize_body {} | 
| 306 |  |  |  |  |  |  |  | 
| 307 |  |  |  |  |  |  | sub DEMOLISH { | 
| 308 | 272 |  |  | 272 | 0 | 581 | my $self = shift; | 
| 309 | 272 | 50 |  |  |  | 3762 | $self->finalize unless $self->finalized; | 
| 310 |  |  |  |  |  |  | } | 
| 311 |  |  |  |  |  |  |  | 
| 312 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; |