zendframework / zend-stratigility

Middleware for PHP built on top of PSR-7 and PSR-15
BSD 3-Clause "New" or "Revised" License
235 stars 57 forks source link

Documented examples fail #164

Closed designermonkey closed 6 years ago

designermonkey commented 6 years ago

Here is the documented code:

use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\Server;

require __DIR__ . '/../vendor/autoload.php';

$app    = new MiddlewarePipe();
$server = Server::createServer($app,
  $_SERVER,
  $_GET,
  $_POST,
  $_COOKIE,
  $_FILES
);

$server->listen(function ($req, $res) {
  return $res;
});

This fails as $app is not callable.

Stratigility v3 and Diactoros v1.7.1

designermonkey commented 6 years ago

Changing the server creation to:

$server = Server::createServer([$app, 'handle'],
  $_SERVER,
  $_GET,
  $_POST,
  $_COOKIE,
  $_FILES
);

seems to work.

geerteltink commented 6 years ago

Good catch. The docs need a fix. The __invokable method has been removed in version 3. However it suggests to use the process() method. Not sure if there is a better way to do this than your example.

https://github.com/zendframework/zend-stratigility/blob/e8c413fcba926ede63099936a5f86acf9b8156c5/src/MiddlewarePipe.php#L75-L88

weierophinney commented 6 years ago

@xtreamwayz Either handle or process work; since process requires that the second argument be a request handler, however, handle is better in this situation. PHP will ignore any arguments the method does not accept, making it safe.