]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
6804cdead609ff0eb4e9c1ca8ff05df88dfec155
[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 $VERSION = '2.11';
10
11 sub import {
12     shift; # not interested in this - we manipulate the caller's @ISA directly
13     my @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
30     my $autosetup=0;
31     my $autoinit=0;
32     my @plugin_modules;
33     {
34         foreach (@plugins) {
35             if    (/^\-Setup$/) { $autosetup++; }
36             elsif (/^\-Init$/)  { $autoinit++ }
37             elsif (/^\-Debug(\d*)$/) {
38                 my $d = $1 || 1;
39                 no strict 'refs';
40                 *{"$caller\::debug"} = sub { $d };
41                 warn "Debugging (level $d) enabled for $caller";
42             }
43             elsif (/^-.*$/) { warn "Unknown flag: $_" }
44             else {
45                 my $plugin = "Maypole::Plugin::$_";
46                 if ($plugin->require) {
47                     push @plugin_modules, "Maypole::Plugin::$_";
48                     warn "Loaded plugin: $plugin for $caller"
49                         if $caller->can('debug') && $caller->debug;
50                 } else {
51                     die qq(Loading plugin "$plugin" for $caller failed: )
52                         . $UNIVERSAL::require::ERROR;
53                 }
54             }
55         }
56     }
57     no strict 'refs';
58     push @{"${caller}::ISA"}, @plugin_modules, $frontend;
59     $caller->config(Maypole::Config->new);
60     $caller->config->masonx({}) if $masonx;
61     $caller->setup() if $autosetup;
62     $caller->init() if $autosetup && $autoinit;
63 }
64
65 1;
66
67 =head1 NAME
68
69 Maypole::Application - Universal Maypole Frontend
70
71 =head1 SYNOPSIS
72
73     use Maypole::Application;
74
75     use Maypole::Application qw(Config::YAML);
76
77     use Maypole::Application qw(-Debug Config::YAML -Setup);
78
79     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
80
81     use Maypole::Application qw(-Debug2 MasonX AutoUntaint);
82
83 =head1 DESCRIPTION
84
85 This is a universal frontend for mod_perl1, mod_perl2, HTML::Mason and CGI.
86
87 Automatically determines the appropriate frontend for your environment (unless
88 you want to use L<MasonX::Maypole>, in which case include C<MasonX> in the
89 arguments).
90
91 Loads plugins supplied in the C<use> statement. 
92
93 Responds to flags supplied in the C<use> statement. 
94
95 Initializes the application's configuration object. 
96
97 You can omit the Maypole::Plugin:: prefix from plugins. So
98 Maypole::Plugin::Config::YAML becomes Config::YAML.
99
100     use Maypole::Application qw(Config::YAML);
101
102 You can also set special flags like -Setup, -Debug and -Init.
103
104     use Maypole::Application qw(-Debug Config::YAML -Setup);
105
106 The position of plugins in the chain is important, because they are
107 loaded/executed in the same order they appear.
108
109 =head1 FRONTEND
110
111 Under mod_perl (1 or 2), selects L<Apache::MVC>. 
112
113 Otherwise, selects L<CGI::Maypole>.
114
115 If C<MasonX> is specified, sets L<MasonX::Maypole> as the frontend. This
116 currently also requires a mod_perl environment.
117
118 =head1 FLAGS
119
120 =over
121
122 =item -Setup
123
124     use Maypole::Application qw(-Setup);
125
126 is equivalent to
127
128     use Maypole::Application;
129     MyApp->setup;
130
131 Note that no options are passed to C<setup()>. You must ensure that the
132 required model config parameters are set in C<MyApp-E<gt>config>. See
133 L<Maypole::Config> for more information.
134
135 =item -Init
136
137     use Maypole::Application qw(-Setup -Init);
138     
139 is equivalent to
140
141     use Maypole::Application;
142     MyApp->setup;
143     MyApp->init;
144     
145 Note that the C<-Setup> flag is required for the C<-Init> flag to work.
146
147 In persistent environments (e.g. C<mod_perl>), it is useful to call C<init> 
148 once in the parent server, rather than at the beginning of the first request
149 to each child server, in order to share the view code loaded during C<init>. 
150 Note that you must supply all the config data to your app before calling 
151 C<setup> and C<init>, probably by using one of the C<Maypole::Plugin::Config::*> 
152 plugins.
153
154 =item -Debug
155
156     use Maypole::Application qw(-Debug);
157
158 is equivalent to
159
160     use Maypole::Application;
161     sub debug { 1 }
162
163 You can specify a higher debug level by saying C<-Debug2> etc. 
164
165 =back
166
167 =head1 AUTHOR
168
169 Sebastian Riedel, C<sri@oook.de>
170 Idea by Marcus Ramberg, C<marcus@thefeed.no>
171
172 =head1 LICENSE
173
174 You may distribute this code under the same terms as Perl itself.
175
176 =cut