Skip to content

Instantly share code, notes, and snippets.

Created October 19, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/f276054e9866456021bc to your computer and use it in GitHub Desktop.
Save anonymous/f276054e9866456021bc to your computer and use it in GitHub Desktop.
Implementing decorators in middlewares to work with immutable Requests
<?php
namespace Acme;
use Symfony\Component\HttpKernel\HTTPKernelInterface;
use Psr\Http\Message\Request;
class MyMiddleware implements HTTPKernelInterface {
private $kernel;
public function __construct(HTTPKernelInterface $kernel)
{
$this->kernel = $kernel;
}
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
return $this->kernel->handle(
new MyRequestDecorator($request),
$type,
$catch
)
}
}
<?php
namespace Acme;
use Psr\Http\Message\Request;
class MyRequestDecorator implements Request {
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function getQueryParams()
{
return ['foo' => 'bar'];
}
// Act as a proxy for all other methods...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment