| 1 | package OX::Application; | ||||||||||
| 2 | 44 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 44 | $OX::Application::AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 4 | } | ||||||||||
| 5 | 44 | $OX::Application::VERSION = '0.14'; | |||||||||
| 6 | 44 | ▶ | use Moose 2.0200; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 44 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | # ABSTRACT: base class for OX applications | ||||||||||
| 9 | |||||||||||
| 10 | 44 | ▶ | use Bread::Board; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 44 | ▶ | use Plack::Middleware::HTTPExceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 44 | ▶ | use Plack::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 44 | ▶ | use Scalar::Util 'weaken'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 44 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 44 | ▶ | use OX::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 44 | extends 'Bread::Board::Container'; | |||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | has name => ( | ||||||||||
| 22 | is => 'rw', | ||||||||||
| 23 | isa => 'Str', | ||||||||||
| 24 | lazy => 1, | ||||||||||
| 25 | 9 | ▶ | default => sub { shift->meta->name }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 44 | ); | |||||||||
| 27 | |||||||||||
| 28 | 193 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 29 | 193 | my $_self = shift; | |||||||||
| 30 | 193 | weaken(my $self = $_self); | |||||||||
| 31 | |||||||||||
| 32 | 193 | ▶ | container $self => as { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 33 | service Middleware => ( | ||||||||||
| 34 | 99 | ▶ | block => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 99 | my $s = shift; | |||||||||
| 36 | 99 | $self->build_middleware($s); | |||||||||
| 37 | }, | ||||||||||
| 38 | 193 | dependencies => $self->middleware_dependencies, | |||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | service App => ( | ||||||||||
| 42 | 99 | ▶ | block => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 43 | 99 | my $s = shift; | |||||||||
| 44 | |||||||||||
| 45 | 99 | my $app = $self->build_app($s); | |||||||||
| 46 | |||||||||||
| 47 | my @middleware = ( | ||||||||||
| 48 | 99 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 99 | my ($app) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 287 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 287 | my $env = shift; | |||||||||
| 53 | |||||||||||
| 54 | 287 | my $res = $app->($env); | |||||||||
| 55 | |||||||||||
| 56 | Plack::Util::response_cb( | ||||||||||
| 57 | $res, | ||||||||||
| 58 | 279 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 560 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 560 | my $content = shift; | |||||||||
| 61 | |||||||||||
| 62 | # flush all services that are | ||||||||||
| 63 | # request-scoped after the response is | ||||||||||
| 64 | # returned | ||||||||||
| 65 | 560 | ▶ | $self->_flush_request_services | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | unless defined $content; | ||||||||||
| 67 | |||||||||||
| 68 | 560 | return $content; | |||||||||
| 69 | 279 | }; | |||||||||
| 70 | } | ||||||||||
| 71 | 283 | ); | |||||||||
| 72 | |||||||||||
| 73 | 283 | return $res; | |||||||||
| 74 | 99 | }; | |||||||||
| 75 | }, | ||||||||||
| 76 | 99 | @{ $s->param('Middleware') }, | |||||||||
| 77 | Plack::Middleware::HTTPExceptions->new(rethrow => 1), | ||||||||||
| 78 | ); | ||||||||||
| 79 | |||||||||||
| 80 | 99 | for my $middleware (reverse @middleware) { | |||||||||
| 81 | 341 | $app = OX::Util::apply_middleware($app, $middleware); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 99 | $app; | |||||||||
| 85 | }, | ||||||||||
| 86 | dependencies => { | ||||||||||
| 87 | Middleware => 'Middleware', | ||||||||||
| 88 | 193 | %{ $self->app_dependencies }, | |||||||||
| 89 | } | ||||||||||
| 90 | ); | ||||||||||
| 91 | 193 | }; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | 91 | ▶ | sub build_middleware { [] } | ||||||||
|
Subroutine
build_middleware: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | 193 | ▶ | sub middleware_dependencies { {} } | ||||||||
|
Subroutine
middleware_dependencies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub build_app { | ||||||||
|
Subroutine
build_app: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 102 | 0 | my $self = shift; | |||||||||
| 103 | 0 | confess(blessed($self) . " must implement the build_app method"); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | 193 | ▶ | sub app_dependencies { {} } | ||||||||
|
Subroutine
app_dependencies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | 101 | ▶ | sub to_app { | ||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 111 | 101 | my $self = shift; | |||||||||
| 112 | 101 | return $self->resolve(service => 'App'); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 279 | ▶ | sub _flush_request_services { | ||||||||
|
Subroutine
_flush_request_services: called CC=3 SCAR=11.0
| |||||||||||
| 116 | 279 | my $self = shift; | |||||||||
| 117 | |||||||||||
| 118 | 279 | for my $service ($self->get_service_list) { | |||||||||
| 119 | 1379 | my $injection = $self->get_service($service); | |||||||||
| 120 | 1379 | ▶ | if ($injection->does('Bread::Board::LifeCycle::Request')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 4 | $injection->flush_instance; | |||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 44 | __PACKAGE__->meta->make_immutable; | |||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | 44 | 1; | |||||||||
| 130 | |||||||||||
| 131 | __END__ | ||||||||||