spore
spore copied to clipboard
Generic Request Object
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
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/
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?
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?
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.
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"));
Interesting idea Markus
I'm going to be looking closer at this on the weekend. Thanks!