[Catalyst-commits] r7531 - in Catalyst-Plugin-SmartURI/1.000/trunk:
lib/Catalyst lib/Catalyst/Plugin t
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Sat Mar 29 05:43:49 GMT 2008
Author: caelum
Date: 2008-03-29 05:43:48 +0000 (Sat, 29 Mar 2008)
New Revision: 7531
Modified:
Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm
Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/SmartURI.pm
Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t
Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t
Catalyst-Plugin-SmartURI/1.000/trunk/t/04-smart-uri-subclass.t
Log:
Plugin fully functional.
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm 2008-03-26 23:42:35 UTC (rev 7530)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm 2008-03-29 05:43:48 UTC (rev 7531)
@@ -2,6 +2,7 @@
use strict;
use warnings;
+use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
=head1 NAME
@@ -17,38 +18,93 @@
=head1 SYNOPSIS
+ smarturi:
+ disposition: hostless
+
Configure whether $c->uri_for and $c->req->uri_with return absolute, hostless or
-relative URIs.
+relative URIs and/or configure which URI class to use, on an application or
+request basis.
This is useful in situations where you're for example, redirecting to a lighttpd
from a firewall rule, instead of a real proxy, and you want your links and
redirects to still work correctly.
+=head1 DESCRIPTION
+
+This plugin allows you to configure, on a application and per-request basis,
+what URI class $c->uri_for and $c->req->uri_with use, as well as whether the
+URIs they produce are absolute, hostless or relative.
+
+To use your own URI class, just subclass L<Catalyst::SmartURI> and set
+uri_class, or write a class that follows the same interface.
+
+This plugin installs a custom $c->request_class, however it does so in a way
+that won't break if you've already set your own request_class.
+
+=head1 CONFIGURATION
+
In myapp.yml:
smarturi:
- default_disposition: hostless
+ dispostion: absolute
+ uri_class: 'Catalyst::SmartURI'
-In MyApp.pm:
+=over
- package MyApp;
+=item disposition
+
+One of 'absolute', 'hostless' or 'relative'. Defaults to 'absolute'.
+
+=item uri_class
+
+The class to use for URIs, defaults to L<Catalyst::SmartURI>.
+
+=back
+
+=head1 PER REQUEST
+
+ package MyAPP::Controller::RSSFeed;
+
...
- use Catalyst qw/SmartURI/;
- ...
+ sub begin : Private {
+ my ($self, $c) = @_;
+
+ $c->uri_class('Your::URI::Class'); # if you need
+ $c->uri_disposition('absolute'); # rest of app configured differently
+ }
+
+=over
+
+=item $c->uri_disposition('absolute'|'hostless'|'relative')
+
+Set URI disposition to use for the duration of the request.
+
+=item $c->uri_class($class)
+
+Set the URI class to use for $c->uri_for and $c->req->uri_with for the duration
+of the request.
+
+=back
+
+=head1 EXTENDING
+
+$c->prepare_uri actually creates the URI, you can overload that to do as you
+please in your own plugins.
+
=cut
use Class::C3;
use Class::C3::Componentised;
-use Catalyst::SmartURI;
+__PACKAGE__->mk_accessors(qw/uri_disposition uri_class/);
+
+my $context; # keep a copy for the Request class to use
+
sub uri_for {
my $c = shift;
- Catalyst::SmartURI->new(
- $c->next::method(@_),
- { reference => $c->req->uri }
- )->hostless;
+ $c->prepare_uri($c->next::method(@_), $c->req)
}
{
@@ -58,16 +114,17 @@
sub uri_with {
my $req = shift;
- Catalyst::SmartURI->new(
- $req->next::method(@_),
- { reference => $req->uri }
- )->hostless;
+ $context->prepare_uri($req->next::method(@_), $req)
}
}
-sub setup_engine {
- my $app = shift;
+sub setup {
+ my $app = shift;
+ my $config = $app->config->{smarturi};
+ $config->{uri_class} ||= 'Catalyst::SmartURI';
+ $config->{disposition} ||= 'absolute';
+
my $request_class = $app->request_class;
unless ($request_class->isa('Catalyst::Request::SmartURI')) {
@@ -85,6 +142,29 @@
$app->next::method(@_)
}
+sub prepare_uri {
+ my ($c, $uri, $req) = @_;
+ my $disposition = $c->uri_disposition;
+
+ eval 'require '.$c->uri_class;
+
+ $c->uri_class->new($uri, { reference => $req->uri })->$disposition
+}
+
+# Reset accessors to configured values at beginning of request.
+sub prepare {
+ my $app = shift;
+ my $config = $app->config->{smarturi};
+
+# Also save a copy of the context for the Request class to use.
+ my $c = $context = $app->next::method(@_);
+
+ $c->uri_class($config->{uri_class});
+ $c->uri_disposition($config->{disposition});
+
+ $c
+}
+
=head1 AUTHOR
Rafael Kitover, C<< <rkitover at cpan.org> >>
@@ -131,9 +211,7 @@
from #catalyst:
vipul came up with the idea
-
-mst came up with the design and implementation notes for the current version
-
+mst came up with the design and implementation details for the current version
kd reviewed my code and offered suggestions
=head1 COPYRIGHT & LICENSE
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/SmartURI.pm
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/SmartURI.pm 2008-03-26 23:42:35 UTC (rev 7530)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/SmartURI.pm 2008-03-29 05:43:48 UTC (rev 7531)
@@ -256,7 +256,7 @@
local $AUTOLOAD = ref($self)."::$method";
local $CAN = 1;
- &{$self->factory_class.'::AUTOLOAD'}($self)
+ $self->$method
}
# Preload some URI classes, the ones that come in files anyway
@@ -277,7 +277,7 @@
my @uri_pms = File::Find::Rule->extras({untaint => 1})->file->name('*.pm')
->in( File::Find::Rule->extras({untaint => 1})->directory
- ->maxdepth(1)->name('URI')->in(@INC)
+ ->maxdepth(1)->name('URI')->in(grep !/^CODE\(/, @INC)
);
my @new_uri_pms;
@@ -477,6 +477,13 @@
Rafael Kitover, C<< <rkitover at cpan.org> >>
+=head1 COPYRIGHT & LICENSE
+
+Copyright (c) 2008 Rafael Kitover
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
=cut
'LONG LIVE THE ALMIGHTY BUNGHOLE';
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t 2008-03-26 23:42:35 UTC (rev 7530)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t 2008-03-29 05:43:48 UTC (rev 7531)
@@ -2,12 +2,12 @@
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
{
package TestApp;
- use Catalyst 'SmartURI';
+ use Catalyst qw/SmartURI/;
sub test_uri_for_redirect : Global {
my ($self, $c) = @_;
@@ -26,6 +26,15 @@
$c->res->output($c->uri_for('/test_uri_object')->path);
}
+ sub per_request : Global {
+ my ($self, $c) = @_;
+ $c->uri_disposition('relative');
+ $c->res->output($c->uri_for('/dummy'));
+ }
+
+ sub dummy : Global {}
+
+ __PACKAGE__->config->{smarturi}{disposition} = 'hostless';
__PACKAGE__->setup();
}
@@ -34,9 +43,11 @@
is(request('/test_uri_for_redirect')->header('location'),
'/test_uri_for_redirect', 'redirect location');
+is(get('/per_request'), 'dummy', 'per-request disposition');
+
is(get('/test_req_uri_with'),
'/test_req_uri_with?the_word_that_must_be_heard=mtfnpy',
- '$c->req->uri_with test');
+ '$c->req->uri_with test, and disposition reset');
is(get('/test_uri_object'), '/test_uri_object',
'URI objects are functional');
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t 2008-03-26 23:42:35 UTC (rev 7530)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t 2008-03-29 05:43:48 UTC (rev 7531)
@@ -24,6 +24,8 @@
$c->res->output($c->req->uri_with({foo => 'bar'}));
}
+ __PACKAGE__->config->{smarturi}{disposition} = 'hostless';
+
__PACKAGE__->setup();
}
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/t/04-smart-uri-subclass.t
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/t/04-smart-uri-subclass.t 2008-03-26 23:42:35 UTC (rev 7530)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/t/04-smart-uri-subclass.t 2008-03-29 05:43:48 UTC (rev 7531)
@@ -1,7 +1,7 @@
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 6;
{
package MyURI;
@@ -13,6 +13,18 @@
$uri->query_form([ $uri->query_form, qw(foo bar) ]);
$uri
}
+
+ package TestApp;
+
+ use Catalyst 'SmartURI';
+
+ sub foo : Global {
+ my ($self, $c) = @_;
+ $c->res->output($c->uri_for('/foo')->mtfnpy)
+ }
+
+ __PACKAGE__->config->{smarturi}{uri_class} = 'MyURI';
+ __PACKAGE__->setup;
}
BEGIN {
@@ -23,12 +35,17 @@
is(MyURI::URL->new('http://search.cpan.org/~lwall/')->path,
'/~lwall/', 'Magic import');
-my $uri = MyURI->new('http://www.catalystframework.org/calendar',
- { reference => 'http://www.catalystframework.org/' });
+ok(my $uri = MyURI->new('http://www.catalystframework.org/calendar',
+ { reference => 'http://www.catalystframework.org/' }),
+ 'new');
is($uri->mtfnpy,
'http://www.catalystframework.org/calendar?foo=bar', 'new method');
is($uri->reference, 'http://www.catalystframework.org/', 'old method');
+use Catalyst::Test 'TestApp';
+
+is(get('/foo'), 'http://localhost/foo?foo=bar', 'configured uri_class');
+
# vim: expandtab shiftwidth=4 ts=4 tw=80:
More information about the Catalyst-commits
mailing list