Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 21st, 2012  |  syntax: None  |  size: 1.61 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package CatalystX::PSGIApp;
  2.  
  3. use strict;
  4. use warnings;
  5. use Catalyst;
  6.  
  7. =head1 NAME
  8.  
  9. CatalystX::PSGIApp - Get a psgi_app in a unified way across different Catalyst versions
  10.  
  11. =head1 DESCRIPTION
  12.  
  13. Provides a psgi_app via ->psgi_app if it's available, or via Catalyst::Engine::PSGI if it's not
  14.  
  15. =head1 OVERVIEW
  16.  
  17. To use Catalyst with PSGI on Catalyst versions pre 5.9 requires the use of
  18. L<Catalyst::Engine::PSGI>. Version 5.9 (and presumably above) of Catalyst
  19. have PSGI baked in, and provide a C<psgi_app> method.
  20.  
  21. If your Catalyst app is being run on versions on both side of this divide -
  22. which is a weird use-case you probably shouldn't have - this module allows you
  23. a unified way of getting the PSGI app back.
  24.  
  25. =head1 WARNINGS
  26.  
  27. You'll need to use L<Catalyst::Engine::PSGI> installed for this to work with
  28. older Catalysts, but it's not listed as a dependency.
  29.  
  30. If you need this module, your developers are using different Catalyst versions
  31. to develop against, which is pretty weird.
  32.  
  33. =head1 SYNOPSIS
  34.  
  35.  use CatalystX::PSGIApp;
  36.  my $app = CatalystX::PSGIApp->psgi_app( 'Your::App::Here' );
  37.  
  38. =head1 METHODS
  39.  
  40. =head2 psgi_app
  41.  
  42. Returns a PSGI app, either via C<<->psgi_app>> if your version of Catalyst
  43. supports it, or using L<Catalyst::Engine::PSGI>.
  44.  
  45. =cut
  46.  
  47. sub psgi_app {
  48.     my $class  = shift;
  49.     my $target = shift;
  50.  
  51.     eval "require $target";
  52.     die $@ if $@;
  53.  
  54.     if ( $target->can('psgi_app') ) {
  55.         return $target->psgi_app;
  56.     } else {
  57.         $target->setup_engine('PSGI');
  58.         return sub { $target->run(@_) }
  59.     }
  60. }
  61.  
  62. =head1 AUTHOR
  63.  
  64. Peter Sergeant C<pete@clueball.com>, while at
  65. L<http://www.net-a-porter.com|NET-A-PORTER>.
  66.  
  67. =cut
  68.  
  69. 1;