1 package CatalystX::Routes;
6 use CatalystX::Routes::Role::Class;
7 use CatalystX::Routes::Role::Controller;
8 use Moose::Util qw( apply_all_roles );
9 use Params::Util qw( _CODELIKE _REGEX _STRING );
10 use Scalar::Util qw( blessed );
11 use Sub::Identify qw( sub_name );
15 Moose::Exporter->setup_import_methods(
16 with_meta => [qw( get get_html post put del chain_point )],
17 as_is => [qw( chained args capture_args path_part action )],
19 class => ['CatalystX::Routes::Role::Class'],
24 _add_route( 'GET', @_ );
28 _add_route( 'GET_html', @_ );
32 _add_route( 'POST', @_ );
36 _add_route( 'PUT', @_ );
40 _add_route( 'DELETE', @_ );
46 my ( $name, $attrs, $sub ) = _process_args( $meta, @_ );
48 my $meth_base = '__route__' . $name;
50 _maybe_add_rest_route( $meta, $meth_base, $attrs );
52 my $meth_name = $meth_base . q{_} . $rest;
54 $meta->add_method( $meth_name => sub { goto &$sub } );
62 _add_chain_point( $meta, $name, chain_point => 1, @_ );
65 sub _add_chain_point {
67 my ( $name, $attrs, $sub ) = _process_args( $meta, @_ );
69 $meta->add_chain_point( $name => [ $attrs, $sub ] );
77 my $caller = ( caller(2) )[3];
80 "The $caller keyword expects a path string or regex as its first argument"
81 unless _STRINGLIKE0($path) || _REGEX($path);
83 die "The $caller keyword expects a sub ref as its final argument"
84 unless _CODELIKE($sub);
88 unless ( delete $p{chain_point} ) {
89 $p{ActionClass} ||= 'REST::ForBrowsers';
92 unless ( $p{PathPart} ) {
95 unless ( exists $p{Chained} ) {
96 unless ( $part =~ s{^/}{} ) {
98 $meta->name()->action_namespace('FakeConfig'), $part;
102 $p{PathPart} = [$part];
105 unless ( $p{CaptureArgs} || $p{Args} ) {
109 unless ( exists $p{Chained} ) {
113 ( my $name = $path ) =~ s/(\W)/'X' . sprintf( '%x', ord($1) )/eg;
115 return $name, \%p, $sub;
118 sub _maybe_add_rest_route {
123 return if $meta->has_method($name);
125 # This could be done by Moose::Exporter, but that would require that the
126 # module has already inherited from Cat::Controller when it calls "use
127 # CatalystX::Routes".
128 unless ( $meta->does_role('CatalystX::Routes::Role::Controller') ) {
131 'CatalystX::Routes::Role::Controller'
135 $meta->add_method( $name => sub { } );
137 $meta->add_route( $name => [ $attrs, $meta->get_method($name) ] );
143 return ( Chained => $_[0] );
147 return ( Args => [ $_[0] ] );
150 sub capture_args ($) {
151 return ( CaptureArgs => [ $_[0] ] );
155 return ( PathPart => [ $_[0] ] );
159 return ( ActionClass => [ $_[0] ] );
162 # XXX - this should be added to Params::Util
163 sub _STRINGLIKE0 ($) {
164 return _STRING( $_[0] )
168 && overload::Method( $_[0], q{""} )
174 # This is a nasty hack around some weird back compat code in
175 # Catalyst::Controller->action_namespace
179 return { case_sensitive => 0 };