New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added App::process() to expose Request -> Response functionality #1688

Merged
merged 2 commits into from Dec 22, 2015

Conversation

Projects
None yet
6 participants
@AndrewCarterUK
Contributor

AndrewCarterUK commented Dec 18, 2015

The Problem

It's currently very difficult to give the application a request and retrieve a response. To have a request that isn't created from the PHP super globals you need to patch the container.

A project of mine (PHPFastCGI) has struggled to provide a clean adapter to the Slim framework for this reason.

The Solution

Split App::run() into two methods by creating App::process(). The latter acts as a simple map from request to response.

Downsides

The are two that I can think of:

  1. It adds a new method to the public API that needs to be maintained and supported.
  2. Confusion over the difference between App:process() and App::__invoke(). Both have the same signature and do similar things, but ultimately at a different level.

Questions

Assuming that people think this is a good idea:

  • How would you like me to test this? I haven't really added any features. Any test of App::run() now covers App:process(). I'm not adverse to adding unit tests, I just thought it was worth asking here what people think they should look like rather than just adding something pointless for the sake of it.
  • Would you like me to add documentation (and where)?

Interested to hear thoughts :)

Show outdated Hide outdated Slim/App.php

@geggleto geggleto referenced this pull request Dec 18, 2015

Open

Slim 4.0 Road Map #1686

@geggleto

This comment has been minimized.

Show comment
Hide comment
@geggleto

geggleto Dec 18, 2015

Contributor

I don't see the problem you are solving with this PR. Is there something that __invoke doesn't do that you need?

To have a request that isn't created from the PHP super globals you need to patch the container. totally agree with you. https://github.com/geggleto/reactive-slim/blob/master/bin/app.php such a hack :)

Contributor

geggleto commented Dec 18, 2015

I don't see the problem you are solving with this PR. Is there something that __invoke doesn't do that you need?

To have a request that isn't created from the PHP super globals you need to patch the container. totally agree with you. https://github.com/geggleto/reactive-slim/blob/master/bin/app.php such a hack :)

@mathmarques

This comment has been minimized.

Show comment
Hide comment
@mathmarques

mathmarques Dec 18, 2015

Contributor

When using directly __invoke, you are ignoring the app level middlewares. I think this is the only problem.

Contributor

mathmarques commented Dec 18, 2015

When using directly __invoke, you are ignoring the app level middlewares. I think this is the only problem.

@geggleto

This comment has been minimized.

Show comment
Hide comment
@geggleto

geggleto Dec 18, 2015

Contributor

I think we should fix that...

Contributor

geggleto commented Dec 18, 2015

I think we should fix that...

@AndrewCarterUK

This comment has been minimized.

Show comment
Hide comment
@AndrewCarterUK

AndrewCarterUK Dec 18, 2015

Contributor

@geggleto

__invoke() also doesn't have the exception handling logic. If __invoke() worked in such a way that doing something like:

$request = Request::createFromGlobals();
$response = $app($request, $response);
$app->send($response);

Was the same as doing:

$app->run();

Then that would solve the problem.

(Edit) - But that sort of change couldn't be made until 4.x because it would break B/C

Contributor

AndrewCarterUK commented Dec 18, 2015

@geggleto

__invoke() also doesn't have the exception handling logic. If __invoke() worked in such a way that doing something like:

$request = Request::createFromGlobals();
$response = $app($request, $response);
$app->send($response);

Was the same as doing:

$app->run();

Then that would solve the problem.

(Edit) - But that sort of change couldn't be made until 4.x because it would break B/C

@geggleto

This comment has been minimized.

Show comment
Hide comment
@geggleto

geggleto Dec 18, 2015

Contributor

@AndrewCarterUK there is 1 other way to do this.

$res = $app->run(true);

https://github.com/slimphp/Slim/blob/3.x/tests/AppTest.php#L1558-L1592

Contributor

geggleto commented Dec 18, 2015

@AndrewCarterUK there is 1 other way to do this.

$res = $app->run(true);

https://github.com/slimphp/Slim/blob/3.x/tests/AppTest.php#L1558-L1592

@AndrewCarterUK

This comment has been minimized.

Show comment
Hide comment
@AndrewCarterUK

AndrewCarterUK Dec 18, 2015

Contributor

@geggleto That only works if you've patched the container or want the request to be created from global variables.

In my project the request is received from a FastCGI connection.

(edit to clarify: That's not PHP-FPM, the application runs as its own FastCGI daemon)

Contributor

AndrewCarterUK commented Dec 18, 2015

@geggleto That only works if you've patched the container or want the request to be created from global variables.

In my project the request is received from a FastCGI connection.

(edit to clarify: That's not PHP-FPM, the application runs as its own FastCGI daemon)

@akrabat akrabat merged commit 8bc17f1 into slimphp:3.x Dec 22, 2015

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.01%) to 89.683%
Details

akrabat added a commit that referenced this pull request Dec 22, 2015

@akrabat

This comment has been minimized.

Show comment
Hide comment
@akrabat

akrabat Dec 22, 2015

Member

Looks good to me and maybe is that start of removing request and response from the container.

Member

akrabat commented Dec 22, 2015

Looks good to me and maybe is that start of removing request and response from the container.

@akrabat akrabat added this to the 3.1.0 milestone Dec 22, 2015

@akrabat akrabat added the improvement label Jan 1, 2016

@adambro

This comment has been minimized.

Show comment
Hide comment
@adambro

adambro Jan 8, 2016

Contributor

The same problem is for creating WebTest class that mocks Slim, creates a request and checks response object. For now I'm using __invoke which works, because I don't have any middleware yet.

Contributor

adambro commented Jan 8, 2016

The same problem is for creating WebTest class that mocks Slim, creates a request and checks response object. For now I'm using __invoke which works, because I don't have any middleware yet.

@stivni

This comment has been minimized.

Show comment
Hide comment
@stivni

stivni Jul 14, 2016

Looks good to me and maybe is that start of removing request and response from the container.

@akrabat is there an issue for that?

stivni commented Jul 14, 2016

Looks good to me and maybe is that start of removing request and response from the container.

@akrabat is there an issue for that?

@geggleto

This comment has been minimized.

Show comment
Hide comment
@geggleto

geggleto Jul 14, 2016

Contributor

@stivni its a 4.0 thing ;)

Contributor

geggleto commented Jul 14, 2016

@stivni its a 4.0 thing ;)

rivetchip pushed a commit to rivetchip/Slim that referenced this pull request Jan 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment