Skip to content

Commit

Permalink
StreamedResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikbjorn committed Dec 22, 2011
1 parent 1f66175 commit 06ceb81
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Configuration/Template.php
Expand Up @@ -42,6 +42,13 @@ class Template extends ConfigurationAnnotation
*/
protected $vars = array();

/**
* Should the template be streamed?
*
* @var Boolean
*/
protected $isStreamable = false;

/**
* Returns the array of templates variables.
*
Expand All @@ -52,6 +59,22 @@ public function getVars()
return $this->vars;
}

/**
* @param Boolean $streamable
*/
public function setIsStreamable($streamable)
{
$this->streamable = $streamable;
}

/**
* @return Boolean
*/
public function isStreamable()
{
return (Boolean) $this->streamable;
}

/**
* Sets the template variables
*
Expand Down
14 changes: 13 additions & 1 deletion EventListener/TemplateListener.php
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/*
Expand Down Expand Up @@ -64,6 +65,7 @@ public function onKernelController(FilterControllerEvent $event)

$request->attributes->set('_template', $configuration->getTemplate());
$request->attributes->set('_template_vars', $configuration->getVars());
$request->attribytes->set('_template_streamable', $configuration->isStreamable());

// all controller method arguments
if (!$configuration->getVars()) {
Expand All @@ -88,6 +90,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$parameters = $event->getControllerResult();
$templating = $this->container->get('templating');

if (null === $parameters) {
if (!$vars = $request->attributes->get('_template_vars')) {
Expand All @@ -110,6 +113,15 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
return $parameters;
}

$event->setResponse(new Response($this->container->get('templating')->render($template, $parameters)));
if (!$request->attributes->get('_template_streamable')) {
$event->setResponse($templating->renderResponse($template, $parameters));
} else {
$callback = function () use ($templating, $template, $parameters) {
return $templating->stream($template, $parameters);
};


$event->setResponse(new StreamedResponse($callback));
}
}
}

0 comments on commit 06ceb81

Please sign in to comment.