]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
applied patch for http://rt.cpan.org/NoAuth/Bug.html?id=12383
[maypole.git] / lib / Maypole / Application.pm
1 package Maypole::Application;
2
3 use strict;
4 use warnings;
5 use UNIVERSAL::require;
6 use Maypole;
7 use Maypole::Config;
8
9 our @ISA;
10 our $VERSION = '2.09';
11
12 sub import {
13     my ( $class, @plugins ) = @_;
14     my $caller = caller(0);
15     
16     my $frontend = 'Apache::MVC' if $ENV{MOD_PERL};
17     
18     my $masonx;
19     if ( grep { /^MasonX$/ } @plugins )
20     {
21         $masonx++;
22         @plugins = grep { ! /^MasonX$/ } @plugins;
23         $frontend = 'MasonX::Maypole';
24     }
25     
26     $frontend ||= 'CGI::Maypole';
27     
28     $frontend->require or die "Loading $frontend frontend failed: $@";
29     push @ISA, $frontend;
30
31     my $autosetup=0;
32     my @plugin_modules;
33     {
34         foreach (@plugins) {
35             if    (/^\-Setup$/) { $autosetup++; }
36             elsif (/^\-Debug(\d*)$/) {
37                 my $d = $1 || 1;
38                 no strict 'refs';
39                 *{"$caller\::debug"} = sub { $d };
40                 warn "Debugging (level $d) enabled for $caller";
41             }
42             elsif (/^-.*$/) { warn "Unknown flag: $_" }
43             else {
44                 my $plugin = "Maypole::Plugin::$_";
45                 if ($plugin->require) {
46                     push @plugin_modules, "Maypole::Plugin::$_";
47                     warn "Loaded plugin: $plugin for $caller"
48                         if $caller->can('debug') && $caller->debug;
49                 } else {
50                     die qq(Loading plugin "$plugin" for $caller failed: )
51                         . $UNIVERSAL::require::ERROR;
52                 }
53             }
54         }
55     }
56     no strict 'refs';
57     push @{"${caller}::ISA"}, @plugin_modules, $class;
58     $caller->config(Maypole::Config->new);
59     $caller->config->masonx({}) if $masonx;
60     $caller->setup() if $autosetup;
61 }
62
63 1;
64
65 =head1 NAME
66
67 Maypole::Application - Universal Maypole Frontend
68
69 =head1 SYNOPSIS
70
71     use Maypole::Application;
72
73     use Maypole::Application qw(Config::YAML);
74
75     use Maypole::Application qw(-Debug Config::YAML -Setup);
76
77     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
78
79     use Maypole::Application qw(-Debug2 MasonX AutoUntaint);
80
81 =head1 DESCRIPTION
82
83 This is a universal frontend for mod_perl1, mod_perl2, HTML::Mason and CGI.
84
85 You can omit the Maypole::Plugin:: prefix from plugins.
86 So Maypole::Plugin::Config::YAML becomes Config::YAML.
87
88     use Maypole::Application qw(Config::YAML);
89
90 You can also set special flags like -Setup and -Debug.
91
92     use Maypole::Application qw(-Debug Config::YAML -Setup);
93
94 The position of plugins and flags in the chain is important,
95 because they are loaded/executed in the same order they appear.
96
97 =head2 -Setup
98
99     use Maypole::Application qw(-Setup);
100
101 is equivalent to
102
103     use Maypole::Application;
104     MyApp->setup;
105
106 Note that no options are passed to C<setup()>. You must ensure that the
107 required model config parameters are set in C<MyApp-E<gt>config>. See
108 L<Maypole::Config> for more information.
109
110 =head2 -Debug
111
112     use Maypole::Application qw(-Debug);
113
114 is equivalent to
115
116     use Maypole::Application;
117     sub debug { 1 }
118
119 You can specify a higher debug level by saying C<-Debug2> etc. 
120
121 =head1 AUTHOR
122
123 Sebastian Riedel, C<sri@oook.de>
124 Idea by Marcus Ramberg, C<marcus@thefeed.no>
125
126 =head1 LICENSE
127
128 You may distribute this code under the same terms as Perl itself.
129
130 =cut