]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
fcbb13cadbae123204652dbb24b3a22770ff1e63
[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
8 our @ISA;
9
10 sub import {
11     my ( $self, @plugins ) = @_;
12     my $caller = caller(0);
13     no strict 'refs';
14     push @{"${caller}::ISA"}, $self;
15     foreach (@plugins) {
16         if    (/^\-Setup$/) { $caller->setup }
17         elsif (/^\-Debug$/) {
18             *{"$caller\::debug"} = sub { 1 };
19             warn "Debugging enabled";
20         }
21         elsif (/^-.*$/) { warn "Unknown flag: $_" }
22         else {
23
24             # The plugin caller should be our application class
25             eval "package $caller; require Maypole::Plugin::$_";
26             if ($@) { warn qq(Loading plugin "Maypole::Plugin::$_" failed: $@) }
27             else {
28                 warn "Loaded plugin: Maypole::Plugin::$_" if $caller->debug;
29                 unshift @ISA, "Maypole::Plugin::$_";
30             }
31         }
32     }
33 }
34
35 if ( $ENV{MOD_PERL} ) {
36     Apache::MVC->require or die "Loading Apache frontend failed: $@";
37     push @ISA, 'Apache::MVC';
38 }
39 else {
40     CGI::Maypole->require or die "Loading CGI frontend failed: $@";
41     push @ISA, 'CGI::Maypole';
42 }
43
44 1;
45
46 =head1 NAME
47
48 Maypole::Application - Maypole Universal Frontend
49
50 =head1 SYNOPSIS
51
52     use Maypole::Application;
53
54     use Maypole::Application qw(Config::YAML);
55
56     use Maypole::Application qw(-Debug Config::YAML -Setup);
57
58     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
59
60 =head1 DESCRIPTION
61
62 This is a universal frontend for mod_perl1, mod_perl2 and CGI.
63
64 You can omit the Maypole::Plugin:: prefix from plugins.
65 So Maypole::Plugin::Config::YAML becomes Config::YAML.
66
67     use Maypole::Application qw(Config::YAML);
68
69 You can also set special flags like -Setup and -Debug.
70
71     use Maypole::Application qw(-Debug Config::YAML -Setup);
72
73 The position of plugins and flags in the chain is important,
74 because they are loaded/executed in the same order they appear.
75
76 =head2 -Setup
77
78     use Maypole::Application qw(-Setup);
79
80 is equivalent to
81
82     use Maypole::Application;
83     MyApp->setup;
84
85 =head2 -Debug
86
87     use Maypole::Application qw(-Debug);
88
89 is equivalent to
90
91     use Maypole::Application;
92     sub debug { 1 }
93
94 =head1 AUTHOR
95
96 Sebastian Riedel, C<sri@oook.de>
97 Idea by Marcus Ramberg, C<marcus@thefeed.no>
98
99 =head1 LICENSE
100
101 You may distribute this code under the same terms as Perl itself.