| 1 | package OX::Application::Role::Sugar; | ||||||||||||||
| 2 | 40 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 40 | $OX::Application::Role::Sugar::AUTHORITY = 'cpan:STEVAN'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | 40 | $OX::Application::Role::Sugar::VERSION = '0.14'; | |||||||||||||
| 6 | 40 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 40 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 40 | ▶ | use Bread::Board; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 40 | ▶ | use Plack::App::URLMap; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 40 | ▶ | use OX::Util; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 40 | has _manual_router_config => ( | |||||||||||||
| 15 | is => 'rw', | ||||||||||||||
| 16 | isa => 'HashRef', | ||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 0 | ▶ | sub BUILD { } | ||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 20 | 88 | ▶ | after BUILD => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||
| 21 | 88 | my $self = shift; | |||||||||||||
| 22 | |||||||||||||||
| 23 | 88 | ▶ | my $manual_router_config = $self->has_service('RouterConfig') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | ? $self->resolve(service => 'RouterConfig') | ||||||||||||||
| 25 | : {}; | ||||||||||||||
| 26 | 88 | $self->_manual_router_config($manual_router_config); | |||||||||||||
| 27 | |||||||||||||||
| 28 | 88 | $self->regenerate_router_config; | |||||||||||||
| 29 | 40 | }; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 89 | ▶ | sub regenerate_router_config { | ||||||||||||
|
Subroutine
regenerate_router_config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 32 | 89 | my $self = shift; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 89 | my $manual_router_config = $self->_manual_router_config; | |||||||||||||
| 35 | 89 | my $sugar_router_config = $self->meta->router_config; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 89 | ▶ | container $self => as { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 89 | service RouterConfig => { | |||||||||||||
| 39 | %$manual_router_config, | ||||||||||||||
| 40 | %$sugar_router_config, | ||||||||||||||
| 41 | }; | ||||||||||||||
| 42 | 89 | }; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 88 | ▶ | around build_middleware => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 88 | my $orig = shift; | |||||||||||||
| 47 | 88 | my $self = shift; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 88 | my @middleware = map { $_->resolve($self) } $self->meta->all_middleware; | |||||||||||||
| 50 | |||||||||||||||
| 51 | return [ | ||||||||||||||
| 52 | 88 | @{ $self->$orig(@_) }, | |||||||||||||
| 53 | @middleware, | ||||||||||||||
| 54 | ]; | ||||||||||||||
| 55 | 40 | }; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 90 | ▶ | around build_app => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||
| 58 | 90 | my $orig = shift; | |||||||||||||
| 59 | 90 | my $self = shift; | |||||||||||||
| 60 | |||||||||||||||
| 61 | 90 | my $app = $self->$orig(@_); | |||||||||||||
| 62 | 90 | ▶ | return $app unless $self->meta->has_mounts; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | |||||||||||||||
| 64 | 25 | my $urlmap = Plack::App::URLMap->new; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 25 | for my $mount ($self->meta->mounts) { | |||||||||||||
| 67 | 30 | ▶ | if ($mount->isa('OX::Meta::Mount::App')) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 68 | 21 | $urlmap->map($mount->path => $mount->app); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | elsif ($mount->isa('OX::Meta::Mount::Class')) { | ||||||||||||||
| 71 | 9 | my $service = Bread::Board::ConstructorInjection->new( | |||||||||||||
| 72 | name => '__ANON__', | ||||||||||||||
| 73 | class => $mount->class, | ||||||||||||||
| 74 | dependencies => $mount->dependencies, | ||||||||||||||
| 75 | parent => $self, | ||||||||||||||
| 76 | ); | ||||||||||||||
| 77 | 9 | my $app = $service->get; | |||||||||||||
| 78 | 9 | $urlmap->map($mount->path => $app->to_app); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | else { | ||||||||||||||
| 81 | 0 | die "Unknown mount type for path " . $mount->path . ": " | |||||||||||||
| 82 | . blessed($mount); | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 25 | ▶ | $urlmap->map('/' => $app) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | unless $self->meta->has_mount_for('/'); | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 25 | return $urlmap->to_app; | |||||||||||||
| 90 | 40 | }; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 87 | ▶ | around to_app => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 93 | 87 | my $orig = shift; | |||||||||||||
| 94 | 87 | my $self = shift; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 87 | ▶ | return $self->$orig(@_) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | unless $self->meta->has_middleware_dependencies; | ||||||||||||||
| 98 | |||||||||||||||
| 99 | # need to re-resolve for every request, to ensure that middleware | ||||||||||||||
| 100 | # dependencies are correct - otherwise, a middleware that depends on a | ||||||||||||||
| 101 | # service in an app will only resolve it once, at to_app time | ||||||||||||||
| 102 | 11 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 103 | 11 | my ($env) = @_; | |||||||||||||
| 104 | 11 | $self->$orig(@_)->($env); | |||||||||||||
| 105 | 6 | }; | |||||||||||||
| 106 | 40 | }; | |||||||||||||
| 107 | |||||||||||||||
| 108 | =pod | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =for Pod::Coverage | ||||||||||||||
| 111 | BUILD | ||||||||||||||
| 112 | regenerate_router_config | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =cut | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 40 | 1; | |||||||||||||