Commit | Line | Data |
3a63a2e7 |
1 | package Mouse::Meta::Module; |
2 | use strict; |
3 | use warnings; |
4 | |
6d28c5cf |
5 | use Carp (); |
ad022aac |
6 | use Scalar::Util qw/blessed weaken/; |
fce211ae |
7 | |
01afd8ff |
8 | use Mouse::Util qw/:meta get_code_package not_supported load_class/; |
6d28c5cf |
9 | |
8536d351 |
10 | |
fe1221ad |
11 | my %METAS; |
8536d351 |
12 | |
739525d0 |
13 | # because Mouse doesn't introspect existing classes, we're forced to |
14 | # only pay attention to other Mouse classes |
15 | sub _metaclass_cache { |
16 | my($class, $name) = @_; |
fe1221ad |
17 | return $METAS{$name}; |
739525d0 |
18 | } |
8536d351 |
19 | |
739525d0 |
20 | sub initialize { |
21 | my($class, $package_name, @args) = @_; |
8536d351 |
22 | |
739525d0 |
23 | ($package_name && !ref($package_name)) |
24 | || $class->throw_error("You must pass a package name and it cannot be blessed"); |
8536d351 |
25 | |
fe1221ad |
26 | return $METAS{$package_name} |
739525d0 |
27 | ||= $class->_construct_meta(package => $package_name, @args); |
28 | } |
8536d351 |
29 | |
739525d0 |
30 | sub class_of{ |
31 | my($class_or_instance) = @_; |
32 | return undef unless defined $class_or_instance; |
23274b88 |
33 | return $METAS{ ref($class_or_instance) || $class_or_instance }; |
8536d351 |
34 | } |
35 | |
739525d0 |
36 | # Means of accessing all the metaclasses that have |
37 | # been initialized thus far |
fe1221ad |
38 | #sub get_all_metaclasses { %METAS } |
39 | sub get_all_metaclass_instances { values %METAS } |
40 | sub get_all_metaclass_names { keys %METAS } |
41 | sub get_metaclass_by_name { $METAS{$_[0]} } |
42 | #sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] } |
43 | #sub weaken_metaclass { weaken($METAS{$_[0]}) } |
44 | #sub does_metaclass_exist { defined $METAS{$_[0]} } |
45 | #sub remove_metaclass_by_name { delete $METAS{$_[0]} } |
739525d0 |
46 | |
47 | |
48 | |
3a63a2e7 |
49 | sub name { $_[0]->{package} } |
3a63a2e7 |
50 | |
739525d0 |
51 | # The followings are Class::MOP specific methods |
52 | |
53 | #sub version { no strict 'refs'; ${shift->name.'::VERSION'} } |
54 | #sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} } |
55 | #sub identifier { |
56 | # my $self = shift; |
57 | # return join '-' => ( |
58 | # $self->name, |
59 | # ($self->version || ()), |
60 | # ($self->authority || ()), |
61 | # ); |
62 | #} |
23264b5b |
63 | |
8536d351 |
64 | # add_attribute is an abstract method |
65 | |
3a683350 |
66 | sub get_attribute_map { |
67 | Carp::cluck('get_attribute_map() has been deprecated'); |
68 | return $_[0]->{attributes}; |
69 | } |
70 | |
8536d351 |
71 | sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } |
72 | sub get_attribute { $_[0]->{attributes}->{$_[1]} } |
73 | sub get_attribute_list{ keys %{$_[0]->{attributes}} } |
6cfa1e5e |
74 | sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} } |
23264b5b |
75 | |
3a63a2e7 |
76 | sub namespace{ |
77 | my $name = $_[0]->{package}; |
78 | no strict 'refs'; |
79 | return \%{ $name . '::' }; |
80 | } |
81 | |
82 | sub add_method { |
83 | my($self, $name, $code) = @_; |
84 | |
85 | if(!defined $name){ |
1b9e472d |
86 | $self->throw_error('You must pass a defined name'); |
3a63a2e7 |
87 | } |
1b9e472d |
88 | if(!defined $code){ |
89 | $self->throw_error('You must pass a defined code'); |
90 | } |
91 | |
3a63a2e7 |
92 | if(ref($code) ne 'CODE'){ |
fce211ae |
93 | not_supported 'add_method for a method object'; |
3a63a2e7 |
94 | } |
95 | |
8e64d0fa |
96 | $self->{methods}->{$name}++; # Moose stores meta object here. |
3a63a2e7 |
97 | |
98 | my $pkg = $self->name; |
99 | no strict 'refs'; |
100 | no warnings 'redefine'; |
101 | *{ $pkg . '::' . $name } = $code; |
102 | } |
103 | |
01afd8ff |
104 | # XXX: for backward compatibility |
105 | my %foreign = map{ $_ => undef } qw( |
106 | Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints |
107 | Carp Scalar::Util |
108 | ); |
109 | sub _code_is_mine{ |
110 | my($self, $code) = @_; |
8e64d0fa |
111 | |
01afd8ff |
112 | my $package = get_code_package($code); |
8e64d0fa |
113 | |
01afd8ff |
114 | return !exists $foreign{$package}; |
3a63a2e7 |
115 | } |
116 | |
117 | sub has_method { |
118 | my($self, $method_name) = @_; |
119 | |
8e64d0fa |
120 | return 1 if $self->{methods}->{$method_name}; |
5fd0bdcf |
121 | |
01afd8ff |
122 | my $code = do{ no strict 'refs'; *{$self->{package} . '::' . $method_name}{CODE} }; |
3a63a2e7 |
123 | |
124 | return $code && $self->_code_is_mine($code); |
125 | } |
126 | |
8536d351 |
127 | sub get_method{ |
6cfa1e5e |
128 | my($self, $method_name) = @_; |
129 | |
130 | if($self->has_method($method_name)){ |
131 | my $method_metaclass = $self->method_metaclass; |
132 | load_class($method_metaclass); |
133 | |
134 | my $package = $self->name; |
135 | return $method_metaclass->new( |
136 | body => $package->can($method_name), |
137 | name => $method_name, |
138 | package => $package, |
139 | ); |
140 | } |
141 | |
142 | return undef; |
8536d351 |
143 | } |
3a63a2e7 |
144 | |
8e64d0fa |
145 | sub get_method_list { |
3a63a2e7 |
146 | my($self) = @_; |
8e64d0fa |
147 | |
148 | return grep { $self->has_method($_) } keys %{ $self->namespace }; |
3a63a2e7 |
149 | } |
150 | |
7a50b450 |
151 | { |
152 | my $ANON_SERIAL = 0; |
153 | my $ANON_PREFIX = 'Mouse::Meta::Module::__ANON__::'; |
154 | |
155 | my %IMMORTALS; |
156 | |
157 | sub create { |
158 | my ($class, $package_name, %options) = @_; |
159 | |
160 | $class->throw_error('You must pass a package name') if @_ == 1; |
161 | |
162 | |
163 | if(exists $options{superclasses}){ |
164 | if($class->isa('Mouse::Meta::Class')){ |
165 | (ref $options{superclasses} eq 'ARRAY') |
166 | || $class->throw_error("You must pass an ARRAY ref of superclasses"); |
167 | } |
168 | else{ # role |
169 | delete $options{superclasses}; |
170 | } |
171 | } |
172 | |
173 | my $attributes; |
174 | if(exists $options{attributes}){ |
175 | $attributes = delete $options{attributes}; |
176 | (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH') |
177 | || $class->throw_error("You must pass an ARRAY ref of attributes") |
178 | } |
179 | |
180 | (ref $options{methods} eq 'HASH') |
181 | || $class->throw_error("You must pass a HASH ref of methods") |
182 | if exists $options{methods}; |
183 | |
184 | (ref $options{roles} eq 'ARRAY') |
185 | || $class->throw_error("You must pass an ARRAY ref of roles") |
186 | if exists $options{roles}; |
187 | |
188 | |
189 | my @extra_options; |
190 | my $mortal; |
191 | my $cache_key; |
192 | |
193 | if(!defined $package_name){ # anonymous |
194 | $mortal = !$options{cache}; |
195 | |
196 | # anonymous but immortal |
197 | if(!$mortal){ |
8e64d0fa |
198 | # something like Super::Class|Super::Class::2=Role|Role::1 |
199 | $cache_key = join '=' => ( |
200 | join('|', @{$options{superclasses} || []}), |
201 | join('|', sort @{$options{roles} || []}), |
7a50b450 |
202 | ); |
203 | return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key}; |
204 | } |
205 | $package_name = $ANON_PREFIX . ++$ANON_SERIAL; |
206 | |
207 | push @extra_options, (anon_serial_id => $ANON_SERIAL); |
208 | } |
209 | |
210 | # instantiate a module |
211 | { |
212 | no strict 'refs'; |
213 | ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version}; |
214 | ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority}; |
215 | } |
216 | |
217 | my %initialize_options = %options; |
218 | delete @initialize_options{qw( |
219 | package |
220 | superclasses |
221 | attributes |
222 | methods |
223 | roles |
224 | )}; |
225 | my $meta = $class->initialize( $package_name, %initialize_options, @extra_options); |
226 | |
fe1221ad |
227 | weaken $METAS{$package_name} |
7a50b450 |
228 | if $mortal; |
229 | |
230 | # FIXME totally lame |
231 | $meta->add_method('meta' => sub { |
232 | $class->initialize(ref($_[0]) || $_[0]); |
233 | }); |
234 | |
235 | $meta->superclasses(@{$options{superclasses}}) |
236 | if exists $options{superclasses}; |
237 | |
238 | # NOTE: |
239 | # process attributes first, so that they can |
240 | # install accessors, but locally defined methods |
241 | # can then overwrite them. It is maybe a little odd, but |
242 | # I think this should be the order of things. |
243 | if (defined $attributes) { |
244 | if(ref($attributes) eq 'ARRAY'){ |
245 | foreach my $attr (@{$attributes}) { |
246 | $meta->add_attribute($attr->{name} => $attr); |
247 | } |
248 | } |
249 | else{ |
250 | while(my($name, $attr) = each %{$attributes}){ |
251 | $meta->add_attribute($name => $attr); |
252 | } |
253 | } |
254 | } |
255 | if (exists $options{methods}) { |
256 | foreach my $method_name (keys %{$options{methods}}) { |
257 | $meta->add_method($method_name, $options{methods}->{$method_name}); |
258 | } |
259 | } |
260 | if (exists $options{roles}){ |
261 | Mouse::Util::apply_all_roles($package_name, @{$options{roles}}); |
262 | } |
263 | |
264 | if(!$mortal && exists $meta->{anon_serial_id}){ |
265 | $IMMORTALS{$cache_key} = $meta; |
266 | } |
267 | |
268 | return $meta; |
269 | } |
270 | |
271 | sub DESTROY{ |
272 | my($self) = @_; |
273 | |
274 | my $serial_id = $self->{anon_serial_id}; |
275 | |
276 | return if !$serial_id; |
277 | |
278 | my $stash = $self->namespace; |
279 | |
280 | @{$self->{superclasses}} = () if exists $self->{superclasses}; |
281 | %{$stash} = (); |
fe1221ad |
282 | delete $METAS{$self->name}; |
7a50b450 |
283 | |
284 | no strict 'refs'; |
285 | delete ${$ANON_PREFIX}{ $serial_id . '::' }; |
286 | |
287 | return; |
288 | } |
289 | } |
290 | |
8536d351 |
291 | sub throw_error{ |
292 | my($class, $message, %args) = @_; |
293 | |
fce211ae |
294 | local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0); |
8e64d0fa |
295 | local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though |
3a63a2e7 |
296 | |
8536d351 |
297 | if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0 |
298 | Carp::croak($message); |
299 | } |
300 | else{ |
301 | Carp::confess($message); |
302 | } |
303 | } |
3a63a2e7 |
304 | |
305 | 1; |
306 | |
307 | __END__ |
308 | |
309 | =head1 NAME |
310 | |
1820fffe |
311 | Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role |
3a63a2e7 |
312 | |
b995be6a |
313 | =head1 SEE ALSO |
314 | |
31c5194b |
315 | L<Class::MOP::Class> |
316 | |
317 | L<Class::MOP::Module> |
318 | |
319 | L<Class::MOP::Package> |
b995be6a |
320 | |
3a63a2e7 |
321 | =cut |
2cea7a5f |
322 | |