Skip to content

Instantly share code, notes, and snippets.

@CarsonF
Created January 14, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarsonF/677484c37723fd92f5c5 to your computer and use it in GitHub Desktop.
Save CarsonF/677484c37723fd92f5c5 to your computer and use it in GitHub Desktop.
Subclassing Response for Twig
<?php
namespace GMO\Common\Web\Twig;
use GMO\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Response;
class TwigResponse extends Response {
public function setTemplate($template) {
$this->template = $template;
return $this;
}
public function getTemplate() {
return $this->template;
}
public function getVariables() {
return $this->variables;
}
public function setVariables($variables) {
$this->variables = new ArrayCollection($variables);
return $this;
}
/**
* @param string $name
* @param mixed $value
* @return $this
*/
public function addVariable($name, $value) {
$this->variables->set($name, $value);
return $this;
}
/**
* @param ArrayCollection|array $values
* @return $this
*/
public function addVariables($values) {
$this->variables->replace($values);
return $this;
}
/**
* Constructor.
*
* @param string $template The name of the template
* @param ArrayCollection|array $variables The variables for the template
* @param int $status The response status code
* @param array $headers An array of response headers
*
* @throws \InvalidArgumentException When the HTTP status code is not valid
*
* @api
*/
public function __construct($template, $variables = array(), $status = 200, $headers = array()) {
$this->template = $template;
$this->variables = new ArrayCollection($variables);
parent::__construct('', $status, $headers);
}
protected $template;
/** @var ArrayCollection */
protected $variables;
}
<?php
namespace GMO\Common\Web\Twig;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class TwigResponseServiceProvider implements ServiceProviderInterface, EventSubscriberInterface {
public function onResponse(FilterResponseEvent $event) {
$response = $event->getResponse();
if (!$response instanceof TwigResponse) {
return;
}
/** @var \Twig_Environment $twig */
$twig = $this->app['twig'];
$content = $twig->render($response->getTemplate(), $response->getVariables()->toArray());
/*
* Since content has been rendered it isn't a TwigResponse anymore.
* This prevents instanceof TwigResponse checks.
*/
$newResponse = new Response($content, $response->getStatusCode());
$newResponse->headers = $response->headers;
$event->setResponse($newResponse);
}
public function register(Application $app) {
$this->app = $app;
}
public function boot(Application $app) {
$app['dispatcher']->addSubscriber($this);
}
/** @inheritdoc */
public static function getSubscribedEvents() {
return array(
KernelEvents::RESPONSE => array('onResponse', -100),
);
}
/** @var Application */
protected $app;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment