| 1 | package Yukki::Web::Router::Route; | ||
| 2 | { | ||
| 3 | $Yukki::Web::Router::Route::VERSION = '0.140290'; | ||
| 4 | } | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | extends 'Path::Router::Route'; | ||
| 8 | |||
| 9 | use Yukki::Types qw( AccessLevel ); | ||
| 10 | use Yukki::Web::Router::Route::Match; | ||
| 11 | |||
| 12 | use MooseX::Types::Moose qw( ArrayRef HashRef ); | ||
| 13 | use MooseX::Types::Structured qw( Tuple ); | ||
| 14 | use List::MoreUtils qw( any ); | ||
| 15 | |||
| 16 | # ABSTRACT: Adds ACLs to routes | ||
| 17 | |||
| 18 | |||
| 19 | has acl => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => ArrayRef[Tuple[AccessLevel,HashRef]], | ||
| 22 | required => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | sub is_component_slurpy { | ||
| 27 | my ($self, $component) = @_; | ||
| 28 | $component =~ /^[+*]:/; | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub is_component_optional { | ||
| 33 | my ($self, $component) = @_; | ||
| 34 | $component =~ /^[?*]:/; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub is_component_variable { | ||
| 39 | my ($self, $component) = @_; | ||
| 40 | $component =~ /^[?*+]?:/; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub get_component_name { | ||
| 45 | my ($self, $component) = @_; | ||
| 46 | my ($name) = ($component =~ /^[?*+]?:(.*)$/); | ||
| 47 | return $name; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub has_slurpy_match { | ||
| 52 | my $self = shift; | ||
| 53 | return any { $self->is_component_slurpy($_) } reverse @{ $self->components }; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub create_default_mapping { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | my %defaults = %{ $self->defaults }; | ||
| 61 | for my $key (keys %defaults) { | ||
| 62 | if (ref $defaults{$key} eq 'ARRAY') { | ||
| 63 | $defaults{$key} = [ @{ $defaults{$key} } ]; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | return \%defaults; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | sub match { | ||
| 72 | my ($self, $parts) = @_; | ||
| 73 | |||
| 74 | return unless ( | ||
| 75 | @$parts >= $self->length_without_optionals && | ||
| 76 | ($self->has_slurpy_match || @$parts <= $self->length) | ||
| 77 | ); | ||
| 78 | |||
| 79 | my @parts = @$parts; # for shifting | ||
| 80 | |||
| 81 | my $mapping = $self->has_defaults ? $self->create_default_mapping : {}; | ||
| 82 | |||
| 83 | for my $c (@{ $self->components }) { | ||
| 84 | unless (@parts) { | ||
| 85 | die "should never get here: " . | ||
| 86 | "no \@parts left, but more required components remain" | ||
| 87 | if ! $self->is_component_optional($c); | ||
| 88 | last; | ||
| 89 | } | ||
| 90 | |||
| 91 | my $part; | ||
| 92 | if ($self->is_component_slurpy($c)) { | ||
| 93 | $part = [ @parts ]; | ||
| 94 | @parts = (); | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | $part = shift @parts; | ||
| 98 | } | ||
| 99 | |||
| 100 | if ($self->is_component_variable($c)) { | ||
| 101 | my $name = $self->get_component_name($c); | ||
| 102 | |||
| 103 | if (my $v = $self->has_validation_for($name)) { | ||
| 104 | return unless $v->check($part); | ||
| 105 | } | ||
| 106 | |||
| 107 | $mapping->{$name} = $part; | ||
| 108 | } | ||
| 109 | |||
| 110 | else { | ||
| 111 | return unless $c eq $part; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | return Yukki::Web::Router::Route::Match->new( | ||
| 116 | path => join('/', @$parts), | ||
| 117 | route => $self, | ||
| 118 | mapping => $mapping, | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ |