Skip to content

[TwigBridge] Support for Twig 3 #33039

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 2 commits into from
Aug 21, 2019
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"ext-xml": "*",
"doctrine/event-manager": "~1.0",
"doctrine/persistence": "~1.0",
"twig/twig": "^2.10",
"twig/twig": "^2.10|^3",
"psr/cache": "~1.0",
"psr/container": "^1.0",
"psr/link": "^1.0",
Expand Down
26 changes: 4 additions & 22 deletions src/Symfony/Bridge/Twig/Extension/AssetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
* Twig extension for the Symfony Asset component.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since Symfony 4.4
*/
class AssetExtension extends AbstractExtension
final class AssetExtension extends AbstractExtension
{
private $packages;

Expand All @@ -33,10 +31,8 @@ public function __construct(Packages $packages)

/**
* {@inheritdoc}
*
* @return TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('asset', [$this, 'getAssetUrl']),
Expand All @@ -49,31 +45,17 @@ public function getFunctions()
*
* If the package used to generate the path is an instance of
* UrlPackage, you will always get a URL and not a path.
*
* @return string The public path of the asset
*/
public function getAssetUrl(string $path, string $packageName = null)
public function getAssetUrl(string $path, string $packageName = null): string
{
return $this->packages->getUrl($path, $packageName);
}

/**
* Returns the version of an asset.
*
* @return string The asset version
*/
public function getAssetVersion(string $path, string $packageName = null)
public function getAssetVersion(string $path, string $packageName = null): string
{
return $this->packages->getVersion($path, $packageName);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'asset';
}
}
44 changes: 11 additions & 33 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@
* Twig extension relate to PHP code and used by the profiler and the default exception templates.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since Symfony 4.4
*/
class CodeExtension extends AbstractExtension
final class CodeExtension extends AbstractExtension
{
private $fileLinkFormat;
private $charset;
private $projectDir;

/**
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
* @param string $projectDir The project directory
* @param string $charset The charset
*/
public function __construct($fileLinkFormat, string $projectDir, string $charset)
{
Expand All @@ -42,10 +38,8 @@ public function __construct($fileLinkFormat, string $projectDir, string $charset

/**
* {@inheritdoc}
*
* @return TwigFilter[]
*/
public function getFilters()
public function getFilters(): array
{
return [
new TwigFilter('abbr_class', [$this, 'abbrClass'], ['is_safe' => ['html']]),
Expand All @@ -61,15 +55,15 @@ public function getFilters()
];
}

public function abbrClass(string $class)
public function abbrClass(string $class): string
{
$parts = explode('\\', $class);
$short = array_pop($parts);

return sprintf('<abbr title="%s">%s</abbr>', $class, $short);
}

public function abbrMethod(string $method)
public function abbrMethod(string $method): string
{
if (false !== strpos($method, '::')) {
list($class, $method) = explode('::', $method, 2);
Expand All @@ -85,10 +79,8 @@ public function abbrMethod(string $method)

/**
* Formats an array as a string.
*
* @return string
*/
public function formatArgs(array $args)
public function formatArgs(array $args): string
{
$result = [];
foreach ($args as $key => $item) {
Expand Down Expand Up @@ -116,20 +108,16 @@ public function formatArgs(array $args)

/**
* Formats an array as a string.
*
* @return string
*/
public function formatArgsAsText(array $args)
public function formatArgsAsText(array $args): string
{
return strip_tags($this->formatArgs($args));
}

/**
* Returns an excerpt of a code file around the given line number.
*
* @return string An HTML string
*/
public function fileExcerpt(string $file, int $line, int $srcContext = 3)
public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?string
{
if (is_file($file) && is_readable($file)) {
// highlight_file could throw warnings
Expand Down Expand Up @@ -160,10 +148,8 @@ public function fileExcerpt(string $file, int $line, int $srcContext = 3)

/**
* Formats a file path.
*
* @return string
*/
public function formatFile(string $file, int $line, string $text = null)
public function formatFile(string $file, int $line, string $text = null): string
{
$file = trim($file);

Expand Down Expand Up @@ -211,7 +197,7 @@ public function getFileRelative(string $file): ?string
return null;
}

public function formatFileFromText(string $text)
public function formatFileFromText(string $text): string
{
return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
return 'in '.$this->formatFile($match[2], $match[3]);
Expand All @@ -221,7 +207,7 @@ public function formatFileFromText(string $text)
/**
* @internal
*/
public function formatLogMessage(string $message, array $context)
public function formatLogMessage(string $message, array $context): string
{
if ($context && false !== strpos($message, '{')) {
$replacements = [];
Expand All @@ -239,15 +225,7 @@ public function formatLogMessage(string $message, array $context)
return htmlspecialchars($message, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'code';
}

protected static function fixCodeMarkup(string $line)
protected static function fixCodeMarkup(string $line): string
{
// </span> ending tag from previous line
$opening = strpos($line, '<span');
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Bridge/Twig/Extension/CsrfExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
* @author Titouan Galopin <galopintitouan@gmail.com>
*
* @final since Symfony 4.4
*/
class CsrfExtension extends AbstractExtension
final class CsrfExtension extends AbstractExtension
{
/**
* {@inheritdoc}
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Bridge/Twig/Extension/CsrfRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
* @author Titouan Galopin <galopintitouan@gmail.com>
*
* @final since Symfony 4.4
*/
class CsrfRuntime
final class CsrfRuntime
{
private $csrfTokenManager;

Expand Down
20 changes: 6 additions & 14 deletions src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Template;
use Twig\TokenParser\TokenParserInterface;
use Twig\TwigFunction;

/**
* Provides integration of the dump() function with Twig.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @final since Symfony 4.4
*/
class DumpExtension extends AbstractExtension
final class DumpExtension extends AbstractExtension
{
private $cloner;
private $dumper;
Expand All @@ -39,29 +36,24 @@ public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
}

/**
* @return TwigFunction[]
* {@inheritdoc}
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('dump', [$this, 'dump'], ['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]),
];
}

/**
* @return TokenParserInterface[]
* {@inheritdoc}
*/
public function getTokenParsers()
public function getTokenParsers(): array
{
return [new DumpTokenParser()];
}

public function getName()
{
return 'dump';
}

public function dump(Environment $env, array $context)
public function dump(Environment $env, array $context): ?string
{
if (!$env->isDebug()) {
return null;
Expand Down
20 changes: 3 additions & 17 deletions src/Symfony/Bridge/Twig/Extension/ExpressionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,21 @@
* ExpressionExtension gives a way to create Expressions from a template.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since Symfony 4.4
*/
class ExpressionExtension extends AbstractExtension
final class ExpressionExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*
* @return TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('expression', [$this, 'createExpression']),
];
}

public function createExpression(string $expression)
public function createExpression(string $expression): Expression
{
return new Expression($expression);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'expression';
}
}
Loading