X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FController.pm;h=445d4afee682d8d35bed1c26cadbb6e43d5cc2c3;hp=fb6df0dfd170c3b1e51729dee95a4f186231c2cf;hb=0b0aee670c39f8cb8f140eb62de9bfaf2c343a24;hpb=aea897b2495d079f3849eb30cd3dc7c89b31f742 diff --git a/lib/Catalyst/Controller.pm b/lib/Catalyst/Controller.pm index fb6df0d..445d4af 100644 --- a/lib/Catalyst/Controller.pm +++ b/lib/Catalyst/Controller.pm @@ -1,7 +1,11 @@ package Catalyst::Controller; use Moose; +use Class::MOP; +use Class::Load ':all'; +use String::RewritePrefix; use Moose::Util qw/find_meta/; +use List::Util qw/first/; use List::MoreUtils qw/uniq/; use namespace::clean -except => 'meta'; @@ -33,6 +37,27 @@ has actions => ( init_arg => undef, ); +has _action_role_args => ( + traits => [qw(Array)], + isa => 'ArrayRef[Str]', + init_arg => 'action_roles', + default => sub { [] }, + handles => { + _action_role_args => 'elements', + }, +); + +has _action_roles => ( + traits => [qw(Array)], + isa => 'ArrayRef[RoleName]', + init_arg => undef, + lazy => 1, + builder => '_build__action_roles', + handles => { + _action_roles => 'elements', + }, +); + has action_args => (is => 'ro'); # ->config(actions => { '*' => ... @@ -53,6 +78,14 @@ sub BUILD { # trigger lazy builder $self->_all_actions_attributes; + $self->_action_roles; +} + +sub _build__action_roles { + my $self = shift; + my @roles = $self->_expand_role_shortname($self->_action_role_args); + load_class($_) for @roles; + return \@roles; } sub _build__all_actions_attributes { @@ -85,10 +118,11 @@ for more info about how Catalyst dispatches to actions. #I think both of these could be attributes. doesn't really seem like they need #to ble class data. i think that attributes +default would work just fine -__PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/; +__PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class _action_role_prefix/; __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] ); __PACKAGE__->_action_class('Catalyst::Action'); +__PACKAGE__->_action_role_prefix([ 'Catalyst::ActionRole::' ]); sub _DISPATCH : Private { @@ -261,6 +295,20 @@ sub register_action_methods { } } +sub _apply_action_class_roles { + my ($self, $class, @roles) = @_; + + load_class($_) for @roles; + my $meta = Moose::Meta::Class->initialize($class)->create_anon_class( + superclasses => [$class], + roles => \@roles, + cache => 1, + ); + $meta->add_method(meta => sub { $meta }); + + return $meta->name; +} + sub action_class { my $self = shift; my %args = @_; @@ -278,6 +326,16 @@ sub create_action { my %args = @_; my $class = $self->action_class(%args); + + load_class($class); + Moose->init_meta(for_class => $class) + unless Class::MOP::does_metaclass_exist($class); + + unless ($args{name} =~ /^_(DISPATCH|BEGIN|AUTO|ACTION|END)$/) { + my @roles = $self->gather_action_roles(%args); + $class = $self->_apply_action_class_roles($class, @roles) if @roles; + } + my $action_args = ( ref($self) ? $self->action_args @@ -292,6 +350,15 @@ sub create_action { return $class->new({ %extra_args, %args }); } +sub gather_action_roles { + my ($self, %args) = @_; + + return ( + (blessed $self ? $self->_action_roles : ()), + @{ $args{attributes}->{Does} || [] }, + ); +} + sub _parse_attrs { my ( $self, $c, $name, @attrs ) = @_; @@ -340,16 +407,30 @@ sub _parse_attrs { my %final_attributes; - foreach my $key (keys %raw_attributes) { + while (my ($key, $value) = each %raw_attributes){ + my $new_attrs = $self->_parse_attr($c, $name, $key => $value ); + push @{ $final_attributes{$_} }, @{ $new_attrs->{$_} } for keys %$new_attrs; + } - my $raw = $raw_attributes{$key}; + return \%final_attributes; +} - foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) { +sub _parse_attr { + my ($self, $c, $name, $key, $values) = @_; - my $meth = "_parse_${key}_attr"; - if ( my $code = $self->can($meth) ) { - ( $key, $value ) = $self->$code( $c, $name, $value ); + my %final_attributes; + foreach my $value (ref($values) eq 'ARRAY' ? @$values : $values) { + my $meth = "_parse_${key}_attr"; + if ( my $code = $self->can($meth) ) { + my %new_attrs = $self->$code( $c, $name, $value ); + while (my ($new_key, $value) = each %new_attrs){ + my $new_attrs = $key eq $new_key ? + { $new_key => [$value] } : + $self->_parse_attr($c, $name, $new_key => $value ); + push @{ $final_attributes{$_} }, @{ $new_attrs->{$_} } for keys %$new_attrs; } + } + else { push( @{ $final_attributes{$key} }, $value ); } } @@ -359,14 +440,16 @@ sub _parse_attrs { sub _parse_Global_attr { my ( $self, $c, $name, $value ) = @_; - return $self->_parse_Path_attr( $c, $name, "/$name" ); + # _parse_attr will call _parse_Path_attr for us + return Path => "/$name"; } sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); } sub _parse_Local_attr { my ( $self, $c, $name, $value ) = @_; - return $self->_parse_Path_attr( $c, $name, $name ); + # _parse_attr will call _parse_Path_attr for us + return Path => $name; } sub _parse_Relative_attr { shift->_parse_Local_attr(@_); } @@ -452,11 +535,37 @@ sub _parse_MyAction_attr { my ( $self, $c, $name, $value ) = @_; my $appclass = Catalyst::Utils::class2appclass($self); - $value = "${appclass}::Action::${value}"; + $value = "+${appclass}::Action::${value}"; return ( 'ActionClass', $value ); } +sub _parse_Does_attr { + my ($self, $app, $name, $value) = @_; + return Does => $self->_expand_role_shortname($value); +} + +sub _expand_role_shortname { + my ($self, @shortnames) = @_; + my $app = $self->_application; + + my $prefix = $self->can('_action_role_prefix') ? $self->_action_role_prefix : ['Catalyst::ActionRole::']; + my @prefixes = (qq{${app}::ActionRole::}, @$prefix); + + return String::RewritePrefix->rewrite( + { '' => sub { + my $loaded = load_first_existing_class( + map { "$_$_[0]" } @prefixes + ); + return first { $loaded =~ /^$_/ } + sort { length $b <=> length $a } @prefixes; + }, + '~' => $prefixes[0], + '+' => '' }, + @shortnames, + ); +} + __PACKAGE__->meta->make_immutable; 1; @@ -574,6 +683,10 @@ action class to use. Called with a hash of data to be use for construction of a new Catalyst::Action (or appropriate sub/alternative class) object. +=head2 $self->gather_action_roles(\%action_args) + +Gathers the list of roles to apply to an action with the given %action_args. + =head2 $self->_application =head2 $self->_app