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