From ec36c7169b0f46b815c994328332887fa48907cb Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Sun, 26 Feb 2017 09:41:46 +0200 Subject: [PATCH 1/9] Add new Template reference class --- .../EventListener/TemplateListener.php | 61 +++++++++ .../Resources/config/templating.xml | 5 + .../FrameworkBundle/Templating/Template.php | 58 +++++++++ .../EventListener/TemplateListenerTest.php | 119 ++++++++++++++++++ 4 files changed, 243 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Templating/Template.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php new file mode 100644 index 000000000000..df9a5619a90e --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\EventListener; + +use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; +use Symfony\Bundle\FrameworkBundle\Templating\Template; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; +use Symfony\Component\HttpKernel\KernelEvents; + +/** + * Listener to convert a template reference to a Response. + * + * @author Pierre du Plessis + */ +class TemplateListener implements EventSubscriberInterface +{ + private $templating; + + public function __construct(EngineInterface $templating) + { + $this->templating = $templating; + } + + public static function getSubscribedEvents() + { + return array( + KernelEvents::VIEW => array('onView', 128), + ); + } + + public function onView(GetResponseForControllerResultEvent $event) + { + $result = $event->getControllerResult(); + + if (!$result instanceof Template) { + return; + } + + $response = $this->templating->renderResponse($result->getTemplate(), $result->getParameters()); + + if ($statusCode = $result->getStatusCode()) { + $response->setStatusCode($statusCode); + } + + if ($headers = $result->getHeaders()) { + $response->headers->add($headers); + } + + $event->setResponse($response); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml index d4d0053c4e4b..d197573f72f3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml @@ -46,5 +46,10 @@ + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Template.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Template.php new file mode 100644 index 000000000000..6bbde5d700d8 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Template.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Templating; + +use Symfony\Component\HttpFoundation\Response; + +/** + * Represents a template reference. + * + * @author Pierre du Plessis + */ +class Template +{ + private $template; + + private $parameters; + + private $statusCode; + + private $headers; + + public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array()) + { + $this->template = $template; + $this->parameters = $parameters; + $this->statusCode = $statusCode; + $this->headers = $headers; + } + + public function getTemplate() + { + return $this->template; + } + + public function getParameters() + { + return $this->parameters; + } + + public function getStatusCode() + { + return $this->statusCode; + } + + public function getHeaders() + { + return $this->headers; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php new file mode 100644 index 000000000000..aab448b204a5 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php @@ -0,0 +1,119 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\EventListener; + +use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener; +use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; +use Symfony\Bundle\FrameworkBundle\Templating\Template; +use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; +use Symfony\Bundle\FrameworkBundle\Tests\TestCase; +use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Templating\Loader\Loader; +use Symfony\Component\Templating\Storage\StringStorage; +use Symfony\Component\Templating\TemplateNameParser; +use Symfony\Component\Templating\TemplateReferenceInterface; + +class TemplateListenerTest extends TestCase +{ + public function testTemplateReference() + { + $template = new Template('dummy_template.html.php', array('var' => 'dummy')); + + $event = $this->getEvent($template); + + $listener = new TemplateListener($this->getPhpEngine()); + $listener->onView($event); + + $response = $event->getResponse(); + + $this->assertSame('This is dummy content', $response->getContent()); + $this->assertSame(200, $response->getStatusCode()); + } + + public function testTemplateReferenceWithStatusCode() + { + $template = new Template('dummy_template.html.php', array('var' => 'dummy'), 404); + + $event = $this->getEvent($template); + + $listener = new TemplateListener($this->getPhpEngine()); + $listener->onView($event); + + $response = $event->getResponse(); + + $this->assertSame('This is dummy content', $response->getContent()); + $this->assertSame(404, $response->getStatusCode()); + } + + public function testTemplateReferenceWithHeaders() + { + $template = new Template('dummy_template.html.php', array('var' => 'dummy'), 200, array('content-type' => 'application/json')); + + $event = $this->getEvent($template); + + $listener = new TemplateListener($this->getPhpEngine()); + $listener->onView($event); + + $response = $event->getResponse(); + + $this->assertSame('This is dummy content', $response->getContent()); + $this->assertSame(array('cache-control' => array('no-cache, private'), 'content-type' => array('application/json')), $response->headers->all()); + } + + private function getEvent($template) + { + $request = new Request(array(), array(), array()); + $mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', '')); + + return new GetResponseForControllerResultEvent($mockKernel, $request, Kernel::MASTER_REQUEST, $template); + } + + private function getPhpEngine() + { + $container = new Container(); + $loader = new ProjectTemplateLoader(); + + $loader->templates['dummy_template.html.php'] = 'This is content'; + + $engine = new PhpEngine(new TemplateNameParser(), $container, $loader); + + return $engine; + } +} + +class ProjectTemplateLoader extends Loader +{ + public $templates = array(); + + public function setTemplate($name, $content) + { + $template = new TemplateReference($name, 'php'); + $this->templates[$template->getLogicalName()] = $content; + } + + public function load(TemplateReferenceInterface $template) + { + if (isset($this->templates[$template->getLogicalName()])) { + return new StringStorage($this->templates[$template->getLogicalName()]); + } + + return false; + } + + public function isFresh(TemplateReferenceInterface $template, $time) + { + return false; + } +} From f9e2e6ebe4813670e891c57b4532069fffd510f7 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 27 Feb 2017 08:40:38 +0200 Subject: [PATCH 2/9] Add TemplateResponse class to get a response from a template reference --- .../EventListener/TemplateListener.php | 17 +++---- .../Resources/config/templating.xml | 2 +- .../{Template.php => TemplatedResponse.php} | 37 ++++---------- .../Templating/TemplatedResponseInterface.php | 27 ++++++++++ .../EventListener/TemplateListenerTest.php | 35 +++++-------- .../Templating/TemplatedResponseTest.php | 49 +++++++++++++++++++ 6 files changed, 105 insertions(+), 62 deletions(-) rename src/Symfony/Bundle/FrameworkBundle/Templating/{Template.php => TemplatedResponse.php} (52%) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php index df9a5619a90e..303b33768777 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php @@ -12,14 +12,13 @@ namespace Symfony\Bundle\FrameworkBundle\EventListener; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; -use Symfony\Bundle\FrameworkBundle\Templating\Template; +use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\HttpKernel\KernelEvents; /** - * Listener to convert a template reference to a Response. - * * @author Pierre du Plessis */ class TemplateListener implements EventSubscriberInterface @@ -42,18 +41,16 @@ public function onView(GetResponseForControllerResultEvent $event) { $result = $event->getControllerResult(); - if (!$result instanceof Template) { + if (!$result instanceof TemplatedResponseInterface) { return; } - $response = $this->templating->renderResponse($result->getTemplate(), $result->getParameters()); + $response = $result->getResponse($this->templating); - if ($statusCode = $result->getStatusCode()) { - $response->setStatusCode($statusCode); - } + if (!$response instanceof Response) { + $msg = sprintf('The method %s::getResponse() must return a response (%s given).', get_class($result), is_object($response) ? get_class($response) : gettype($response)); - if ($headers = $result->getHeaders()) { - $response->headers->add($headers); + throw new \LogicException($msg); } $event->setResponse($response); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml index d197573f72f3..65bf9d52d4a8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml @@ -47,7 +47,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Template.php b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php similarity index 52% rename from src/Symfony/Bundle/FrameworkBundle/Templating/Template.php rename to src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php index 6bbde5d700d8..5afe9ccb5e8a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Template.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php @@ -14,45 +14,26 @@ use Symfony\Component\HttpFoundation\Response; /** - * Represents a template reference. - * * @author Pierre du Plessis */ -class Template +class TemplatedResponse implements TemplatedResponseInterface { private $template; - private $parameters; + private $response; - private $statusCode; - - private $headers; - - public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array()) + public function __construct($template, array $parameters = array(), Response $response = null) { $this->template = $template; $this->parameters = $parameters; - $this->statusCode = $statusCode; - $this->headers = $headers; - } - - public function getTemplate() - { - return $this->template; - } - - public function getParameters() - { - return $this->parameters; - } - - public function getStatusCode() - { - return $this->statusCode; + $this->response = $response ?: new Response(); } - public function getHeaders() + /** + * {@inheritdoc} + */ + public function getResponse(EngineInterface $templating) { - return $this->headers; + return $templating->renderResponse($this->template, $this->parameters, $this->response); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php new file mode 100644 index 000000000000..d7bcd7121b83 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Templating; + +use Symfony\Component\HttpFoundation\Response; + +/** + * @author Pierre du Plessis + */ +interface TemplatedResponseInterface +{ + /** + * @param EngineInterface $templating + * + * @return Response + */ + public function getResponse(EngineInterface $templating); +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php index aab448b204a5..8c8ccd494621 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php @@ -13,8 +13,9 @@ use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener; use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; -use Symfony\Bundle\FrameworkBundle\Templating\Template; +use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; +use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; @@ -29,7 +30,7 @@ class TemplateListenerTest extends TestCase { public function testTemplateReference() { - $template = new Template('dummy_template.html.php', array('var' => 'dummy')); + $template = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy')); $event = $this->getEvent($template); @@ -42,34 +43,22 @@ public function testTemplateReference() $this->assertSame(200, $response->getStatusCode()); } - public function testTemplateReferenceWithStatusCode() + public function testInvalidResponse() { - $template = new Template('dummy_template.html.php', array('var' => 'dummy'), 404); + $templating = $this->getPhpEngine(); - $event = $this->getEvent($template); - - $listener = new TemplateListener($this->getPhpEngine()); - $listener->onView($event); + $template = $this->getMockBuilder(TemplatedResponseInterface::class)->getMock(); + $template->expects($this->once()) + ->method('getResponse') + ->with($templating) + ->will($this->throwException(new \LogicException())); - $response = $event->getResponse(); - - $this->assertSame('This is dummy content', $response->getContent()); - $this->assertSame(404, $response->getStatusCode()); - } - - public function testTemplateReferenceWithHeaders() - { - $template = new Template('dummy_template.html.php', array('var' => 'dummy'), 200, array('content-type' => 'application/json')); + $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException'); $event = $this->getEvent($template); - $listener = new TemplateListener($this->getPhpEngine()); + $listener = new TemplateListener($templating); $listener->onView($event); - - $response = $event->getResponse(); - - $this->assertSame('This is dummy content', $response->getContent()); - $this->assertSame(array('cache-control' => array('no-cache, private'), 'content-type' => array('application/json')), $response->headers->all()); } private function getEvent($template) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php new file mode 100644 index 000000000000..27887a0e012b --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Templating; + +use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; +use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse; +use Symfony\Bundle\FrameworkBundle\Tests\TestCase; +use Symfony\Component\HttpFoundation\Response; + +class TemplatedResponseTest extends TestCase +{ + public function testResponse() + { + $templating = $this->getMockBuilder(EngineInterface::class)->getMock(); + + $templating->expects($this->once()) + ->method('renderResponse') + ->with('dummy_template.html.php', array('var' => 'dummy')) + ->will($this->returnValue(new Response())); + + $templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy')); + + $this->assertInstanceOf(Response::class, $templateResponse->getResponse($templating)); + } + + public function testSameResponse() + { + $templating = $this->getMockBuilder(EngineInterface::class)->getMock(); + + $response = new Response(); + $templating->expects($this->once()) + ->method('renderResponse') + ->with('dummy_template.html.php', array('var' => 'dummy')) + ->will($this->returnValue($response)); + + $templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'), $response); + + $this->assertSame($response, $templateResponse->getResponse($templating)); + } +} From 0689f353279a2eaa480d3d187359ac86ed2b7e84 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 13:19:18 +0200 Subject: [PATCH 3/9] Update template listener to use Twig instead of the templating component --- .../EventListener/TemplateListener.php | 9 ++-- .../Resources/config/templating.xml | 2 +- .../Templating/TemplatedResponse.php | 6 ++- .../Templating/TemplatedResponseInterface.php | 4 +- .../EventListener/TemplateListenerTest.php | 54 ++----------------- .../Templating/TemplatedResponseTest.php | 17 +++--- .../Bundle/FrameworkBundle/composer.json | 1 + 7 files changed, 25 insertions(+), 68 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php index 303b33768777..6875998413ce 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php @@ -11,7 +11,6 @@ namespace Symfony\Bundle\FrameworkBundle\EventListener; -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Response; @@ -23,11 +22,11 @@ */ class TemplateListener implements EventSubscriberInterface { - private $templating; + private $twig; - public function __construct(EngineInterface $templating) + public function __construct(\Twig_Environment $twig) { - $this->templating = $templating; + $this->twig = $twig; } public static function getSubscribedEvents() @@ -45,7 +44,7 @@ public function onView(GetResponseForControllerResultEvent $event) return; } - $response = $result->getResponse($this->templating); + $response = $result->getResponse($this->twig); if (!$response instanceof Response) { $msg = sprintf('The method %s::getResponse() must return a response (%s given).', get_class($result), is_object($response) ? get_class($response) : gettype($response)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml index 65bf9d52d4a8..df13bd0a9537 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml @@ -49,7 +49,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php index 5afe9ccb5e8a..eecf70b28411 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php @@ -32,8 +32,10 @@ public function __construct($template, array $parameters = array(), Response $re /** * {@inheritdoc} */ - public function getResponse(EngineInterface $templating) + public function getResponse(\Twig_Environment $twig) { - return $templating->renderResponse($this->template, $this->parameters, $this->response); + $this->response->setContent($twig->render($this->template, $this->parameters)); + + return $this->response; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php index d7bcd7121b83..c257c4795da5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponseInterface.php @@ -19,9 +19,9 @@ interface TemplatedResponseInterface { /** - * @param EngineInterface $templating + * @param \Twig_Environment $twig * * @return Response */ - public function getResponse(EngineInterface $templating); + public function getResponse(\Twig_Environment $twig); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php index 8c8ccd494621..ec2ad127d2d5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php @@ -12,19 +12,12 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\EventListener; use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener; -use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine; use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse; -use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; -use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\Templating\Loader\Loader; -use Symfony\Component\Templating\Storage\StringStorage; -use Symfony\Component\Templating\TemplateNameParser; -use Symfony\Component\Templating\TemplateReferenceInterface; class TemplateListenerTest extends TestCase { @@ -34,7 +27,7 @@ public function testTemplateReference() $event = $this->getEvent($template); - $listener = new TemplateListener($this->getPhpEngine()); + $listener = new TemplateListener($this->getMockBuilder(\Twig_Environment::class)->getMock()); $listener->onView($event); $response = $event->getResponse(); @@ -45,19 +38,19 @@ public function testTemplateReference() public function testInvalidResponse() { - $templating = $this->getPhpEngine(); + $twig = $this->getMockBuilder(\Twig_Environment::class)->getMock(); $template = $this->getMockBuilder(TemplatedResponseInterface::class)->getMock(); $template->expects($this->once()) ->method('getResponse') - ->with($templating) + ->with($twig) ->will($this->throwException(new \LogicException())); $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException'); $event = $this->getEvent($template); - $listener = new TemplateListener($templating); + $listener = new TemplateListener($twig); $listener->onView($event); } @@ -68,41 +61,4 @@ private function getEvent($template) return new GetResponseForControllerResultEvent($mockKernel, $request, Kernel::MASTER_REQUEST, $template); } - - private function getPhpEngine() - { - $container = new Container(); - $loader = new ProjectTemplateLoader(); - - $loader->templates['dummy_template.html.php'] = 'This is content'; - - $engine = new PhpEngine(new TemplateNameParser(), $container, $loader); - - return $engine; - } -} - -class ProjectTemplateLoader extends Loader -{ - public $templates = array(); - - public function setTemplate($name, $content) - { - $template = new TemplateReference($name, 'php'); - $this->templates[$template->getLogicalName()] = $content; - } - - public function load(TemplateReferenceInterface $template) - { - if (isset($this->templates[$template->getLogicalName()])) { - return new StringStorage($this->templates[$template->getLogicalName()]); - } - - return false; - } - - public function isFresh(TemplateReferenceInterface $template, $time) - { - return false; - } -} +} \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php index 27887a0e012b..dd7467e08378 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php @@ -11,7 +11,6 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating; -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Component\HttpFoundation\Response; @@ -20,29 +19,29 @@ class TemplatedResponseTest extends TestCase { public function testResponse() { - $templating = $this->getMockBuilder(EngineInterface::class)->getMock(); + $templating = $this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock(); $templating->expects($this->once()) - ->method('renderResponse') - ->with('dummy_template.html.php', array('var' => 'dummy')) + ->method('render') + ->with('dummy_template.html.twig', array('var' => 'dummy')) ->will($this->returnValue(new Response())); - $templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy')); + $templateResponse = new TemplatedResponse('dummy_template.html.twig', array('var' => 'dummy')); $this->assertInstanceOf(Response::class, $templateResponse->getResponse($templating)); } public function testSameResponse() { - $templating = $this->getMockBuilder(EngineInterface::class)->getMock(); + $templating = $this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock(); $response = new Response(); $templating->expects($this->once()) - ->method('renderResponse') - ->with('dummy_template.html.php', array('var' => 'dummy')) + ->method('render') + ->with('dummy_template.html.twig', array('var' => 'dummy')) ->will($this->returnValue($response)); - $templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'), $response); + $templateResponse = new TemplatedResponse('dummy_template.html.twig', array('var' => 'dummy'), $response); $this->assertSame($response, $templateResponse->getResponse($templating)); } diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index d82fd511b3d6..bf4603c05911 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -17,6 +17,7 @@ ], "require": { "php": ">=5.5.9", + "twig/twig": "^1.32|^2.0", "symfony/cache": "~3.3", "symfony/class-loader": "~3.2", "symfony/dependency-injection": "~3.3", From 4019f25ed7a5d663d330abef732f53a238699023 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 13:22:45 +0200 Subject: [PATCH 4/9] Add Fabbot.io fixes --- .../Tests/EventListener/TemplateListenerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php index ec2ad127d2d5..b9c1a99578df 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php @@ -61,4 +61,4 @@ private function getEvent($template) return new GetResponseForControllerResultEvent($mockKernel, $request, Kernel::MASTER_REQUEST, $template); } -} \ No newline at end of file +} From a4e36ca43453c542154aa5d76b5ebd59c5c5f88d Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 13:38:04 +0200 Subject: [PATCH 5/9] Renamed listener to TwigTemplateListener, and register the service only when twig is available --- .../DependencyInjection/FrameworkExtension.php | 4 ++++ ...emplateListener.php => TwigTemplateListener.php} | 2 +- .../FrameworkBundle/Resources/config/templating.xml | 5 ----- .../FrameworkBundle/Resources/config/twig.xml | 13 +++++++++++++ .../Tests/EventListener/TemplateListenerTest.php | 6 +++--- 5 files changed, 21 insertions(+), 9 deletions(-) rename src/Symfony/Bundle/FrameworkBundle/EventListener/{TemplateListener.php => TwigTemplateListener.php} (96%) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/twig.xml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 259906e90fd4..a99e9385834f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -181,6 +181,10 @@ public function load(array $configs, ContainerBuilder $container) $this->registerTemplatingConfiguration($config['templating'], $container, $loader); } + if ($container->hasDefinition('twig')) { + $loader->load('twig.xml'); + } + $this->registerValidationConfiguration($config['validation'], $container, $loader); $this->registerEsiConfiguration($config['esi'], $container, $loader); $this->registerSsiConfiguration($config['ssi'], $container, $loader); diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TwigTemplateListener.php similarity index 96% rename from src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php rename to src/Symfony/Bundle/FrameworkBundle/EventListener/TwigTemplateListener.php index 6875998413ce..9e73e78d33dd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TwigTemplateListener.php @@ -20,7 +20,7 @@ /** * @author Pierre du Plessis */ -class TemplateListener implements EventSubscriberInterface +class TwigTemplateListener implements EventSubscriberInterface { private $twig; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml index df13bd0a9537..d4d0053c4e4b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml @@ -46,10 +46,5 @@ - - - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/twig.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/twig.xml new file mode 100644 index 000000000000..b68fb7586f5d --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/twig.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php index b9c1a99578df..77586a5e3636 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php @@ -11,7 +11,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\EventListener; -use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener; +use Symfony\Bundle\FrameworkBundle\EventListener\TwigTemplateListener; use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse; use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; @@ -27,7 +27,7 @@ public function testTemplateReference() $event = $this->getEvent($template); - $listener = new TemplateListener($this->getMockBuilder(\Twig_Environment::class)->getMock()); + $listener = new TwigTemplateListener($this->getMockBuilder(\Twig_Environment::class)->getMock()); $listener->onView($event); $response = $event->getResponse(); @@ -50,7 +50,7 @@ public function testInvalidResponse() $event = $this->getEvent($template); - $listener = new TemplateListener($twig); + $listener = new TwigTemplateListener($twig); $listener->onView($event); } From 6793c4dc584a432efa295d13a2e19cb8dabc6ff7 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 13:38:41 +0200 Subject: [PATCH 6/9] Remove twig from composer require since it's optional now to activate the listener --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 266e517918e0..e149e777daa1 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,6 @@ "require": { "php": ">=5.5.9", "doctrine/common": "~2.4", - "twig/twig": "~1.28|~2.0", "psr/cache": "~1.0", "psr/container": "^1.0", "psr/log": "~1.0", From 2c1dd08f2a550283431417a29287ed5635d25ca3 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 13:44:07 +0200 Subject: [PATCH 7/9] Oops, changed wrong composer.json :) --- composer.json | 1 + src/Symfony/Bundle/FrameworkBundle/composer.json | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e149e777daa1..266e517918e0 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require": { "php": ">=5.5.9", "doctrine/common": "~2.4", + "twig/twig": "~1.28|~2.0", "psr/cache": "~1.0", "psr/container": "^1.0", "psr/log": "~1.0", diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index bf4603c05911..d82fd511b3d6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -17,7 +17,6 @@ ], "require": { "php": ">=5.5.9", - "twig/twig": "^1.32|^2.0", "symfony/cache": "~3.3", "symfony/class-loader": "~3.2", "symfony/dependency-injection": "~3.3", From 92867c24e6d60021d1dd5a9dac76852679c854a4 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 13:58:17 +0200 Subject: [PATCH 8/9] Update test name and set mock methods for twig in test --- ...eListenerTest.php => TwigTemplateListenerTest.php} | 11 ++++++++--- .../Tests/Templating/TemplatedResponseTest.php | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) rename src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/{TemplateListenerTest.php => TwigTemplateListenerTest.php} (84%) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php similarity index 84% rename from src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php rename to src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php index 77586a5e3636..07d8fbb39241 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php @@ -19,7 +19,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\HttpKernel\Kernel; -class TemplateListenerTest extends TestCase +class TwigTemplateListenerTest extends TestCase { public function testTemplateReference() { @@ -27,7 +27,12 @@ public function testTemplateReference() $event = $this->getEvent($template); - $listener = new TwigTemplateListener($this->getMockBuilder(\Twig_Environment::class)->getMock()); + $twig = $this->getMockBuilder('Twig_Environment')->getMock(); + $twig->expects($this->once()) + ->method('render') + ->willReturn('This is dummy content'); + + $listener = new TwigTemplateListener($twig); $listener->onView($event); $response = $event->getResponse(); @@ -38,7 +43,7 @@ public function testTemplateReference() public function testInvalidResponse() { - $twig = $this->getMockBuilder(\Twig_Environment::class)->getMock(); + $twig = $this->getMockBuilder('Twig_Environment')->getMock(); $template = $this->getMockBuilder(TemplatedResponseInterface::class)->getMock(); $template->expects($this->once()) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php index dd7467e08378..b6447c3d36c6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplatedResponseTest.php @@ -19,7 +19,7 @@ class TemplatedResponseTest extends TestCase { public function testResponse() { - $templating = $this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock(); + $templating = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock(); $templating->expects($this->once()) ->method('render') @@ -33,7 +33,7 @@ public function testResponse() public function testSameResponse() { - $templating = $this->getMockBuilder(\Twig_Environment::class)->disableOriginalConstructor()->getMock(); + $templating = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock(); $response = new Response(); $templating->expects($this->once()) From e2bee38b00e4edcabf2da2ba7c257e5b13697650 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 10 Mar 2017 14:05:21 +0200 Subject: [PATCH 9/9] Disable twig constructor in tests --- .../Tests/EventListener/TwigTemplateListenerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php index 07d8fbb39241..b87c406da588 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TwigTemplateListenerTest.php @@ -27,7 +27,7 @@ public function testTemplateReference() $event = $this->getEvent($template); - $twig = $this->getMockBuilder('Twig_Environment')->getMock(); + $twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock(); $twig->expects($this->once()) ->method('render') ->willReturn('This is dummy content'); @@ -43,7 +43,7 @@ public function testTemplateReference() public function testInvalidResponse() { - $twig = $this->getMockBuilder('Twig_Environment')->getMock(); + $twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock(); $template = $this->getMockBuilder(TemplatedResponseInterface::class)->getMock(); $template->expects($this->once())