1 package MooseX::Declare::Syntax::NamespaceHandling;
4 use Moose::Util qw( does_role );
5 use MooseX::Declare::Util qw( outer_stack_peek );
8 use aliased 'MooseX::Declare::Context::Namespaced';
9 use aliased 'MooseX::Declare::Context::WithOptions';
10 use aliased 'MooseX::Declare::Context::Parameterized';
11 use aliased 'MooseX::Declare::StackItem';
13 use namespace::clean -except => 'meta';
16 MooseX::Declare::Syntax::KeywordHandling
17 MooseX::Declare::Syntax::InnerSyntaxHandling
24 sub add_namespace_customizations { }
25 sub add_optional_customizations { }
26 sub handle_post_parsing { }
27 sub make_anon_metaclass { }
29 around context_traits => sub { super, WithOptions, Namespaced };
31 sub parse_specification {
32 my ($self, $ctx) = @_;
34 $self->parse_namespace_specification($ctx);
35 $self->parse_option_specification($ctx);
40 sub parse_namespace_specification {
41 my ($self, $ctx) = @_;
42 return scalar $ctx->strip_namespace;
45 sub parse_option_specification {
46 my ($self, $ctx) = @_;
47 return scalar $ctx->strip_options;
50 sub generate_inline_stack {
51 my ($self, $ctx) = @_;
56 $self->generate_current_stack_item($ctx);
59 sub generate_current_stack_item {
60 my ($self, $ctx) = @_;
62 return StackItem->new(
63 identifier => $self->identifier,
64 is_dirty => $ctx->options->{is}{dirty},
65 is_parameterized => does_role($ctx, Parameterized) && $ctx->has_parameter_signature,
66 handler => ref($self),
67 namespace => $ctx->namespace,
72 my ($self, $ctx) = @_;
75 $ctx->skip_declarator;
77 # read the name and unwrap the options
78 $self->parse_specification($ctx);
80 my $name = $ctx->namespace;
84 # we have a name in the declaration, which will be used as package name
88 # there is an outer namespace stack item, meaning we namespace below
89 # it, if the name starts with ::
90 if (my $outer = outer_stack_peek $ctx->caller_file) {
91 $package = $outer . $package
96 # no name, no options, no block. Probably { class => 'foo' }
97 elsif (not(keys %{ $ctx->options }) and $ctx->peek_next_char ne '{') {
101 # we have options and/or a block, but not name
103 $anon = $self->make_anon_metaclass
104 or croak sprintf 'Unable to create an anonymized %s namespace', $self->identifier;
105 $package = $anon->name;
108 # namespace and mx:d initialisations
109 $ctx->add_preamble_code_parts(
110 "package ${package}",
112 "use %s %s => '%s', file => __FILE__, stack => [ %s ]",
114 outer_package => $package,
115 $self->generate_inline_stack($ctx),
119 # allow consumer to provide specialisations
120 $self->add_namespace_customizations($ctx, $package);
122 # make options a separate step
123 $self->add_optional_customizations($ctx, $package);
125 # finish off preamble with a namespace cleanup
126 $ctx->add_preamble_code_parts(
127 $ctx->options->{is}->{dirty}
128 ? 'use namespace::clean -except => [qw( meta )]'
129 : 'use namespace::autoclean'
132 # clean up our stack afterwards, if there was a name
133 $ctx->add_cleanup_code_parts(
135 'MooseX::Declare::Util::outer_stack_pop __FILE__',
139 # actual code injection
140 $ctx->inject_code_parts(
141 missing_block_handler => sub { $self->handle_missing_block(@_) },
144 # a last chance to change things
145 $self->handle_post_parsing($ctx, $package, defined($name) ? $name : $anon);
154 MooseX::Declare::Syntax::NamespaceHandling - Handle namespaced blocks
158 Allows the implementation of namespaced blocks like the
159 L<role|MooseX::Declare::Syntax::Keyword::Role> and
160 L<class|MooseX::Declare::Syntax::Keyword::Class> keyword handlers.
162 Namespaces are automatically nested. Meaning that, for example, a C<class Bar>
163 declaration inside another C<class Foo> block gives the inner one actually the
170 =item * L<MooseX::Declare::Syntax::KeywordHandling>
172 =item * L<MooseX::Declare::Syntax::InnerSyntaxHandling>
176 =head1 REQUIRED METHODS
178 =head2 handle_missing_block
180 Object->handle_missing_block (Object $context, Str $body, %args)
182 This must be implemented to decide what to do in case the statement is
183 terminated rather than followed by a block. It will receive the context
184 object, the produced code that needs to be injected, and all the arguments
185 that were passed to the call to L<MooseX::Declare::Context/inject_code_parts>.
187 The return value will be ignored.
189 =head1 EXTENDABLE STUB METHODS
191 =head2 add_namespace_customizations
193 =head2 add_optional_customizations
195 Object->add_namespace_customizations (Object $context, Str $package, HashRef $options)
196 Object->add_optional_customizations (Object $context, Str $package, HashRef $options)
198 These will be called (in this order) by the L</parse> method. They allow specific hooks
199 to attach before/after/around the customizations for the namespace and the provided
200 options that are not attached to the namespace directly.
202 While this distinction might seem superficial, we advise library developers faciliating
203 this role to follow the precendent. This ensures that when another component needs to
204 tie between the namspace and any additional customizations everythign will run in the
205 correct order. An example of this separation would be
207 class Foo is mutable ...
209 being an option of the namespace generation, while
211 class Foo with Bar ...
213 is an additional optional customization.
215 =head2 handle_post_parsing
217 Object->handle_post_parsing (Object $context, Str $package, Str | Object $name)
219 Allows for additional modifications to te namespace after everything else has been
220 done. It will receive the context, the fully qualified package name, and either a
221 string with the name that was specified (might not be fully qualified, since
222 namespaces can be nested) or the anonymous metaclass instance if no name was
225 The return value of this method will be the value returned to the user of the
226 keyword. If you always return the C<$package> argument like this:
228 sub handle_post_parsing {
229 my ($self, $context, $package, $name) = @_;
233 and set this up in a C<foo> keyword handler, you can use it like this:
237 my $fhtagn = foo Fhtagn { }
240 say $fhtagn; # Cthulhu::Fhtagn
241 say $anon; # some autogenerated package name
244 =head2 make_anon_metaclass
246 Class::MOP::Class Object->make_anon_metaclass ()
248 This method should be overridden if you want to provide anonymous namespaces.
250 It does not receive any arguments for customization of the metaclass, because
251 the configuration and customization will be done by L<MooseX::Declare> in the
252 package of the generated class in the same way as in those that have specified
253 names. This way ensures that anonymous and named namespaces are always handled
256 If you do not extend this method (it will return nothing by default), an error
257 will be thrown when a user attempts to declare an anonymous namespace.
263 Any Object->parse (Object $context)
265 This is the main handling routine for namespaces. It will remove the namespace
266 name and its options. If the handler was invoked without a name, options or
267 a following block, it is assumed that this is an instance of an autoquoted
268 bareword like C<class => "Foo">.
270 The return value of the C<parse> method is also the value that is returned
271 to the user of the keyword.
277 =item * L<MooseX::Declare>
279 =item * L<MooseX::Declare::Syntax::MooseSetup>
283 =head1 AUTHOR, COPYRIGHT & LICENSE
285 See L<MooseX::Declare>