Initial Upload
This commit is contained in:
49
lib/Twig/ByteFormatterTwigExtension.php
Normal file
49
lib/Twig/ByteFormatterTwigExtension.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2020 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - http://www.xibo.org.uk
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Xibo\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Xibo\Helper\ByteFormatter;
|
||||
|
||||
/**
|
||||
* Class ByteFormatterTwigExtension
|
||||
* @package Xibo\Twig
|
||||
*/
|
||||
class ByteFormatterTwigExtension extends AbstractExtension
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'byteFormatter';
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new \Twig\TwigFilter('byteFormat', array($this, 'byteFormat'))
|
||||
);
|
||||
}
|
||||
|
||||
public function byteFormat($bytes)
|
||||
{
|
||||
return ByteFormatter::format($bytes);
|
||||
}
|
||||
}
|
||||
64
lib/Twig/DateFormatTwigExtension.php
Normal file
64
lib/Twig/DateFormatTwigExtension.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2020 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - http://www.xibo.org.uk
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Xibo\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
|
||||
/**
|
||||
* Class DateFormatTwigExtension
|
||||
* @package Xibo\Twig
|
||||
*/
|
||||
class DateFormatTwigExtension extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new \Twig\TwigFilter('datehms', [$this, 'dateFormat'])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date
|
||||
*
|
||||
* @param string $date in seconds
|
||||
*
|
||||
* @return string formated as HH:mm:ss
|
||||
*/
|
||||
public function dateFormat($date)
|
||||
{
|
||||
return gmdate('H:i:s', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return string The extension name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'datehms';
|
||||
}
|
||||
}
|
||||
60
lib/Twig/TransExtension.php
Normal file
60
lib/Twig/TransExtension.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2019 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - http://www.xibo.org.uk
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
namespace Xibo\Twig;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
|
||||
class TransExtension extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* Returns the token parser instances to add to the existing list.
|
||||
*
|
||||
* @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
|
||||
*/
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array(new TransTokenParser());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of filters to add to the existing list.
|
||||
*
|
||||
* @return array An array of filters
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new \Twig\TwigFilter('trans', '__'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return string The extension name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'i18n';
|
||||
}
|
||||
}
|
||||
181
lib/Twig/TransNode.php
Normal file
181
lib/Twig/TransNode.php
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2025 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - https://xibosignage.com
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Xibo\Twig;
|
||||
|
||||
use Twig\Compiler;
|
||||
use Twig\Node\CheckToStringNode;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\FilterExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\Expression\TempNameExpression;
|
||||
use Twig\Node\Node;
|
||||
use Twig\Node\PrintNode;
|
||||
|
||||
/**
|
||||
* Twig Extension for supporting gettext
|
||||
*/
|
||||
class TransNode extends Node
|
||||
{
|
||||
public function __construct(
|
||||
?Node $body,
|
||||
?Node $plural = null,
|
||||
?AbstractExpression $count = null,
|
||||
?Node $notes = null,
|
||||
int $lineno = 0,
|
||||
$tag = null
|
||||
) {
|
||||
$nodes = ['body' => $body];
|
||||
if (null !== $count) {
|
||||
$nodes['count'] = $count;
|
||||
}
|
||||
if (null !== $plural) {
|
||||
$nodes['plural'] = $plural;
|
||||
}
|
||||
if (null !== $notes) {
|
||||
$nodes['notes'] = $notes;
|
||||
}
|
||||
parent::__construct($nodes, [], $lineno, $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles the node to PHP.
|
||||
*
|
||||
* @param \Twig\Compiler $compiler A Twig_Compiler instance
|
||||
*/
|
||||
public function compile(Compiler $compiler)
|
||||
{
|
||||
$compiler->addDebugInfo($this);
|
||||
|
||||
list($msg, $vars) = $this->compileString($this->getNode('body'));
|
||||
|
||||
if ($this->hasNode('plural')) {
|
||||
list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
|
||||
$vars = array_merge($vars, $vars1);
|
||||
}
|
||||
|
||||
$function = $this->getTransFunction($this->hasNode('plural'));
|
||||
|
||||
if ($this->hasNode('notes')) {
|
||||
$message = trim($this->getNode('notes')->getAttribute('data'));
|
||||
// line breaks are not allowed cause we want a single line comment
|
||||
$message = str_replace(["\n", "\r"], ' ', $message);
|
||||
$compiler->write("// notes: {$message}\n");
|
||||
}
|
||||
|
||||
if ($vars) {
|
||||
// Add a hint for xgettext so that poedit goes not treat the variable substitution as PHP format
|
||||
// https://github.com/xibosignage/xibo/issues/1284
|
||||
$compiler
|
||||
->write('/* xgettext:no-php-format */')
|
||||
->write('echo strtr(' . $function . '(')
|
||||
->subcompile($msg);
|
||||
;
|
||||
if ($this->hasNode('plural')) {
|
||||
$compiler
|
||||
->raw(', ')
|
||||
->subcompile($msg1)
|
||||
->raw(', abs(')
|
||||
->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
|
||||
->raw(')')
|
||||
;
|
||||
}
|
||||
|
||||
$compiler->raw('), array(');
|
||||
|
||||
foreach ($vars as $var) {
|
||||
if ('count' === $var->getAttribute('name')) {
|
||||
$compiler
|
||||
->string('%count%')
|
||||
->raw(' => abs(')
|
||||
->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
|
||||
->raw('), ')
|
||||
;
|
||||
} else {
|
||||
$compiler
|
||||
->string('%'.$var->getAttribute('name').'%')
|
||||
->raw(' => ')
|
||||
->subcompile($var)
|
||||
->raw(', ')
|
||||
;
|
||||
}
|
||||
}
|
||||
$compiler->raw("));\n");
|
||||
} else {
|
||||
$compiler
|
||||
->write('echo '.$function.'(')
|
||||
->subcompile($msg)
|
||||
;
|
||||
if ($this->hasNode('plural')) {
|
||||
$compiler
|
||||
->raw(', ')
|
||||
->subcompile($msg1)
|
||||
->raw(', abs(')
|
||||
->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
|
||||
->raw(')')
|
||||
;
|
||||
}
|
||||
|
||||
$compiler->raw(");\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node $body A Twig_Node instance
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function compileString(Node $body): array
|
||||
{
|
||||
if ($body instanceof NameExpression || $body instanceof ConstantExpression || $body instanceof TempNameExpression) {
|
||||
return [$body, []];
|
||||
}
|
||||
$vars = [];
|
||||
if (\count($body)) {
|
||||
$msg = '';
|
||||
foreach ($body as $node) {
|
||||
if ($node instanceof PrintNode) {
|
||||
$n = $node->getNode('expr');
|
||||
while ($n instanceof FilterExpression) {
|
||||
$n = $n->getNode('node');
|
||||
}
|
||||
while ($n instanceof CheckToStringNode) {
|
||||
$n = $n->getNode('expr');
|
||||
}
|
||||
$msg .= sprintf('%%%s%%', $n->getAttribute('name'));
|
||||
$vars[] = new NameExpression($n->getAttribute('name'), $n->getTemplateLine());
|
||||
} else {
|
||||
$msg .= $node->getAttribute('data');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$msg = $body->getAttribute('data');
|
||||
}
|
||||
return [new Node([new ConstantExpression(trim($msg), $body->getTemplateLine())]), $vars];
|
||||
}
|
||||
|
||||
private function getTransFunction(bool $plural): string
|
||||
{
|
||||
return $plural ? 'n__' : '__';
|
||||
}
|
||||
}
|
||||
116
lib/Twig/TransTokenParser.php
Normal file
116
lib/Twig/TransTokenParser.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2019 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - http://www.xibo.org.uk
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
namespace Xibo\Twig;
|
||||
|
||||
|
||||
use Twig\Node\Node;
|
||||
use Twig\Token;
|
||||
|
||||
class TransTokenParser extends \Twig\TokenParser\AbstractTokenParser
|
||||
{
|
||||
/**
|
||||
* Parses a token and returns a node.
|
||||
*
|
||||
* @param Token $token A Twig_Token instance
|
||||
*
|
||||
* @return TransNode A Twig_Node instance
|
||||
* @throws \Twig\Error\SyntaxError
|
||||
*/
|
||||
public function parse(Token $token)
|
||||
{
|
||||
$lineno = $token->getLine();
|
||||
$stream = $this->parser->getStream();
|
||||
$count = null;
|
||||
$plural = null;
|
||||
$notes = null;
|
||||
|
||||
if (!$stream->test(Token::BLOCK_END_TYPE)) {
|
||||
$body = $this->parser->getExpressionParser()->parseExpression();
|
||||
} else {
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$body = $this->parser->subparse([$this, 'decideForFork']);
|
||||
$next = $stream->next()->getValue();
|
||||
if ('plural' === $next) {
|
||||
$count = $this->parser->getExpressionParser()->parseExpression();
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$plural = $this->parser->subparse([$this, 'decideForFork']);
|
||||
if ('notes' === $stream->next()->getValue()) {
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$notes = $this->parser->subparse([$this, 'decideForEnd'], true);
|
||||
}
|
||||
} elseif ('notes' === $next) {
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$notes = $this->parser->subparse([$this, 'decideForEnd'], true);
|
||||
}
|
||||
}
|
||||
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$this->checkTransString($body, $lineno);
|
||||
|
||||
return new TransNode($body, $plural, $count, $notes, $lineno, $this->getTag());
|
||||
}
|
||||
|
||||
public function
|
||||
decideForFork(Token $token)
|
||||
{
|
||||
return $token->test(array('plural', 'notes', 'endtrans'));
|
||||
}
|
||||
|
||||
public function decideForEnd(Token $token)
|
||||
{
|
||||
return $token->test('endtrans');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tag name associated with this token parser.
|
||||
*
|
||||
* @param string The tag name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return 'trans';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node $body
|
||||
* @param $lineno
|
||||
* @throws \Twig\Error\SyntaxError
|
||||
*/
|
||||
protected function checkTransString(Node $body, $lineno)
|
||||
{
|
||||
foreach ($body as $i => $node) {
|
||||
if (
|
||||
$node instanceof \Twig\Node\TextNode
|
||||
||
|
||||
($node instanceof \Twig\Node\PrintNode && $node->getNode('expr') instanceof \Twig\Node\Expression\NameExpression)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new \Twig\Error\SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
lib/Twig/TwigMessages.php
Normal file
79
lib/Twig/TwigMessages.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2019 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - http://www.xibo.org.uk
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Xibo\Twig;
|
||||
|
||||
use Slim\Flash\Messages;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class TwigMessages extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var Messages
|
||||
*/
|
||||
protected $flash;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Messages $flash the Flash messages service provider
|
||||
*/
|
||||
public function __construct(Messages $flash)
|
||||
{
|
||||
$this->flash = $flash;
|
||||
}
|
||||
/**
|
||||
* Extension name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'slim-twig-flash';
|
||||
}
|
||||
/**
|
||||
* Callback for twig.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('flash', [$this, 'getMessages']),
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Returns Flash messages; If key is provided then returns messages
|
||||
* for that key.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMessages($key = null)
|
||||
{
|
||||
if (null !== $key) {
|
||||
return $this->flash->getMessage($key);
|
||||
}
|
||||
return $this->flash->getMessages();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user