From: Moritz Onken Date: Sat, 6 Jun 2009 13:33:53 +0000 (+0000) Subject: refactor of namespace handling X-Git-Tag: 5.80006~36^2~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=17b3d80076b6acb25d56ed83b0ed7134ed4fb343 refactor of namespace handling --- diff --git a/Makefile.PL b/Makefile.PL index a15ed5d..cd3dd33 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -34,6 +34,7 @@ requires 'Tree::Simple::Visitor::FindByPath'; requires 'URI' => '1.35'; requires 'Text::Balanced'; # core in 5.8.x but mentioned for completeness requires 'MRO::Compat'; +requires 'String::RewritePrefix' => '0.004'; # Catalyst::Utils::resolve_namespace recommends 'B::Hooks::OP::Check::StashChange'; diff --git a/TODO b/TODO index 8c5bc68..4862bfe 100644 --- a/TODO +++ b/TODO @@ -1,25 +1,10 @@ -Known Bugs: - - - Bug ->go or ->visit causes actions which have Args or CaptureArgs called - twice when called via ->go or ->visit. - - Test app: http://github.com/bobtfish/catalyst-app-bug-go_chain/tree/master - -Compatibility warnings to add: - - - $self->config should warn as config should only ever be called as a - class method. - -Proposed functionality / feature additions: - - - Log setup needs to be less lame, so Catalyst::Plugin::Log::* can die - in a fire. Having $c->log_class would be a good start. kane volunteered - to do some of this. - - Simple example: Catalyst::Plugin::Log::Colorful should just be a - subclass of Catalyst::Log, no ::Plugin:: needed. - - See also: Catalyst::Plugin::Log::Dispatch and - http://github.com/willert/catalyst-plugin-log4perl-simple/tree +TODO for brach namespace_handling_refactor: +- refactor code in + * Catalyst::Dispatcher::get_containers # No Idea + * Catalyst::Dispatcher::get_containers # No Idea + * Catalyst::Controller::_parse_ActionClass_attr # DONE + * Catalyst::Dispatcher::_load_dispatch_types # DONE + * Catalyst::setup_plugins # DONE + to use the same namespacing method \ No newline at end of file diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index b38be49..426cbb3 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -2502,8 +2502,8 @@ the plugin name does not begin with C. $class->_plugins( {} ) unless $class->_plugins; $plugins ||= []; - - my @plugins = map { s/\A\+// ? $_ : "Catalyst::Plugin::$_" } @$plugins; + + my @plugins = Catalyst::Utils::resolve_namespace('Catalyst::Plugin', @$plugins); for my $plugin ( reverse @plugins ) { Class::MOP::load_class($plugin); diff --git a/lib/Catalyst/Controller.pm b/lib/Catalyst/Controller.pm index 8b391df..d1c6274 100644 --- a/lib/Catalyst/Controller.pm +++ b/lib/Catalyst/Controller.pm @@ -376,9 +376,7 @@ sub _parse_PathPrefix_attr { sub _parse_ActionClass_attr { my ( $self, $c, $name, $value ) = @_; - unless ( $value =~ s/^\+// ) { - $value = join('::', $self->_action_class, $value ); - } + $value = Catalyst::Utils::resolve_namespace($self->_action_class, $value); return ( 'ActionClass', $value ); } diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 214d031..5df4526 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -643,9 +643,8 @@ sub _load_dispatch_types { # Preload action types for my $type (@types) { - my $class = - ( $type =~ /^\+(.*)$/ ) ? $1 : "Catalyst::DispatchType::${type}"; - + my $class = Catalyst::Utils::resolve_namespace('Catalyst::DispatchType', $type); + eval { Class::MOP::load_class($class) }; Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) if $@; @@ -668,9 +667,7 @@ of course it's being used.) sub dispatch_type { my ($self, $name) = @_; - unless ($name =~ s/^\+//) { - $name = "Catalyst::DispatchType::" . $name; - } + $name = Catalyst::Utils::resolve_namespace('Catalyst::DispatchType', $name); for (@{ $self->_dispatch_types }) { return $_ if ref($_) eq $name; diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index 4d5f0ed..76f0733 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -9,6 +9,8 @@ use URI; use Carp qw/croak/; use Cwd; +use String::RewritePrefix; + use namespace::clean; =head1 NAME @@ -377,6 +379,27 @@ sub term_width { return $_term_width = $width; } + +=head2 resolve_namespace + +Method which adds the namespace for plugins and actions. + + __PACKAGE__->setup(qw(MyPlugin)); + + # will load Catalyst::Plugin::MyPlugin + +=cut + + +sub resolve_namespace { + my $namespace = shift; + my @classes = @_; + return String::RewritePrefix->rewrite( + { '' => $namespace.'::', '+' => '' }, @classes, + ); +} + + =head1 AUTHORS Catalyst Contributors, see Catalyst.pm