| 1 | package Catalyst::ControllerRole::View; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use Scalar::Util; | ||
| 5 | use String::CamelCase; | ||
| 6 | |||
| 7 | requires 'ctx'; | ||
| 8 | |||
| 9 | our %global_content_prefixes = ( | ||
| 10 | 'HTML' => ['application/xhtml+xml', 'text/html'], | ||
| 11 | 'JSON' => ['application/json'], | ||
| 12 | 'XML' => ['application/xml', 'text/xml'], | ||
| 13 | 'JS' => ['application/javascript', 'text/javascript'], | ||
| 14 | ); | ||
| 15 | |||
| 16 | sub view { | ||
| 17 | my ($self, @args) = @_; | ||
| 18 | return $self->ctx->stash->{current_view_instance} if exists($self->ctx->stash->{current_view_instance}) && !@args; | ||
| 19 | return $self->view_for($self->ctx->action, @args); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub view_at { | ||
| 23 | my ($self, $view_name_proto, @args) = @_; | ||
| 24 | my ($view_name, $view_fragment) = split(/\./, $view_name_proto); | ||
| 25 | push @args, view_fragment => $view_fragment if $view_fragment; | ||
| 26 | |||
| 27 | $view_name = "@{[$self->path_prefix]}/$view_name" if $self->path_prefix; | ||
| 28 | my $namepart = String::CamelCase::camelize($view_name); | ||
| 29 | $namepart =~s/\//::/g; | ||
| 30 | |||
| 31 | my $view = $self->_build_view_name($namepart); | ||
| 32 | $self->ctx->log->debug("Initializing View: $view") if $self->ctx->debug; | ||
| 33 | return $self->ctx->view($view, @args); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub view_for { | ||
| 37 | my ($self, $proto, @args) = @_; | ||
| 38 | my ($action_proto, $view_fragment) = Scalar::Util::blessed($proto) | ||
| 39 | ? ($proto, undef) | ||
| 40 | : split(/\./, $proto); | ||
| 41 | |||
| 42 | push @args, view_fragment => $view_fragment if $view_fragment; | ||
| 43 | |||
| 44 | my $action = Scalar::Util::blessed($action_proto) | ||
| 45 | ? $action_proto | ||
| 46 | : $self->action_for($action_proto); | ||
| 47 | |||
| 48 | return $self->view_at($proto, @args) unless $action; # Not sure if this is right | ||
| 49 | |||
| 50 | my $action_namepart = $self->_action_namepart_from_action($action); | ||
| 51 | my $view = $self->_build_view_name($action_namepart); | ||
| 52 | |||
| 53 | $self->ctx->log->debug("Initializing View: $view") if $self->ctx->debug; | ||
| 54 | return $self->ctx->view($view, @args); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _action_namepart_from_action { | ||
| 58 | my ($self, $action) = @_; | ||
| 59 | my $action_namepart = String::CamelCase::camelize($action->reverse); | ||
| 60 | $action_namepart =~s/\//::/g; | ||
| 61 | return $action_namepart; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _build_view_name { | ||
| 65 | my ($self, $action_namepart) = @_; | ||
| 66 | |||
| 67 | my $accept = $self->ctx->request->headers->header('Accept'); | ||
| 68 | my $available_content_types = $self->_content_negotiation->{content_types}; | ||
| 69 | my $content_type = $self->_content_negotiation->{negotiator}->choose_media_type($available_content_types, $accept); | ||
| 70 | my $matched_content_type = $self->_content_negotiation->{content_types_to_prefixes}->{$content_type}; | ||
| 71 | |||
| 72 | $self->ctx->log->warn("no matching type for $accept") unless $matched_content_type; | ||
| 73 | $self->ctx->detach_error(406, +{error=>"Requested not acceptable."}) unless $matched_content_type; | ||
| 74 | $self->ctx->log->debug( "Content-Type: $content_type, Matched: $matched_content_type") if $self->ctx->debug; | ||
| 75 | |||
| 76 | my $view = $self->_view_from_parts($matched_content_type, $action_namepart); | ||
| 77 | return $view if $view; | ||
| 78 | |||
| 79 | $self->ctx->log->warn("Using default prefix 'HTML'"); | ||
| 80 | return $self->_view_from_parts('HTML', $action_namepart); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _view_from_parts { | ||
| 84 | my ($self, $matched_content_type, $action_namepart) = @_; | ||
| 85 | my $view = join('::', $matched_content_type, $action_namepart); | ||
| 86 | $self->ctx->log->debug("Negotiated View: $view") if $self->ctx->debug; | ||
| 87 | |||
| 88 | my $appclass = ref($self->ctx); | ||
| 89 | my $check = "${appclass}::View::${view}"; | ||
| 90 | my $comps = $self->ctx->components; | ||
| 91 | return $view if exists $comps->{$check}; | ||
| 92 | |||
| 93 | $self->ctx->log->warn("View not found: $check"); | ||
| 94 | return undef; | ||
| 95 | } | ||
| 96 | |||
| 97 | has '_content_negotiation' => (is => 'ro', required=>1); | ||
| 98 | |||
| 99 | sub process_component_args { | ||
| 100 | my ($class, $app, $args) = @_; | ||
| 101 | |||
| 102 | my $n = HTTP::Headers::ActionPack->new->get_content_negotiator; | ||
| 103 | my %content_prefixes = ( | ||
| 104 | %global_content_prefixes, | ||
| 105 | %{ delete($args->{content_prefixes}) || +{} }, | ||
| 106 | ); | ||
| 107 | my @content_types = map { @$_ } values %content_prefixes; | ||
| 108 | my %content_types_to_prefixes = map { | ||
| 109 | my $prefix = $_; | ||
| 110 | map { | ||
| 111 | $_ => $prefix | ||
| 112 | } @{$content_prefixes{$prefix}} | ||
| 113 | } keys %content_prefixes; | ||
| 114 | |||
| 115 | return +{ | ||
| 116 | %$args, | ||
| 117 | _content_negotiation => +{ | ||
| 118 | content_prefixes => \%content_prefixes, | ||
| 119 | content_types_to_prefixes => \%content_types_to_prefixes, | ||
| 120 | content_types => \@content_types, | ||
| 121 | negotiator => $n, | ||
| 122 | }, | ||
| 123 | }; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | =head1 NAME | ||
| 129 | |||
| 130 | Catalyst::ControllerRole::View - Call Views from your Controller | ||
| 131 | |||
| 132 | =head1 SYNOPSIS | ||
| 133 | |||
| 134 | package Example::Controller::Register; | ||
| 135 | |||
| 136 | use Moose; | ||
| 137 | use MooseX::MethodAttributes; | ||
| 138 | |||
| 139 | extends 'Catalyst::ControllerPerRequest'; | ||
| 140 | with 'Catalyst::ControllerRole::View'; | ||
| 141 | |||
| 142 | |||
| 143 | =head1 DESCRIPTION | ||
| 144 | |||
| 145 | Experimental role for L<Catalyst::ControllerPerRequest> that calls a view based on the | ||
| 146 | the namespace. This is a work in progress and may change in the future. | ||
| 147 | |||
| 148 | I'm not documenting this more, if you can't follow the source you shouldn't be | ||
| 149 | using this. I may trash it eventually. | ||
| 150 | |||
| 151 | =head1 ALSO SEE | ||
| 152 | |||
| 153 | L<Catalyst::Runtime>, L<Catalyst::Controller> | ||
| 154 | |||
| 155 | =head1 AUTHOR | ||
| 156 | |||
| 157 | John Napiorkowski <jjnapiork@cpan.org> | ||
| 158 | |||
| 159 | =head1 COPYRIGHT | ||
| 160 | |||
| 161 | 2023 | ||
| 162 | |||
| 163 | =head1 LICENSE | ||
| 164 | |||
| 165 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 166 |