spore icon indicating copy to clipboard operation
spore copied to clipboard

Generic Request Object

Open mathielen opened this issue 11 years ago • 6 comments

Next one :) A request-wrapper is instantiated in Spore\ReST\AutoRoute\Router. What do you think about using a factory here? Thus there would be the possibility to use a custom request-wrapper (that implements some request-wrapper-interface?).

In my case, I would like to have a request object that has a getUsername() method (that actually returns the PHP_AUTH_USER header. But maybe its also nice to have the factory decide what "kind" of Request the current request is. So it might create an AnonymousRequest or a SomeSpecialHeaderInformationRequest and so on...

What do you think?

All the best, Markus

mathielen avatar Feb 24 '13 19:02 mathielen

That's a great idea! I think adding some basic dependency injection is worth it, and i can think of a couple use-cases for this suggestion.

Perhaps something like Pimple? http://pimple.sensiolabs.org/

dannykopping avatar Feb 24 '13 21:02 dannykopping

I am using the Symfony Component for that purpose. But I dont think spore needs a dependency to any DIC, right? Maybe just a new constructor argument to the Router (the reference to the Request/Response Factory) and the job is done?

mathielen avatar Feb 24 '13 21:02 mathielen

Hhmm, yeah - or maybe a configuration option where you can set the Request/Response class? For example:

$s = new Spore(array("requestClass" => "MyCustomReqClass"));
class MyCustomReqClass extends Request
{
  public function doSomethingSpecial()
  {
     // ...
  }
}

That way you'll be able to override the Request class without having to modify any of the internals. This same principle can be rolled out to a few of the other classes like Response, Router, etc...

Thoughts?

dannykopping avatar Feb 24 '13 21:02 dannykopping

Sorry, I'm a little sleepy :)

I'm basically just restating what you initially suggested. What I mentioned above will basically make it a Factory-based architecture, which is exactly what's needed.

dannykopping avatar Feb 24 '13 21:02 dannykopping

No problem, i am too... working way too much :(

The static approach to configure the classname would be enough for my usecase (the getUsername() method). But wouldnt it be great to have multiple Request classes depending on the actual HTTP-Request values?

Something like

class MySpecialRequestFactory implements Spore\ReST\AutoRoute\RequestFactory
{
  public function factor(\Slim\Route $route, $params)
  {
    if (thereAreSomeSpecialHeadersSet())
      return new MyApp\SpecialHeaderRequest();
    else
      return new Spore\ReST\Model\Request();
  }  
}

And in Spore\ReST\AutoRoute\Router:

    public function __construct(Spore $app, Spore\ReST\AutoRoute\RequestFactory $reqFac)

So the factory classname might become the configuration. I think that was what you meant, right? :)

$s = new Spore(array("requestFactory" => "MySpecialRequestFactory"));

mathielen avatar Feb 24 '13 21:02 mathielen

Interesting idea Markus

I'm going to be looking closer at this on the weekend. Thanks!

dannykopping avatar Feb 26 '13 20:02 dannykopping