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