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

Conversation

covex-nn
Copy link
Contributor

@covex-nn covex-nn commented Feb 5, 2018

I am targeting this branch, because this is BC break.

See #456

Changelog

### Changed
- Use Twig directly in Blocks and Exception renderers
- `AbstractBlockService::getTemplating` method renamed to `getTwig`

### Removed
- Removed default null values for arguments of `AbstractBlockService` class
- Removed dependency on `symfony/templating` in `composer.json`
- Removed all temporary classes for templating
- Removed `FakeTemplating` class

Subject

Switch from Templating Component to Twig, Stage 2

E_USER_DEPRECATED
);
} elseif (!$templating instanceof Environment) {
throw new \TypeError('Argument 2 passed to '.get_class($this).'::__construct() must be instance of Twig\Environment, instance of '.get_class($templating).' given');
Copy link
Contributor

Choose a reason for hiding this comment

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

Please linebreak this, use sprintf, __METHOD__ and Environment::class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

*/
protected $templating;
protected $twig;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be private

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, $twig property is private in InlineRenderer and InlineDebugRenderer

*
* @return string The evaluated template as a string
*/
public function render($name, array $parameters = []);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add return type hints

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added type hints and updated classes, implementing this interface

@@ -63,7 +72,13 @@ public function __construct($name = null, EngineInterface $templating = null)
*/
public function renderResponse($view, array $parameters = [], Response $response = null)
{
return $this->getTemplating()->renderResponse($view, $parameters, $response);
if (null === $response) {
Copy link
Contributor

@kunicmarko20 kunicmarko20 Feb 5, 2018

Choose a reason for hiding this comment

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

this can probably be changed to $response = $response ?? new Response(); but I am not sure that makes it better 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. But, i searched for ?? new string in master branches of all sonata branches. This will be for a first time, where $obj = $obj ?? new className() is used =)

@covex-nn covex-nn force-pushed the twig branch 2 times, most recently from de0842e to 59eb208 Compare February 5, 2018 08:06
{
if (null === $name || null === $templating) {
if ($templating instanceof EngineInterface) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to add deprecation messages on master? can't we just move to twig direclty? wdyt @sonata-project/contributors @covex-nn ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that is a better option.

Copy link
Contributor Author

@covex-nn covex-nn Feb 5, 2018

Choose a reason for hiding this comment

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

At the begining i thought, that if i will remove type hint from __construct of AbstractBlockService, it will be easier to upgrade all other bundles, because they will be compatible with 3.x and with dev-master of BlockBundle.

I searched in src/Block directory inside each bundle and found, that there are 12 bundles, containing Blocks:

  1. 7 bundles contain at least one Block with extended __construct (with more than 2 parameters): AdminBundle, DoctrineORMAdminBundle, MediaBundle, NewsBundle, PageBundle, SeoBundle, TimelineBundle.
  2. 4 bundles have Blocks without __construct: CommentBundle: DashboardBundle, FormatterBundle, TranslationBundle
  3. DoctrinePhpcrAdminBundle contain Block, that extends deprecated BaseBlockService class

So, first 7 bundles will not be compatible with new AbstractBlockService::__construct =(( May be it is too much and a should just change EngineInterface $templating to Environment $templating? And there will be no deprecation message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jordisala1991 @kunicmarko20 with type hint Environment for second constructor's parameter of AbstractBlockService there won't be Stage 3 =) and there won't be a message about deprecation, of course. I like it more, than current proposal.

But if we will leave second parameter without type hint, new Block classes, created after 3.11 and before 4.0 release could be compatible with new AbstractBlockService, if they will be without own __construct. And i guess, that all of new Blocks will use templating service, not new sonata.templating!

So, i think, that we will not reach BC. And if you do not mind, tomorrow i will add Environment type hint to a second parameter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, 4.x does not have to be BC with 3.x? /cc @sonata-project/contributors

Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, the issue is that some bundle need to be compatible with 2 versions of some other bundles, right?

* @param MenuProviderInterface $menuProvider
* @param MenuRegistryInterface|null $menuRegistry
*/
public function __construct($name, EngineInterface $templating, MenuProviderInterface $menuProvider, $menuRegistry = null)
public function __construct($name, $templating, MenuProviderInterface $menuProvider, $menuRegistry = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you replace parameters with typehint and remove the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kunicmarko20 i am sorry, i did not understans what you mean =( you say, that i have to replace $templating with Environment $templating? But exception renderers contain this type hint and your comment is the same there.

Copy link
Contributor

Choose a reason for hiding this comment

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

@covex-nn sorry, my bad, here you can skip it but on other places you can replace comment with typehint in constructor, right?

*/
public function __construct(EngineInterface $templating, $template, $debug, $forceStyle = true)
public function __construct(Environment $twig, $template, $debug, $forceStyle = true)
Copy link
Contributor

@kunicmarko20 kunicmarko20 Feb 5, 2018

Choose a reason for hiding this comment

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

Can you add typehint to parameters and remove the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But type hint is already exists here. Removing type hint for AbstractBlockService was made for compability with other bundles (for easier upgrade). Should i remove type hint from Environment $twig or comment @param Environment $twig Templating engine ?

Copy link
Contributor

Choose a reason for hiding this comment

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

My comment was badly explained, I updated it. So we can typehint all parameters and remove the phpdoc comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. It is public function __construct(Environment $twig, string $template, bool $debug, bool $forceStyle = true) now without phpdoc comment

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, thank you very much!

*/
public function __construct(EngineInterface $templating, $template)
public function __construct(Environment $twig, $template)
Copy link
Contributor

@kunicmarko20 kunicmarko20 Feb 5, 2018

Choose a reason for hiding this comment

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

Can you add typehint to parameters and remove the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. It is public function __construct(Environment $twig, string $template) now without phpdoc comment

@covex-nn covex-nn changed the title Remove dependency on symfony/templating [WIP] Remove dependency on symfony/templating Feb 5, 2018
@@ -20,6 +20,8 @@
* Mocking class for template usage.
*
* @author Thomas Rabaix <[email protected]>
*
* @deprecated since 4.0, to be removed with 5.0.
*/
class FakeTemplating implements EngineInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This class is not used any more. Can i remove it instead of deprecating?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

return $this->getTemplating()->renderResponse($view, $parameters, $response);
$response = $response ?? new Response();

$response->setContent($this->getTemplating()->render($view, $parameters));
Copy link
Member

Choose a reason for hiding this comment

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

setContent returns the response, so you can return directly if you want :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not like it =) with these two lines i can set breakpoint on a second line with return $response after setContent; with one line it won't be convenient


interface EngineInterface extends TemplatingEngineInterface
/**
* @deprecated since 4.0, to be removed with 5.0.
Copy link
Member

Choose a reason for hiding this comment

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

If not used, you can remove it also

Copy link
Contributor Author

@covex-nn covex-nn Feb 7, 2018

Choose a reason for hiding this comment

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

It is not used =)
If so, in next PR i will remove templating helper too! All methods will be copied to twig extension

@SonataCI
Copy link
Collaborator

SonataCI commented Feb 8, 2018

Could you please rebase your PR and fix merge conflicts?

@covex-nn covex-nn changed the title [WIP] Remove dependency on symfony/templating Remove dependency on symfony/templating Feb 8, 2018
@covex-nn covex-nn force-pushed the twig branch 2 times, most recently from b7c0fdb to 88fff71 Compare February 12, 2018 05:22
E_USER_DEPRECATED
);
}

$this->name = $name;
$this->templating = $templating;
Copy link
Member

Choose a reason for hiding this comment

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

Can this be renamed to twig? And maybe the getTemplate method too? it will be a lof of changes maybe so I am not sure, but it seems wrong

Copy link
Contributor Author

@covex-nn covex-nn Feb 14, 2018

Choose a reason for hiding this comment

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

I was afraid, that you would ask =) It is not a lot of changes here, in BlockBundle, but getTemplating could be used in other bundles.
I will rename it everywhere. Also i will make AbstractBlockService::$templating private.

Copy link
Contributor Author

@covex-nn covex-nn Feb 14, 2018

Choose a reason for hiding this comment

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

@jordisala1991 done
Travis failed, because 41 is not 42 again =) Please, restart travis job https://travis-ci.org/sonata-project/SonataBlockBundle/jobs/341291098

@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

@@ -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

@@ -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');
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 use ::class notation? Only on the lines you changed. No need to fix the whole file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Also i renamed all $templating to $twig in test classes

Copy link
Member

@jordisala1991 jordisala1991 left a comment

Choose a reason for hiding this comment

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

Probably the docs could be updated, and an upgrade note to know what to do when migrating from 3.x to 4.x of this Bundle.

The changelog could be simplified.

Except for this minor comments, 👍

@covex-nn
Copy link
Contributor Author

@jordisala1991 i've updated upgrading note and some docs. But this is the most difficult part of PR =)

UPGRADE-4.0.md Outdated

## Block constructor

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

Choose a reason for hiding this comment

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

"overrode"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

@OskarStark OskarStark requested review from greg0ire and core23 March 6, 2018 08:47
@kunicmarko20
Copy link
Contributor

ping @sonata-project/contributors can you review this and maybe get it merged.

@OskarStark
Copy link
Member

IMO 3 approvals are enough 👍

@OskarStark OskarStark merged commit 160da4a into sonata-project:master Mar 14, 2018
@OskarStark
Copy link
Member

Thank you very much @covex-nn 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants