);
sub BUILD {
- my $self = shift;
+ my ( $self, $params ) = @_;
$self->add_service(
$self->${\"build_${_}_service"}
/;
$self->add_sub_container(
- $self->${ \"build_${_}_subcontainer" }
- ) for qw/ model view controller /;
+ $self->build_controller_subcontainer
+ );
+
+ my $config = $self->resolve( service => 'config' );
+ my $default_view = $params->{default_view} || $config->{default_view};
+ my $default_model = $params->{default_model} || $config->{default_model};
+
+ $self->add_sub_container(
+ $self->build_view_subcontainer(
+ default_component => $default_view,
+ )
+ );
+
+ $self->add_sub_container(
+ $self->build_model_subcontainer(
+ default_component => $default_model,
+ )
+ );
}
sub build_model_subcontainer {
my $self = shift;
- return $self->new_sub_container( name => 'model' );
+ return $self->new_sub_container( @_,
+ name => 'model',
+ );
}
sub build_view_subcontainer {
my $self = shift;
- return $self->new_sub_container( name => 'view' );
+ return $self->new_sub_container( @_,
+ name => 'view',
+ );
}
sub build_controller_subcontainer {
my $self = shift;
- return $self->new_sub_container( name => 'controller' );
+ return $self->new_sub_container(
+ name => 'controller',
+ );
}
sub build_name_service {
return $arg;
}
+sub get_component_from_sub_container {
+ my ( $self, $sub_container_name, $name, $c, @args ) = @_;
+
+ my $sub_container = $self->get_sub_container( $sub_container_name );
+
+ if (!$name) {
+ my $default = $sub_container->default_component;
+
+ return $sub_container->get_component( $default, $c, @args )
+ if $default && $sub_container->has_service( $default );
+
+ # this is never a controller, so this is safe
+ $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
+ $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
+ $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
+ $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
+
+ return;
+ }
+
+ return $sub_container->get_component_regexp( $name, $c, @args )
+ if ref $name;
+
+ return $sub_container->get_component( $name, $c, @args )
+ if $sub_container->has_service( $name );
+
+ $c->log->warn(
+ "Attempted to use $sub_container_name '$name', " .
+ "but it does not exist"
+ );
+
+ return;
+}
+
+sub find_component {
+ my ( $self, $component, @args ) = @_;
+ my @result;
+
+ my $query = ref $component
+ ? $component
+ : qr{^$component$}
+ ;
+
+ for my $subcontainer_name (qw/model view controller/) {
+ my $subcontainer = $self->get_sub_container($subcontainer_name);
+ my @components = $subcontainer->get_service_list;
+ @result = grep { m{$component} } @components;
+
+ return map { $subcontainer->get_component( $_, @args ) } @result
+ if @result;
+ }
+
+ # it expects an empty list on failed searches
+ return @result;
+}
+
+sub find_component_regexp {
+ my ( $self, $components, $component, @args ) = @_;
+ my @result;
+
+ my @components = grep { m{$component} } keys %{ $components };
+
+ for (@components) {
+ # FIXME this is naughty enough being called inside Catalyst.pm
+ # find some alternative for this sub and remember to delete here
+ my ($type, $name) = Catalyst::_get_component_type_name($_);
+
+ push @result, $self->get_component_from_sub_container(
+ $type, $name, @args
+ ) if $type;
+ }
+
+ return @result;
+}
+
+
1;
__END__
=head2 build_config_local_suffix_service
+=head2 get_component_from_sub_container
+
+=head2 find_component
+
+=head2 find_component_regexp
+
=head2 _fix_syntax
=head2 _config_substitutions