blib/lib/CatalystX/Controller/PSGI.pm

filter: Statementstmt 100% 32 / 32 Branchbran 100% 4 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 95.8% 46 / 48 CC 5 SCAR 11.0
CC
5
Cov
97%
__ANON__
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
↑ Summary
1 package CatalystX::Controller::PSGI;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶BEGIN { extends 'Catalyst::Controller' }
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.001002';
8 1 $VERSION = eval $VERSION;
9  
10 1 my @_psgi_actions;
11 2 ▶after 'register_actions' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
12 2 my ( $self, $c ) = @_;
13  
14 2 my $class = $self->catalyst_component_name;
15 2 my $namespace = $self->action_namespace( $c );
16  
17 2 ▶ if ( my $app = $self->can('call') ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $app = $self->can("call")) 1 1 2
18 1 push @_psgi_actions, {
19 name => 'call',
20 path => '',
21 class => ref $self,
22 app => $app,
23 };
24 }
25  
26 2 foreach my $psgi_action ( @_psgi_actions ){
27 7 ▶ next if ( $psgi_action->{class} ne $class ) || $psgi_action->{registered};
Branch: 1 branch
BranchTrueFalseTotal
if $psgi_action->{"class"} ne $class or $psgi_action->{"registered"} 3 4 7
Condition66%2/3
$psgi_action->{"class"} ne $class or $psgi_action->{"registered"}
l!l&&r!l&&!r
304
MC/DC50%1/2
$psgi_action->{"class"} ne $class or $psgi_action->{"registered"}
$psgi_action->{"class"} ne $class $psgi_action->{"registered"}
Truth table66%2/3
A or B
A= $psgi_action->{"class"} ne $classB= $psgi_action->{"registered"}
ABresult
1X1
011
000
28  
29 4 my $reverse = "${namespace}/" . $psgi_action->{path};
30  
31 my $action = $self->create_action(
32 name => $psgi_action->{name},
33 reverse => $reverse,
34 namespace => $namespace,
35 class => $class,
36 attributes => {Path => [$reverse]},
37 4 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 4 my ( $self, $c ) = @_;
39  
40 4 my $env = $c->req->env;
41  
42 4 $env->{PATH_INFO} =~ s|^/$namespace||g;
43 4 $env->{SCRIPT_NAME} = "/$namespace";
44  
45 $c->res->from_psgi_response(
46 4 $psgi_action->{app}->( $self, $env )
47 );
48 },
49 4 );
50  
51 4 $c->dispatcher->register( $c, $action );
52 4 $psgi_action->{registered} = 1;
53 }
54 1 };
55  
56 3 ▶sub mount {
Subroutine mount: called CC=1 SCAR=0.0
Pod documented
57 3 my ( $class, $path, $app ) = @_;
58  
59 3 $path =~ s|^/||g;
60 3 my $name = $path;
61  
62 3 push @_psgi_actions, {
63 name => $name,
64 class => $class,
65 path => $path,
66 app => $app,
67 };
68 }
69  
70 =head1 NAME
71  
72 CatalystX::Controller::PSGI - use a PSGI app in a Catalyst Controller
73  
74 =head1 SYNOPSIS
75  
76 package TestApp::Controller::File;
77 use Moose;
78 use namespace::autoclean;
79  
80 BEGIN { extends 'CatalystX::Controller::PSGI'; }
81  
82 use Plack::App::File;
83 use Plack::Response;
84  
85 has 'app_file' => (
86 is => 'ro',
87 default => sub {
88 return Plack::App::File->new(
89 file => __FILE__,
90 content_type => 'text/plain',
91 )->to_app;
92 },
93 );
94  
95 sub call {
96 my ( $self, $env ) = @_;
97  
98 $self->app_file->( $env );
99 }
100  
101 my $hello_app = sub {
102 my ( $self, $env ) = @_;
103  
104 my $res = Plack::Response->new(200);
105 $res->content_type('text/plain');
106 $res->body("hello world");
107  
108 return $res->finalize;
109 };
110  
111 __PACKAGE__->mount( '/hello/world' => $hello_app );
112  
113 __PACKAGE__->meta->make_immutable;
114  
115 =head1 DESCRIPTION
116  
117 Use PSGI apps inside Catalyst Controllers.
118  
119 Combine this with L<Catalyst::Component::InstancePerContext> if you want to access $c in your psgi app
120  
121 =head1 Usage
122  
123 =head2 call method
124  
125 If this method is provided, it will be called as the root action of that controller.
126  
127 package TestApp::Controller::File;
128 use Moose;
129 use namespace::autoclean;
130  
131 BEGIN { extends 'CatalystX::Controller::PSGI'; }
132  
133 use Plack::App::File;
134  
135 has 'app_file' => (
136 is => 'ro',
137 default => sub {
138 return Plack::App::File->new(
139 file => __FILE__,
140 content_type => 'text/plain',
141 )->to_app;
142 },
143 );
144  
145 sub call {
146 my ( $self, $env ) = @_;
147  
148 $self->app_file->( $env );
149 }
150  
151 __PACKAGE__->meta->make_immutable;
152  
153 E.g. in the above example it will be /file/
154  
155 Works similar to L<Plack::Component>, except that as well as $env being passed in, $self is as well. Where $env is the psgi env, and $self is the Catalyst Controller.
156  
157 =head2 mount
158  
159 Mount a path within the controller to an app.
160  
161 package TestApp::Controller::Hello;
162 use Moose;
163 use namespace::autoclean;
164  
165 BEGIN { extends 'CatalystX::Controller::PSGI'; }
166  
167 use Plack::Response;
168  
169 my $hello_app = sub {
170 my ( $self, $env ) = @_;
171  
172 my $res = Plack::Response->new(200);
173 $res->content_type('text/plain');
174 $res->body("hello world");
175  
176 return $res->finalize;
177 };
178  
179 __PACKAGE__->mount( '/world' => $hello_app );
180  
181 __PACKAGE__->meta->make_immutable;
182  
183 In the above example the url /hello/world will be bound to the $hello_app. As with call, $self and $env will be passed in.
184  
185 =head1 EXAMPLES
186  
187 L<http://www.catalystframework.org/calendar/2013/16>
188 L<http://www.catalystframework.org/calendar/2013/17>
189  
190 There is also an example app in the test suite
191  
192 =head1 AUTHOR
193  
194 Mark Ellis E<lt>markellis@cpan.orgE<gt>
195  
196 =head1 SEE ALSO
197  
198 L<Catalyst::Component::InstancePerContext>
199  
200 =head1 LICENSE
201  
202 Copyright 2014 by Mark Ellis E<lt>markellis@cpan.orgE<gt>
203  
204 This library is free software, you can redistribute it and/or modify
205 it under the same terms as Perl itself.
206  
207 =cut
208  
209 1 1;
↑ Summary