1<?php
2
3/*
4 * This file is part of the Silex framework.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Silex\Provider;
13
14use Symfony\Component\HttpFoundation\Request;
15use Symfony\Component\HttpKernel\HttpKernelInterface;
16
17/**
18 * Twig extension.
19 *
20 * @author Fabien Potencier <fabien@symfony.com>
21 *
22 * @deprecated deprecated since 1.2, will be removed in 1.3. Use HttpFragmentServiceProvider instead
23 */
24class TwigCoreExtension extends \Twig_Extension
25{
26    public function getFunctions()
27    {
28        return array(
29            'render' => new \Twig_Function_Method($this, 'render', array('needs_environment' => true, 'is_safe' => array('html'))),
30        );
31    }
32
33    public function render(\Twig_Environment $twig, $uri)
34    {
35        $globals = $twig->getGlobals();
36        $request = $globals['app']['request'];
37
38        $subRequest = Request::create($uri, 'get', array(), $request->cookies->all(), array(), $request->server->all());
39        if ($request->getSession()) {
40            $subRequest->setSession($request->getSession());
41        }
42
43        $response = $globals['app']->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
44
45        if (!$response->isSuccessful()) {
46            throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
47        }
48
49        return $response->getContent();
50    }
51
52    public function getName()
53    {
54        return 'silex';
55    }
56}
57