5121fc83b8096a7619c20c014581624838253899
[catagits/Catalyst-Runtime.git] / t / lib / TestApp / Context.pm
1 package TestApp::Context;
2 use Moose;
3 extends 'Catalyst::Context'; 
4 with 'Catalyst::TraitFor::Context::TestHeaders';
5 with 'Catalyst::TraitFor::Context::TestErrors';
6
7 if (eval { Class::MOP::load_class('CatalystX::LeakChecker'); 1 }) {
8     with 'CatalystX::LeakChecker';
9
10     has leaks => (
11         is      => 'ro',
12         default => sub { [] },
13     );
14 }
15
16 sub found_leaks {
17     my ($ctx, @leaks) = @_;
18     push @{ $ctx->leaks }, @leaks;
19 }
20
21 sub count_leaks {
22     my ($ctx) = @_;
23     return scalar @{ $ctx->leaks };
24 }
25
26 sub execute {
27     my $c      = shift;
28     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
29     my $action = $_[1]->reverse;
30
31     my $method;
32
33     if ( $action =~ /->(\w+)$/ ) {
34         $method = $1;
35     }
36     elsif ( $action =~ /\/(\w+)$/ ) {
37         $method = $1;
38     }
39     elsif ( $action =~ /^(\w+)$/ ) {
40         $method = $action;
41     }
42
43     if ( $class && $method && $method !~ /^_/ ) {
44         my $executed = sprintf( "%s->%s", $class, $method );
45         my @executed = $c->response->headers->header('X-Catalyst-Executed');
46         push @executed, $executed;
47         $c->response->headers->header(
48             'X-Catalyst-Executed' => join ', ',
49             @executed
50         );
51     }
52     no warnings 'recursion';
53     return $c->SUPER::execute(@_);
54 }
55
56 1;
57