From 46a3c98ba5b092c527754ca04984ae48ead92e35 Mon Sep 17 00:00:00 2001 From: MaxPerl Date: Sun, 27 Nov 2016 23:09:30 +0100 Subject: [PATCH 1/4] Get $env easier with $self->env Get $env easier with $self->env if CGI::Application is used as PSGI Application --- lib/CGI/Application.pm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/CGI/Application.pm b/lib/CGI/Application.pm index a6da533..246ea74 100644 --- a/lib/CGI/Application.pm +++ b/lib/CGI/Application.pm @@ -60,6 +60,11 @@ sub new { if (exists($rprops->{QUERY})) { $self->query($rprops->{QUERY}); } + + # Set the ENV variable for PSGI Application + if (exists($rprops->{ENV})) { + $self->env($rprops->{ENV}); + } # Set up init param() values if (exists($rprops->{PARAMS})) { @@ -258,6 +263,8 @@ sub psgi_app { sub run_as_psgi { my $self = shift; $self->{__IS_PSGI} = 1; + my $env = shift; + $self->env($env); # Run doesn't officially support any args, but pass them through in case some sub-class uses them. return $self->run(@_); @@ -535,7 +542,17 @@ sub delete { delete $self->{__PARAMS}->{$param}; } - +sub env { + my $self = shift; + my ($env) = @_; + + # If data is provided, set it! + if (defined($env)) { + $self->{__ENV} = $env; + } + + return $self->{__ENV}; +} sub query { my $self = shift; my ($query) = @_; From 22651c95ddcfa2fb647acae714bd4b0d32c4d272 Mon Sep 17 00:00:00 2001 From: MaxPerl Date: Tue, 29 Nov 2016 20:19:18 +0100 Subject: [PATCH 2/4] Added pod Documentation --- lib/CGI/Application.pm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/CGI/Application.pm b/lib/CGI/Application.pm index 246ea74..e1cc5fe 100644 --- a/lib/CGI/Application.pm +++ b/lib/CGI/Application.pm @@ -1035,6 +1035,11 @@ CGI::Application will instantiate its own CGI.pm query object. Under certain conditions, it might be useful to be able to use one which has already been created. +B - This optional parameter allows you to save the PSGI environment hash. +This is useful, because you can later get this environment hash in your Application +Module with the method $self->env which could be important for using Plack::Middlewares +and similiar. + B - This parameter, if used, allows you to set a number of custom parameters at run-time. By passing in different values in different instance scripts which use the same application @@ -1101,7 +1106,7 @@ It's possible that we'll change from CGI::PSGI to a different-but-compatible query object for PSGI support in the future, perhaps if CGI.pm adds native PSGI support. -=head3 run_as_psgi() +=head3 run_as_psgi($env) my $psgi_aref = $webapp->run_as_psgi; @@ -1132,6 +1137,11 @@ PSGI spec. to handle the input, you need to use a CGI.pm-like query object that is PSGI-compliant, such as L. This query object must provide L and L methods. +You can pass the PSGI enivornment hash as first argument to the run_as_psgi. This +is the same as passing C<< {ENV => $env} >> to the method C<< new >> or C<< as_psgi >>. +The benefit of this is that you can later in your Application Module easily access to this +PSGI environment hash by C<< $self->env >> + The final result might look like this: use WebApp; From 32a888a7244ad54dc7964d24d204ab80fb5c373a Mon Sep 17 00:00:00 2001 From: MaxPerl Date: Tue, 29 Nov 2016 20:27:34 +0100 Subject: [PATCH 3/4] Added patch and POD to psgi_app --- lib/CGI/Application.pm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/CGI/Application.pm b/lib/CGI/Application.pm index e1cc5fe..117ad08 100644 --- a/lib/CGI/Application.pm +++ b/lib/CGI/Application.pm @@ -251,6 +251,7 @@ sub psgi_app { # PR from alter https://github.com/markstos/CGI--Application/pull/17 #if (not defined $args_to_new->{QUERY}) { + $args_to_new->{ENV} = $env; require CGI::PSGI; $args_to_new->{QUERY} = CGI::PSGI->new($env); #} @@ -1099,8 +1100,10 @@ support to it. The simplest way to create and return a PSGI-compatible coderef. Pass in arguments to a hashref just as would to new. This returns a PSGI-compatible -coderef, using L as the query object. To use a different query -object, construct your own object using C<< run_as_psgi() >>, as shown below. +coderef, using L as the query object and saving the PSGI +environment hash in the key ENV so that it can be accesed with C<< $self->env >>. +To use a different query object, construct your own object using C<< run_as_psgi() >>, +as shown below. It's possible that we'll change from CGI::PSGI to a different-but-compatible query object for PSGI support in the future, perhaps if CGI.pm adds native From a0c4fc7f0ceea94c63dc52b4a089e88156c1cb89 Mon Sep 17 00:00:00 2001 From: MaxPerl Date: Fri, 2 Dec 2016 22:03:26 +0100 Subject: [PATCH 4/4] Complete Docs --- lib/CGI/Application.pm | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/CGI/Application.pm b/lib/CGI/Application.pm index 117ad08..6528b92 100644 --- a/lib/CGI/Application.pm +++ b/lib/CGI/Application.pm @@ -1578,6 +1578,16 @@ you can pass it to c like this: $webapp->query($new_query_object); my $q = $webapp->query(); # now uses $new_query_object +=head3 env() + + my $q = $webapp->env(); + my $session = Plack::Session->new($env); + +This method retrieves the PSGI environment hash which has been created +by instantiating your Application Module as a psgi script. This is important +for using Plack::Middlewares, such as Plack::Middleware::Session in the +example above. + =head3 run_modes() # The common usage: an arrayref of run mode names that exactly match subroutine names