| 1 | package Silki::Role::Schema::URIMaker; | ||
| 2 | { | ||
| 3 | $Silki::Role::Schema::URIMaker::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use MooseX::Params::Validate qw( validate ); | ||
| 11 | use Silki::Types qw( Bool HashRef Int Str ); | ||
| 12 | use Silki::Util qw( string_is_empty ); | ||
| 13 | use Silki::URI qw( dynamic_uri ); | ||
| 14 | |||
| 15 | use Moose::Role; | ||
| 16 | |||
| 17 | #requires_attr_or_method (??) 'domain'; | ||
| 18 | |||
| 19 | requires '_base_uri_path'; | ||
| 20 | |||
| 21 | sub uri { | ||
| 22 | # MX::P::V doesn't handle class methods | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | my %p = validate( | ||
| 26 | \@_, | ||
| 27 | view => { isa => Str, optional => 1 }, | ||
| 28 | fragment => { isa => Str, optional => 1 }, | ||
| 29 | query => { isa => HashRef, default => {} }, | ||
| 30 | host => { isa => Str, optional => 1 }, | ||
| 31 | port => { isa => Int, optional => 1 }, | ||
| 32 | with_host => { isa => Bool, default => 0 }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | my $path = $self->_base_uri_path(); | ||
| 36 | unless ( string_is_empty( $p{view} ) ) { | ||
| 37 | $path .= q{/} unless $path =~ m{/$}; | ||
| 38 | $path .= $p{view}; | ||
| 39 | } | ||
| 40 | |||
| 41 | delete $p{view}; | ||
| 42 | |||
| 43 | $self->_make_uri( | ||
| 44 | path => $path, | ||
| 45 | %p, | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _make_uri { | ||
| 50 | my $self = shift; | ||
| 51 | my %p = @_; | ||
| 52 | |||
| 53 | delete $p{fragment} | ||
| 54 | if string_is_empty( $p{fragment} ); | ||
| 55 | |||
| 56 | return dynamic_uri( | ||
| 57 | $self->_host_params_for_uri( delete $p{with_host} ), | ||
| 58 | %p, | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _host_params_for_uri { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | return unless $_[0]; | ||
| 66 | |||
| 67 | return ( | ||
| 68 | %{ $self->domain()->uri_params() }, | ||
| 69 | ( | ||
| 70 | $ENV{SERVER_PORT} | ||
| 71 | ? ( port => $ENV{SERVER_PORT} ) | ||
| 72 | : () | ||
| 73 | ) | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | # ABSTRACT: Adds an $object->uri() method | ||
| 80 | |||
| 81 | __END__ |