BasePerRequest.pm

filter: Statementstmt 26.8% 159 / 592 Branchbran 18.1% 42 / 232 Conditioncond 38.8% 14 / 36 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 17.9% 37 / 206 Podpod 5.3% 10 / 188 totaltotal 20.8% 266 / 1275 CC 135 SCAR 17.3
CC
135
Cov
25%
inject_view_helpers
33.1
inject_http_status_helpers
25.4
content_replace
20.0
CRAP 5.7
↑ Summary BasePerRequest/Lifecycle/Request.pm »
1 package Catalyst::View::BasePerRequest;
2  
3 1 our $VERSION = '0.013';
4 1 our $DEFAULT_FACTORY = 'Catalyst::View::BasePerRequest::Lifecycle::Request';
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 extends 'Catalyst::View';
12  
13 1 has 'catalyst_component_name' => (is=>'ro');
14 1 has 'app' => (is=>'ro', required=>1);
15 1 has 'ctx' => (is=>'ro', required=>1);
16 24 ▶has 'root' => (is=>'rw', required=>1, default=>sub { shift });
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 has 'parent' => (is=>'rw', predicate=>'has_parent');
18 1 has 'content_type' => (is=>'ro', required=>0, predicate=>'has_content_type');
19 1 has 'code' => (is=>'rw', predicate=>'has_code');
20 1 has 'status_codes' => (is=>'rw', predicate=>'has_status_codes');
21 1 has 'injected_views' => (is=>'rw', predicate=>'has_injected_views');
22 1 has 'forwarded_args' => (is=>'rw', predicate=>'has_forwarded_args');
23  
24 0 ▶sub application { shift->app }
Subroutine application: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 0 ▶sub views {
Subroutine views: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my ($app, %views) = @_;
28 0 $app->config->{views} = \%views;
29 }
30  
31 0 ▶sub view {
Subroutine view: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my ($app, $method, @args) = @_;
33 0 ▶ if(scalar(@args) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 1) { } 0 0 0
34 0 $app->config->{views}{$method} = \@args;
35 } else {
36 0 $app->config->{views}{$method} = $args[0];
37 }
38 }
39  
40 0 ▶sub set_content_type {
Subroutine set_content_type: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my ($app, $ct) = @_;
42 0 $app->config->{content_type} = $ct;
43 }
44  
45 0 ▶sub set_status_codes {
Subroutine set_status_codes: not called CC=2 SCAR=17.9
Pod undocumented
46 0 my $app = shift;
47 0 ▶ my $codes = ref($_[0]) ? shift : [shift];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
48 0 $app->config->{status_codes} = $codes;
49 }
50  
51 3 ▶sub COMPONENT {
Subroutine COMPONENT: called CC=4 SCAR=14.0
Pod documented
52 3 my ($class, $app, $args) = @_;
53 3 my $merged_args = $class->merge_config_hashes($class->config, $args);
54  
55 3 ▶ $merged_args = $class->modify_init_args($app, $merged_args) if $class->can('modify_init_args');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("modify_init_args") 0 3 3
56 3 my %status_codes = $class->inject_http_status_helpers($merged_args);
57 3 ▶ $merged_args->{status_codes} = \%status_codes if scalar(keys(%status_codes));
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %status_codes 2 1 3
58 3 my @injected_views = $class->inject_view_helpers($merged_args);
59 3 ▶ $merged_args->{injected_views} = \@injected_views if scalar @injected_views;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @injected_views 0 3 3
60  
61 3 my $factory_class = Module::Runtime::use_module($class->factory_class($app, $merged_args));
62 3 return my $factory = $class->build_factory($factory_class, $app, $merged_args);
63 }
64  
65 3 ▶sub inject_view_helpers {
Subroutine inject_view_helpers: called CC=7 SCAR=35.0
Pod undocumented
66 3 my ($class, $merged_args) = @_;
67 3 ▶ if(my $views = $merged_args->{views}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $views = $merged_args->{"views"}) 0 3 3
68 0 require Sub::Util;
69 0 foreach my $method (keys %$views) {
70 0 my ($view_name, @args_proto) = ();
71 0 my $options_proto = $views->{$method};
72  
73 0 my $global_args_generator;
74 0 ▶ if( (ref($options_proto)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $options_proto || '') eq 'ARRAY') { } 0 0 0
Condition0%0/2
ref $options_proto || ''
l!l
00
MC/DC0%0/1
ref $options_proto || ''
ref $options_proto
Truth table0%0/2
A
A= ref $options_proto
Aresult
11
00
75 0 ($view_name, @args_proto) = @$options_proto;
76 $global_args_generator = (ref($args_proto[0])||'') eq 'CODE' ?
77 shift(@args_proto) :
78 0 ▶ sub { @args_proto };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
(ref $args_proto[0] || '') eq 'CODE' ? : 0 0 0
Condition0%0/2
ref $args_proto[0] || ''
l!l
00
MC/DC0%0/1
ref $args_proto[0] || ''
ref $args_proto[0]
Truth table0%0/2
A
A= ref $args_proto[0]
Aresult
11
00
79 } else {
80 0 $view_name = $options_proto;
81 }
82  
83 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 0 ▶ *{"${class}::${method}"} = Sub::Util::set_subname "${class}::${method}" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
85 0 my ($self, @args) = @_;
86 0 ▶ my @global_args = $global_args_generator ? $global_args_generator->($self, $self->ctx, @args) : ();
Branch: 1 branch
BranchTrueFalseTotal
$global_args_generator ? : 0 0 0
87 0 my $view = $self->ctx->view($view_name, @global_args, @args);
88  
89 0 $view->root($self->root);
90 0 $view->parent($self);
91  
92 0 return $view;
93 0 };
94 }
95 0 return keys %$views;
96 }
97 3 return ();
98 }
99  
100 3 ▶sub inject_http_status_helpers {
Subroutine inject_http_status_helpers: called CC=5 SCAR=32.3
Pod undocumented
101 3 my ($class, $merged_args) = @_;
102  
103 3 my %status_codes = ();
104 3 ▶ if(exists $merged_args->{status_codes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $merged_args->{"status_codes"}) 2 1 3
105 2 %status_codes = map { $_=>1 } @{$merged_args->{status_codes}};
106 }
107  
108 3 foreach my $helper( grep { $_=~/^http/i} @HTTP::Status::EXPORT_OK) {
109 213 my $subname = lc $helper;
110 213 my $code = HTTP::Status->$helper;
111 213 ▶ if(scalar(keys(%status_codes))) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %status_codes) 142 71 213
112 142 ▶ next unless $status_codes{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless $status_codes{$code} 136 6 142
113 }
114 0 ▶ eval "sub ${\$class}::${\$subname} { return shift->respond(HTTP::Status::$helper,\\\@_) }";
Subroutine http_accepted: not called CC=1 SCAR=6.0
Subroutine http_already_reported: not called CC=1 SCAR=6.0
Subroutine http_bad_gateway: not called CC=1 SCAR=6.0
Subroutine http_bad_request: called CC=1 SCAR=6.0
Subroutine http_bad_request: not called CC=1 SCAR=6.0
Subroutine http_bad_request: not called CC=1 SCAR=6.0
Subroutine http_bandwidth_limit_exceeded: not called CC=1 SCAR=6.0
Subroutine http_conflict: not called CC=1 SCAR=6.0
Subroutine http_content_too_large: not called CC=1 SCAR=6.0
Subroutine http_continue: not called CC=1 SCAR=6.0
Subroutine http_created: called CC=1 SCAR=6.0
Subroutine http_created: not called CC=1 SCAR=6.0
Subroutine http_created: not called CC=1 SCAR=6.0
Subroutine http_early_hints: not called CC=1 SCAR=6.0
Subroutine http_expectation_failed: not called CC=1 SCAR=6.0
Subroutine http_failed_dependency: not called CC=1 SCAR=6.0
Subroutine http_forbidden: not called CC=1 SCAR=6.0
Subroutine http_found: not called CC=1 SCAR=6.0
Subroutine http_gateway_timeout: not called CC=1 SCAR=6.0
Subroutine http_gone: not called CC=1 SCAR=6.0
Subroutine http_http_version_not_supported: not called CC=1 SCAR=6.0
Subroutine http_i_am_a_teapot: not called CC=1 SCAR=6.0
Subroutine http_im_used: not called CC=1 SCAR=6.0
Subroutine http_insufficient_storage: not called CC=1 SCAR=6.0
Subroutine http_internal_server_error: not called CC=1 SCAR=6.0
Subroutine http_length_required: not called CC=1 SCAR=6.0
Subroutine http_locked: not called CC=1 SCAR=6.0
Subroutine http_loop_detected: not called CC=1 SCAR=6.0
Subroutine http_method_not_allowed: not called CC=1 SCAR=6.0
Subroutine http_misdirected_request: not called CC=1 SCAR=6.0
Subroutine http_moved_permanently: not called CC=1 SCAR=6.0
Subroutine http_multi_status: not called CC=1 SCAR=6.0
Subroutine http_multiple_choices: not called CC=1 SCAR=6.0
Subroutine http_network_authentication_required: not called CC=1 SCAR=6.0
Subroutine http_no_code: not called CC=1 SCAR=6.0
Subroutine http_no_content: not called CC=1 SCAR=6.0
Subroutine http_non_authoritative_information: not called CC=1 SCAR=6.0
Subroutine http_not_acceptable: not called CC=1 SCAR=6.0
Subroutine http_not_extended: not called CC=1 SCAR=6.0
Subroutine http_not_found: not called CC=1 SCAR=6.0
Subroutine http_not_implemented: not called CC=1 SCAR=6.0
Subroutine http_not_modified: not called CC=1 SCAR=6.0
Subroutine http_ok: not called CC=1 SCAR=6.0
Subroutine http_ok: not called CC=1 SCAR=6.0
Subroutine http_ok: not called CC=1 SCAR=6.0
Subroutine http_partial_content: not called CC=1 SCAR=6.0
Subroutine http_payload_too_large: not called CC=1 SCAR=6.0
Subroutine http_payment_required: not called CC=1 SCAR=6.0
Subroutine http_permanent_redirect: not called CC=1 SCAR=6.0
Subroutine http_precondition_failed: not called CC=1 SCAR=6.0
Subroutine http_precondition_required: not called CC=1 SCAR=6.0
Subroutine http_processing: not called CC=1 SCAR=6.0
Subroutine http_proxy_authentication_required: not called CC=1 SCAR=6.0
Subroutine http_range_not_satisfiable: not called CC=1 SCAR=6.0
Subroutine http_request_entity_too_large: not called CC=1 SCAR=6.0
Subroutine http_request_header_fields_too_large: not called CC=1 SCAR=6.0
Subroutine http_request_range_not_satisfiable: not called CC=1 SCAR=6.0
Subroutine http_request_timeout: not called CC=1 SCAR=6.0
Subroutine http_request_uri_too_large: not called CC=1 SCAR=6.0
Subroutine http_reset_content: not called CC=1 SCAR=6.0
Subroutine http_retry_with: not called CC=1 SCAR=6.0
Subroutine http_see_other: not called CC=1 SCAR=6.0
Subroutine http_service_unavailable: not called CC=1 SCAR=6.0
Subroutine http_switching_protocols: not called CC=1 SCAR=6.0
Subroutine http_temporary_redirect: not called CC=1 SCAR=6.0
Subroutine http_too_early: not called CC=1 SCAR=6.0
Subroutine http_too_many_requests: not called CC=1 SCAR=6.0
Subroutine http_unauthorized: not called CC=1 SCAR=6.0
Subroutine http_unavailable_for_legal_reasons: not called CC=1 SCAR=6.0
Subroutine http_unordered_collection: not called CC=1 SCAR=6.0
Subroutine http_unprocessable_content: not called CC=1 SCAR=6.0
Subroutine http_unprocessable_entity: not called CC=1 SCAR=6.0
Subroutine http_unsupported_media_type: not called CC=1 SCAR=6.0
Subroutine http_upgrade_required: not called CC=1 SCAR=6.0
Subroutine http_uri_too_long: not called CC=1 SCAR=6.0
Subroutine http_use_proxy: not called CC=1 SCAR=6.0
Subroutine http_variant_also_negotiates: not called CC=1 SCAR=6.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
115 0 ▶ eval "sub ${\$class}::set_${\$subname} {
Subroutine set_http_accepted: not called CC=2 SCAR=17.4
Subroutine set_http_already_reported: not called CC=2 SCAR=17.4
Subroutine set_http_bad_gateway: not called CC=2 SCAR=17.4
Subroutine set_http_bad_request: not called CC=2 SCAR=17.4
Subroutine set_http_bad_request: not called CC=2 SCAR=17.4
Subroutine set_http_bad_request: not called CC=2 SCAR=17.4
Subroutine set_http_bandwidth_limit_exceeded: not called CC=2 SCAR=17.4
Subroutine set_http_conflict: not called CC=2 SCAR=17.4
Subroutine set_http_content_too_large: not called CC=2 SCAR=17.4
Subroutine set_http_continue: not called CC=2 SCAR=17.4
Subroutine set_http_created: called CC=2 SCAR=17.4
Subroutine set_http_created: not called CC=2 SCAR=17.4
Subroutine set_http_created: not called CC=2 SCAR=17.4
Subroutine set_http_early_hints: not called CC=2 SCAR=17.4
Subroutine set_http_expectation_failed: not called CC=2 SCAR=17.4
Subroutine set_http_failed_dependency: not called CC=2 SCAR=17.4
Subroutine set_http_forbidden: not called CC=2 SCAR=17.4
Subroutine set_http_found: not called CC=2 SCAR=17.4
Subroutine set_http_gateway_timeout: not called CC=2 SCAR=17.4
Subroutine set_http_gone: not called CC=2 SCAR=17.4
Subroutine set_http_http_version_not_supported: not called CC=2 SCAR=17.4
Subroutine set_http_i_am_a_teapot: not called CC=2 SCAR=17.4
Subroutine set_http_im_used: not called CC=2 SCAR=17.4
Subroutine set_http_insufficient_storage: not called CC=2 SCAR=17.4
Subroutine set_http_internal_server_error: not called CC=2 SCAR=17.4
Subroutine set_http_length_required: not called CC=2 SCAR=17.4
Subroutine set_http_locked: not called CC=2 SCAR=17.4
Subroutine set_http_loop_detected: not called CC=2 SCAR=17.4
Subroutine set_http_method_not_allowed: not called CC=2 SCAR=17.4
Subroutine set_http_misdirected_request: not called CC=2 SCAR=17.4
Subroutine set_http_moved_permanently: not called CC=2 SCAR=17.4
Subroutine set_http_multi_status: not called CC=2 SCAR=17.4
Subroutine set_http_multiple_choices: not called CC=2 SCAR=17.4
Subroutine set_http_network_authentication_required: not called CC=2 SCAR=17.4
Subroutine set_http_no_code: not called CC=2 SCAR=17.4
Subroutine set_http_no_content: not called CC=2 SCAR=17.4
Subroutine set_http_non_authoritative_information: not called CC=2 SCAR=17.4
Subroutine set_http_not_acceptable: not called CC=2 SCAR=17.4
Subroutine set_http_not_extended: not called CC=2 SCAR=17.4
Subroutine set_http_not_found: not called CC=2 SCAR=17.4
Subroutine set_http_not_implemented: not called CC=2 SCAR=17.4
Subroutine set_http_not_modified: not called CC=2 SCAR=17.4
Subroutine set_http_ok: called CC=2 SCAR=17.4
Subroutine set_http_ok: not called CC=2 SCAR=17.4
Subroutine set_http_ok: not called CC=2 SCAR=17.4
Subroutine set_http_partial_content: not called CC=2 SCAR=17.4
Subroutine set_http_payload_too_large: not called CC=2 SCAR=17.4
Subroutine set_http_payment_required: not called CC=2 SCAR=17.4
Subroutine set_http_permanent_redirect: not called CC=2 SCAR=17.4
Subroutine set_http_precondition_failed: not called CC=2 SCAR=17.4
Subroutine set_http_precondition_required: not called CC=2 SCAR=17.4
Subroutine set_http_processing: not called CC=2 SCAR=17.4
Subroutine set_http_proxy_authentication_required: not called CC=2 SCAR=17.4
Subroutine set_http_range_not_satisfiable: not called CC=2 SCAR=17.4
Subroutine set_http_request_entity_too_large: not called CC=2 SCAR=17.4
Subroutine set_http_request_header_fields_too_large: not called CC=2 SCAR=17.4
Subroutine set_http_request_range_not_satisfiable: not called CC=2 SCAR=17.4
Subroutine set_http_request_timeout: not called CC=2 SCAR=17.4
Subroutine set_http_request_uri_too_large: not called CC=2 SCAR=17.4
Subroutine set_http_reset_content: not called CC=2 SCAR=17.4
Subroutine set_http_retry_with: not called CC=2 SCAR=17.4
Subroutine set_http_see_other: not called CC=2 SCAR=17.4
Subroutine set_http_service_unavailable: not called CC=2 SCAR=17.4
Subroutine set_http_switching_protocols: not called CC=2 SCAR=17.4
Subroutine set_http_temporary_redirect: not called CC=2 SCAR=17.4
Subroutine set_http_too_early: not called CC=2 SCAR=17.4
Subroutine set_http_too_many_requests: not called CC=2 SCAR=17.4
Subroutine set_http_unauthorized: not called CC=2 SCAR=17.4
Subroutine set_http_unavailable_for_legal_reasons: not called CC=2 SCAR=17.4
Subroutine set_http_unordered_collection: not called CC=2 SCAR=17.4
Subroutine set_http_unprocessable_content: not called CC=2 SCAR=17.4
Subroutine set_http_unprocessable_entity: not called CC=2 SCAR=17.4
Subroutine set_http_unsupported_media_type: not called CC=2 SCAR=17.4
Subroutine set_http_upgrade_required: not called CC=2 SCAR=17.4
Subroutine set_http_uri_too_long: not called CC=2 SCAR=17.4
Subroutine set_http_use_proxy: not called CC=2 SCAR=17.4
Subroutine set_http_variant_also_negotiates: not called CC=2 SCAR=17.4
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 77 branches
BranchTrueFalseTotal
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 1 0 1
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 1 1
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
if @headers 0 0 0
116 my (\$self, \@headers) = \@_;
117 \$self->ctx->res->status(HTTP::Status::$helper);
118 \$self->ctx->res->headers->push_header(\@headers) if \@headers;
119 return \$self;
120 }";
121 }
122  
123 3 return %status_codes;
124 }
125  
126 3 ▶sub factory_class {
Subroutine factory_class: called CC=3 SCAR=11.0
Pod undocumented
127 3 my ($class, $app, $merged_args) = @_;
128 3 ▶ if(exists $merged_args->{lifecycle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $merged_args->{"lifecycle"}) 1 2 3
129 1 my $lifecycle = $merged_args->{lifecycle};
130 1 ▶ $lifecycle = "Catalyst::View::BasePerRequest::Lifecycle::${lifecycle}" unless $lifecycle=~/^\+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lifecycle =~ /^\+/ 1 0 1
131 1 return $lifecycle;
132 }
133 2 return $DEFAULT_FACTORY;
134 }
135  
136 24 ▶sub build {
Subroutine build: called CC=1 SCAR=0.0
Pod documented
137 24 my ($class, %args) = @_;
138 24 return $class->new(%args);
139 }
140  
141 3 ▶sub build_factory {
Subroutine build_factory: called CC=1 SCAR=0.0
Pod undocumented
142 3 my ($class, $factory_class, $app, $merged_args) = @_;
143 3 return $factory_class->new(
144 class => $class,
145 app => $app,
146 merged_args => $merged_args
147 );
148 }
149  
150 0 ▶sub render {
Subroutine render: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my ($self, $ctx, @args) = @_;
152 0 die "You need to write a 'render' method!";
153 }
154  
155 1 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod documented
156 1 my ($self, $c, @args) = @_;
157 1 $self->forwarded_args(\@args);
158 1 return $self->respond();
159 }
160  
161 3 ▶sub respond {
Subroutine respond: called CC=11 SCAR=27.4
Pod documented
162 3 my ($self, $status, $headers, @args) = @_;
163 3 for my $r ($self->ctx->res) {
164 3 ▶ $r->status($status) if $status && $r->status; # != 200; # Catalyst sets 200
Branch: 1 branch
BranchTrueFalseTotal
if $status and $r->status 2 1 3
Condition66%2/3
$status and $r->status
!ll&&!rl&&r
102
MC/DC50%1/2
$status and $r->status
$status $r->status
Truth table66%2/3
A and B
A= $statusB= $r->status
ABresult
0X0
100
111
165  
166 Module::Runtime::use_module('Catalyst::View::BasePerRequest::Exception::InvalidStatusCode')->throw(status_code=>$r->status)
167 3 ▶ if $self->has_status_codes && !$self->status_codes->{$r->status};
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_status_codes and not $self->status_codes->{$r->status} 0 3 3
Condition33%1/3
$self->has_status_codes and not $self->status_codes->{$r->status}
!ll&&!rl&&r
030
MC/DC0%0/2
$self->has_status_codes and not $self->status_codes->{$r->status}
$self->has_status_codes not $self->status_codes->{$r->status}
Truth table33%1/3
A and not(B)
A= $self->has_status_codesB= not $self->status_codes->{$r->status}
ABresult
0X0
100
111
168
169 3 ▶ $r->content_type($self->content_type) if !$r->content_type && $self->has_content_type;
Branch: 1 branch
BranchTrueFalseTotal
if not $r->content_type and $self->has_content_type 3 0 3
Condition33%1/3
not $r->content_type and $self->has_content_type
!ll&&!rl&&r
003
MC/DC0%0/2
not $r->content_type and $self->has_content_type
not $r->content_type $self->has_content_type
Truth table33%1/3
not(A) and B
A= not $r->content_typeB= $self->has_content_type
ABresult
0X0
100
111
170 3 ▶ $r->headers->push_header(@{$headers}) if $headers;
Branch: 1 branch
BranchTrueFalseTotal
if $headers 2 1 3
171  
172 # In some cases this can be an object, like a 'safe for HTML' string object.
173 # TODO: I suppose this breaks using a filehandle, but that's pretty uncommon
174 # and has little advantage with the current Catalyst.
175  
176 3 my $response_body = $self->get_rendered(@args);
177 3 ▶ if( Scalar::Util::blessed($response_body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($response_body)) 0 3 3
178 0 ▶ $response_body = $self->can('stringify_response_body')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('stringify_response_body') ? : 0 0 0
179 ? $self->stringify_response_body($response_body)
180 : "${response_body}"; # assume they are using overloading.
181 }
182  
183 # Set the response.
184 3 $r->body($response_body);
185 }
186
187 3 return $self; # allow chaining
188 }
189  
190 24 ▶sub get_rendered {
Subroutine get_rendered: called CC=4 SCAR=14.1
Pod undocumented
191 24 my $self = shift;
192 24 my @rendered = ();
193  
194 eval {
195 24 my @args = $self->prepare_render_args(@_);
196 @rendered = map {
197 24 ▶ Scalar::Util::blessed($_) && $_->can('get_rendered') ? $_->get_rendered : $_;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->can('get_rendered') ? : 6 18 24
Condition66%2/3
builtin::blessed($_) and $_->can("get_rendered")
!ll&&!rl&&r
1806
MC/DC50%1/2
builtin::blessed($_) and $_->can("get_rendered")
builtin::blessed($_) $_->can("get_rendered")
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->can("get_rendered")
ABresult
0X0
100
111
198 } $self->render(@args);
199 24 1;
200 24 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my(@args) = $self->prepare_render_args(@_); @rendered = map({builtin::blessed($_) && $_->can("get_rendered") ? $_->get_rendered : $_;} $self->render(@args)); 1 } } or do { $self->do_handle_render_exception($@) } 24 0 24
201 0 $self->do_handle_render_exception($@);
202 };
203  
204 24 return $self->flatten_rendered_for_response_body(@rendered);
205 }
206  
207 24 ▶sub flatten_rendered_for_response_body { return shift->flatten_rendered(@_) }
Subroutine flatten_rendered_for_response_body: called CC=1 SCAR=0.0
Pod undocumented
208  
209 0 ▶sub render_error_class { 'Catalyst::View::BasePerRequest::Exception::RenderError' }
Subroutine render_error_class: not called CC=1 SCAR=6.9
Pod undocumented
210  
211 0 ▶sub do_handle_render_exception {
Subroutine do_handle_render_exception: not called CC=3 SCAR=24.8
Pod undocumented
212 0 my ($self, $err) = @_;
213 0 ▶ return $err->rethrow if Scalar::Util::blessed($err) && $err->can('rethrow');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->can("rethrow") 0 0 0
Condition0%0/3
builtin::blessed($err) and $err->can("rethrow")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($err) and $err->can("rethrow")
builtin::blessed($err) $err->can("rethrow")
Truth table0%0/3
A and B
A= builtin::blessed($err)B= $err->can("rethrow")
ABresult
0X0
100
111
214 0 my $class = Module::Runtime::use_module($self->render_error_class);
215 0 $class->throw(render_error=>$err);
216 }
217  
218 24 ▶sub prepare_render_args {
Subroutine prepare_render_args: called CC=2 SCAR=6.9
Pod undocumented
219 24 my ($self, @args) = @_;
220  
221 24 ▶ if($self->has_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_code) 6 18 24
222 6 my $inner = $self->render_code;
223 6 unshift @args, $inner; # pass any $inner as an argument to ->render()
224 }
225  
226 24 return ($self->ctx, @args);
227 }
228  
229 6 ▶sub render_code {
Subroutine render_code: called CC=3 SCAR=11.0
Pod undocumented
230 6 my $self = shift;
231 my @inner = map {
232 6 ▶ Scalar::Util::blessed($_) && $_->can('get_rendered') ? $_->get_rendered : $_;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->can('get_rendered') ? : 12 6 18
Condition66%2/3
builtin::blessed($_) and $_->can("get_rendered")
!ll&&!rl&&r
6012
MC/DC50%1/2
builtin::blessed($_) and $_->can("get_rendered")
builtin::blessed($_) $_->can("get_rendered")
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->can("get_rendered")
ABresult
0X0
100
111
233 } $self->execute_code_callback($self->prepare_render_code_args);
234  
235 6 my $flat = $self->flatten_rendered_for_inner_content(@inner);
236 6 return $flat;
237 }
238  
239 6 ▶sub execute_code_callback {
Subroutine execute_code_callback: called CC=1 SCAR=0.0
Pod undocumented
240 6 my ($self, @args) = @_;
241 6 return $self->code->(@args);
242 }
243  
244 6 ▶sub prepare_render_code_args {
Subroutine prepare_render_code_args: called CC=1 SCAR=0.0
Pod undocumented
245 6 my ($self) = @_;
246 6 return $self;
247 }
248  
249 6 ▶sub flatten_rendered_for_inner_content { return shift->flatten_rendered(@_) }
Subroutine flatten_rendered_for_inner_content: called CC=1 SCAR=0.0
Pod undocumented
250  
251 48 ▶sub flatten_rendered {
Subroutine flatten_rendered: called CC=1 SCAR=0.0
Pod undocumented
252 48 my $self = shift;
253 48 return join '', grep { defined($_) } @_;
254 }
255  
256 0 ▶sub render_content_value {
Subroutine render_content_value: not called CC=3 SCAR=24.8
Pod undocumented
257 0 my $self = shift;
258 0 ▶ if((ref($_[0])||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $_[0] || '') eq 'CODE') { } 0 0 0
Condition0%0/2
ref $_[0] || ''
l!l
00
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
259 0 return $self->flatten_rendered_for_content_blocks($_[0]->($_[1]));
260 } else {
261 0 return $self->flatten_rendered_for_content_blocks(@_);
262 }
263 }
264  
265 18 ▶sub flatten_rendered_for_content_blocks {
Subroutine flatten_rendered_for_content_blocks: called CC=1 SCAR=0.0
Pod undocumented
266 18 my $self = shift;
267 18 return $self->flatten_rendered(@_);
268 }
269  
270 # Get a content block, with optional default value
271 12 ▶sub content {
Subroutine content: called CC=4 SCAR=13.9
Pod documented
272 12 my $self = shift;
273 12 my $name = shift;
274 0 ▶ my $default = ref($_[-1])||'' eq 'CODE' ? pop : sub { '' };
Subroutine __ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] || !1 ? : 6 6 12
Condition100%2/2
ref $_[-1] or
l!l
66
MC/DC100%1/1
ref $_[-1] or
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
275 12 my @args = @_;
276  
277 12 ▶ return $default->(@args) unless exists $self->ctx->stash->{view_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ctx->stash->{"view_blocks"}{$name} 6 6 12
278  
279 6 my $proto = $self->ctx->stash->{view_blocks}{$name};
280  
281 6 return $self->flatten_rendered_for_content_blocks($proto->(@args));
282 }
283  
284 # Define a content block
285 6 ▶sub content_for {
Subroutine content_for: called CC=4 SCAR=15.2
Pod documented
286 6 my ($self, $name, $proto) = @_;
287 0 ▶ my $value = ref($proto)||'' eq 'CODE' ? $proto : sub { $proto };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
ref $proto || !1 ? : 6 0 6
Condition50%1/2
ref $proto or
l!l
60
MC/DC0%0/1
ref $proto or
ref $proto
Truth table50%1/2
A
A= ref $proto
Aresult
11
00
288 $self->throw_content_block_error($name, "Can't create content block '$name', it's already defined.")
289 6 ▶ if exists $self->ctx->stash->{view_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->ctx->stash->{"view_blocks"}{$name} 0 6 6
290 6 $self->ctx->stash->{view_blocks}{$name} = $value;
291 6 return;
292 }
293  
294 # Replace a content block
295 0 ▶sub content_replace {
Subroutine content_replace: not called CC=4 SCAR=30.0
Pod documented
296 0 my ($self, $name, $proto) = @_;
297 0 ▶ my $value = ref($proto)||'' eq 'CODE' ? $proto : sub { $proto };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $proto || !1 ? : 0 0 0
Condition0%0/2
ref $proto or
l!l
00
MC/DC0%0/1
ref $proto or
ref $proto
Truth table0%0/2
A
A= ref $proto
Aresult
11
00
298 $self->throw_content_block_error($name, "Can't replace content block '$name', it's not defined.")
299 0 ▶ unless exists $self->ctx->stash->{view_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ctx->stash->{"view_blocks"}{$name} 0 0 0
300 0 $self->ctx->stash->{view_blocks}{$name} = $value;
301 0 return;
302 }
303  
304 # wrap a content block
305 6 ▶sub content_around {
Subroutine content_around: called CC=4 SCAR=14.3
Pod documented
306 6 my ($self, $name, $wrapper) = @_;
307 6 ▶ $self->throw_content_block_error($name, "Content must be a coderef")
Branch: 1 branch
BranchTrueFalseTotal
unless 0 6 6
Condition50%1/2
ref $wrapper or
l!l
60
MC/DC0%0/1
ref $wrapper or
ref $wrapper
Truth table50%1/2
A
A= ref $wrapper
Aresult
11
00
308 unless ref($wrapper)||'' eq 'CODE';
309 $self->throw_content_block_error($name, "Can't wrap content block '$name', it's not defined.")
310 6 ▶ unless exists $self->ctx->stash->{view_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ctx->stash->{"view_blocks"}{$name} 0 6 6
311 6 my $existing = $self->ctx->stash->{view_blocks}{$name};
312 6 ▶ $self->ctx->stash->{view_blocks}{$name} = sub { $wrapper->($existing, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
313 6 return;
314 }
315  
316 # Prepend to a content block (must have same args as orginal block)
317 6 ▶sub content_prepend {
Subroutine content_prepend: called CC=4 SCAR=14.2
Pod undocumented
318 6 my ($self, $name, $proto) = @_;
319 6 ▶ my $value = ref($proto)||'' eq 'CODE' ? $proto : sub { $proto };
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
ref $proto || !1 ? : 0 6 6
Condition50%1/2
ref $proto or
l!l
06
MC/DC0%0/1
ref $proto or
ref $proto
Truth table50%1/2
A
A= ref $proto
Aresult
11
00
320 $self->throw_content_block_error($name, "Can't prepend to content block '$name', it's not defined.")
321 6 ▶ unless exists $self->ctx->stash->{view_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ctx->stash->{"view_blocks"}{$name} 0 6 6
322 6 my $existing = $self->ctx->stash->{view_blocks}{$name};
323 6 ▶ $self->ctx->stash->{view_blocks}{$name} = sub { return $self->flatten_rendered_for_content_blocks($value->(@_), $existing->(@_)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
324 6 return;
325 }
326  
327 # Append to a content block (must have same args as orginal block)
328 6 ▶sub content_append {
Subroutine content_append: called CC=4 SCAR=14.2
Pod documented
329 6 my ($self, $name, $proto) = @_;
330 6 ▶ my $value = ref($proto)||'' eq 'CODE' ? $proto : sub { $proto };
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
ref $proto || !1 ? : 0 6 6
Condition50%1/2
ref $proto or
l!l
06
MC/DC0%0/1
ref $proto or
ref $proto
Truth table50%1/2
A
A= ref $proto
Aresult
11
00
331 $self->throw_content_block_error($name, "Can't append to content block '$name', it's not defined.")
332 6 ▶ unless exists $self->ctx->stash->{view_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ctx->stash->{"view_blocks"}{$name} 0 6 6
333 6 my $existing = $self->ctx->stash->{view_blocks}{$name};
334 6 ▶ $self->ctx->stash->{view_blocks}{$name} = sub { return $self->flatten_rendered_for_content_blocks($existing->(@_), $value->(@_)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
335 6 return;
336 }
337  
338 0 ▶sub throw_content_block_error {
Subroutine throw_content_block_error: not called CC=1 SCAR=6.9
Pod undocumented
339 0 my ($self, $name, $msg) = @_;
340 0 Module::Runtime::use_module($self->_content_exception_class)
341 ->throw(content_name=>$name, content_msg=>$msg);
342 }
343  
344 0 ▶sub _content_exception_class { return 'Catalyst::View::BasePerRequest::Exception::ContentBlockError' }
Subroutine _content_exception_class: not called CC=1 SCAR=6.9
345  
346 0 ▶sub _content_exists {
Subroutine _content_exists: not called CC=2 SCAR=17.9
347 0 my ($self, $name) = @_;
348 0 ▶ return exists $self->ctx->stash->{view_blocks}{$name} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->ctx->stash->{'view_blocks'}{$name} ? : 0 0 0
349 }
350  
351 0 ▶sub detach { return shift->ctx->detach }
Subroutine detach: not called CC=1 SCAR=6.9
Pod documented
352  
353 1 __PACKAGE__->meta->make_immutable;
354  
355 =head1 NAME
356
357 Catalyst::View::Template::BasePerRequest - Catalyst base view for per request, strongly typed templates
358  
359 =head1 SYNOPSIS
360  
361 package Example::View::Hello;
362  
363 use Moose;
364  
365 extends 'Catalyst::View::BasePerRequest';
366  
367 has name => (is=>'ro', required=>1);
368 has age => (is=>'ro', required=>1);
369  
370 sub render {
371 my ($self, $c) = @_;
372 return "<div>Hello @{[ $self->name] }",
373 "I see you are @{[ $self->age]} years old!</div>";
374 }
375  
376 __PACKAGE__->config(
377 content_type=>'text/html',
378 status_codes=>[200]
379 );
380  
381 __PACKAGE__->meta->make_immutable();
382  
383 One way to use it in a controller:
384  
385 package Example::Controller::Root;
386  
387 use Moose;
388 use MooseX::MethodAttributes;
389  
390 extends 'Catalyst::Controller';
391  
392 sub root :Chained(/) PathPart('') CaptureArgs(0) { }
393  
394 sub hello :Chained(root) Args(0) {
395 my ($self, $c) = @_;
396 return $c->view(Hello =>
397 name => 'John',
398 age => 53
399 )->http_ok;
400 }
401  
402 __PACKAGE__->config(namespace=>'');
403 __PACKAGE__->meta->make_immutable;
404  
405 =head1 DESCRIPTION
406  
407 B<NOTE>: This is early access code. Although it's based on several other internal projects
408 which I have iterated over this concept for a number of years I still reserve the right
409 to make breaking changes as needed.
410  
411 B<NOTE>: You probably won't actually use this directly, it's intended to be a base framework
412 for building / prototyping strongly typed / per request views in L<Catalyst>. This
413 documentation serves as an overview of the concept. In particular please note that
414 this code does not address any issues around HTML / Javascript injection attacks or
415 provides any auto escaping. You'll need to bake those features into whatever you
416 build on top of this. Because of this the following documentation is light and is mostly
417 intended to help anyone who is planning to build something on top of this framework
418 rather than use it directly.
419  
420 B<NOTE>: This distribution's C</example> directory gives you a toy prototype using L<HTML::Tags>
421 as the basis to a view as well as some raw examples using this code directly (again,
422 not recommended for anything other than learning).
423  
424 In a classic L<Catalyst> application with server side templates, the canonical approach
425 is to use a 'view' as a sort of handler for an underlying template system (such as
426 L<Template::Toolkit> or L<Xslate>) and to send data to this template by populating
427 the stash. These views are very lean, and in general don't provide much in the way
428 of view logic processing; they generally are just a thin proxy for the underlying
429 templating system.
430  
431 This approach has the upside of being very simple to understand and in general works
432 ok with a simple websites. There are however downsides as your site becomes more
433 complex. First of all the stash as a means to pass data from the Controller to the
434 template can be fragile. For example just making a simple typo in the stash key
435 can break your templates in ways that might not be easy to figure out. Also your
436 template can't enforce its requirements very easily (and it's not easy for someone
437 working in the controller to know exactly what things need to go into the stash in
438 order for the template to function as desired.) The view itself has no way of
439 providing view / display oriented logic; generally that logic ends up creeping back up
440 into the controller in ways that break the notion of MVC's separation of concerns.
441  
442 Lastly the controller doesn't have a defined API with the view. All it can ask the view
443 is 'go ahead and process yourself using the current context' and all it gets back from
444 the view is a string response. If the controller wishes to introspect this response
445 or modify it in some way prior to it being sent back to the client, you have few options
446 apart from using regular expression matching to try and extract the required information
447 or to modify the response string.
448  
449 Basically the classic approach works acceptable well for a simple website but starts to
450 break down as your site becomes more complicated.
451  
452 An alternative approach, which is explored in this distribution, is to have a defined view for
453 each desired response and for it to define an explicit API that the controller uses to provide the required
454 and optional data to the view. This defined view can further define its own methods
455 used to generate suitable information for display. Such an approach is more initial work
456 as well as learning for the website developers, but in the long term it can provide
457 an easier path to sustainable development and maintainence with hopefully fewer bugs
458 and overall site issues.
459  
460 =head1 EXAMPLE: Basic
461  
462 The most minimal thing your view must provide in a C<render> method. This method gets
463 the view object and the context (it can also receive additional arguments if this view is
464 being called from other views as a wrapper or parent view; more on that later).
465  
466 The C<render> method should return a string or array of strings suitable for the body of
467 the response> B<NOTE> if you return an array of strings we flatten the array into a single
468 string since the C<body> method of L<Catalyst::Response> can't take an array.
469  
470 Here's a minimal example:
471  
472 package Example::View::Hello;
473  
474 use Moose;
475  
476 extends 'Catalyst::View::BasePerRequest';
477  
478 sub render {
479 my ($self, $c) = @_;
480 return "<p>Hello</p>";
481 }
482  
483 __PACKAGE__->config(content_type=>'text/html');
484  
485 And here's an example view with attributes:
486  
487 package Example::View::HelloPerson;
488  
489 use Moose;
490  
491 extends 'Catalyst::View::BasePerRequest';
492  
493 has name => (is=>'ro', required=>1);
494  
495 sub render {
496 my ($self, $c) = @_;
497 return qq[
498 <div>
499 Hello @{[ $self->name ]}
500 </div>];
501 }
502  
503 __PACKAGE__->meta->make_immutable();
504  
505 One way to invoke this view from the controller using the traditional C<forward> method:
506  
507 package Example::Controller::Root;
508  
509 use Moose;
510 use MooseX::MethodAttributes;
511  
512 extends 'Catalyst::Controller';
513  
514 sub root :Chained(/) PathPart('') CaptureArgs(0) { }
515  
516 sub hello :Chained(root) Args(0) {
517 my ($self, $c) = @_;
518 my $view = $c->view(HelloPerson => (name => 'John'));
519 return $c->forward($view);
520 }
521  
522 __PACKAGE__->config(namespace=>'');
523 __PACKAGE__->meta->make_immutable;
524  
525 Alternatively using L</"RESPONSE HELPERS">:
526  
527 package Example::Controller::Root;
528  
529 use Moose;
530 use MooseX::MethodAttributes;
531  
532 extends 'Catalyst::Controller';
533  
534 sub root :Chained(/) PathPart('') CaptureArgs(0) { }
535  
536 sub hello :Chained(root) Args(0) {
537 my ($self, $c) = @_;
538 return $c->view(HelloPerson => (name => 'John'))->http_ok;
539 }
540  
541 __PACKAGE__->config(namespace=>'');
542 __PACKAGE__->meta->make_immutable;
543  
544 =head1 ATTRIBUTES
545  
546 The following Moose attributes are considered part of this classes public API
547  
548 =head2 app
549  
550 The string namespace of your L<Catalyst> application.
551  
552 =head2 ctx
553  
554 The current L<Catalyst> context
555  
556 =head2 root
557  
558 The root view object (that is the top view that was called first, usually from
559 the controller).
560  
561 =head2 parent
562  
563 =head2 has_parent
564  
565 If the view was called from another view, that first view is set as the parent.
566  
567 =head2 injected_views
568  
569 =head2 has_injected_views
570  
571 An arrayref of the method names associated with any injected views.
572  
573 =head1 METHODS
574  
575 The following methods are considered part of this classes public API
576  
577 =head2 process
578  
579 Renders a view and sets up the response object. Generally this is called from a
580 controller via the C<forward> method and not directly:
581  
582 $c->forward($view);
583  
584 =head2 respond
585  
586 Accepts an HTTP status code and an arrayref of key / values used to set HTTP headers for a
587 response. Example:
588  
589 $view->respond(201, [ location=>$url ]);
590  
591 Returns the view object to make it easier to do method chaining
592  
593 =head2 detach
594  
595 Just a shortcut to ->detach via the context
596  
597 =head1 CONTENT BLOCK HELPERS
598  
599 Content block helpers are an optional feature to make it easy to create and populate content
600 areas between different views. Although you can also do this with object attributes you may
601 wish to separate template / text from data. Example:
602  
603 package Example::View::Layout;
604  
605 use Moose;
606  
607 extends 'Catalyst::View::BasePerRequest';
608  
609 has title => (is=>'ro', required=>1, default=>'Missing Title');
610  
611 sub render {
612 my ($self, $c, $inner) = @_;
613 return "
614 <html>
615 <head>
616 <title>@{[ $self->title ]}</title>
617 @{[ $self->content('css') ]}
618 </head>
619 <body>$inner</body>
620 </html>";
621 }
622  
623 __PACKAGE__->config(content_type=>'text/html');
624 __PACKAGE__->meta->make_immutable();
625  
626 package Example::View::Hello;
627  
628 use Moose;
629  
630 extends 'Catalyst::View::BasePerRequest';
631  
632 has name => (is=>'ro', required=>1);
633  
634 sub render {
635 my ($self, $c) = @_;
636 return $c->view(Layout => title=>'Hello', sub {
637 my $layout = shift;
638 $self->content_for('css', "<style>...</style>");
639 return "<div>Hello @{[ $self->name ]}!</div>";
640 });
641 }
642  
643 __PACKAGE__->config(content_type=>'text/html', status_codes=>[200]);
644 __PACKAGE__->meta->make_immutable();
645  
646  
647 =head2 content_for
648  
649 $self->content_for($name, $string);
650 $self->content_for($name, $\&callback);
651  
652 Sets a named content block or throws an exception if the content block already exists.
653  
654 $self->content_for('hello', sub {
655 my ($name) = @_;
656 return "Hello $name!";
657 });
658  
659 =head2 content
660  
661 Examples:
662  
663 $self->content($name, ?@args, ?\&default);
664  
665 Gets a content block string by '$name'. If the block has not been defined returns either
666 a zero length string or whatever you set the default code block to be. Examples:
667  
668 $self->content('hello', 'John', sub { return "$name!" });
669  
670 =head2 content_append
671  
672 Appends to a named content block or throws an exception if the content block doesn't exist.
673  
674 =head2 content_replace
675  
676 Replaces a named content block or throws an exception if the content block doesn't exist.
677  
678 =head2 content_around
679  
680 Wraps an existing content with new content. Throws an exception if the named content block doesn't exist.
681  
682 $self->content_around('footer', sub {
683 my $footer = $_[0]->();
684 return "wrapped $footer end wrap";
685 });
686  
687 =head1 VIEW INJECTION
688  
689 Usually when building a website of more than toy complexity you will find that you will
690 decompose your site into sub views and view wrappers. Although you can call the C<view>
691 method on the context, I think its more in the spirit of the idea of a strong or structured
692 view to have a view declare upfront what views its calling as sub views. That lets you
693 have more central control over view initalization and decouples how you are calling your
694 views from the actual underlying views. It can also tidy up some of the code and lastly
695 makes it easy to immediately know what views are needed for the current one. This can
696 help with later refactoring (I've worked on projects where sub views got detached from
697 actual use but nobody ever cleaned them up.)
698  
699 To inject a view into the current one, you need to declare it in configuration:
700  
701 __PACKAGE__->config(
702 content_type=>'text/html',
703 status_codes=>[200,404,400],
704 views=>+{
705 layout1 => [ Layout => sub { my ($self, $c) = @_; return title=>'Hey!' } ],
706 layout2 => [ Layout => (title=>'Yeah!') ],
707 layout3 => 'Layout',
708 },
709 );
710  
711 Basically this is a hashref under the C<views> key, where each key in the hashref is the name
712 of the method you are injecting into the current view which is responsible for creating the
713 sub view and the value is one of three options:
714  
715 =over
716  
717 =item A scalar value
718  
719 __PACKAGE__->config(
720 content_type=>'text/html',
721 status_codes=>[200,404,400],
722 views=>+{
723 layout => 'Layout',
724 },
725 );
726  
727 This is the simplest option, it just injects a method that will call the named view and pass
728 any arguments from the method but does not add any global arguments.
729  
730 =item An arrayref
731  
732 __PACKAGE__->config(
733 content_type=>'text/html',
734 status_codes=>[200,404,400],
735 views=>+{
736 layout => [ Layout => (title=>'Yeah!') ],
737 },
738 );
739  
740 This option allows you to set some argument defaults to the view called. The first item in the
741 arrayref must be the real name of the view, followed by arguments which are merged with any provided
742 to the method.
743  
744 =item A coderef
745  
746 __PACKAGE__->config(
747 content_type=>'text/html',
748 status_codes=>[200,404,400],
749 views=>+{
750 layout => [ Layout => sub { my ($self, $c) = @_; return title=>'Hey!' } ],
751 },
752 );
753  
754 The most complex option, you should probably reserve for very special needs. Basically this coderef
755 will be called with the current view instance and Catalyst context; it should return arguments which
756 with then be merged and treated as in the arrayref option.
757  
758 =back
759  
760 Now you can call for the sub view via a simple method call on the view, rather than via the context:
761  
762 package Example::View::Hello;
763  
764 use Moose;
765  
766 extends 'Catalyst::View::BasePerRequest';
767  
768 has name => (is=>'ro', required=>1);
769 has age => (is=>'ro', required=>1);
770  
771  
772 sub render {
773 my ($self, $c) = @_;
774  
775 return $self->layout(title=>'Hello!', sub {
776 my $layout = shift;
777 return "Hello @{[$self->name]}; you are @{[$self->age]} years old!";
778 });
779 }
780  
781  
782 __PACKAGE__->config(
783 content_type=>'text/html',
784 status_codes=>[200,404,400],
785 views=>+{
786 layout => 'Layout',
787 },
788 );
789  
790 __PACKAGE__->meta->make_immutable;
791  
792  
793 =head1 RESPONSE HELPERS
794  
795 When you create a view instance the actual response is not send to the client until
796 the L</respond> method is called (either directly, via L</process> or thru the generated
797 response helpers).
798  
799 Response helpers are just methods that call L</respond> with the correct status code
800 and using a more easy to remember name (and possibly a more self documenting one).
801  
802 For example:
803  
804 $c->view('Login')->http_ok;
805  
806 calls the L</respond> method with the expected http status code. You can also pass
807 arguments to the response helper which are send to L</respond> and used to add HTTP
808 headers to the response.
809  
810 $c->view("NewUser")
811 ->http_created(location=>$url);
812  
813 Please note that calling a response helper only sets up the response object, it doesn't
814 stop any future actions in you controller. If you really want to stop action processing
815 you'll need to call L</detach>:
816  
817 return $c->view("Error")
818 ->http_bad_request
819 ->detach;
820  
821 If you don't want to generate the response yet (perhaps you'll leave that to a global 'end'
822 action) you can use the 'set_http_$STATUS' helpers instead which wil just set the response
823 status.
824  
825 return $c->view("Error")
826 ->set_http_bad_request
827 ->detach;
828  
829 Response helpers are just lowercased names you'll find for the codes listed in L<HTTP::Status>.
830 Some of the most common ones I find in my code:
831  
832 http_ok
833 http_created
834 http_bad_request
835 http_unauthorized
836 http_not_found
837 http_internal_server_error
838  
839 By default we create response helpers for all the status codes in L<HTTP::Status>. However
840 if you set the C<status_codes> configuration key (see L</status_codes>) you can limit the
841 generated helpers to specific codes. This can be useful since most views are only meaningful
842 with a limited set of response codes.
843  
844 =head1 APPLICATION CONTEXT
845  
846 Generally views using this will need to be called in request context (that is with
847 a L<Catalyst> context, or C<'$c'>). However if you call the view in application context
848 you will get the underlying factory object. Useful if you need access to any complex
849 constructs built at startup.
850  
851 If none of this makes sense to you, you don't need to worry about it. It's advanced
852 edge case stuff. Added because I found a use case around view inheritance.
853  
854 =head1 RUNTIME HOOKS
855
856 This class defines the following method hooks you may optionally defined in your
857 view subclass in order to control or otherwise influence how the view works.
858
859 =head2 $class->modify_init_args($app, $args)
860
861 Runs when C<COMPONENT> is called during C<setup_components>. This gets a reference
862 to the merged arguments from all configuration. You should return this reference
863 after modification.
864  
865 This is for modifying or adding arguments that are application scoped rather than context
866 scoped.
867  
868 =head2 prepare_build_args
869  
870 This method will be called (if defined) by the factory class during build time. It can be used
871 to inject args and modify args. It gets the context and C<@args> as arguments and should return
872 all the arguments you want to pass to C<new>. Example:
873  
874 sub prepare_build_args {
875 my ($class, $c, @args) = @_;
876 # Mess with @args
877 return @args;
878 }
879  
880 =head2 build
881  
882 Receives the initialization hash and should return a new instance of the the view. By default this
883 just calls C<new> on the class with the hash of args but if you need to call some other method or
884 have some complex initialization work that can't be handled with L</prepare_build_args> you can
885 override.
886  
887 =head1 CONFIGURATION
888
889 This Catalyst Component supports the following configuation.
890
891 =head2 content_type
892  
893 The HTTP content type of the response. For example 'text/html'. Required.
894  
895 =head2 status_codes
896
897 An ArrayRef of HTTP status codes used to provide response helpers. This is optional
898 but it allows you to specify the permitted HTTP response codes that a template can
899 generate. for example a NotFound view probably makes no sense to return anything
900 other than a 404 Not Found code.
901  
902 =head2 lifecycle
903  
904 By default your view lifecycle is 'per request' which means we only build it one during the entire
905 request cycle. This is handled by the lifecycle module L<Catalyst::View::BasePerRequest::Lifecycle::Request>.
906 However sometimes you would like your view to be build newly each you you request it. For example
907 you might have a view called from inside a loop, passing it different arguments each time. In that
908 case you want the 'Factory' lifecycle, which is handled by L<Catalyst::View::BasePerRequest::Lifecycle::Factory>.
909 In order to do that set this configuration value to 'Factory'. For example
910  
911 __PACKAGE__->config(
912 content_type => 'text/html',
913 status_codes => [200,201,400],
914 lifecycle => 'Factory',
915 );
916  
917 You can create your own lifecycle classes, but that's very advanced so for now if you
918 want to do that you should review the source of the existing ones. F<D-d>or example you might
919 create a view with a 'session' lifecycle, which returns the same view as long as the user
920 is logged in.
921  
922 =head1 ALSO SEE
923
924 L<Catalyst>
925  
926 =head1 AUTHORS & COPYRIGHT
927
928 John Napiorkowski L<email:jjnapiork@cpan.org>
929
930 =head1 LICENSE
931
932 Copyright 2023, John Napiorkowski L<email:jjnapiork@cpan.org>
933
934 This library is free software; you can redistribute it and/or modify
935 it under the same terms as Perl itself.
936
937 =cut
↑ Summary BasePerRequest/Lifecycle/Request.pm »