From: John Napiorkowski Date: Fri, 17 Apr 2015 01:14:17 +0000 (-0500) Subject: slightly better abstraction X-Git-Tag: 5.90089_002~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=e39312ba37f13fad13d7f1acf967e40dea096278;hp=3e5607485bfedb02a06193f653a2f05202db7a4e slightly better abstraction --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 0e50180..851ff1d 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -710,12 +710,17 @@ sub _comp_names { } # Filter a component before returning by calling ACCEPT_CONTEXT if available + +#our %tracker = (); sub _filter_component { my ( $c, $comp, @args ) = @_; + # die "Circular Dependencies Detected." if $tracker{$comp}; + # $tracker{$comp}++; if(ref $comp eq 'CODE') { $comp = $comp->(); } + #$tracker{$comp}++; if ( eval { $comp->can('ACCEPT_CONTEXT'); } ) { return $comp->ACCEPT_CONTEXT( $c, @args ); @@ -2838,7 +2843,7 @@ sub setup_components { } for my $component (@comps) { - my $instance = $class->components->{ $component } = $class->setup_component($component); + my $instance = $class->components->{ $component } = $class->delayed_setup_component($component); } # Inject a component or wrap a stand alone class in an adaptor. This makes a list @@ -2916,6 +2921,21 @@ sub expand_component_module { return Devel::InnerPackage::list_packages( $module ); } +=head2 $app->delayed_setup_component + +Returns a coderef that points to a setup_component instance. Used +internally for when you want to delay setup until the first time +the component is called. + +=cut + +sub delayed_setup_component { + my($class, $component, @more) = @_; + return sub { + return my $instance = $class->setup_component($component, @more); + }; +} + =head2 $c->setup_component =cut @@ -2923,7 +2943,6 @@ sub expand_component_module { sub setup_component { my( $class, $component ) = @_; -return sub { unless ( $component->can( 'COMPONENT' ) ) { return $component; } @@ -2956,19 +2975,17 @@ return sub { ); } -my @expanded_components = $instance->can('expand_modules') - ? $instance->expand_modules( $component, $config ) - : $class->expand_component_module( $component, $config ); -for my $component (@expanded_components) { - next if $class->components->{ $component }; - $class->components->{ $component } = $class->setup_component($component); -} + my @expanded_components = $instance->can('expand_modules') + ? $instance->expand_modules( $component, $config ) + : $class->expand_component_module( $component, $config ); + for my $component (@expanded_components) { + next if $class->components->{ $component }; + $class->components->{ $component } = $class->setup_component($component); + } return $instance; } -} - =head2 $c->setup_dispatcher Sets up dispatcher. diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index 847a1c3..9fb1e92 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -575,13 +575,10 @@ sub inject_component { my $_setup_component = sub { my $into = shift; my $component_package = shift; - $into->components->{$component_package} = $into->setup_component( $component_package ); + $into->components->{$component_package} = $into->delayed_setup_component( $component_package ); }; $_setup_component->( $into, $component_package ); - # for my $inner_component_package ( Devel::InnerPackage::list_packages( $component_package ) ) { - # $_setup_component->( $into, $inner_component_package ); - # } } =head1 PSGI Helpers diff --git a/t/configured_comps.t b/t/configured_comps.t index 1cc0d1a..8a84fd6 100644 --- a/t/configured_comps.t +++ b/t/configured_comps.t @@ -62,9 +62,9 @@ use Test::More; sub user :Local Args(1) { my ($self, $c, $int) = @_; - my $user = $c->model("User")->find($int); - - $c->model("User")->zoo->a; + + Test::More::ok(my $user = $c->model("User")->find($int)); + Test::More::ok($c->model("User")->zoo->a); $c->res->body("name: $user->{name}, age: $user->{age}"); } @@ -86,13 +86,12 @@ use Test::More; }, 'Model::Zoo' => { from_component => 'Local::Model::Foo', - args => {a=>2}, + args => { a=>2 }, }, 'Model::Foo' => { from_component => 'Local::Model::Foo', args => { a=> 100 }, }, - }); MyApp->setup;