Commit | Line | Data |
c0e30cf5 |
1 | |
2 | package Moose::Meta::Class; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
0addec44 |
7 | use Class::MOP; |
648e79ae |
8 | |
6ba6d68c |
9 | use Carp 'confess'; |
21f1e231 |
10 | use Scalar::Util 'weaken', 'blessed'; |
a15dff8d |
11 | |
a94188ac |
12 | our $VERSION = '0.56'; |
d44714be |
13 | our $AUTHORITY = 'cpan:STEVAN'; |
bc1e29b5 |
14 | |
8ee73eeb |
15 | use Moose::Meta::Method::Overriden; |
3f9e4b0a |
16 | use Moose::Meta::Method::Augmented; |
8ee73eeb |
17 | |
c0e30cf5 |
18 | use base 'Class::MOP::Class'; |
19 | |
598340d5 |
20 | __PACKAGE__->meta->add_attribute('roles' => ( |
ef333f17 |
21 | reader => 'roles', |
22 | default => sub { [] } |
23 | )); |
24 | |
590868a3 |
25 | sub initialize { |
26 | my $class = shift; |
27 | my $pkg = shift; |
685f7e44 |
28 | return Class::MOP::get_metaclass_by_name($pkg) |
29 | || $class->SUPER::initialize($pkg, |
30 | 'attribute_metaclass' => 'Moose::Meta::Attribute', |
31 | 'method_metaclass' => 'Moose::Meta::Method', |
32 | 'instance_metaclass' => 'Moose::Meta::Instance', |
33 | @_ |
34 | ); |
ac2dc464 |
35 | } |
590868a3 |
36 | |
61bdd94f |
37 | sub create { |
38 | my ($self, $package_name, %options) = @_; |
39 | |
40 | (ref $options{roles} eq 'ARRAY') |
41 | || confess "You must pass an ARRAY ref of roles" |
42 | if exists $options{roles}; |
43 | |
44 | my $class = $self->SUPER::create($package_name, %options); |
45 | |
48045612 |
46 | if (exists $options{roles}) { |
61bdd94f |
47 | Moose::Util::apply_all_roles($class, @{$options{roles}}); |
48 | } |
49 | |
50 | return $class; |
51 | } |
52 | |
17594769 |
53 | my %ANON_CLASSES; |
54 | |
55 | sub create_anon_class { |
56 | my ($self, %options) = @_; |
57 | |
58 | my $cache_ok = delete $options{cache}; |
17594769 |
59 | |
60 | # something like Super::Class|Super::Class::2=Role|Role::1 |
61 | my $cache_key = join '=' => ( |
6d5cbd2b |
62 | join('|', sort @{$options{superclasses} || []}), |
63 | join('|', sort @{$options{roles} || []}), |
17594769 |
64 | ); |
65 | |
6d5cbd2b |
66 | if ($cache_ok && defined $ANON_CLASSES{$cache_key}) { |
17594769 |
67 | return $ANON_CLASSES{$cache_key}; |
68 | } |
69 | |
70 | my $new_class = $self->SUPER::create_anon_class(%options); |
71 | |
6d5cbd2b |
72 | $ANON_CLASSES{$cache_key} = $new_class |
73 | if $cache_ok; |
17594769 |
74 | |
75 | return $new_class; |
76 | } |
77 | |
ef333f17 |
78 | sub add_role { |
79 | my ($self, $role) = @_; |
80 | (blessed($role) && $role->isa('Moose::Meta::Role')) |
81 | || confess "Roles must be instances of Moose::Meta::Role"; |
82 | push @{$self->roles} => $role; |
83 | } |
84 | |
b8aeb4dc |
85 | sub calculate_all_roles { |
86 | my $self = shift; |
87 | my %seen; |
88 | grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles }; |
89 | } |
90 | |
ef333f17 |
91 | sub does_role { |
92 | my ($self, $role_name) = @_; |
93 | (defined $role_name) |
94 | || confess "You must supply a role name to look for"; |
9c429218 |
95 | foreach my $class ($self->class_precedence_list) { |
81c3738f |
96 | next unless $class->can('meta') && $class->meta->can('roles'); |
9c429218 |
97 | foreach my $role (@{$class->meta->roles}) { |
98 | return 1 if $role->does_role($role_name); |
99 | } |
ef333f17 |
100 | } |
101 | return 0; |
102 | } |
103 | |
d79e62fd |
104 | sub excludes_role { |
105 | my ($self, $role_name) = @_; |
106 | (defined $role_name) |
107 | || confess "You must supply a role name to look for"; |
ac2dc464 |
108 | foreach my $class ($self->class_precedence_list) { |
109 | next unless $class->can('meta'); |
5cb193ed |
110 | # NOTE: |
111 | # in the pretty rare instance when a Moose metaclass |
ac2dc464 |
112 | # is itself extended with a role, this check needs to |
5cb193ed |
113 | # be done since some items in the class_precedence_list |
ac2dc464 |
114 | # might in fact be Class::MOP based still. |
115 | next unless $class->meta->can('roles'); |
9c429218 |
116 | foreach my $role (@{$class->meta->roles}) { |
117 | return 1 if $role->excludes_role($role_name); |
118 | } |
d79e62fd |
119 | } |
120 | return 0; |
121 | } |
122 | |
65e14c86 |
123 | sub new_object { |
d7af0635 |
124 | my $class = shift; |
125 | my $params = @_ == 1 ? $_[0] : {@_}; |
126 | my $self = $class->SUPER::new_object($params); |
65e14c86 |
127 | foreach my $attr ($class->compute_all_applicable_attributes()) { |
128 | # if we have a trigger, then ... |
129 | if ($attr->can('has_trigger') && $attr->has_trigger) { |
130 | # make sure we have an init-arg ... |
131 | if (defined(my $init_arg = $attr->init_arg)) { |
132 | # now make sure an init-arg was passes ... |
d7af0635 |
133 | if (exists $params->{$init_arg}) { |
65e14c86 |
134 | # and if get here, fire the trigger |
135 | $attr->trigger->( |
136 | $self, |
137 | # check if there is a coercion |
138 | ($attr->should_coerce |
139 | # and if so, we need to grab the |
140 | # value that is actually been stored |
141 | ? $attr->get_read_method_ref->($self) |
142 | # otherwise, just get the value from |
143 | # the constructor params |
d7af0635 |
144 | : $params->{$init_arg}), |
65e14c86 |
145 | $attr |
146 | ); |
147 | } |
148 | } |
149 | } |
150 | } |
151 | return $self; |
152 | } |
153 | |
a15dff8d |
154 | sub construct_instance { |
d7af0635 |
155 | my $class = shift; |
156 | my $params = @_ == 1 ? $_[0] : {@_}; |
ddd0ec20 |
157 | my $meta_instance = $class->get_meta_instance; |
575db57d |
158 | # FIXME: |
159 | # the code below is almost certainly incorrect |
160 | # but this is foreign inheritence, so we might |
ac2dc464 |
161 | # have to kludge it in the end. |
d7af0635 |
162 | my $instance = $params->{'__INSTANCE__'} || $meta_instance->create_instance(); |
ac2dc464 |
163 | foreach my $attr ($class->compute_all_applicable_attributes()) { |
d7af0635 |
164 | $attr->initialize_instance_slot($meta_instance, $instance, $params); |
a15dff8d |
165 | } |
166 | return $instance; |
167 | } |
168 | |
093b12c2 |
169 | # FIXME: |
170 | # This is ugly |
ac2dc464 |
171 | sub get_method_map { |
093b12c2 |
172 | my $self = shift; |
53dd42d8 |
173 | |
d5c56b0f |
174 | my $current = Class::MOP::check_package_cache_flag($self->name); |
175 | |
176 | if (defined $self->{'_package_cache_flag'} && $self->{'_package_cache_flag'} == $current) { |
0d1c8e55 |
177 | return $self->{'methods'}; |
53dd42d8 |
178 | } |
179 | |
d5c56b0f |
180 | $self->{_package_cache_flag} = $current; |
181 | |
0d1c8e55 |
182 | my $map = $self->{'methods'}; |
ac2dc464 |
183 | |
093b12c2 |
184 | my $class_name = $self->name; |
185 | my $method_metaclass = $self->method_metaclass; |
ac2dc464 |
186 | |
0addec44 |
187 | my %all_code = $self->get_all_package_symbols('CODE'); |
ac2dc464 |
188 | |
0addec44 |
189 | foreach my $symbol (keys %all_code) { |
190 | my $code = $all_code{$symbol}; |
ac2dc464 |
191 | |
192 | next if exists $map->{$symbol} && |
193 | defined $map->{$symbol} && |
194 | $map->{$symbol}->body == $code; |
195 | |
53dd42d8 |
196 | my ($pkg, $name) = Class::MOP::get_code_info($code); |
ac2dc464 |
197 | |
53dd42d8 |
198 | if ($pkg->can('meta') |
4f8f3aab |
199 | # NOTE: |
200 | # we don't know what ->meta we are calling |
53dd42d8 |
201 | # here, so we need to be careful cause it |
202 | # just might blow up at us, or just complain |
203 | # loudly (in the case of Curses.pm) so we |
4f8f3aab |
204 | # just be a little overly cautious here. |
205 | # - SL |
206 | && eval { no warnings; blessed($pkg->meta) } |
207 | && $pkg->meta->isa('Moose::Meta::Role')) { |
093b12c2 |
208 | #my $role = $pkg->meta->name; |
209 | #next unless $self->does_role($role); |
210 | } |
211 | else { |
2887c827 |
212 | |
213 | # NOTE: |
214 | # in 5.10 constant.pm the constants show up |
215 | # as being in the right package, but in pre-5.10 |
216 | # they show up as constant::__ANON__ so we |
217 | # make an exception here to be sure that things |
218 | # work as expected in both. |
219 | # - SL |
220 | unless ($pkg eq 'constant' && $name eq '__ANON__') { |
221 | next if ($pkg || '') ne $class_name || |
222 | (($name || '') ne '__ANON__' && ($pkg || '') ne $class_name); |
223 | } |
53dd42d8 |
224 | |
093b12c2 |
225 | } |
ac2dc464 |
226 | |
1b2aea39 |
227 | $map->{$symbol} = $method_metaclass->wrap( |
228 | $code, |
229 | package_name => $class_name, |
230 | name => $symbol |
231 | ); |
093b12c2 |
232 | } |
ac2dc464 |
233 | |
093b12c2 |
234 | return $map; |
a7d0cd00 |
235 | } |
236 | |
093b12c2 |
237 | ### --------------------------------------------- |
238 | |
a2eec5e7 |
239 | sub add_attribute { |
240 | my $self = shift; |
e472c9a5 |
241 | $self->SUPER::add_attribute( |
242 | (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute') |
243 | ? $_[0] |
244 | : $self->_process_attribute(@_)) |
245 | ); |
a2eec5e7 |
246 | } |
247 | |
78cd1d3b |
248 | sub add_override_method_modifier { |
249 | my ($self, $name, $method, $_super_package) = @_; |
18c2ec0e |
250 | |
d05cd563 |
251 | (!$self->has_method($name)) |
252 | || confess "Cannot add an override method if a local method is already present"; |
18c2ec0e |
253 | |
254 | $self->add_method($name => Moose::Meta::Method::Overriden->new( |
3f9e4b0a |
255 | method => $method, |
256 | class => $self, |
257 | package => $_super_package, # need this for roles |
258 | name => $name, |
18c2ec0e |
259 | )); |
78cd1d3b |
260 | } |
261 | |
262 | sub add_augment_method_modifier { |
ac2dc464 |
263 | my ($self, $name, $method) = @_; |
d05cd563 |
264 | (!$self->has_method($name)) |
ac2dc464 |
265 | || confess "Cannot add an augment method if a local method is already present"; |
3f9e4b0a |
266 | |
267 | $self->add_method($name => Moose::Meta::Method::Augmented->new( |
268 | method => $method, |
269 | class => $self, |
270 | name => $name, |
271 | )); |
78cd1d3b |
272 | } |
273 | |
1341f10c |
274 | ## Private Utility methods ... |
275 | |
05d9eaf6 |
276 | sub _find_next_method_by_name_which_is_not_overridden { |
277 | my ($self, $name) = @_; |
68efb014 |
278 | foreach my $method ($self->find_all_methods_by_name($name)) { |
ac2dc464 |
279 | return $method->{code} |
05d9eaf6 |
280 | if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden'); |
281 | } |
282 | return undef; |
283 | } |
284 | |
1341f10c |
285 | sub _fix_metaclass_incompatability { |
286 | my ($self, @superclasses) = @_; |
287 | foreach my $super (@superclasses) { |
288 | # don't bother if it does not have a meta. |
fa411d22 |
289 | my $meta = Class::MOP::Class->initialize($super) or next; |
290 | next unless $meta->isa("Class::MOP::Class"); |
291 | |
ac2dc464 |
292 | # get the name, make sure we take |
8ecb1fa0 |
293 | # immutable classes into account |
fa411d22 |
294 | my $super_meta_name = ($meta->is_immutable |
295 | ? $meta->get_mutable_metaclass_name |
296 | : ref($meta)); |
297 | |
ac2dc464 |
298 | # but if we have anything else, |
1341f10c |
299 | # we need to check it out ... |
300 | unless (# see if of our metaclass is incompatible |
fa411d22 |
301 | $self->isa($super_meta_name) |
302 | and |
303 | # and see if our instance metaclass is incompatible |
304 | $self->instance_metaclass->isa($meta->instance_metaclass) |
305 | ) { |
306 | if ( ref($self) eq 'Moose::Meta::Class' ) { # FIXME better check for vanilla case (check for no attrs, no custom meta, etc etc) |
307 | # NOTE: |
308 | # We might want to consider actually |
309 | # transfering any attributes from the |
310 | # original meta into this one, but in |
311 | # general you should not have any there |
312 | # at this point anyway, so it's very |
313 | # much an obscure edge case anyway |
314 | $self = $meta->reinitialize( |
b7fa9b61 |
315 | $self->name, |
fa411d22 |
316 | attribute_metaclass => $meta->attribute_metaclass, |
317 | method_metaclass => $meta->method_metaclass, |
318 | instance_metaclass => $meta->instance_metaclass, |
319 | ); |
320 | } else { |
321 | # this will be called soon enough, for now we let it slide |
322 | # $self->check_metaclass_compatability() |
323 | } |
1341f10c |
324 | } |
325 | } |
ac2dc464 |
326 | return $self; |
1341f10c |
327 | } |
328 | |
d7d8a8c7 |
329 | # NOTE: |
d9bb6c63 |
330 | # this was crap anyway, see |
331 | # Moose::Util::apply_all_roles |
d7d8a8c7 |
332 | # instead |
4498537c |
333 | sub _apply_all_roles { |
547dda77 |
334 | Carp::croak 'DEPRECATED: use Moose::Util::apply_all_roles($meta, @roles) instead' |
4498537c |
335 | } |
1341f10c |
336 | |
337 | sub _process_attribute { |
a3738e5b |
338 | my ( $self, $name, @args ) = @_; |
7e59b803 |
339 | |
340 | @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH'; |
d9bb6c63 |
341 | |
1341f10c |
342 | if ($name =~ /^\+(.*)/) { |
7e59b803 |
343 | return $self->_process_inherited_attribute($1, @args); |
1341f10c |
344 | } |
345 | else { |
7e59b803 |
346 | return $self->_process_new_attribute($name, @args); |
347 | } |
348 | } |
349 | |
350 | sub _process_new_attribute { |
351 | my ( $self, $name, @args ) = @_; |
7e59b803 |
352 | |
d5c30e52 |
353 | $self->attribute_metaclass->interpolate_class_and_new($name, @args); |
1341f10c |
354 | } |
355 | |
356 | sub _process_inherited_attribute { |
357 | my ($self, $attr_name, %options) = @_; |
358 | my $inherited_attr = $self->find_attribute_by_name($attr_name); |
359 | (defined $inherited_attr) |
360 | || confess "Could not find an attribute by the name of '$attr_name' to inherit from"; |
1341f10c |
361 | if ($inherited_attr->isa('Moose::Meta::Attribute')) { |
d7d8a8c7 |
362 | return $inherited_attr->clone_and_inherit_options(%options); |
1341f10c |
363 | } |
364 | else { |
365 | # NOTE: |
366 | # kind of a kludge to handle Class::MOP::Attributes |
d7d8a8c7 |
367 | return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options); |
ac2dc464 |
368 | } |
1341f10c |
369 | } |
370 | |
5cf3dbcf |
371 | ## ------------------------------------------------- |
372 | |
373 | use Moose::Meta::Method::Constructor; |
1f779926 |
374 | use Moose::Meta::Method::Destructor; |
5cf3dbcf |
375 | |
ac2dc464 |
376 | # This could be done by using SUPER and altering ->options |
377 | # I am keeping it this way to make it more explicit. |
378 | sub create_immutable_transformer { |
379 | my $self = shift; |
380 | my $class = Class::MOP::Immutable->new($self, { |
381 | read_only => [qw/superclasses/], |
382 | cannot_call => [qw/ |
383 | add_method |
384 | alias_method |
385 | remove_method |
386 | add_attribute |
387 | remove_attribute |
ac2dc464 |
388 | remove_package_symbol |
389 | add_role |
390 | /], |
391 | memoize => { |
392 | class_precedence_list => 'ARRAY', |
393 | compute_all_applicable_attributes => 'ARRAY', |
394 | get_meta_instance => 'SCALAR', |
395 | get_method_map => 'SCALAR', |
396 | # maybe .... |
397 | calculate_all_roles => 'ARRAY', |
8453c358 |
398 | }, |
399 | # NOTE: |
400 | # this is ugly, but so are typeglobs, |
401 | # so whattayahgonnadoboutit |
402 | # - SL |
403 | wrapped => { |
404 | add_package_symbol => sub { |
405 | my $original = shift; |
406 | confess "Cannot add package symbols to an immutable metaclass" |
407 | unless (caller(2))[3] eq 'Class::MOP::Package::get_package_symbol'; |
408 | goto $original->body; |
409 | }, |
410 | }, |
ac2dc464 |
411 | }); |
412 | return $class; |
413 | } |
414 | |
415 | sub make_immutable { |
416 | my $self = shift; |
417 | $self->SUPER::make_immutable |
418 | ( |
419 | constructor_class => 'Moose::Meta::Method::Constructor', |
420 | destructor_class => 'Moose::Meta::Method::Destructor', |
421 | inline_destructor => 1, |
422 | # NOTE: |
423 | # no need to do this, |
424 | # Moose always does it |
425 | inline_accessors => 0, |
426 | @_, |
427 | ); |
5cf3dbcf |
428 | } |
429 | |
c0e30cf5 |
430 | 1; |
431 | |
432 | __END__ |
433 | |
434 | =pod |
435 | |
436 | =head1 NAME |
437 | |
e522431d |
438 | Moose::Meta::Class - The Moose metaclass |
c0e30cf5 |
439 | |
c0e30cf5 |
440 | =head1 DESCRIPTION |
441 | |
ac2dc464 |
442 | This is a subclass of L<Class::MOP::Class> with Moose specific |
e522431d |
443 | extensions. |
444 | |
ac2dc464 |
445 | For the most part, the only time you will ever encounter an |
446 | instance of this class is if you are doing some serious deep |
447 | introspection. To really understand this class, you need to refer |
6ba6d68c |
448 | to the L<Class::MOP::Class> documentation. |
449 | |
c0e30cf5 |
450 | =head1 METHODS |
451 | |
452 | =over 4 |
453 | |
590868a3 |
454 | =item B<initialize> |
455 | |
61bdd94f |
456 | =item B<create> |
457 | |
17594769 |
458 | Overrides original to accept a list of roles to apply to |
61bdd94f |
459 | the created class. |
460 | |
17594769 |
461 | my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] ); |
462 | |
463 | =item B<create_anon_class> |
464 | |
465 | Overrides original to support roles and caching. |
466 | |
467 | my $metaclass = Moose::Meta::Class->create_anon_class( |
468 | superclasses => ['Foo'], |
469 | roles => [qw/Some Roles Go Here/], |
470 | cache => 1, |
471 | ); |
472 | |
5cf3dbcf |
473 | =item B<make_immutable> |
474 | |
ac2dc464 |
475 | Override original to add default options for inlining destructor |
476 | and altering the Constructor metaclass. |
477 | |
478 | =item B<create_immutable_transformer> |
479 | |
480 | Override original to lock C<add_role> and memoize C<calculate_all_roles> |
481 | |
65e14c86 |
482 | =item B<new_object> |
483 | |
484 | We override this method to support the C<trigger> attribute option. |
485 | |
a15dff8d |
486 | =item B<construct_instance> |
487 | |
ac2dc464 |
488 | This provides some Moose specific extensions to this method, you |
489 | almost never call this method directly unless you really know what |
490 | you are doing. |
6ba6d68c |
491 | |
492 | This method makes sure to handle the moose weak-ref, type-constraint |
ac2dc464 |
493 | and type coercion features. |
ef1d5f4b |
494 | |
093b12c2 |
495 | =item B<get_method_map> |
e9ec68d6 |
496 | |
ac2dc464 |
497 | This accommodates Moose::Meta::Role::Method instances, which are |
498 | aliased, instead of added, but still need to be counted as valid |
e9ec68d6 |
499 | methods. |
500 | |
78cd1d3b |
501 | =item B<add_override_method_modifier ($name, $method)> |
502 | |
ac2dc464 |
503 | This will create an C<override> method modifier for you, and install |
02a0fb52 |
504 | it in the package. |
505 | |
78cd1d3b |
506 | =item B<add_augment_method_modifier ($name, $method)> |
507 | |
ac2dc464 |
508 | This will create an C<augment> method modifier for you, and install |
02a0fb52 |
509 | it in the package. |
510 | |
2b14ac61 |
511 | =item B<calculate_all_roles> |
512 | |
ef333f17 |
513 | =item B<roles> |
514 | |
ac2dc464 |
515 | This will return an array of C<Moose::Meta::Role> instances which are |
02a0fb52 |
516 | attached to this class. |
517 | |
ef333f17 |
518 | =item B<add_role ($role)> |
519 | |
ac2dc464 |
520 | This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it |
02a0fb52 |
521 | to the list of associated roles. |
522 | |
ef333f17 |
523 | =item B<does_role ($role_name)> |
524 | |
ac2dc464 |
525 | This will test if this class C<does> a given C<$role_name>. It will |
526 | not only check it's local roles, but ask them as well in order to |
02a0fb52 |
527 | cascade down the role hierarchy. |
528 | |
d79e62fd |
529 | =item B<excludes_role ($role_name)> |
530 | |
ac2dc464 |
531 | This will test if this class C<excludes> a given C<$role_name>. It will |
532 | not only check it's local roles, but ask them as well in order to |
d79e62fd |
533 | cascade down the role hierarchy. |
534 | |
9e93dd19 |
535 | =item B<add_attribute ($attr_name, %params|$params)> |
4e848edb |
536 | |
9e93dd19 |
537 | This method does the same thing as L<Class::MOP::Class::add_attribute>, but adds |
538 | support for taking the C<$params> as a HASH ref. |
ac1ef2f9 |
539 | |
c0e30cf5 |
540 | =back |
541 | |
542 | =head1 BUGS |
543 | |
ac2dc464 |
544 | All complex software has bugs lurking in it, and this module is no |
c0e30cf5 |
545 | exception. If you find a bug please either email me, or add the bug |
546 | to cpan-RT. |
547 | |
c0e30cf5 |
548 | =head1 AUTHOR |
549 | |
550 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
551 | |
552 | =head1 COPYRIGHT AND LICENSE |
553 | |
778db3ac |
554 | Copyright 2006-2008 by Infinity Interactive, Inc. |
c0e30cf5 |
555 | |
556 | L<http://www.iinteractive.com> |
557 | |
558 | This library is free software; you can redistribute it and/or modify |
ac2dc464 |
559 | it under the same terms as Perl itself. |
c0e30cf5 |
560 | |
8a7a9c53 |
561 | =cut |
1a563243 |
562 | |