Dispatcher is a bit more complicated (I knocked it together in 10 minuets,
it could be refined) but what I was referring to was the middleware
implementation e.g. the timer example I provided.

I'm not going to aggressively push this idea, I just thought it was worth
floating.


On Aug 2, 2017 6:01 PM, "Woody Gilk" <woody.g...@gmail.com> wrote:

> This would require >= PHP 7

That's a non-starter. There are plenty of people stuck on PHP 5.x for
various reasons and I don't want to shut them out.

> you loose some type hinting

Also a non-starter for me, static typing is good and we should keep it.

Your dispatching example is pretty complicated, I don't think it "reduces
mental overhead" at all. Compare to:

https://github.com/equip/dispatch/blob/d517cae02d97e6db354a9c20037e8f
8bf9d6cd40/src/Delegate.php#L45-L49

--
Woody Gilk
http://about.me/shadowhand

On Wed, Aug 2, 2017 at 10:52 AM, Simon Holloway <holloway...@gmail.com>
wrote:

> I haven't been keeping up with this mailing list so maybe this has already
> come up and I missed it.
>
> Did anyone consider using generators/yield instead of a delegate/frame?
>
> So instead of:
>
> interface MiddlewareInterface
> {
>     public function process(
>         ServerRequestInterface $request,
>         DelegateInterface $delegate
>     ): ResponseInterface
> }
>
>
> <https://github.com/php-fig/fig-standards/blob/master/proposed/http-middleware/middleware.md#22-psrhttpservermiddlewaredelegateinterface>
>
> class Timer implements MiddlewareInterface
> {
>     public function process(ServerRequestInterface $request, 
> DelegateInterface $delegate)
>     {
>         $startTime = microtime(true);
>
>         $response = $delegate->process($request);
>         return $response->withHeader('X-Timer', sprintf('%2.3fms', 
> (microtime(true) - $startTime) * 1000));
>
>    }
> }
>
>
> We could do
>
> interface MiddlewareInterface
> {
>     public function process(ServerRequestInterface $request): Generator
> }
>
>
> <https://github.com/php-fig/fig-standards/blob/master/proposed/http-middleware/middleware.md#22-psrhttpservermiddlewaredelegateinterface>
>
> class Timer implements MiddlewareInterface
> {
>     public function process(ServerRequestInterface $request)
>     {
>         $startTime = microtime(true);
>
>         $response = (yield $request);
>         return $response->withHeader('X-Timer', sprintf('%2.3fms', 
> (microtime(true) - $startTime) * 1000));
>
>    }
> }
>
>
> Then to dispatch middleware, you would do the following:
>
> function dispatch(ServerRequestInterface $request, ResponseInterface 
> $response,
> array $middleware)
> {
>     $postProcessors = [];
>     foreach ($middleware as $middleware) {
>         $frame = $middleware($request);
>         if ($frame->valid()) {
>             $request = $frame->current();
>             $postProcessors[] = $frame;
>         } else {
>             $response = $frame->getResponse();
>         }
>     }
>
>     $postProcessors = array_reverse($postProcessors);
>     foreach ($postProcessors as $frame) {
>         $frame->send($response);
>         $response = $frame->getReturn();
>     }
>     return $response;
> }
>
>
> This would require >= PHP 7 due to needing Generator::getReturn()
> <http://php.net/manual/en/generator.getreturn.php> and you loose some
> type hinting. Terminating middleware has to be a bit hacky, it would have
> to use `yield from []` to create an empty generator, then return a
> response. Also no catching thrown objects further down the app.
>
> On the positive side, I think it reduces the mental overhead a bit, makes
> PSR-15 only 1 interface, keeps middleware out of stack traces, and promotes
> a great feature of PHP.
>
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to php-fig+unsubscr...@googlegroups.com.
>
> To post to this group, send email to php-fig@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/ms
> gid/php-fig/8303b4eb-38af-4e2a-b63a-b0f45eafa697%40googlegroups.com
> <https://groups.google.com/d/msgid/php-fig/8303b4eb-38af-4e2a-b63a-b0f45eafa697%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to a topic in the
Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/
topic/php-fig/lkpuaZPcBsY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
php-fig+unsubscr...@googlegroups.com.
To post to this group, send email to php-fig@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/
msgid/php-fig/CAGOJM6%2Bsd%3DrkLQnmUQ_QN6ufqQEwUZiE6_
9VUeLN3XX3MSwezw%40mail.gmail.com
<https://groups.google.com/d/msgid/php-fig/CAGOJM6%2Bsd%3DrkLQnmUQ_QN6ufqQEwUZiE6_9VUeLN3XX3MSwezw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.

For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to php-fig+unsubscr...@googlegroups.com.
To post to this group, send email to php-fig@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/CAErSA%3DYAEA9wLUegwcp_eWFqmKzoE-PGSLgXbcZu_fDQ2wwhVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to