| 1 | package OX::Application::Role::Router; | ||
| 2 | 45 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 45 | $OX::Application::Role::Router::AUTHORITY = 'cpan:STEVAN'; | |
| 4 | } | ||
| 5 | 45 | $OX::Application::Role::Router::VERSION = '0.14'; | |
| 6 | 45 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 45 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | # ABSTRACT: base role for applications with routers | ||
| 9 | |||
| 10 | 45 | ▶ | use Bread::Board; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 45 | ▶ | use Class::Load 'load_class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 45 | ▶ | use Scalar::Util 'weaken'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | 45 | requires qw(router_class app_from_router); | |
| 17 | |||
| 18 | 0 | ▶ | sub BUILD { } |
|
Subroutine
BUILD: not called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 19 | 97 | ▶ | before BUILD => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 20 | 97 | my $_self = shift; | |
| 21 | 97 | weaken(my $self = $_self); | |
| 22 | |||
| 23 | 97 | ▶ | container $self => as { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 24 | service Router => ( | ||
| 25 | class => $self->router_class, | ||
| 26 | 205 | ▶ | block => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 27 | 205 | my $s = shift; | |
| 28 | 205 | my $router = $self->build_router($s); | |
| 29 | 205 | $self->configure_router($router); | |
| 30 | 203 | return $router; | |
| 31 | }, | ||
| 32 | 97 | dependencies => $self->router_dependencies, | |
| 33 | ); | ||
| 34 | 97 | }; | |
| 35 | 45 | }; | |
| 36 | |||
| 37 | |||
| 38 | 5 | ▶ | sub router { shift->resolve(service => 'Router') } |
|
Subroutine
router: called CC=1 SCAR=0.0
Pod
documented
| |||
| 39 | |||
| 40 | |||
| 41 | 202 | ▶ | sub build_router { |
|
Subroutine
build_router: called CC=1 SCAR=0.0
Pod
documented
| |||
| 42 | 202 | my $self = shift; | |
| 43 | 202 | my ($s) = @_; | |
| 44 | 202 | my $router_class = $self->router_class; | |
| 45 | 202 | load_class($router_class); | |
| 46 | 202 | return $router_class->new(%{ $s->params }); | |
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | 187 | ▶ | sub configure_router { } |
|
Subroutine
configure_router: called CC=1 SCAR=0.0
Pod
documented
| |||
| 51 | |||
| 52 | |||
| 53 | 93 | ▶ | sub router_dependencies { {} } |
|
Subroutine
router_dependencies: called CC=1 SCAR=0.0
Pod
documented
| |||
| 54 | |||
| 55 | 97 | ▶ | around build_middleware => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 56 | 97 | my $orig = shift; | |
| 57 | 97 | my $self = shift; | |
| 58 | 97 | my ($s) = @_; | |
| 59 | |||
| 60 | 97 | my $router = $s->param('Router'); | |
| 61 | |||
| 62 | return [ | ||
| 63 | 97 | ▶ | sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 64 | 97 | my $app = shift; | |
| 65 | 283 | ▶ | return sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 66 | 283 | my $env = shift; | |
| 67 | # not just using plack.router (set by Plack::App::Path::Router) | ||
| 68 | # because we want this to be accessible to user middleware | ||
| 69 | 283 | $env->{'ox.router'} = $router; | |
| 70 | 283 | $app->($env); | |
| 71 | } | ||
| 72 | 97 | }, | |
| 73 | 97 | @{ $self->$orig(@_) }, | |
| 74 | ]; | ||
| 75 | 45 | }; | |
| 76 | |||
| 77 | 193 | ▶ | around middleware_dependencies => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 78 | 193 | my $orig = shift; | |
| 79 | 193 | my $self = shift; | |
| 80 | |||
| 81 | return { | ||
| 82 | 193 | %{ $self->$orig(@_) }, | |
| 83 | Router => 'Router', | ||
| 84 | }; | ||
| 85 | 45 | }; | |
| 86 | |||
| 87 | 99 | ▶ | sub build_app { |
|
Subroutine
build_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 88 | 99 | my $self = shift; | |
| 89 | 99 | my ($s) = @_; | |
| 90 | |||
| 91 | 99 | return $self->app_from_router($s->param('Router')); | |
| 92 | } | ||
| 93 | 193 | ▶ | around app_dependencies => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 94 | 193 | my $orig = shift; | |
| 95 | 193 | my $self = shift; | |
| 96 | return { | ||
| 97 | 193 | %{ $self->$orig(@_) }, | |
| 98 | Router => 'Router', | ||
| 99 | }; | ||
| 100 | 45 | }; | |
| 101 | |||
| 102 | |||
| 103 | 45 | 1; | |
| 104 | |||
| 105 | __END__ | ||