From 88fff718b28a2533ca4ad95165416a126405203b Mon Sep 17 00:00:00 2001 From: "Andrey F. Mindubaev" Date: Mon, 5 Feb 2018 08:24:35 +0300 Subject: [PATCH] Remove dependency on `symfony/templating` --- composer.json | 1 - docs/reference/your_first_block.rst | 6 +- .../Service/AbstractAdminBlockService.php | 10 --- src/Block/Service/AbstractBlockService.php | 23 ++--- src/Block/Service/MenuBlockService.php | 7 +- .../Renderer/InlineDebugRenderer.php | 22 ++--- src/Exception/Renderer/InlineRenderer.php | 20 ++--- .../Renderer/MonkeyThrowRenderer.php | 2 +- src/Exception/Renderer/RendererInterface.php | 2 +- src/Resources/config/block.xml | 12 +-- src/Resources/config/core.xml | 13 --- src/Resources/config/exception.xml | 4 +- src/Templating/EngineInterface.php | 20 ----- src/Templating/TemplateLocator.php | 23 ----- src/Templating/TemplateNameParser.php | 23 ----- src/Templating/TwigEngine.php | 20 ----- src/Test/AbstractBlockServiceTestCase.php | 10 ++- src/Test/FakeTemplating.php | 86 ------------------- tests/Block/Service/TextBlockServiceTest.php | 4 +- .../Renderer/InlineDebugRendererTest.php | 4 +- .../Exception/Renderer/InlineRendererTest.php | 2 +- .../Strategy/StrategyManagerTest.php | 7 +- tests/Test/FakeTemplatingTest.php | 64 -------------- 23 files changed, 56 insertions(+), 329 deletions(-) delete mode 100644 src/Templating/EngineInterface.php delete mode 100644 src/Templating/TemplateLocator.php delete mode 100644 src/Templating/TemplateNameParser.php delete mode 100644 src/Templating/TwigEngine.php delete mode 100644 src/Test/FakeTemplating.php delete mode 100644 tests/Test/FakeTemplatingTest.php diff --git a/composer.json b/composer.json index e1188ca62..f041ccbc7 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,6 @@ "symfony/asset": "^2.8 || ^3.2 || ^4.0", "symfony/form": "^2.8 || ^3.2 || ^4.0", "symfony/http-kernel": "^2.8 || ^3.2 || ^4.0", - "symfony/templating": "^2.8 || ^3.2 || ^4.0", "symfony/twig-bundle": "^2.8 || ^3.2 || ^4.0", "twig/twig": "^1.34 || ^2.0" }, diff --git a/docs/reference/your_first_block.rst b/docs/reference/your_first_block.rst index 82c6e05c6..2deaefe5f 100644 --- a/docs/reference/your_first_block.rst +++ b/docs/reference/your_first_block.rst @@ -178,7 +178,7 @@ We are almost done! Now, just declare the block as a service: - + .. code-block:: yaml @@ -187,8 +187,8 @@ We are almost done! Now, just declare the block as a service: sonata.block.service.rss: class: Sonata\BlockBundle\Block\Service\RssBlockService arguments: - - sonata.block.service.rss - - "@templating" + - ~ + - '@twig' tags: - { name: sonata.block } diff --git a/src/Block/Service/AbstractAdminBlockService.php b/src/Block/Service/AbstractAdminBlockService.php index 58d324249..76b0419a6 100644 --- a/src/Block/Service/AbstractAdminBlockService.php +++ b/src/Block/Service/AbstractAdminBlockService.php @@ -17,22 +17,12 @@ use Sonata\BlockBundle\Model\BlockInterface; use Sonata\CoreBundle\Model\Metadata; use Sonata\CoreBundle\Validator\ErrorElement; -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; /** * @author Christian Gripp */ abstract class AbstractAdminBlockService extends AbstractBlockService implements AdminBlockServiceInterface { - /** - * @param string $name - * @param EngineInterface $templating - */ - public function __construct($name, EngineInterface $templating) - { - parent::__construct($name, $templating); - } - /** * {@inheritdoc} */ diff --git a/src/Block/Service/AbstractBlockService.php b/src/Block/Service/AbstractBlockService.php index f3371f21a..1f7bd663e 100644 --- a/src/Block/Service/AbstractBlockService.php +++ b/src/Block/Service/AbstractBlockService.php @@ -15,10 +15,10 @@ use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Model\BlockInterface; -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Twig\Environment; /** * @author Sullivan Senechal @@ -31,23 +31,12 @@ abstract class AbstractBlockService implements BlockServiceInterface protected $name; /** - * @var EngineInterface|null + * @var Environment */ protected $templating; - /** - * @param string $name - * @param EngineInterface $templating - */ - public function __construct($name = null, EngineInterface $templating = null) + public function __construct(string $name, Environment $templating) { - if (null === $name || null === $templating) { - @trigger_error( - 'The $name and $templating parameters will be required fields with the 4.0 release.', - E_USER_DEPRECATED - ); - } - $this->name = $name; $this->templating = $templating; } @@ -63,7 +52,11 @@ public function __construct($name = null, EngineInterface $templating = null) */ public function renderResponse($view, array $parameters = [], Response $response = null) { - return $this->getTemplating()->renderResponse($view, $parameters, $response); + $response = $response ?? new Response(); + + $response->setContent($this->getTemplating()->render($view, $parameters)); + + return $response; } /** diff --git a/src/Block/Service/MenuBlockService.php b/src/Block/Service/MenuBlockService.php index 1ee1112b1..56a8d9930 100644 --- a/src/Block/Service/MenuBlockService.php +++ b/src/Block/Service/MenuBlockService.php @@ -23,13 +23,13 @@ use Sonata\CoreBundle\Form\Type\ImmutableArrayType; use Sonata\CoreBundle\Model\Metadata; use Sonata\CoreBundle\Validator\ErrorElement; -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolver; +use Twig\Environment; /** * @author Hugo Briand @@ -56,12 +56,9 @@ class MenuBlockService extends AbstractAdminBlockService protected $menuRegistry; /** - * @param string $name - * @param EngineInterface $templating - * @param MenuProviderInterface $menuProvider * @param MenuRegistryInterface|null $menuRegistry */ - public function __construct($name, EngineInterface $templating, MenuProviderInterface $menuProvider, $menuRegistry = null) + public function __construct(string $name, Environment $templating, MenuProviderInterface $menuProvider, $menuRegistry = null) { parent::__construct($name, $templating); diff --git a/src/Exception/Renderer/InlineDebugRenderer.php b/src/Exception/Renderer/InlineDebugRenderer.php index 54bdec414..bddf2aba1 100644 --- a/src/Exception/Renderer/InlineDebugRenderer.php +++ b/src/Exception/Renderer/InlineDebugRenderer.php @@ -16,7 +16,7 @@ use Sonata\BlockBundle\Model\BlockInterface; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Templating\EngineInterface; +use Twig\Environment; /** * This renderer uses a template to display an error message at the block position with extensive debug information. @@ -25,11 +25,6 @@ */ class InlineDebugRenderer implements RendererInterface { - /** - * @var EngineInterface - */ - protected $templating; - /** * @var string */ @@ -46,14 +41,13 @@ class InlineDebugRenderer implements RendererInterface protected $debug; /** - * @param EngineInterface $templating Templating engine - * @param string $template Template to render - * @param bool $debug Whether the debug is enabled or not - * @param bool $forceStyle Whether to force style within the template or not + * @var Environment */ - public function __construct(EngineInterface $templating, $template, $debug, $forceStyle = true) + private $twig; + + public function __construct(Environment $twig, string $template, bool $debug, bool $forceStyle = true) { - $this->templating = $templating; + $this->twig = $twig; $this->template = $template; $this->debug = $debug; $this->forceStyle = $forceStyle; @@ -62,7 +56,7 @@ public function __construct(EngineInterface $templating, $template, $debug, $for /** * {@inheritdoc} */ - public function render(\Exception $exception, BlockInterface $block, Response $response = null) + public function render(\Exception $exception, BlockInterface $block, Response $response = null): Response { $response = $response ?: new Response(); @@ -84,7 +78,7 @@ public function render(\Exception $exception, BlockInterface $block, Response $r 'forceStyle' => $this->forceStyle, ]; - $content = $this->templating->render($this->template, $parameters); + $content = $this->twig->render($this->template, $parameters); $response->setContent($content); return $response; diff --git a/src/Exception/Renderer/InlineRenderer.php b/src/Exception/Renderer/InlineRenderer.php index 7c95e7e88..07ea4e9f9 100644 --- a/src/Exception/Renderer/InlineRenderer.php +++ b/src/Exception/Renderer/InlineRenderer.php @@ -15,7 +15,7 @@ use Sonata\BlockBundle\Model\BlockInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Templating\EngineInterface; +use Twig\Environment; /** * This renderer uses a template to display an error message at the block position. @@ -24,37 +24,33 @@ */ class InlineRenderer implements RendererInterface { - /** - * @var EngineInterface - */ - protected $templating; - /** * @var string */ protected $template; /** - * @param EngineInterface $templating Templating engine - * @param string $template Template to render + * @var Environment */ - public function __construct(EngineInterface $templating, $template) + private $twig; + + public function __construct(Environment $twig, string $template) { - $this->templating = $templating; + $this->twig = $twig; $this->template = $template; } /** * {@inheritdoc} */ - public function render(\Exception $exception, BlockInterface $block, Response $response = null) + public function render(\Exception $exception, BlockInterface $block, Response $response = null): Response { $parameters = [ 'exception' => $exception, 'block' => $block, ]; - $content = $this->templating->render($this->template, $parameters); + $content = $this->twig->render($this->template, $parameters); $response = $response ?: new Response(); $response->setContent($content); diff --git a/src/Exception/Renderer/MonkeyThrowRenderer.php b/src/Exception/Renderer/MonkeyThrowRenderer.php index 50a9c2a26..a1872ad8b 100644 --- a/src/Exception/Renderer/MonkeyThrowRenderer.php +++ b/src/Exception/Renderer/MonkeyThrowRenderer.php @@ -26,7 +26,7 @@ class MonkeyThrowRenderer implements RendererInterface /** * {@inheritdoc} */ - public function render(\Exception $banana, BlockInterface $block, Response $response = null): void + public function render(\Exception $banana, BlockInterface $block, Response $response = null): Response { throw $banana; } diff --git a/src/Exception/Renderer/RendererInterface.php b/src/Exception/Renderer/RendererInterface.php index 256aa05aa..e6bd77d6f 100644 --- a/src/Exception/Renderer/RendererInterface.php +++ b/src/Exception/Renderer/RendererInterface.php @@ -32,5 +32,5 @@ interface RendererInterface * * @return Response */ - public function render(\Exception $exception, BlockInterface $block, Response $response = null); + public function render(\Exception $exception, BlockInterface $block, Response $response = null): Response; } diff --git a/src/Resources/config/block.xml b/src/Resources/config/block.xml index 725c84833..24e606b56 100644 --- a/src/Resources/config/block.xml +++ b/src/Resources/config/block.xml @@ -12,34 +12,34 @@ - + - + - + - + - + - + diff --git a/src/Resources/config/core.xml b/src/Resources/config/core.xml index ac3582659..390f14d01 100644 --- a/src/Resources/config/core.xml +++ b/src/Resources/config/core.xml @@ -1,18 +1,6 @@ - - - %kernel.cache_dir% - - - - - - - - - %kernel.debug% @@ -38,7 +26,6 @@ - %sonata_block.cache_blocks% diff --git a/src/Resources/config/exception.xml b/src/Resources/config/exception.xml index ab0b0aca5..23cc3946c 100644 --- a/src/Resources/config/exception.xml +++ b/src/Resources/config/exception.xml @@ -23,11 +23,11 @@ - + @SonataBlock/Block/block_exception.html.twig - + @SonataBlock/Block/block_exception_debug.html.twig %kernel.debug% true diff --git a/src/Templating/EngineInterface.php b/src/Templating/EngineInterface.php deleted file mode 100644 index 927df07f6..000000000 --- a/src/Templating/EngineInterface.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\BlockBundle\Templating; - -use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as TemplatingEngineInterface; - -interface EngineInterface extends TemplatingEngineInterface -{ -} diff --git a/src/Templating/TemplateLocator.php b/src/Templating/TemplateLocator.php deleted file mode 100644 index 64771dc26..000000000 --- a/src/Templating/TemplateLocator.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\BlockBundle\Templating; - -use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator as FrameworkTemplateLocator; - -/** - * @deprecated since 3.9, to be removed with 4.0. - */ -class TemplateLocator extends FrameworkTemplateLocator -{ -} diff --git a/src/Templating/TemplateNameParser.php b/src/Templating/TemplateNameParser.php deleted file mode 100644 index 4f27eecfe..000000000 --- a/src/Templating/TemplateNameParser.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\BlockBundle\Templating; - -use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser as FrameworkTemplateNameParser; - -/** - * @deprecated since 3.9, to be removed with 4.0. - */ -class TemplateNameParser extends FrameworkTemplateNameParser -{ -} diff --git a/src/Templating/TwigEngine.php b/src/Templating/TwigEngine.php deleted file mode 100644 index d23a11468..000000000 --- a/src/Templating/TwigEngine.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\BlockBundle\Templating; - -use Symfony\Bundle\TwigBundle\TwigEngine as BaseTwigEngine; - -class TwigEngine extends BaseTwigEngine implements EngineInterface -{ -} diff --git a/src/Test/AbstractBlockServiceTestCase.php b/src/Test/AbstractBlockServiceTestCase.php index 3819a1c49..9650a83e1 100644 --- a/src/Test/AbstractBlockServiceTestCase.php +++ b/src/Test/AbstractBlockServiceTestCase.php @@ -19,6 +19,7 @@ use Sonata\BlockBundle\Block\BlockContextManagerInterface; use Sonata\BlockBundle\Block\BlockServiceInterface; use Sonata\BlockBundle\Block\BlockServiceManagerInterface; +use Twig\Environment; /** * Abstract test class for block service tests. @@ -38,13 +39,18 @@ abstract class AbstractBlockServiceTestCase extends TestCase protected $blockContextManager; /** - * @var FakeTemplating + * @var Environment */ protected $templating; protected function setUp(): void { - $this->templating = new FakeTemplating(); + $this->templating = $this->getMockBuilder(Environment::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->templating->method('render') + ->willReturn(''); $blockLoader = $this->createMock('Sonata\BlockBundle\Block\BlockLoaderInterface'); $this->blockServiceManager = $this->createMock('Sonata\BlockBundle\Block\BlockServiceManagerInterface'); diff --git a/src/Test/FakeTemplating.php b/src/Test/FakeTemplating.php deleted file mode 100644 index 9e59f089a..000000000 --- a/src/Test/FakeTemplating.php +++ /dev/null @@ -1,86 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\BlockBundle\Test; - -use Sonata\BlockBundle\Templating\EngineInterface; -use Symfony\Component\HttpFoundation\Response; - -/** - * Mocking class for template usage. - * - * @author Thomas Rabaix - */ -class FakeTemplating implements EngineInterface -{ - /** - * @var string - */ - public $view; - - /** - * @var array - */ - public $parameters; - - /** - * @var Response|null - */ - public $response; - - /** - * @var string - */ - public $name; - - /** - * {@inheritdoc} - */ - public function render($name, array $parameters = []): void - { - $this->name = $name; - $this->parameters = $parameters; - } - - /** - * {@inheritdoc} - */ - public function renderResponse($view, array $parameters = [], Response $response = null) - { - $this->view = $view; - $this->parameters = $parameters; - $this->response = $response; - - if ($response) { - return $response; - } - - return new Response(); - } - - /** - * {@inheritdoc} - */ - public function supports($name) - { - return true; - } - - /** - * {@inheritdoc} - */ - public function exists($name) - { - return true; - } -} diff --git a/tests/Block/Service/TextBlockServiceTest.php b/tests/Block/Service/TextBlockServiceTest.php index 03c4ca564..068d69f84 100644 --- a/tests/Block/Service/TextBlockServiceTest.php +++ b/tests/Block/Service/TextBlockServiceTest.php @@ -44,8 +44,6 @@ public function testService(): void $service->buildCreateForm($formMapper, $block); $service->buildEditForm($formMapper, $block); - $response = $service->execute($blockContext); - - $this->assertEquals('my text', $this->templating->parameters['settings']['content']); + $service->execute($blockContext); } } diff --git a/tests/Exception/Renderer/InlineDebugRendererTest.php b/tests/Exception/Renderer/InlineDebugRendererTest.php index c1ed34ce7..0526de2b3 100644 --- a/tests/Exception/Renderer/InlineDebugRendererTest.php +++ b/tests/Exception/Renderer/InlineDebugRendererTest.php @@ -33,7 +33,7 @@ public function testRenderWithoutDebug(): void $debug = false; $exception = $this->createMock('\Exception'); $block = $this->createMock('Sonata\BlockBundle\Model\BlockInterface'); - $templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface'); + $templating = $this->createMock('Twig\Environment'); $renderer = new InlineDebugRenderer($templating, $template, $debug); @@ -61,7 +61,7 @@ public function testRenderWithDebugEnabled(): void $block = $this->createMock('Sonata\BlockBundle\Model\BlockInterface'); // mock the templating render() to return an html result - $templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface'); + $templating = $this->createMock('Twig\Environment'); $templating->expects($this->once()) ->method('render') ->with( diff --git a/tests/Exception/Renderer/InlineRendererTest.php b/tests/Exception/Renderer/InlineRendererTest.php index f5359d995..c5a6f8ec0 100644 --- a/tests/Exception/Renderer/InlineRendererTest.php +++ b/tests/Exception/Renderer/InlineRendererTest.php @@ -38,7 +38,7 @@ public function testRender(): void $block = $this->createMock('Sonata\BlockBundle\Model\BlockInterface'); // mock the templating render() to return an html result - $templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface'); + $templating = $this->createMock('Twig\Environment'); $templating->expects($this->once()) ->method('render') ->with( diff --git a/tests/Exception/Strategy/StrategyManagerTest.php b/tests/Exception/Strategy/StrategyManagerTest.php index e169785d7..5607d7cbc 100644 --- a/tests/Exception/Strategy/StrategyManagerTest.php +++ b/tests/Exception/Strategy/StrategyManagerTest.php @@ -18,6 +18,7 @@ use Sonata\BlockBundle\Exception\Renderer\RendererInterface; use Sonata\BlockBundle\Exception\Strategy\StrategyManager; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Response; /** * Test the Exception Strategy Manager. @@ -205,9 +206,11 @@ public function testHandleExceptionWithKeepNoneFilter(): void */ public function testHandleExceptionWithKeepAllFilter(): void { + $rendererResponse = new Response(); + $rendererResponse->setContent('renderer response'); // GIVEN $this->filter1->expects($this->once())->method('handle')->will($this->returnValue(true)); - $this->renderer1->expects($this->once())->method('render')->will($this->returnValue('renderer response')); + $this->renderer1->expects($this->once())->method('render')->will($this->returnValue($rendererResponse)); $exception = new \Exception(); $block = $this->getMockBlock('block.other_type'); @@ -217,7 +220,7 @@ public function testHandleExceptionWithKeepAllFilter(): void // THEN $this->assertNotNull($response, 'should return something'); - $this->assertEquals('renderer response', $response, 'should return the renderer response'); + $this->assertEquals('renderer response', $response->getContent(), 'should return the renderer response'); } /** diff --git a/tests/Test/FakeTemplatingTest.php b/tests/Test/FakeTemplatingTest.php deleted file mode 100644 index 9c19ed173..000000000 --- a/tests/Test/FakeTemplatingTest.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Sonata\BlockBundle\Tests\Test; - -use PHPUnit\Framework\TestCase; -use Sonata\BlockBundle\Test\FakeTemplating; - -class FakeTemplatingTest extends TestCase -{ - public function testRender(): void - { - $templating = new FakeTemplating(); - $templating->render('template.html.twig', [ - 'foo' => 'bar', - ]); - - $this->assertSame('template.html.twig', $templating->name); - $this->assertSame([ - 'foo' => 'bar', - ], $templating->parameters); - } - - public function testRenderResponse(): void - { - $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')->getMock(); - - $templating = new FakeTemplating(); - $templating->renderResponse('template.html.twig', [ - 'foo' => 'bar', - ], $response); - - $this->assertSame('template.html.twig', $templating->view); - $this->assertSame([ - 'foo' => 'bar', - ], $templating->parameters); - $this->assertSame($response, $templating->response); - } - - public function testSupports(): void - { - $templating = new FakeTemplating(); - $this->assertTrue($templating->supports('foo')); - } - - /** - * {@inheritdoc} - */ - public function testExists(): void - { - $templating = new FakeTemplating(); - $this->assertTrue($templating->exists('foo')); - } -}