Web-ComposableRequest

 view release on metacpan or  search on metacpan

lib/Web/ComposableRequest/Role/Static.pm  view on Meta::CPAN

package Web::ComposableRequest::Role::Static;

use namespace::autoclean;

use Web::ComposableRequest::Constants qw( FALSE NUL TRUE );
use Web::ComposableRequest::Util      qw( first_char is_arrayref
                                          is_hashref new_uri );
use Moo::Role;

requires qw( locale path query_params scheme _base );

around '_build__base' => sub {
   my ($orig, $self) = @_; $self->mode eq 'static' or return $orig->( $self );

   my $count = () = split m{ / }mx, $self->path, -1;

   return '../' x $count;
};

around '_build_uri' => sub {
   my ($orig, $self) = @_; $self->mode eq 'static' or return $orig->( $self );

   my $path = $self->_base.$self->locale.'/'.$self->path.'.html';

   return new_uri $self->scheme, $path;
};

around 'uri_for' => sub {
   my ($orig, $self, $path, @args) = @_; $path //= NUL;

   ($self->mode eq 'static' and '/' ne substr $path, -1, 1)
      or return $orig->( $self, $path, @args );

   my $base = $self->_base; my $extn = '.html';

   my @query_params = (); my $uri_params = [];

   if (is_arrayref $args[ 0 ]) {
      $uri_params = shift @args; @query_params = @args;
   }
   elsif (is_hashref $args[ 0 ]) {
      $uri_params   =    $args[ 0 ]->{uri_params  } // [];
      @query_params = @{ $args[ 0 ]->{query_params} // [] };
      $args[ 0 ]->{base     } and $base = $args[ 0 ]->{base     };
      $args[ 0 ]->{extension} and $extn = $args[ 0 ]->{extension};
   }

   $path or $path = 'index'; $path = $base.$self->locale."/${path}";

   $uri_params->[ 0 ] and $path = join '/', $path, @{ $uri_params };

   my $uri = new_uri $self->scheme, $path.$extn;

   $query_params[ 0 ] and $uri->query_form( @query_params );

   return $uri;
};

sub mode {
   return $_[ 0 ]->query_params->( 'mode', { optional => TRUE } ) // 'online';
}

1;

__END__

=pod

=encoding utf-8

=head1 Name

Web::ComposableRequest::Role::Static - Generate static web site URIs

=head1 Synopsis



( run in 0.520 second using v1.01-cache-2.11-cpan-71847e10f99 )