CatalystX-InjectComponent-0.025000755000765000024 011774404537 15613 5ustar00robstaff000000000000README100644000765000024 476011774404537 16563 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025NAME CatalystX::InjectComponent - Inject components into your Catalyst application VERSION version 0.025 SYNOPSIS package My::App; use Catalyst::Runtime '5.80'; use Moose; BEGIN { extends qw/Catalyst/ } ... after 'setup_components' => sub { my $class = shift; CatalystX::InjectComponent->inject( into => $class, component => 'MyModel' ); if ( $class->config->{ ... ) { CatalystX::InjectComponent->inject( into => $class, component => 'MyRootV2', as => 'Controller::Root' ); } else { CatalystX::InjectComponent->inject( into => $class, component => 'MyRootV1', as => 'Root' ); # Controller:: will be automatically prefixed } }; DESCRIPTION CatalystX::InjectComponent will inject Controller, Model, and View components into your Catalyst application at setup (run)time. It does this by creating a new package on-the-fly, having that package extend the given component, and then having Catalyst setup the new component (via "->setup_component") So, how do I use this thing? You should inject your components when appropriate, typically after "setup_compenents" runs If you're using the Moose version of Catalyst, then you can use the following technique: use Moose; BEGIN { extends qw/Catalyst/ } after 'setup_components' => sub { my $class = shift; CatalystX::InjectComponent->inject( into => $class, ... ) }; METHODS CatalystX::InjectComponent->inject( ... ) into The Catalyst package to inject into (e.g. My::App) component The component package to inject as An optional moniker to use as the package name for the derived component For example: ->inject( into => My::App, component => Other::App::Controller::Apple ) The above will create 'My::App::Controller::Other::App::Controller::Apple' ->inject( into => My::App, component => Other::App::Controller::Apple, as => Apple ) The above will create 'My::App::Controller::Apple' ACKNOWLEDGEMENTS Inspired by Catalyst::Plugin::AutoCRUD AUTHOR Robert Krimen COPYRIGHT AND LICENSE This software is copyright (c) 2012 by Robert Krimen. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Changes100644000765000024 126011774404537 17166 0ustar00robstaff000000000000CatalystX-InjectComponent-0.0250.025 Monday July 02 13:47:45 PDT 2012: - Incorporate documentation fix (rt76363) 0.024 Wednesday October 14 18:30:13 PDT 2009: - Make sure we're being used with Moose-based Catalyst 0.023 Monday October 12 16:10:13 PDT 2009 - Added Catalyst::Runtime dependency (thanks Andreas Koenig) 0.022 Wednesday October 07 22:22:18 PDT 2009: - Remove plugins from tests to fix dependency failure (thanks bobtfish) 0.021 Wednesday June 24 00:49:08 PDT 2009: - Added parent dependency 0.020 Wednesday June 10 11:24:59 PDT 2009: - Altered injection interface: into =>, component =>, as => 0.010 Monday June 08 16:08:08 PDT 2009: - Initial release META.yml100644000765000024 104111774404537 17141 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025--- abstract: 'Inject components into your Catalyst application' author: - 'Robert Krimen ' build_requires: Test::Most: 0 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300003, CPAN::Meta::Converter version 2.112150' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: CatalystX-InjectComponent requires: Catalyst::Runtime: 5.8 Class::Inspector: 0 Devel::InnerPackage: 0 parent: 0 version: 0.025 MANIFEST100644000765000024 21311774404537 17001 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025Changes MANIFEST META.yml Makefile.PL README lib/CatalystX/InjectComponent.pm t/00-load.t t/01-basic.t t/02-basic-legacy.t t/Test/Apple.pm t000755000765000024 011774404537 15777 5ustar00robstaff000000000000CatalystX-InjectComponent-0.02500-load.t100644000765000024 27111774404537 17440 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025/t#!perl use Test::More tests => 1; BEGIN { use_ok( 'CatalystX::InjectComponent' ); } diag( "Testing CatalystX::InjectComponent $CatalystX::InjectComponent::VERSION, Perl $], $^X" ); Makefile.PL100644000765000024 217711774404537 17655 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025 use strict; use warnings; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Inject components into your Catalyst application", "AUTHOR" => "Robert Krimen ", "BUILD_REQUIRES" => { "Test::Most" => 0 }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "CatalystX-InjectComponent", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "CatalystX::InjectComponent", "PREREQ_PM" => { "Catalyst::Runtime" => "5.8", "Class::Inspector" => 0, "Devel::InnerPackage" => 0, "parent" => 0 }, "VERSION" => "0.025", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 01-basic.t100644000765000024 200711774404537 17622 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025/t#!/usr/bin/perl -w use strict; use warnings; use Test::Most; plan qw/no_plan/; use CatalystX::InjectComponent; BEGIN { package Model::Banana; use parent qw/Catalyst::Model/; package TestCatalyst; $INC{'TestCatalyst.pm'} = 1; use Catalyst::Runtime '5.70'; use Moose; BEGIN { extends qw/Catalyst/ } use Catalyst; after 'setup_components' => sub { my $self = shift; CatalystX::InjectComponent->inject( into => __PACKAGE__, component => 'Model::Banana' ); CatalystX::InjectComponent->inject( into => __PACKAGE__, component => 't::Test::Apple' ); CatalystX::InjectComponent->inject( into => __PACKAGE__, component => 'Model::Banana', as => 'Cherry' ); CatalystX::InjectComponent->inject( into => __PACKAGE__, component => 't::Test::Apple', as => 'Apple' ); }; TestCatalyst->config( 'home' => '.' ); TestCatalyst->setup; } package main; use Catalyst::Test qw/TestCatalyst/; ok( TestCatalyst->controller( $_ ) ) for qw/ Apple t::Test::Apple /; ok( TestCatalyst->model( $_ ) ) for qw/ Banana Cherry /; Test000755000765000024 011774404537 16716 5ustar00robstaff000000000000CatalystX-InjectComponent-0.025/tApple.pm100644000765000024 21211774404537 20430 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025/t/Testpackage t::Test::Apple; use strict; use warnings; use parent qw/Catalyst::Controller/; sub default :Path { } sub apple :Local { } 1; 02-basic-legacy.t100644000765000024 203311774404537 21064 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025/t#!/usr/bin/perl -w use strict; use warnings; use Test::Most; plan qw/no_plan/; use CatalystX::InjectComponent; BEGIN { package Model::Banana; use parent qw/Catalyst::Model/; package TestCatalyst; $INC{'TestCatalyst.pm'} = 1; use Catalyst::Runtime '5.70'; use Moose; BEGIN { extends qw/Catalyst/ } use Catalyst; after 'setup_components' => sub { my $self = shift; CatalystX::InjectComponent->inject( catalyst => __PACKAGE__, component => 'Model::Banana' ); CatalystX::InjectComponent->inject( catalyst => __PACKAGE__, component => 't::Test::Apple' ); CatalystX::InjectComponent->inject( catalyst => __PACKAGE__, component => 'Model::Banana', into => 'Cherry' ); CatalystX::InjectComponent->inject( catalyst => __PACKAGE__, component => 't::Test::Apple', into => 'Apple' ); }; TestCatalyst->config( 'home' => '.' ); TestCatalyst->setup; } package main; use Catalyst::Test qw/TestCatalyst/; ok( TestCatalyst->controller( $_ ) ) for qw/ Apple t::Test::Apple /; ok( TestCatalyst->model( $_ ) ) for qw/ Banana Cherry /; CatalystX000755000765000024 011774404537 20216 5ustar00robstaff000000000000CatalystX-InjectComponent-0.025/libInjectComponent.pm100644000765000024 1133311774404537 24034 0ustar00robstaff000000000000CatalystX-InjectComponent-0.025/lib/CatalystXpackage CatalystX::InjectComponent; { $CatalystX::InjectComponent::VERSION = '0.025'; } # ABSTRACT: Inject components into your Catalyst application use warnings; use strict; use Devel::InnerPackage; use Class::Inspector; use Carp; sub put_package_into_INC ($) { my $package = shift; (my $file = "$package.pm") =~ s{::}{/}g; $INC{$file} ||= 1; } sub loaded ($) { my $package = shift; if ( Class::Inspector->loaded( $package ) ) { put_package_into_INC $package; # As a courtesy return 1; } return 0; } sub inject { my $self = shift; my %given = @_; my ($into, $component, $as); if ( $given{catalyst} ) { # Legacy argument parsing ($into, $component, $as) = @given{ qw/catalyst component into/ }; } else { ($into, $component, $as) = @given{ qw/into component as/ }; } croak "No Catalyst (package) given" unless $into; croak "No component (package) given" unless $component; unless ( loaded $component ) { eval "require $component;" or croak "Couldn't require (component base) $component: $@"; } $as ||= $component; unless ( $as =~ m/^(?:Controller|Model|View)::/ || $given{skip_mvc_renaming} ) { my $category; for (qw/ Controller Model View /) { if ( $component->isa( "Catalyst::$_" ) ) { $category = $_; last; } } croak "Don't know what kind of component \"$component\" is" unless $category; $as = "${category}::$as"; } my $component_package = join '::', $into, $as; unless ( loaded $component_package ) { eval "package $component_package; use parent qw/$component/; 1;" or croak "Unable to build component package for \"$component_package\": $@"; put_package_into_INC $component_package; # As a courtesy } $self->_setup_component( $into => $component_package ); for my $inner_component_package ( Devel::InnerPackage::list_packages( $component_package ) ) { $self->_setup_component( $into => $inner_component_package ); } } sub _setup_component { my $self = shift; my $into = shift; my $component_package = shift; $into->components->{$component_package} = $into->setup_component( $component_package ); } 1; # End of CatalystX::InjectComponent __END__ =pod =head1 NAME CatalystX::InjectComponent - Inject components into your Catalyst application =head1 VERSION version 0.025 =head1 SYNOPSIS package My::App; use Catalyst::Runtime '5.80'; use Moose; BEGIN { extends qw/Catalyst/ } ... after 'setup_components' => sub { my $class = shift; CatalystX::InjectComponent->inject( into => $class, component => 'MyModel' ); if ( $class->config->{ ... ) { CatalystX::InjectComponent->inject( into => $class, component => 'MyRootV2', as => 'Controller::Root' ); } else { CatalystX::InjectComponent->inject( into => $class, component => 'MyRootV1', as => 'Root' ); # Controller:: will be automatically prefixed } }; =head1 DESCRIPTION CatalystX::InjectComponent will inject Controller, Model, and View components into your Catalyst application at setup (run)time. It does this by creating a new package on-the-fly, having that package extend the given component, and then having Catalyst setup the new component (via C<< ->setup_component >>) =head1 So, how do I use this thing? You should inject your components when appropriate, typically after C runs If you're using the Moose version of Catalyst, then you can use the following technique: use Moose; BEGIN { extends qw/Catalyst/ } after 'setup_components' => sub { my $class = shift; CatalystX::InjectComponent->inject( into => $class, ... ) }; =head1 METHODS =head2 CatalystX::InjectComponent->inject( ... ) into The Catalyst package to inject into (e.g. My::App) component The component package to inject as An optional moniker to use as the package name for the derived component For example: ->inject( into => My::App, component => Other::App::Controller::Apple ) The above will create 'My::App::Controller::Other::App::Controller::Apple' ->inject( into => My::App, component => Other::App::Controller::Apple, as => Apple ) The above will create 'My::App::Controller::Apple' =head1 ACKNOWLEDGEMENTS Inspired by L =head1 AUTHOR Robert Krimen =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2012 by Robert Krimen. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut