]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/CLI.pm
Added parse_args to Maypole::CLI and CGI::Maypole
[maypole.git] / lib / Maypole / CLI.pm
1 package Maypole::CLI;
2 use UNIVERSAL::require;
3 use URI; use URI::QueryParam;
4 use Maypole::Constants;
5
6 use strict;
7 use warnings;
8 my $package;
9 our $buffer;
10 sub import { 
11     $package = $_[1];
12     $package->require;
13     die "Couldn't require $package - $@" if $@;
14     no strict 'refs';
15     unshift @{$package."::ISA"}, "Maypole::CLI";
16 }
17
18 sub get_request {}
19 sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
20
21 sub parse_location {
22     my $self = shift;
23     my $url = URI->new(shift @ARGV);
24     my $root = URI->new($self->config->{uri_base})->path;
25     $self->{path} = $url->path;
26     $self->{path} =~ s/^$root//i if $root;
27     $self->parse_path;
28     $self->parse_args;
29 }
30
31 sub parse_args {
32     my $self = shift;
33     $self->{params} = $url->query_form_hash;
34     $self->{query} = $url->query_form_hash;
35 }
36
37 sub send_output { $buffer = shift->{output} }
38
39 # Do it!
40 CHECK { if ((caller(0))[1] eq "-e") { 
41             $package->handler() == OK and print $buffer; 
42        } }
43
44 1;
45
46 =head1 NAME
47
48 Maypole::CLI - Command line interface to Maypole for testing and debugging
49
50 =head1 SYNOPSIS
51
52   % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
53   % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
54
55 =head1 DESCRIPTION
56
57 This module is used to test Maypole sites without going through a web
58 server or modifying them to use a CGI frontend. To use it, you should
59 first either be in the template root for your Maypole site or set the
60 environment variable C<MAYPOLE_TEMPLATES> to the right value.
61
62 Next, you import the C<Maypole::CLI> module specifying your base Maypole
63 subclass. The usual way to do this is with the C<-M> flag: 
64 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
65
66     use Maypole::CLI qw(MyApp);
67
68 Now Maypole will automatically call your application's handler with the
69 URL specified as the first command line parameter. This should be the
70 full URL, starting from whatever you have defined as the C<uri_base> in
71 your application's configuration, and may include query parameters.
72
73 The Maypole HTML output should then end up on standard output.
74
75 =head1 Support for testing
76
77 The module can also be used as part of a test script. 
78
79 When used programmatically, rather than from the command line, its
80 behaviour is slightly different. 
81
82 Although the URL is taken from C<@ARGV> as normal, your application's
83 C<handler> method is not called automatically, as it is when used on the
84 command line; you need to call it manually. Additionally, when
85 C<handler> is called, the output is not printed to standard output but
86 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
87 more easily.
88
89 For instance, a test script could look like this:
90
91     use Test::More tests => 5;
92     use Maypole::CLI qw(BeerDB);
93     use Maypole::Constants;
94     $ENV{MAYPOLE_TEMPLATES} = "t/templates";
95
96     # Hack because isa_ok only supports object isa not class isa
97     isa_ok( (bless {},"BeerDB") , "Maypole");
98
99     @ARGV = ("http://localhost/beerdb/");
100     is(BeerDB->handler, OK, "OK");
101     like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page");
102
103     @ARGV = ("http://localhost/beerdb/beer/list");
104     is(BeerDB->handler, OK, "OK");
105     like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list");
106