Download project

This commit is contained in:
Roman Pyrih
2024-11-20 09:09:44 +01:00
parent 547a138d6a
commit 5ff041757f
40737 changed files with 7766183 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace PrestaShop\TranslationToolsBundle\Twig\Extension;
use Symfony\Component\Translation\TranslatorInterface;
use Twig_Environment;
use Twig_Extension_InitRuntimeInterface;
class AppExtension extends \Twig_Extension implements Twig_Extension_InitRuntimeInterface
{
/**
* @var TranslatorInterface
*/
private $translation;
/**
* AppExtension constructor.
*/
public function __construct(TranslatorInterface $translation)
{
$this->translation = $translation;
}
public function initRuntime(Twig_Environment $environment)
{
$environment->registerUndefinedFunctionCallback(function () {
return;
});
$environment->registerUndefinedFilterCallback(function () {
return;
});
}
/**
* We need to define and reset each twig function as the definition
* of theses function is stored in PrestaShop codebase.
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('renderhooksarray', [$this, 'transChoice']),
];
}
/**
* @param $string
*
* @return string
*/
public function transChoice($string)
{
return $this->translation->transChoice($string);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'app';
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace PrestaShop\TranslationToolsBundle\Twig;
class Lexer extends \Twig_Lexer
{
/**
* @var array
*/
protected $comments = [];
/**
* @var bool
*/
protected $call = false;
protected function lexComment()
{
parent::lexComment();
if (true === $this->call) {
return;
}
preg_match_all('|\{#\s(.+)\s#\}|i', $this->code, $commentMatch);
if (is_array($commentMatch[1])) {
foreach ($commentMatch[1] as $comment) {
$this->comments[] = [
'line' => $this->lineno,
'comment' => $comment,
'file' => $this->filename,
];
}
}
$this->call = true;
}
public function getComments()
{
return $this->comments;
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace PrestaShop\TranslationToolsBundle\Twig\NodeVisitor;
use Symfony\Bridge\Twig\Node\TransNode;
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor as BaseTranslationNodeVisitor;
class TranslationNodeVisitor extends BaseTranslationNodeVisitor
{
const UNDEFINED_DOMAIN = '_undefined';
private $enabled = true;
private $messages = [];
public function enable()
{
$this->enabled = true;
$this->messages = [];
}
public function getMessages()
{
return $this->messages;
}
/**
* {@inheritdoc}
*/
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
{
if (!$this->enabled) {
return $node;
}
if (
$node instanceof \Twig_Node_Expression_Filter &&
'trans' === $node->getNode('filter')->getAttribute('value') &&
$node->getNode('node') instanceof \Twig_Node_Expression_Constant
) {
// extract constant nodes with a trans filter
$this->messages[] = [
$node->getNode('node')->getAttribute('value'),
$this->getReadDomainFromArguments($node->getNode('arguments'), 1),
'line' => $node->getTemplateLine(),
];
} elseif (
$node instanceof \Twig_Node_Expression_Filter &&
'transchoice' === $node->getNode('filter')->getAttribute('value') &&
$node->getNode('node') instanceof \Twig_Node_Expression_Constant
) {
// extract constant nodes with a trans filter
$this->messages[] = [
$node->getNode('node')->getAttribute('value'),
$this->getReadDomainFromArguments($node->getNode('arguments'), 2),
'line' => $node->getTemplateLine(),
];
} elseif ($node instanceof TransNode) {
// extract trans nodes
$this->messages[] = [
$node->getNode('body')->getAttribute('data'),
$this->getReadDomainFromNode($node->getNode('domain')),
'line' => $node->getTemplateLine(),
];
}
return $node;
}
/**
* @param int $index
*
* @return string|null
*/
private function getReadDomainFromArguments(\Twig_Node $arguments, $index)
{
if ($arguments->hasNode('domain')) {
$argument = $arguments->getNode('domain');
} elseif ($arguments->hasNode($index)) {
$argument = $arguments->getNode($index);
} else {
return;
}
return $this->getReadDomainFromNode($argument);
}
/**
* @param \Twig_Node $node
*
* @return string|null
*/
private function getReadDomainFromNode(\Twig_Node $node = null)
{
if (null === $node) {
return;
}
if ($node instanceof \Twig_Node_Expression_Constant) {
return $node->getAttribute('value');
}
return self::UNDEFINED_DOMAIN;
}
}