| 1 | package Silki::URI; | ||
| 2 | { | ||
| 3 | $Silki::URI::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Exporter qw( import ); | ||
| 10 | |||
| 11 | our @EXPORT_OK = qw( dynamic_uri static_uri ); | ||
| 12 | |||
| 13 | use List::AllUtils qw( all ); | ||
| 14 | use Silki::Config; | ||
| 15 | use Silki::Util qw( string_is_empty ); | ||
| 16 | use URI::FromHash (); | ||
| 17 | |||
| 18 | sub dynamic_uri { | ||
| 19 | my %p = @_; | ||
| 20 | |||
| 21 | $p{path} | ||
| 22 | = _prefixed_path( Silki::Config->instance()->path_prefix(), $p{path} ); | ||
| 23 | |||
| 24 | return URI::FromHash::uri(%p); | ||
| 25 | } | ||
| 26 | |||
| 27 | { | ||
| 28 | my $StaticPathPrefix; | ||
| 29 | |||
| 30 | my $config = Silki::Config->instance(); | ||
| 31 | if ( $config->is_production() ) { | ||
| 32 | $StaticPathPrefix = $config->path_prefix(); | ||
| 33 | $StaticPathPrefix .= q{/}; | ||
| 34 | $StaticPathPrefix .= $Silki::Config::VERSION || 'wc'; | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | $StaticPathPrefix = q{}; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub static_uri { | ||
| 41 | my $path = shift; | ||
| 42 | |||
| 43 | return _prefixed_path( | ||
| 44 | $StaticPathPrefix, | ||
| 45 | $path | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _prefixed_path { | ||
| 51 | my $prefix = shift; | ||
| 52 | my $path = shift; | ||
| 53 | |||
| 54 | return '/' | ||
| 55 | if all { string_is_empty($_) } $prefix, $path; | ||
| 56 | |||
| 57 | $path = ( $prefix || q{} ) . ( $path || q{} ); | ||
| 58 | |||
| 59 | return $path; | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | # ABSTRACT: A utility module for generating URIs | ||
| 65 | |||
| 66 | __END__ |