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