-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Deprecate some interface in Twig Bridge #16333
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Bumped Twig version to 1.24 and get rid of initRuntime
- Loading branch information
commit aa0f3a3e1722ce2f8f938d39cf1baeb54b503c13
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/Symfony/Bridge/Twig/Extension/ContainerAwareRuntimeLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Extension; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Loads Twig extension runtimes. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ContainerAwareRuntimeLoader implements \Twig_RuntimeLoaderInterface | ||
{ | ||
private $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load($name) | ||
{ | ||
$id = 'twig.extension.runtime.'.$name; | ||
|
||
if ($this->container->has($id)) { | ||
return $this->container->get($id); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Symfony/Bridge/Twig/Extension/FormExtensionRuntime.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Extension; | ||
|
||
use Symfony\Bridge\Twig\Form\TwigRenderer; | ||
use Symfony\Component\Form\Extension\Core\View\ChoiceView; | ||
|
||
/** | ||
* FormExtension extends Twig with form capabilities. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class FormExtensionRuntime | ||
{ | ||
/** | ||
* This property is public so that it can be accessed directly from compiled | ||
* templates without having to call a getter, which slightly decreases performance. | ||
* | ||
* @var TwigRenderer | ||
*/ | ||
public $renderer; | ||
|
||
public function __construct(TwigRenderer $renderer) | ||
{ | ||
$this->renderer = $renderer; | ||
} | ||
|
||
/** | ||
* Renders a CSRF token. | ||
* | ||
* @param string $intention The intention of the protected action. | ||
* | ||
* @return string A CSRF token. | ||
*/ | ||
public function renderCsrfToken($intention) | ||
{ | ||
return $this->renderer->renderCsrfToken($intention); | ||
} | ||
|
||
/** | ||
* Makes a technical name human readable. | ||
* | ||
* @param string $text The text to humanize. | ||
* | ||
* @return string The humanized text. | ||
*/ | ||
public function humanize($text) | ||
{ | ||
return $this->renderer->humanize($text); | ||
} | ||
|
||
/** | ||
* Returns whether a choice is selected for a given form value. | ||
* | ||
* Unfortunately Twig does not support an efficient way to execute the | ||
* "is_selected" closure passed to the template by ChoiceType. It is faster | ||
* to implement the logic here (around 65ms for a specific form). | ||
* | ||
* Directly implementing the logic here is also faster than doing so in | ||
* ChoiceView (around 30ms). | ||
* | ||
* The worst option tested so far is to implement the logic in ChoiceView | ||
* and access the ChoiceView method directly in the template. Doing so is | ||
* around 220ms slower than doing the method call here in the filter. Twig | ||
* seems to be much more efficient at executing filters than at executing | ||
* methods of an object. | ||
* | ||
* @param ChoiceView $choice The choice to check. | ||
* @param string|array $selectedValue The selected value to compare. | ||
* | ||
* @return bool Whether the choice is selected. | ||
* | ||
* @see ChoiceView::isSelected() | ||
*/ | ||
public function isSelectedChoice(ChoiceView $choice, $selectedValue) | ||
{ | ||
if (is_array($selectedValue)) { | ||
return in_array($choice->value, $selectedValue, true); | ||
} | ||
|
||
return $choice->value === $selectedValue; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't force this convention: it forbids third-party bundles to use this behavior while following best practices for shared bundles. The id of the runtime should be provided as an optional attribute in the
twig.extension
tag IMO, to build a mapping between extension names and runtimesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, this is just a quick POC to prove that it works.