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';
with 'Catalyst::Component::ApplicationAttribute';
-has path_prefix =>
- (
- is => 'rw',
- isa => 'Str',
- init_arg => 'path',
- predicate => 'has_path_prefix',
- );
+has path_prefix => (
+ is => 'rw',
+ isa => 'Str',
+ init_arg => 'path',
+ predicate => 'has_path_prefix',
+);
-has action_namespace =>
- (
- is => 'rw',
- isa => 'Str',
- init_arg => 'namespace',
- predicate => 'has_action_namespace',
- );
+has action_namespace => (
+ is => 'rw',
+ isa => 'Str',
+ init_arg => 'namespace',
+ predicate => 'has_action_namespace',
+);
-has actions =>
- (
- accessor => '_controller_actions',
- isa => 'HashRef',
- init_arg => undef,
- );
+has actions => (
+ accessor => '_controller_actions',
+ isa => 'HashRef',
+ 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 => { '*' => ...
has _all_actions_attributes => (
# 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 {
#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 {
my $class = ref($self) || $self;
my $appclass = ref($c) || $c;
+
+ # FIXME - catalyst_component_name is no longer a class accessor, because
+ # 'MyApp as a controller' behavior is removed. But is this call to
+ # catalyst_component_name necessary, or is it always the same as $class?
+ my $component_name = ref($self) ? $self->catalyst_component_name : $self;
+
if( ref($self) ){
return $self->$orig if $self->has_action_namespace;
} else {
}
}
- my $namespace = Catalyst::Utils::class2prefix($self->catalyst_component_name, $case_s) || '';
+ my $namespace = Catalyst::Utils::class2prefix($component_name, $case_s) || '';
$self->$orig($namespace) if ref($self);
return $namespace;
};
my $attributes = $method->can('attributes') ? $method->attributes : [];
my $attrs = $self->_parse_attrs( $c, $name, @{ $attributes } );
if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
- $c->log->debug( 'Bad action definition "'
+ $c->log->warn( 'Bad action definition "'
. join( ' ', @{ $attributes } )
. qq/" for "$class->$name"/ )
if $c->debug;
}
}
+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 = @_;
my %args = @_;
my $class = $self->action_class(%args);
- my $action_args = $self->config->{action_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
+ : $self->config->{action_args}
+ );
my %extra_args = (
%{ $action_args->{'*'} || {} },
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 ) = @_;
%raw_attributes = (
%raw_attributes,
- exists $actions_config->{$name} ? %{ $actions_config->{$name } } : (),
+ # Note we deep copy array refs here to stop crapping on config
+ # when attributes are parsed. RT#65463
+ exists $actions_config->{$name} ? map { ref($_) eq 'ARRAY' ? [ @$_ ] : $_ } %{ $actions_config->{$name } } : (),
);
# Private actions with additional attributes will raise a warning and then
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 );
}
}
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(@_); }
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;
Allows you to set the attributes that the dispatcher creates actions out of.
This allows you to do 'rails style routes', or override some of the
-attribute defintions of actions composed from Roles.
+attribute definitions of actions composed from Roles.
You can set arguments globally (for all actions of the controller) and
specifically (for a single action).
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