class HttpKernel extends BaseHttpKernel {
protected $container;
private $esiSupport;
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver) {
parent::__construct($dispatcher, $controllerResolver);
$this->container = $container;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) {
$request->headers
->set('X-Php-Ob-Level', ob_get_level());
$this->container
->enterScope('request');
$this->container
->set('request', $request, 'request');
try {
$response = parent::handle($request, $type, $catch);
} catch (\Exception $e) {
$this->container
->leaveScope('request');
throw $e;
}
$this->container
->leaveScope('request');
return $response;
}
public function forward($controller, array $attributes = array(), array $query = array()) {
$attributes['_controller'] = $controller;
$subRequest = $this->container
->get('request')
->duplicate($query, null, $attributes);
return $this
->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
public function render($controller, array $options = array()) {
$options = array_merge(array(
'attributes' => array(),
'query' => array(),
'ignore_errors' => !$this->container
->getParameter('kernel.debug'),
'alt' => array(),
'standalone' => false,
'comment' => '',
), $options);
if (!is_array($options['alt'])) {
$options['alt'] = array(
$options['alt'],
);
}
if (null === $this->esiSupport) {
$this->esiSupport = $this->container
->has('esi') && $this->container
->get('esi')
->hasSurrogateEsiCapability($this->container
->get('request'));
}
if ($this->esiSupport && (true === $options['standalone'] || 'esi' === $options['standalone'])) {
$uri = $this
->generateInternalUri($controller, $options['attributes'], $options['query']);
$alt = '';
if ($options['alt']) {
$alt = $this
->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array());
}
return $this->container
->get('esi')
->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']);
}
if ('js' === $options['standalone']) {
$uri = $this
->generateInternalUri($controller, $options['attributes'], $options['query'], false);
$defaultContent = null;
if ($template = $this->container
->getParameter('templating.hinclude.default_template')) {
$defaultContent = $this->container
->get('templating')
->render($template);
}
return $this
->renderHIncludeTag($uri, $defaultContent);
}
$request = $this->container
->get('request');
if (0 === strpos($controller, '/')) {
$subRequest = Request::create($request
->getUriForPath($controller), 'get', array(), $request->cookies
->all(), array(), $request->server
->all());
if ($session = $request
->getSession()) {
$subRequest
->setSession($session);
}
}
else {
$options['attributes']['_controller'] = $controller;
if (!isset($options['attributes']['_format'])) {
$options['attributes']['_format'] = $request
->getRequestFormat();
}
$options['attributes']['_route'] = '_internal';
$subRequest = $request
->duplicate($options['query'], null, $options['attributes']);
$subRequest
->setMethod('GET');
}
$level = ob_get_level();
try {
$response = $this
->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
if (!$response
->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request
->getUri(), $response
->getStatusCode()));
}
if (!$response instanceof StreamedResponse) {
return $response
->getContent();
}
$response
->sendContent();
} catch (\Exception $e) {
if ($options['alt']) {
$alt = $options['alt'];
unset($options['alt']);
$options['attributes'] = isset($alt[1]) ? $alt[1] : array();
$options['query'] = isset($alt[2]) ? $alt[2] : array();
return $this
->render($alt[0], $options);
}
if (!$options['ignore_errors']) {
throw $e;
}
while (ob_get_level() > $level) {
ob_get_clean();
}
}
}
public function generateInternalUri($controller, array $attributes = array(), array $query = array(), $secure = true) {
if (0 === strpos($controller, '/')) {
return $controller;
}
$path = http_build_query($attributes, '', '&');
$uri = $this->container
->get('router')
->generate($secure ? '_internal' : '_internal_public', array(
'controller' => $controller,
'path' => $path ?: 'none',
'_format' => $this->container
->get('request')
->getRequestFormat(),
));
if ($queryString = http_build_query($query, '', '&')) {
$uri .= '?' . $queryString;
}
return $uri;
}
public function renderHIncludeTag($uri, $defaultContent = null) {
return sprintf('<hx:include src="%s">%s</hx:include>', $uri, $defaultContent);
}
public function hasEsiSupport() {
return $this->esiSupport;
}
}