| 1 | package Catalyst::ControllerRole::URI; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | requires 'ctx'; | ||
| 5 | |||
| 6 | use Scalar::Util; | ||
| 7 | use Encode 2.21 'decode_utf8'; | ||
| 8 | |||
| 9 | after 'register_action_methods', sub { | ||
| 10 | my ( $self, $app, @methods ) = @_; | ||
| 11 | my $class = ref $self; | ||
| 12 | |||
| 13 | my @endpoints = (); | ||
| 14 | my @types = @{ $app->dispatcher->dispatch_types ||+[] }; | ||
| 15 | foreach my $type (@types) { | ||
| 16 | if(ref($type) eq 'Catalyst::DispatchType::Chained') { | ||
| 17 | foreach my $endpoint(@{ $type->{_endpoints} || [] }) { | ||
| 18 | if ($endpoint->class eq $class) { | ||
| 19 | push @endpoints, [$endpoint, $self->_generate_uri_pattern($type, $endpoint)]; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | my $avail_width = Catalyst::Utils::term_width() - 9; | ||
| 26 | my $col1_width = ($avail_width * .6) < 35 ? 35 : int($avail_width * .6); | ||
| 27 | my $col2_width = $avail_width - $col1_width; | ||
| 28 | my $paths = Text::SimpleTable->new( | ||
| 29 | [ $col1_width, 'uri' ], [ $col2_width, 'helper' ], | ||
| 30 | ); | ||
| 31 | |||
| 32 | foreach my $endpoint_proto (@endpoints) { | ||
| 33 | my $endpoint = $endpoint_proto->[0]; | ||
| 34 | my $uri = $endpoint_proto->[1]; | ||
| 35 | my $path_args = ''; | ||
| 36 | my $path_arg_count = 0; | ||
| 37 | if(my @path_args = @{$endpoint_proto->[2]||[]}) { | ||
| 38 | $path_arg_count = scalar(@path_args); | ||
| 39 | $path_args = join ',', @path_args if scalar(@path_args) > 0; | ||
| 40 | $path_args = "([$path_args])"; | ||
| 41 | } | ||
| 42 | my $name = $endpoint->name; | ||
| 43 | my $private_path = $endpoint->private_path; $private_path =~s/^\///; $private_path =~s/\//_/g; | ||
| 44 | my $sub = sub { | ||
| 45 | my $self = shift; | ||
| 46 | my ($parts, @args) = $self->_normalize_uri_args(@_); | ||
| 47 | return $self->ctx->uri_for($self->action_for($name), $parts, @args); | ||
| 48 | }; | ||
| 49 | $paths->row($uri, "${name}_uri${path_args}\n${private_path}_uri${path_args}"); | ||
| 50 | |||
| 51 | { | ||
| 52 | no strict 'refs'; | ||
| 53 | my $controller_helper = "${class}::${name}_uri"; | ||
| 54 | *{$controller_helper} = Sub::Util::set_subname $controller_helper => $sub; | ||
| 55 | my $app_helper = "${app}::${private_path}_uri"; | ||
| 56 | my $helper = $endpoint->private_path; | ||
| 57 | *{$app_helper} = Sub::Util::set_subname $app_helper => sub { | ||
| 58 | my $c = shift; | ||
| 59 | return $c->uri_for($c->dispatcher->get_action_by_path($helper), @_); | ||
| 60 | }; | ||
| 61 | } | ||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | $app->log->debug("URI Helpers for: $class\n" . $paths->draw . "\n"); | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub _generate_uri_pattern { | ||
| 69 | my ($self, $dispatcher, $endpoint) = @_; | ||
| 70 | my $args = $endpoint->list_extra_info->{Args}; | ||
| 71 | |||
| 72 | my @parts; | ||
| 73 | if($endpoint->has_args_constraints) { | ||
| 74 | @parts = map { "{$_}" } $endpoint->all_args_constraints; | ||
| 75 | } elsif(defined $endpoint->attributes->{Args}) { | ||
| 76 | @parts = (defined($endpoint->attributes->{Args}[0]) ? (("*") x $args) : '...'); | ||
| 77 | } | ||
| 78 | |||
| 79 | my @parents = (); | ||
| 80 | my $parent = "DUMMY"; | ||
| 81 | my $extra = $dispatcher->_list_extra_http_methods($endpoint); | ||
| 82 | my $consumes = $dispatcher->_list_extra_consumes($endpoint); | ||
| 83 | my $scheme = $dispatcher->_list_extra_scheme($endpoint); | ||
| 84 | my $curr = $endpoint; | ||
| 85 | while ($curr) { | ||
| 86 | if (my $cap = $curr->list_extra_info->{CaptureArgs}) { | ||
| 87 | if($curr->has_captures_constraints) { | ||
| 88 | my $names = join '/', map { "{$_}" } $curr->all_captures_constraints; | ||
| 89 | unshift(@parts, $names); | ||
| 90 | } else { | ||
| 91 | unshift(@parts, (("*") x $cap)); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | if (my $pp = $curr->attributes->{PathPart}) { | ||
| 95 | unshift(@parts, $pp->[0]) | ||
| 96 | if (defined $pp->[0] && length $pp->[0]); | ||
| 97 | } | ||
| 98 | $parent = $curr->attributes->{Chained}->[0]; | ||
| 99 | $curr = $dispatcher->_actions->{$parent}; | ||
| 100 | unshift(@parents, $curr) if $curr; | ||
| 101 | } | ||
| 102 | my @path_args = (); | ||
| 103 | foreach my $p (@parents) { | ||
| 104 | my $name = "/${p}"; | ||
| 105 | |||
| 106 | if (defined(my $extra = $dispatcher->_list_extra_http_methods($p))) { | ||
| 107 | $name = "${extra} ${name}"; | ||
| 108 | } | ||
| 109 | if (defined(my $cap = $p->list_extra_info->{CaptureArgs})) { | ||
| 110 | if($p->has_captures_constraints) { | ||
| 111 | my $tc = join ',', @{$p->captures_constraints}; | ||
| 112 | $name .= " ($tc)"; | ||
| 113 | push @path_args, $tc if $tc; | ||
| 114 | } else { | ||
| 115 | $name .= " ($cap)"; | ||
| 116 | push @path_args, $cap if $cap; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | if (defined(my $ct = $p->list_extra_info->{Consumes})) { | ||
| 120 | $name .= ' :'.$ct; | ||
| 121 | } | ||
| 122 | if (defined(my $s = $p->list_extra_info->{Scheme})) { | ||
| 123 | $scheme = uc $s; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | my @display_parts = map { $_ =~s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; decode_utf8 $_ } @parts; | ||
| 128 | my $path = join('/', '', @display_parts) || '/'; | ||
| 129 | $path = "${extra} ${path}" if $extra; | ||
| 130 | |||
| 131 | return $path, \@path_args; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _normalize_uri_args { | ||
| 135 | my $self = shift; | ||
| 136 | my $parts_proto = shift if $_[0] && ((ref($_[0]) eq 'ARRAY') || ( blessed($_[0]) )); | ||
| 137 | my $query = shift if $_[0] && (ref($_[0]) eq 'HASH'); | ||
| 138 | my $fragment = shift if $_[0] && (ref($_[0]) eq 'SCALAR'); | ||
| 139 | |||
| 140 | my $c = $self->ctx; | ||
| 141 | my @parts = (); | ||
| 142 | |||
| 143 | # If parts are passed in then use them. If just one and its a blessed object | ||
| 144 | # then use its id. If an arrayref then use the ids of the objects. | ||
| 145 | if(blessed $parts_proto) { | ||
| 146 | push @parts, $parts_proto->id; | ||
| 147 | } elsif(ref($parts_proto) eq 'ARRAY') { | ||
| 148 | my @part_ids = map { blessed $_ ? $_->id : $_ } @$parts_proto; | ||
| 149 | push @parts, @part_ids; | ||
| 150 | } | ||
| 151 | |||
| 152 | my @return_args = (\@parts); | ||
| 153 | push @return_args, $query if $query; | ||
| 154 | push @return_args, $fragment if $fragment; | ||
| 155 | |||
| 156 | return @return_args; | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | =head1 NAME | ||
| 162 | |||
| 163 | Catalyst::ControllerRole::URI - Inject local URI helpers | ||
| 164 | |||
| 165 | =head1 SYNOPSIS | ||
| 166 | |||
| 167 | package Example::Controller::Register; | ||
| 168 | |||
| 169 | use Moose; | ||
| 170 | use MooseX::MethodAttributes; | ||
| 171 | |||
| 172 | extends 'Catalyst::ControllerPerRequest'; | ||
| 173 | with 'Catalyst::ControllerRole::URI'; | ||
| 174 | |||
| 175 | |||
| 176 | =head1 DESCRIPTION | ||
| 177 | |||
| 178 | Experimental role for L<Catalyst::ControllerPerRequest> that injects URI helper methods | ||
| 179 | into your controller and application. This is a work in progress and may change in the future. | ||
| 180 | |||
| 181 | I'm not documenting this more, if you can't follow the source you shouldn't be | ||
| 182 | using this. I may trash it eventually. | ||
| 183 | |||
| 184 | =head1 ALSO SEE | ||
| 185 | |||
| 186 | L<Catalyst::Runtime>, L<Catalyst::Controller> | ||
| 187 | |||
| 188 | =head1 AUTHOR | ||
| 189 | |||
| 190 | John Napiorkowski <jjnapiork@cpan.org> | ||
| 191 | |||
| 192 | =head1 COPYRIGHT | ||
| 193 | |||
| 194 | 2023 | ||
| 195 | |||
| 196 | =head1 LICENSE | ||
| 197 | |||
| 198 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 199 |