Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBridge] Add emojify twig filter #54432

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

Merged
merged 1 commit into from
Apr 5, 2024
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
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add `emojify` twig filter

7.0
---

Expand Down
55 changes: 55 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/EmojiExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Emoji\EmojiTransliterator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* @author GrΓ©goire Pineau <[email protected]>
*/
final class EmojiExtension extends AbstractExtension
{
private static array $transliterators = [];

public function __construct(
private readonly string $defaultCatalog = 'text',
) {
if (!class_exists(EmojiTransliterator::class)) {
throw new \LogicException('You cannot use the "emojify" filter as the "Emoji" component is not installed. Try running "composer require symfony/emoji".');
}
}

public function getFilters(): array
{
return [
new TwigFilter('emojify', $this->emojify(...)),
];
}

/**
* Converts emoji short code (:wave:) to real emoji (πŸ‘‹)
*/
public function emojify(string $string, ?string $catalog = null): string
{
$catalog ??= $this->defaultCatalog;

try {
$tr = self::$transliterators[$catalog] ??= EmojiTransliterator::create($catalog, EmojiTransliterator::REVERSE);
} catch (\IntlException $e) {
throw new \LogicException(sprintf('The emoji catalog "%s" is not available.', $catalog), previous: $e);
}

return (string) $tr->transliterate($string);
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/EmojiExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Tests\Extension;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Extension\EmojiExtension;

/**
* @requires extension intl
*/
class EmojiExtensionTest extends TestCase
{
/**
* @testWith ["πŸ…°οΈ", ":a:"]
* ["πŸ…°οΈ", ":a:", "slack"]
* ["πŸ…°", ":a:", "github"]
*/
public function testEmojify(string $expected, string $string, ?string $catalog = null)
{
$extension = new EmojiExtension();
$this->assertSame($expected, $extension->emojify($string, $catalog));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/UndefinedCallableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class UndefinedCallableHandler
'asset_version' => 'asset',
'importmap' => 'asset-mapper',
'dump' => 'debug-bundle',
'emojify' => 'emoji',
'encore_entry_link_tags' => 'webpack-encore-bundle',
'encore_entry_script_tags' => 'webpack-encore-bundle',
'expression' => 'expression-language',
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"symfony/asset": "^6.4|^7.0",
"symfony/asset-mapper": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/emoji": "^7.1",
"symfony/finder": "^6.4|^7.0",
"symfony/form": "^6.4|^7.0",
"symfony/html-sanitizer": "^6.4|^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Emoji\EmojiTransliterator;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Workflow\Workflow;
Expand All @@ -31,6 +32,10 @@ public function process(ContainerBuilder $container): void
$container->removeDefinition('twig.extension.assets');
}

if (!class_exists(EmojiTransliterator::class)) {
$container->removeDefinition('twig.extension.emoji');
}

if (!class_exists(Expression::class)) {
$container->removeDefinition('twig.extension.expression');
}
Expand Down Expand Up @@ -125,6 +130,10 @@ public function process(ContainerBuilder $container): void
$container->getDefinition('twig.extension.expression')->addTag('twig.extension');
}

if ($container->hasDefinition('twig.extension.emoji')) {
$container->getDefinition('twig.extension.emoji')->addTag('twig.extension');
}

if (!class_exists(Workflow::class) || !$container->has('workflow.registry')) {
$container->removeDefinition('workflow.twig_extension');
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer;
use Symfony\Bridge\Twig\EventListener\TemplateAttributeListener;
use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\EmojiExtension;
use Symfony\Bridge\Twig\Extension\ExpressionExtension;
use Symfony\Bridge\Twig\Extension\HtmlSanitizerExtension;
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
Expand Down Expand Up @@ -115,6 +116,8 @@

->set('twig.extension.expression', ExpressionExtension::class)

->set('twig.extension.emoji', EmojiExtension::class)

->set('twig.extension.htmlsanitizer', HtmlSanitizerExtension::class)
->args([tagged_locator('html_sanitizer', 'sanitizer')])

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"composer-runtime-api": ">=2.1",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/twig-bridge": "^6.4|^7.0",
"symfony/twig-bridge": "^7.1",
"symfony/http-foundation": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"twig/twig": "^3.0.4"
Expand Down