dannykopping / spore

ReSTful extensions for Slim
29 stars 2 forks source link

Generic Request Object #15

Open mathielen opened 11 years ago

mathielen commented 11 years ago

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

dannykopping commented 11 years ago

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/

mathielen commented 11 years ago

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?

dannykopping commented 11 years ago

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 commented 11 years ago

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.

mathielen commented 11 years ago

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"));
dannykopping commented 11 years ago

Interesting idea Markus

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