Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ec36c71

Browse filesBrowse files
committed
Add new Template reference class
1 parent e58be70 commit ec36c71
Copy full SHA for ec36c71

File tree

4 files changed

+243
-0
lines changed
Filter options

4 files changed

+243
-0
lines changed
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
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+
12+
namespace Symfony\Bundle\FrameworkBundle\EventListener;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15+
use Symfony\Bundle\FrameworkBundle\Templating\Template;
16+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
18+
use Symfony\Component\HttpKernel\KernelEvents;
19+
20+
/**
21+
* Listener to convert a template reference to a Response.
22+
*
23+
* @author Pierre du Plessis <pdples@gmail.com>
24+
*/
25+
class TemplateListener implements EventSubscriberInterface
26+
{
27+
private $templating;
28+
29+
public function __construct(EngineInterface $templating)
30+
{
31+
$this->templating = $templating;
32+
}
33+
34+
public static function getSubscribedEvents()
35+
{
36+
return array(
37+
KernelEvents::VIEW => array('onView', 128),
38+
);
39+
}
40+
41+
public function onView(GetResponseForControllerResultEvent $event)
42+
{
43+
$result = $event->getControllerResult();
44+
45+
if (!$result instanceof Template) {
46+
return;
47+
}
48+
49+
$response = $this->templating->renderResponse($result->getTemplate(), $result->getParameters());
50+
51+
if ($statusCode = $result->getStatusCode()) {
52+
$response->setStatusCode($statusCode);
53+
}
54+
55+
if ($headers = $result->getHeaders()) {
56+
$response->headers->add($headers);
57+
}
58+
59+
$event->setResponse($response);
60+
}
61+
}

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,10 @@
4646
</service>
4747

4848
<service id="templating.loader" alias="templating.loader.filesystem" />
49+
50+
<service id="template_listener" class="Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener">
51+
<tag name="kernel.event_subscriber" />
52+
<argument type="service" id="templating" />
53+
</service>
4954
</services>
5055
</container>
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
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+
12+
namespace Symfony\Bundle\FrameworkBundle\Templating;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
/**
17+
* Represents a template reference.
18+
*
19+
* @author Pierre du Plessis <pdples@gmail.com>
20+
*/
21+
class Template
22+
{
23+
private $template;
24+
25+
private $parameters;
26+
27+
private $statusCode;
28+
29+
private $headers;
30+
31+
public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array())
32+
{
33+
$this->template = $template;
34+
$this->parameters = $parameters;
35+
$this->statusCode = $statusCode;
36+
$this->headers = $headers;
37+
}
38+
39+
public function getTemplate()
40+
{
41+
return $this->template;
42+
}
43+
44+
public function getParameters()
45+
{
46+
return $this->parameters;
47+
}
48+
49+
public function getStatusCode()
50+
{
51+
return $this->statusCode;
52+
}
53+
54+
public function getHeaders()
55+
{
56+
return $this->headers;
57+
}
58+
}
+119Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
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+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\EventListener;
13+
14+
use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener;
15+
use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
16+
use Symfony\Bundle\FrameworkBundle\Templating\Template;
17+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
18+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
19+
use Symfony\Component\DependencyInjection\Container;
20+
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
22+
use Symfony\Component\HttpKernel\Kernel;
23+
use Symfony\Component\Templating\Loader\Loader;
24+
use Symfony\Component\Templating\Storage\StringStorage;
25+
use Symfony\Component\Templating\TemplateNameParser;
26+
use Symfony\Component\Templating\TemplateReferenceInterface;
27+
28+
class TemplateListenerTest extends TestCase
29+
{
30+
public function testTemplateReference()
31+
{
32+
$template = new Template('dummy_template.html.php', array('var' => 'dummy'));
33+
34+
$event = $this->getEvent($template);
35+
36+
$listener = new TemplateListener($this->getPhpEngine());
37+
$listener->onView($event);
38+
39+
$response = $event->getResponse();
40+
41+
$this->assertSame('This is dummy content', $response->getContent());
42+
$this->assertSame(200, $response->getStatusCode());
43+
}
44+
45+
public function testTemplateReferenceWithStatusCode()
46+
{
47+
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 404);
48+
49+
$event = $this->getEvent($template);
50+
51+
$listener = new TemplateListener($this->getPhpEngine());
52+
$listener->onView($event);
53+
54+
$response = $event->getResponse();
55+
56+
$this->assertSame('This is dummy content', $response->getContent());
57+
$this->assertSame(404, $response->getStatusCode());
58+
}
59+
60+
public function testTemplateReferenceWithHeaders()
61+
{
62+
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 200, array('content-type' => 'application/json'));
63+
64+
$event = $this->getEvent($template);
65+
66+
$listener = new TemplateListener($this->getPhpEngine());
67+
$listener->onView($event);
68+
69+
$response = $event->getResponse();
70+
71+
$this->assertSame('This is dummy content', $response->getContent());
72+
$this->assertSame(array('cache-control' => array('no-cache, private'), 'content-type' => array('application/json')), $response->headers->all());
73+
}
74+
75+
private function getEvent($template)
76+
{
77+
$request = new Request(array(), array(), array());
78+
$mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', ''));
79+
80+
return new GetResponseForControllerResultEvent($mockKernel, $request, Kernel::MASTER_REQUEST, $template);
81+
}
82+
83+
private function getPhpEngine()
84+
{
85+
$container = new Container();
86+
$loader = new ProjectTemplateLoader();
87+
88+
$loader->templates['dummy_template.html.php'] = 'This is <?= $var ?> content';
89+
90+
$engine = new PhpEngine(new TemplateNameParser(), $container, $loader);
91+
92+
return $engine;
93+
}
94+
}
95+
96+
class ProjectTemplateLoader extends Loader
97+
{
98+
public $templates = array();
99+
100+
public function setTemplate($name, $content)
101+
{
102+
$template = new TemplateReference($name, 'php');
103+
$this->templates[$template->getLogicalName()] = $content;
104+
}
105+
106+
public function load(TemplateReferenceInterface $template)
107+
{
108+
if (isset($this->templates[$template->getLogicalName()])) {
109+
return new StringStorage($this->templates[$template->getLogicalName()]);
110+
}
111+
112+
return false;
113+
}
114+
115+
public function isFresh(TemplateReferenceInterface $template, $time)
116+
{
117+
return false;
118+
}
119+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.