CatalystX-PathContext

 view release on metacpan or  search on metacpan

lib/CatalystX/PathContext.pm  view on Meta::CPAN

package CatalystX::PathContext;

use Moose::Role;

our $VERSION = 'v0.0.1';

use Catalyst::Exception;

after 'prepare_path' => sub {
    my $c = shift;

    my @removed;

    # we have to concatenate __PACKAGE__ with an empty string
    # otherwise it's interpreted as hash key
    my $config_ref   = $c->config()->{ q{} . __PACKAGE__ };
    my $request_path = $c->request()->path();
    if ( $config_ref ) {
        for my $context_ref ( @{$config_ref} ) {
            if ( ref $context_ref->{match} eq 'CODE' ) {
                for my $item ( $context_ref->{match}->($c, $request_path) ) {
                    $request_path =~ s{\A $item (?: / | \z ) }{}xms;
                    push @removed, $item;
                }
            }
            else {
                if ( 'Regexp' ne ref $context_ref->{match} ) {
                    # upgrade string based regex in config ref for performance++
                    $context_ref->{match}
                        = qr{\A $context_ref->{match} (?: / | \z )}xms;
                }

                # find if we should handle this url part
                if ( $request_path =~ m{$context_ref->{match}}xms ) {
                    # write a copy of named captures into path_context stash key
                    $c->stash()->{path_context}->{$context_ref->{name}} = { %+ };

                    # push each path part into removed array for
                    # rewrite of path_segments
                    push @removed,
                        split m{/}xms, $&;  ## no critic (ProhibitMatchVars)

                    # remove from request path
                    $request_path =~ s{$context_ref->{match}}{}xms;
                }
            }
        }
    }

    # Stuff modified request path back into request:
    $c->request()->path($request_path);

    # Modify the path part of the request base to include the path prefix:
    my $base = $c->request()->base();
    $base->path_segments(
        $base->path_segments() ? $base->path_segments()
                               : (),
        @removed,
    );

    return;
};

1;
__END__

=head1 NAME

CatalystX::PathContext - use different context based on the request path


=head1 VERSION

This document describes CatalystX::PathContext version 0.0.1


=head1 SYNOPSIS

    package MyApp;
    use Moose;
    use namespace::autoclean;

    use Catalyst::Runtime;
    use Catalyst;
    extends 'Catalyst';

    # our Catalyst uses the CatalystX::PathContext Role
    with 'CatalystX::PathContext';

    our $VERSION = '0.01';
    $VERSION = eval $VERSION;

    __PACKAGE__->config(
        name => 'MyApp',

        # we configure our CatalystX::PathContext object
        # to convert uri's like
        #    http://localhost:3000/eng/hello
        # to a controller which would be normally accessed via
        #    http://localhost:3000/hello
        # if we found 3 lower case chars (eg. eng) as first part of our url



( run in 0.782 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )