Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on symfony/templating #476

Merged
merged 1 commit into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ See also the [diff code](https://github.com/sonata-project/SonataAdminBundle/com
## Block id

If you have created a custom `AbstractBlockService` you must now implement the new constructor, because all blocks use the service id as the block id now. Because of that, the translation keys for the block ids have changed from `sonata.block.$NAME` to `sonata.block.service.$NAME`.

## Block constructor

Blocks are using `twig` service to render templates, so if you already overrode a constructor of a custom `AbstractBlockService`, you must update it.

Also, both arguments of `AbstractBlockService` constructor became required.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"symfony/http-foundation": "^2.8 || ^3.2 || ^4.0",
"symfony/http-kernel": "^2.8 || ^3.2 || ^4.0",
"symfony/options-resolver": "^2.8 || ^3.2 || ^4.0",
"symfony/templating": "^2.8 || ^3.2 || ^4.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if we can get rid of framework-bundle also? AFAIK it was used for the EngineInterface, not sure if we need it for anything else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

symfony/framework-bundle is used only in Sonata\BlockBundle\Command\BaseCommand:

namespace Sonata\BlockBundle\Command;

use Sonata\BlockBundle\Block\BlockServiceManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

abstract class BaseCommand extends ContainerAwareCommand { /* ... */ }

To remove dependency extends ContainerAwareCommand could be replaced with implements \Symfony\Component\DependencyInjection\ContainerAwareInterface here

"symfony/twig-bundle": "^2.8 || ^3.2 || ^4.0",
"twig/twig": "^1.34 || ^2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Final Response TTL computation
The ``BlockRendered`` stores a global state for the smallest TTL available, there is another service used to store the smallest
TTL for the page: ``HttpCacheHandler``. Why two services? This has been done to add an extra layer of control.

The ``HttpCacheHandler::updateMetadata`` is called by the templating helper when the response is retrieved, then an event listener is registered to alter the final Response.
The ``HttpCacheHandler::updateMetadata`` is called by the twig extension when the response is retrieved, then an event listener is registered to alter the final Response.

The service can be configured using the ``http_cache_handler`` key.

Expand Down
9 changes: 2 additions & 7 deletions docs/reference/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ You can write unit tests for block services with the following code.
{
public function testDefaultSettings()
{
$blockService = new CustomBlockService('foo', $this->templating);
$blockService = new CustomBlockService('foo', $this->twig);
$blockContext = $this->getBlockContext($blockService);

$this->assertSettings(array(
Expand All @@ -55,14 +55,9 @@ You can write unit tests for block services with the following code.

public function testExecute()
{
$blockService = new CustomBlockService('foo', $this->templating);
$blockService = new CustomBlockService('foo', $this->twig);
$blockContext = $this->getBlockContext($blockService);

$service->execute($blockContext);

$this->assertSame($blockContext, $this->templating->parameters['context']);
$this->assertInternalType('array', $this->templating->parameters['settings']);
$this->assertInstanceOf('Sonata\BlockBundle\Model\BlockInterface', $this->templating->parameters['block']);
$this->assertSame('bar', $this->templating->parameters['foo']);
}
}
6 changes: 3 additions & 3 deletions docs/reference/your_first_block.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ We are almost done! Now, just declare the block as a service:
<service id="sonata.block.service.rss" class="Sonata\BlockBundle\Block\Service\RssBlockService">
<tag name="sonata.block" />
<argument/>
<argument type="service" id="sonata.templating" />
<argument type="service" id="twig" />
</service>

.. code-block:: yaml
Expand All @@ -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 }

Expand Down
10 changes: 0 additions & 10 deletions src/Block/Service/AbstractAdminBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
abstract class AbstractAdminBlockService extends AbstractBlockService implements AdminBlockServiceInterface
{
/**
* @param string $name
* @param EngineInterface $templating
*/
public function __construct($name, EngineInterface $templating)
{
parent::__construct($name, $templating);
}

/**
* {@inheritdoc}
*/
Expand Down
31 changes: 12 additions & 19 deletions src/Block/Service/AbstractBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -31,25 +31,14 @@ abstract class AbstractBlockService implements BlockServiceInterface
protected $name;

/**
* @var EngineInterface|null
* @var Environment
*/
protected $templating;
private $twig;

/**
* @param string $name
* @param EngineInterface $templating
*/
public function __construct($name = null, EngineInterface $templating = null)
public function __construct(string $name, Environment $twig)
{
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;
$this->twig = $twig;
}

/**
Expand All @@ -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->twig->render($view, $parameters));

return $response;
}

/**
Expand Down Expand Up @@ -157,8 +150,8 @@ public function getName()
/**
* {@inheritdoc}
*/
public function getTemplating()
public function getTwig()
{
return $this->templating;
return $this->twig;
}
}
9 changes: 3 additions & 6 deletions src/Block/Service/MenuBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -56,14 +56,11 @@ 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 $twig, MenuProviderInterface $menuProvider, $menuRegistry = null)
{
parent::__construct($name, $templating);
parent::__construct($name, $twig);

$this->menuProvider = $menuProvider;

Expand Down
22 changes: 8 additions & 14 deletions src/Exception/Renderer/InlineDebugRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,11 +25,6 @@
*/
class InlineDebugRenderer implements RendererInterface
{
/**
* @var EngineInterface
*/
protected $templating;

/**
* @var string
*/
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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;
Expand Down
20 changes: 8 additions & 12 deletions src/Exception/Renderer/InlineRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/Renderer/MonkeyThrowRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/Renderer/RendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions src/Resources/config/block.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@
<service id="sonata.block.service.container" class="%sonata.block.service.container.class%">
<tag name="sonata.block"/>
<argument/>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
</service>
<service id="sonata.block.service.empty" class="%sonata.block.service.empty.class%">
<tag name="sonata.block"/>
<argument/>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
</service>
<service id="sonata.block.service.text" class="%sonata.block.service.text.class%">
<tag name="sonata.block"/>
<argument/>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
</service>
<service id="sonata.block.service.rss" class="%sonata.block.service.rss.class%">
<tag name="sonata.block"/>
<argument/>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
</service>
<service id="sonata.block.service.menu" class="%sonata.block.service.menu.class%">
<tag name="sonata.block"/>
<argument/>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
<argument type="service" id="knp_menu.menu_provider"/>
<argument type="service" id="sonata.block.menu.registry"/>
</service>
<service id="sonata.block.service.template" class="%sonata.block.service.template.class%">
<tag name="sonata.block"/>
<argument/>
<argument type="service" id="sonata.templating"/>
<argument type="service" id="twig"/>
</service>
</services>
</container>
13 changes: 0 additions & 13 deletions src/Resources/config/core.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sonata.templating.locator" class="Sonata\BlockBundle\Templating\TemplateLocator">
<argument type="service" id="file_locator"/>
<argument>%kernel.cache_dir%</argument>
</service>
<service id="sonata.templating.name_parser" class="Sonata\BlockBundle\Templating\TemplateNameParser">
<argument type="service" id="kernel"/>
</service>
<service id="sonata.templating" class="Sonata\BlockBundle\Templating\TwigEngine">
<argument type="service" id="twig"/>
<argument type="service" id="sonata.templating.name_parser"/>
<argument type="service" id="sonata.templating.locator"/>
</service>
<service id="sonata.block.manager" class="Sonata\BlockBundle\Block\BlockServiceManager" public="true">
<argument type="service" id="service_container"/>
<argument>%kernel.debug%</argument>
Expand All @@ -38,7 +26,6 @@
<argument type="service" id="sonata.block.templating.helper"/>
</service>
<service id="sonata.block.templating.helper" class="Sonata\BlockBundle\Templating\Helper\BlockHelper">
<tag name="templating.helper" alias="sonata_block"/>
<argument type="service" id="sonata.block.manager"/>
<argument>%sonata_block.cache_blocks%</argument>
<argument type="service" id="sonata.block.renderer"/>
Expand Down
Loading