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 );
14 Moose::Exporter->setup_import_methods(
15 with_meta => [qw( get get_html post put del chain_point )],
16 as_is => [qw( chained args capture_args path_part action )],
18 class => ['CatalystX::Routes::Role::Class'],
23 _add_route( 'GET', @_ );
27 _add_route( 'GET_html', @_ );
31 _add_route( 'POST', @_ );
35 _add_route( 'PUT', @_ );
39 _add_route( 'DELETE', @_ );
45 my ( $name, $attrs, $sub ) = _process_args( $meta, @_ );
47 my $meth_base = '__route__' . $name;
49 _maybe_add_rest_route( $meta, $meth_base, $attrs );
51 my $meth_name = $meth_base . q{_} . $rest;
53 $meta->add_method( $meth_name => sub { goto &$sub } );
61 _add_chain_point( $meta, $name, chain_point => 1, @_ );
64 sub _add_chain_point {
66 my ( $name, $attrs, $sub ) = _process_args( $meta, @_ );
68 $meta->add_chain_point( $name => [ $attrs, $sub ] );
76 my $caller = ( caller(2) )[3];
79 "The $caller keyword expects a path string or regex as its first argument"
80 unless _STRINGLIKE0($path) || _REGEX($path);
82 die "The $caller keyword expects a sub ref as its final argument"
83 unless _CODELIKE($sub);
87 unless ( delete $p{chain_point} ) {
88 $p{ActionClass} ||= 'REST::ForBrowsers';
91 unless ( $p{PathPart} ) {
94 unless ( exists $p{Chained} ) {
95 unless ( $part =~ s{^/}{} ) {
97 $meta->name()->action_namespace('FakeConfig'), $part;
101 $p{PathPart} = [$part];
104 unless ( $p{CaptureArgs} || $p{Args} ) {
108 unless ( exists $p{Chained} ) {
112 ( my $name = $path ) =~ s/(\W)/'X' . sprintf( '%x', ord($1) )/eg;
114 return $name, \%p, $sub;
117 sub _maybe_add_rest_route {
122 return if $meta->has_method($name);
124 # This could be done by Moose::Exporter, but that would require that the
125 # module has already inherited from Cat::Controller when it calls "use
126 # CatalystX::Routes".
127 unless ( $meta->does_role('CatalystX::Routes::Role::Controller') ) {
130 'CatalystX::Routes::Role::Controller'
134 $meta->add_method( $name => sub { } );
136 $meta->add_route( $name => [ $attrs, $meta->get_method($name) ] );
142 return ( Chained => $_[0] );
146 return ( Args => [ $_[0] ] );
149 sub capture_args ($) {
150 return ( CaptureArgs => [ $_[0] ] );
154 return ( PathPart => [ $_[0] ] );
158 return ( ActionClass => [ $_[0] ] );
161 # XXX - this should be added to Params::Util
162 sub _STRINGLIKE0 ($) {
163 return _STRING( $_[0] )
167 && overload::Method( $_[0], q{""} )
173 # This is a nasty hack around some weird back compat code in
174 # Catalyst::Controller->action_namespace
178 return { case_sensitive => 0 };