-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit replace using TextExtension from `twig/extensions` by copy truncate function to `DeprecatedTextExtension` method.
- Loading branch information
Showing
7 changed files
with
96 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Twig\Extension; | ||
|
||
use Twig\Environment; | ||
use Twig\Extension\AbstractExtension; | ||
|
||
/** | ||
* NEXT_MAJOR: Remove this class. | ||
* | ||
* @internal | ||
* | ||
* @deprecated since sonata-project/admin-bundle 3.x, to be removed in 4.0. | ||
* | ||
* This class is used to support `Sonata\AdminBundle\Twig\Extension\StringExtensions` when `sonata_admin.options.legacy_twig_text_extension` | ||
* is set to true and deprecated `twig/extensions` is not installed. It is copy of required function, which keep BC for `sonata_truncate` | ||
* twig filter until sonata-project/admin-bundle 4.0 where this filter will be dropped. | ||
*/ | ||
final class DeprecatedTextExtension extends AbstractExtension | ||
{ | ||
public function twigTruncateFilter(Environment $env, ?string $value, int $length = 30, bool $preserve = false, $separator = '...') | ||
{ | ||
if (\function_exists('mb_get_info')) { | ||
if (mb_strlen($value, $env->getCharset()) > $length) { | ||
if ($preserve) { | ||
// If breakpoint is on the last word, return the value without separator. | ||
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) { | ||
return $value; | ||
} | ||
|
||
$length = $breakpoint; | ||
} | ||
|
||
return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator; | ||
} | ||
} else { | ||
if (\strlen($value) > $length) { | ||
if ($preserve) { | ||
if (false !== ($breakpoint = strpos($value, ' ', $length))) { | ||
$length = $breakpoint; | ||
} | ||
} | ||
|
||
return rtrim(substr($value, 0, $length)).$separator; | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -29,18 +29,28 @@ | |
* @internal | ||
* | ||
* @see https://github.com/symfony/symfony/pull/35649 | ||
* @deprecated since sonata-project/admin-bundle 3.69, to be removed in 4.0. | ||
* | ||
* @author Javier Spagnoletti <[email protected]> | ||
*/ | ||
final class StringExtension extends AbstractExtension | ||
{ | ||
/** | ||
* @var TextExtension | ||
* @var AbstractExtension | ||
*/ | ||
private $legacyExtension; | ||
|
||
public function __construct(?TextExtension $legacyExtension = null) | ||
public function __construct(?AbstractExtension $legacyExtension = null) | ||
{ | ||
if (!$legacyExtension instanceof TextExtension && !$legacyExtension instanceof DeprecatedTextExtension) { | ||
throw new \TypeError(sprintf( | ||
'Argument 1 passed to %s::__construct() must be instance of %s or %s', | ||
self::class, | ||
TextExtension::class, | ||
DeprecatedTextExtension::class | ||
)); | ||
} | ||
|
||
$this->legacyExtension = $legacyExtension; | ||
} | ||
|
||
|
@@ -65,16 +75,15 @@ public function deprecatedTruncate(Environment $env, ?string $text, int $length | |
E_USER_DEPRECATED | ||
); | ||
|
||
if (null !== $this->legacyExtension) { | ||
if ($this->legacyExtension instanceof TextExtension) { | ||
return twig_truncate_filter($env, $text, $length, $preserve, $ellipsis); | ||
} elseif ($this->legacyExtension instanceof DeprecatedTextExtension) { | ||
return $this->legacyExtension->twigTruncateFilter($env, $text, $length, $preserve, $ellipsis); | ||
} | ||
|
||
return $this->legacyTruncteWithUnicodeString($text, $length, $preserve, $ellipsis); | ||
} | ||
|
||
/** | ||
* NEXT_MAJOR: Fix the arguments in order to respect the signature at `UnicodeString::truncate()`. | ||
*/ | ||
public function legacyTruncteWithUnicodeString(?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...'): SymfonyUnicodeString | ||
{ | ||
return (new SymfonyUnicodeString($text ?? ''))->truncate($length, $ellipsis, $preserve); | ||
|
This file contains 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