blib/lib/Catalyst/Plugin/C3.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 101 CC 6 SCAR 30.2
CC
6
Cov
0%
__hacked_next_autoload
42.0
_is_cat_based
2.0
handle_request
2.0
CRAP 20.5
1 package Catalyst::Plugin::C3;
2  
3 use strict;
4 use warnings;
5 use NEXT;
6 use Class::C3;
7  
8 our $VERSION = '0.03';
9  
10 =head1 NAME
11  
12 Catalyst::Plugin::C3 - Catalyst Plugin to subvert NEXT to use Class::C3
13  
14 =head1 SYNOPSIS
15  
16 Use it in an application:
17  
18 package MyCatApp;
19 use Catalyst qw/ -Debug C3 Static::Simple /;
20  
21 Use it in another plugin:
22  
23 package Catalyst::Plugin::FooBar;
24 use base qw/ Catalyst::Plugin::C3 /;
25  
26 =head1 DESCRIPTION
27  
28 *** WARNING *** THIS MODULE IS STILL EXPERIMENTAL !!!
29  
30 This module is related to the possible transition of Catalyst from L<NEXT>
31 to L<Class::C3>. This transition hasn't happened yet, and might not for
32 a while.
33  
34 This module is only intended for use by Catalyst module developers at
35 this time. You would know it if you should be using this module.
36  
37 *** END WARNING ***
38  
39 This module makes the use of the following idiom:
40  
41 use NEXT;
42  
43 sub foo {
44 my $self = shift->NEXT::foo(@_);
45 }
46  
47 actually accomplish something more like:
48  
49 use Class::C3;
50  
51 sub foo {
52 my $self = shift->next::method(@_);
53 }
54  
55 It does this by temporarily replacing L<NEXT/AUTOLOAD> during the
56 entire L<Catalyst> request-handling phase. The behavior of the replacement
57 function is dependant upon the class (or the class of the object) that
58 L<NEXT> was being called on.
59  
60 If the class is not Catalyst-related, the function falls back to normal
61 L<NEXT> behavior. If the class is Catalyst-related, but its inheritance
62 hierarchy is not C3-compatible, the function warns about the situation
63 (once per class per interpreter) and again falls back to normal L<NEXT>
64 behavior.
65  
66 If the class is both Catalyst-related and has a C3-compatible hierarchy,
67 then L<NEXT> calls are silently upgraded to their L<Class::C3> equivalents.
68  
69 The difference between C<$self-E<gt>NEXT::foo()> and
70 C<$self-E<gt>NEXT::ACTUAL::foo()> is preserved (the former becomes
71 C<maybe::next::method> and the latter becomes C<next::method>).
72  
73 If you are going to place this module in your plugins list for
74 a Catalyst app, it is best placed at the top of the list, above
75 all other plugins.
76  
77 Some other plugins may need require this plugin as a base-class in order
78 to transition themselves to L<Class::C3> without worrying about being
79 broken by other plugins which haven't made the transition. Most plugins
80 will *not* need to do so. Any that do would have wierd hacks involving
81 C<*{NEXT::NEXT}> in them prior to their C3 conversion, so you'd know it.
82  
83 In other words, a plugin should only base on this plugin if it
84 needed L<NEXT> hacks to work right under L<NEXT>, and you're
85 transitioning it to use L<Class::C3>.
86  
87 =head1 METHODS
88  
89 =cut
90  
91 # boolean result: does this class inherit from a Cat base class?
92 sub _is_cat_based {
93 my $class = shift;
94  
95 # This could be simplified if there were a singular real Catalyst
96 # base class that parented all of these:
97  
98 return $class->isa('Catalyst::Component')
99 || $class->isa('Catalyst::Request')
100 || $class->isa('Catalyst::Response')
101 || $class->isa('Catalyst::Action')
102 || $class->isa('Catalyst::Engine')
103 || $class->isa('Catalyst::Dispatcher')
104 || $class->isa('Catalyst::DispatchType')
105 || $class->isa('Catalyst::Exception')
106 || $class->isa('Catalyst::Log');
107 }
108  
109 # Hack NEXT::AUTOLOAD to use C3's ->next::method iff the object
110 # ->isa Catalyst object of some sort AND the object has a C3-valid
111 # inheritance heirarchy.
112 {
113 my %__c3_mro_ok;
114 my $__saved_next_autoload = \&NEXT::AUTOLOAD;
115  
116 sub __hacked_next_autoload {
117 # $class is the class of the object ->NEXT::methodname was used on
118 my $class = ref $_[0] || $_[0];
119  
120 my $wanted = $Catalyst::Plugin::C3::AUTOLOAD || 'NEXT::AUTOLOAD';
121 my ($wanted_class, $wanted_method) = $wanted =~ m{(.*)::(.*)}g;
122  
123 # Check each class only once
124 if(!exists $__c3_mro_ok{$class}) {
125 if(_is_cat_based($class)) {
126 eval { Class::C3::calculateMRO($class) };
127 my $err = $@;
128 if($err) {
129 warn "Class::C3::calculateMRO('$class') Error: '$err'; Falling"
130 . ' back to plain NEXT.pm behavior for this class';
131 $__c3_mro_ok{$class} = 0;
132 }
133 else {
134 $__c3_mro_ok{$class} = 1;
135 }
136 }
137 else {
138 # don't muck around with non-Cat stuff that uses NEXT,
139 # but don't warn about it either
140 $__c3_mro_ok{$class} = 0;
141 }
142 }
143  
144 goto &$__saved_next_autoload if !$__c3_mro_ok{$class};
145 goto &next::method if $wanted_class =~ /^NEXT:.*:ACTUAL/;
146 goto &maybe::next::method;
147 }
148 }
149  
150 package Catalyst::Plugin::C3;
151  
152 =head2 handle_request
153  
154 This plugin hooks into the C<handle_request> method, sets up a C<local>
155 override of the standard version of L<NEXT/AUTOLOAD> for the entire
156 request.
157  
158 =cut
159  
160 sub handle_request {
161 no warnings 'redefine';
162 local *NEXT::AUTOLOAD = \&__hacked_next_autoload;
163 shift->next::method(@_);
164 }
165  
166 =head1 CAVEAT
167  
168 Because calculating the MRO of every class every time ->NEXT::foo is used
169 from within it is too expensive, runtime manipulations of @ISA are prohibited.
170  
171 =head1 AUTHOR
172  
173 Brandon Black C<blblack@gmail.com>
174  
175 =head1 COPYRIGHT
176  
177 This program is free software, you can redistribute it and/or modify it
178 under the same terms as Perl itself.
179  
180 =cut
181  
182 1;