blib/lib/Catalyst/Controller/BindLex.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 190 CC 14 SCAR 31.7
CC
14
Cov
0%
_bind_lex
72.0
_handle_bindlex_attrs
30.0
_get_c_obj
6.0
CRAP 23.7
1 package Catalyst::Controller::BindLex;
2 use base qw/Catalyst::Controller/;
3  
4 # these won't help... ;-)
5 use strict;
6 use warnings;
7 no warnings 'uninitialized'; # i hate those
8  
9 # dear god
10 use attributes ();
11 use NEXT ();
12 use PadWalker ();
13 use Array::RefElem ();
14 use Devel::Caller ();
15 use Devel::LexAlias ();
16 use Scalar::Util ();
17 use Carp ();
18  
19 our $VERSION = '0.05';
20  
21 sub bindlex_default_config {
22 map { ucfirst() . 'ed' => $_, ucfirst() => $_} qw/stash session flash/;
23 }
24  
25 sub _bind_lex {
26 my ( $self, $c, $store, $ref, $varname ) = @_;
27  
28 my ( $sigil, $key ) = ( $varname =~ /^([\$\@\%])(.*)/ );
29  
30 if ( $sigil eq '$' ) {
31 next if ref $$ref and $$ref == $c;
32  
33 if ( exists $store->{$key} ) {
34 # when binding '$x' and 'x' already exists in the hash, we
35 # alias the variable to the value in the hash
36 Devel::LexAlias::lexalias( $Carp::CarpLevel, $varname, \$store->{$key} );
37 }
38 else {
39 # when binding '$x' and 'x' doesn't exist in the hash yet, we
40 # alias the hash entry to the variable
41 Array::RefElem::hv_store( %$store, $key, $$ref );
42 }
43 }
44 elsif ( $sigil eq '@' or $sigil eq '%' ) {
45 if ( exists $store->{$key} ) {
46 # we are binding '@x' or '%x' and 'x' is already in the hash
47 if ( my $reftype = ref( my $exists = $store->{$key} ) ) {
48 if ( $sigil eq '@' && $reftype ne 'ARRAY'
49 or $sigil eq '%' and $reftype ne 'HASH' )
50 {
51 # @x needs to bind to an array ref, %x needs a HASH
52 # otherwise we can't expand
53 Carp::croak(
54 "$varname can't bind to a reference of type $reftype");
55 }
56 else {
57 # since it already exists and the variable sigil matches
58 # the reference in the hash, we alias the variable to the
59 # value in the hash
60 Devel::LexAlias::lexalias( $Carp::CarpLevel, $varname,
61 $exists );
62 }
63 }
64 else {
65 # we can't bind a non reference value to a variable that
66 # requires dereferencing
67 Carp::croak("Can't bind $varname to a non-reference value");
68 }
69 }
70 else {
71 # since the key doesn't exist we alias the hash entry to the variable
72 # for aggregate structures this consist of just setting a reference
73 $store->{$key} = $ref;
74 }
75 }
76 }
77  
78 sub _get_c_obj {
79 # used to find $c from some catalyst action called long long ago
80 # needed in the attribute handlers
81 my $level = shift; # how many levels to go up the stack
82  
83 for ( my $i = 0; $i < 10; $i++ ) {
84 my $c = ( eval { Devel::Caller::caller_args($level + $i) } )[1]; # ( $self, $c )[1]
85 return $c if Scalar::Util::blessed($c) and $c->isa("Catalyst"); # FIXME Catalyst::Context ?
86 }
87  
88 die "panic: Can't find \$c object";
89 }
90  
91 sub _find_in_pad {
92 # find the name that corresponds to a reference
93 my ( $level, $var_ref ) = @_;
94  
95 # first we need to sub to look in
96 # for some reason peek_my($level) doesn't work here
97 # perhaps it's with respect to the point at which the attribute handler
98 # was invoked, when the variables don't exist yet.
99 my $sub = Devel::Caller::caller_cv($level);
100 my $pad = PadWalker::peek_sub($sub);
101  
102 my %ref_to_name = reverse %$pad;
103 return $ref_to_name{$var_ref} || die "panic: Can't find $var_ref in the the caller's lexical pad";
104 }
105  
106 BEGIN {
107 # generate generic handler wrappers for all ref types that try to play safe with other plugins
108 for ( qw/ARRAY SCALAR HASH/ ) {
109 eval 'sub MODIFY_' . $_ . '_ATTRIBUTES {
110 my ( $pkg, $ref, @attrs ) = @_;
111 my @remain = $pkg->NEXT::MODIFY_' . $_ . '_ATTRIBUTES( $ref, @attrs );
112 @remain = @attrs unless @remain;
113 _handle_bindlex_attrs( $pkg, $ref, @remain );
114 }';
115 }
116 }
117  
118 # the actual MODIFY_FOO_ATTRIBUTES body
119 sub _handle_bindlex_attrs {
120 my ( $pkg, $ref, @attrs ) = @_;
121  
122 # this is attributes::import + our handler + this + the next
123 local $Carp::CarpLevel = 4;
124  
125 my $c = _get_c_obj($Carp::CarpLevel);
126 my $varname = _find_in_pad( $Carp::CarpLevel, $ref );
127  
128 # the attributes we didn't handle
129 my @remain;
130  
131 # FIXME this should be gone by 5.7 when config was fixed for subclassing
132 my %config = ( $pkg->bindlex_default_config, %{ $pkg->config->{bindlex} || {} });
133  
134 foreach my $attr ( @attrs ) {
135 if ( my $handler = $config{$attr} ) {
136 if ( !ref $handler ) {
137 unless ( $c->can( $handler ) ) {
138 $Carp::CarpLevel--;
139 Carp::croak "there's no $handler method in $c";
140 }
141  
142 $pkg->_bind_lex( $c, $c->$handler, $ref, $varname );
143 } elsif ( ref $handler eq "CODE" ) {
144 $pkg->_bind_lex( $c, $handler->( $c, $ref, $varname ), $ref, $varname );
145 } else {
146 die "unknown handler type $handler";
147 }
148 } else {
149 push @remain, $attr;
150 }
151 }
152  
153 @remain;
154 }
155  
156 sub COMPONENT {
157 my $class = shift;
158 $class->_bindlex_setup_warning(@_);
159 return $class->NEXT::COMPONENT(@_);
160 }
161  
162 sub _bindlex_setup_warning {
163 my ($class, $app) = @_;
164 return if $class->config->{unsafe_bindlex_ok};
165 $app->log->warn("****");
166 $app->log->warn("**** IMPORTANT WARNING: BindLex");
167 $app->log->warn("****");
168 $app->log->warn("Controller class $class using Catalyst::Controller::BindLex; this module is unmaintained and considered -dangerous-");
169 $app->log->warn("Please see the documentation for an explanation and how to disable this warning if you want to take the risk");
170 $app->log->warn("****");
171 }
172  
173 1;
174  
175 __END__