Update deprecated Twig code.

This commit is contained in:
James Cole 2019-12-28 09:44:56 +01:00
parent 48a0eafa0c
commit 156e2e79c7
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 153 additions and 186 deletions

View File

@ -58,16 +58,8 @@ use FireflyIII\Support\Form\RuleForm;
use FireflyIII\Support\Navigation;
use FireflyIII\Support\Preferences;
use FireflyIII\Support\Steam;
use FireflyIII\Support\Twig\AmountFormat;
use FireflyIII\Support\Twig\General;
use FireflyIII\Support\Twig\Rule;
use FireflyIII\Support\Twig\TransactionGroupTwig;
use FireflyIII\Support\Twig\Translation;
use FireflyIII\Validation\FireflyValidator;
use Illuminate\Support\ServiceProvider;
use Twig;
use Twig\Extension\DebugExtension;
use TwigBridge\Extension\Loader\Functions;
use Validator;
/**
@ -85,19 +77,10 @@ class FireflyServiceProvider extends ServiceProvider
public function boot(): void
{
Validator::resolver(
function ($translator, $data, $rules, $messages) {
static function ($translator, $data, $rules, $messages) {
return new FireflyValidator($translator, $data, $rules, $messages);
}
);
//$config = app('config');
//Twig::addExtension(new Functions($config));
Twig::addExtension(new General);
Twig::addExtension(new TransactionGroupTwig);
Twig::addExtension(new Translation);
Twig::addExtension(new Rule);
Twig::addExtension(new AmountFormat);
//Twig::addExtension(new DebugExtension);
}
/**

View File

@ -25,15 +25,14 @@ namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account as AccountModel;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
use Twig\TwigFunction;
use Twig\TwigFilter;
use Twig\Extension\AbstractExtension;
/**
* Contains all amount formatting routines.
*/
class AmountFormat extends Twig_Extension
class AmountFormat extends AbstractExtension
{
/**
* {@inheritdoc}
@ -59,13 +58,13 @@ class AmountFormat extends Twig_Extension
}
/**
* @return Twig_SimpleFilter
* @return TwigFilter
*/
protected function formatAmount(): Twig_SimpleFilter
protected function formatAmount(): TwigFilter
{
return new Twig_SimpleFilter(
return new TwigFilter(
'formatAmount',
function (string $string): string {
static function (string $string): string {
$currency = app('amount')->getDefaultCurrency();
return app('amount')->formatAnything($currency, $string, true);
@ -77,11 +76,11 @@ class AmountFormat extends Twig_Extension
/**
* Will format the amount by the currency related to the given account.
*
* @return Twig_SimpleFunction
* @return TwigFunction;
*/
protected function formatAmountByAccount(): Twig_SimpleFunction
protected function formatAmountByAccount(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'formatAmountByAccount',
static function (AccountModel $account, string $amount, bool $coloured = null): string {
$coloured = $coloured ?? true;
@ -98,13 +97,13 @@ class AmountFormat extends Twig_Extension
/**
* Will format the amount by the currency related to the given account.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function formatAmountByCurrency(): Twig_SimpleFunction
protected function formatAmountByCurrency(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'formatAmountByCurrency',
function (TransactionCurrency $currency, string $amount, bool $coloured = null): string {
static function (TransactionCurrency $currency, string $amount, bool $coloured = null): string {
$coloured = $coloured ?? true;
return app('amount')->formatAnything($currency, $amount, $coloured);
@ -116,11 +115,11 @@ class AmountFormat extends Twig_Extension
/**
* Will format the amount by the currency related to the given account.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function formatAmountBySymbol(): Twig_SimpleFunction
protected function formatAmountBySymbol(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'formatAmountBySymbol',
static function (string $amount, string $symbol, int $decimalPlaces = null, bool $coloured = null): string {
@ -137,11 +136,11 @@ class AmountFormat extends Twig_Extension
}
/**
* @return Twig_SimpleFilter
* @return TwigFilter
*/
protected function formatAmountPlain(): Twig_SimpleFilter
protected function formatAmountPlain(): TwigFilter
{
return new Twig_SimpleFilter(
return new TwigFilter(
'formatAmountPlain',
static function (string $string): string {
$currency = app('amount')->getDefaultCurrency();

View File

@ -27,16 +27,15 @@ use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use League\CommonMark\CommonMarkConverter;
use Log;
use Route;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Class TwigSupport.
*/
class General extends Twig_Extension
class General extends AbstractExtension
{
/**
* @return array
@ -71,13 +70,13 @@ class General extends Twig_Extension
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function activeRoutePartial(): Twig_SimpleFunction
protected function activeRoutePartial(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'activeRoutePartial',
function (): string {
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
@ -94,11 +93,11 @@ class General extends Twig_Extension
* This function will return "active" when the current route matches the first argument (even partly)
* but, the variable $what has been set and matches the second argument.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function activeRoutePartialObjectType(): Twig_SimpleFunction
protected function activeRoutePartialObjectType(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'activeRoutePartialObjectType',
static function ($context): string {
[, $route, $objectType] = func_get_args();
@ -118,13 +117,13 @@ class General extends Twig_Extension
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function activeRouteStrict(): Twig_SimpleFunction
protected function activeRouteStrict(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'activeRouteStrict',
function (): string {
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
@ -140,11 +139,11 @@ class General extends Twig_Extension
/**
* Show account balance. Only used on the front page of Firefly III.
*
* @return Twig_SimpleFilter
* @return TwigFilter
*/
protected function balance(): Twig_SimpleFilter
protected function balance(): TwigFilter
{
return new Twig_SimpleFilter(
return new TwigFilter(
'balance',
static function (?Account $account): string {
if (null === $account) {
@ -161,11 +160,11 @@ class General extends Twig_Extension
/**
* Formats a string as a thing by converting it to a Carbon first.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function formatDate(): Twig_SimpleFunction
protected function formatDate(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'formatDate',
function (string $date, string $format): string {
$carbon = new Carbon($date);
@ -178,13 +177,13 @@ class General extends Twig_Extension
/**
* Used to convert 1024 to 1kb etc.
*
* @return Twig_SimpleFilter
* @return TwigFilter
*/
protected function formatFilesize(): Twig_SimpleFilter
protected function formatFilesize(): TwigFilter
{
return new Twig_SimpleFilter(
return new TwigFilter(
'filesize',
function (int $size): string {
static function (int $size): string {
// less than one GB, more than one MB
if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) {
return round($size / (1024 * 1024), 2) . ' MB';
@ -201,11 +200,11 @@ class General extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function getMetaField(): Twig_SimpleFunction
protected function getMetaField(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'accountGetMetaField',
static function (Account $account, string $field): string {
/** @var AccountRepositoryInterface $repository */
@ -223,11 +222,11 @@ class General extends Twig_Extension
/**
* Will return true if the user is of role X.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function hasRole(): Twig_SimpleFunction
protected function hasRole(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'hasRole',
static function (string $role): bool {
$repository = app(UserRepositoryInterface::class);
@ -241,11 +240,11 @@ class General extends Twig_Extension
}
/**
* @return Twig_SimpleFilter
* @return TwigFilter
*/
protected function markdown(): Twig_SimpleFilter
protected function markdown(): TwigFilter
{
return new Twig_SimpleFilter(
return new TwigFilter(
'markdown',
static function (string $text): string {
$converter = new CommonMarkConverter;
@ -258,11 +257,11 @@ class General extends Twig_Extension
/**
* Show icon with attachment.
*
* @return Twig_SimpleFilter
* @return TwigFilter
*/
protected function mimeIcon(): Twig_SimpleFilter
protected function mimeIcon(): TwigFilter
{
return new Twig_SimpleFilter(
return new TwigFilter(
'mimeIcon',
static function (string $string): string {
switch ($string) {
@ -337,11 +336,11 @@ class General extends Twig_Extension
/**
* Basic example thing for some views.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
protected function phpdate(): Twig_SimpleFunction
protected function phpdate(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'phpdate',
static function (string $str): string {
return date($str);

View File

@ -23,20 +23,20 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use Config;
use Twig_Extension;
use Twig_SimpleFunction;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Class Rule.
*/
class Rule extends Twig_Extension
class Rule extends AbstractExtension
{
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function allActionTriggers(): Twig_SimpleFunction
public function allActionTriggers(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'allRuleActions',
static function () {
// array of valid values for actions
@ -54,11 +54,11 @@ class Rule extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function allJournalTriggers(): Twig_SimpleFunction
public function allJournalTriggers(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'allJournalTriggers',
static function () {
return [
@ -70,11 +70,11 @@ class Rule extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function allRuleTriggers(): Twig_SimpleFunction
public function allRuleTriggers(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'allRuleTriggers',
static function () {
$ruleTriggers = array_keys(Config::get('firefly.rule-triggers'));

View File

@ -30,13 +30,13 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Log;
use Twig_Extension;
use Twig_SimpleFunction;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Class TransactionGroupTwig
*/
class TransactionGroupTwig extends Twig_Extension
class TransactionGroupTwig extends AbstractExtension
{
/** @noinspection PhpMissingParentCallCommonInspection */
/**
@ -56,11 +56,11 @@ class TransactionGroupTwig extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function groupAmount(): Twig_SimpleFunction
public function groupAmount(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'groupAmount',
static function (array $array): string {
$sums = $array['sums'];
@ -92,11 +92,11 @@ class TransactionGroupTwig extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function journalGetMetaDate(): Twig_SimpleFunction
public function journalGetMetaDate(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
if ('testing' === config('app.env')) {
@ -117,11 +117,11 @@ class TransactionGroupTwig extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function journalGetMetaField(): Twig_SimpleFunction
public function journalGetMetaField(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'journalGetMetaField',
static function (int $journalId, string $metaField) {
if ('testing' === config('app.env')) {
@ -142,11 +142,11 @@ class TransactionGroupTwig extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function journalHasMeta(): Twig_SimpleFunction
public function journalHasMeta(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = DB::table('journal_meta')
@ -163,11 +163,11 @@ class TransactionGroupTwig extends Twig_Extension
/**
* Shows the amount for a single journal array.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function journalArrayAmount(): Twig_SimpleFunction
public function journalArrayAmount(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'journalArrayAmount',
function (array $array): string {
// if is not a withdrawal, amount positive.
@ -187,11 +187,11 @@ class TransactionGroupTwig extends Twig_Extension
/**
* Shows the amount for a single journal object.
*
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function journalObjectAmount(): Twig_SimpleFunction
public function journalObjectAmount(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'journalObjectAmount',
function (TransactionJournal $journal): string {
// if is not a withdrawal, amount positive.
@ -283,6 +283,7 @@ class TransactionGroupTwig extends Twig_Extension
if ($type === TransactionType::OPENING_BALANCE && AccountType::INITIAL_BALANCE === $destinationType) {
$amount = bcmul($amount, '-1');
}
if ($type === TransactionType::TRANSFER) {
$colored = false;
}

View File

@ -22,31 +22,29 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Class Budget.
*/
class Translation extends Twig_Extension
class Translation extends AbstractExtension
{
/**
* @return array
*/
public function getFilters(): array
{
$filters = [];
$filters[] = new Twig_SimpleFilter(
'_',
function ($name) {
return (string)trans(sprintf('firefly.%s', $name));
},
['is_safe' => ['html']]
);
return $filters;
return [
new TwigFilter(
'_',
static function ($name) {
return (string)trans(sprintf('firefly.%s', $name));
},
['is_safe' => ['html']]
),
];
}
/**
@ -60,13 +58,13 @@ class Translation extends Twig_Extension
}
/**
* @return Twig_SimpleFunction
* @return TwigFunction
*/
public function journalLinkTranslation(): Twig_SimpleFunction
public function journalLinkTranslation(): TwigFunction
{
return new Twig_SimpleFunction(
return new TwigFunction(
'journalLinkTranslation',
function (string $direction, string $original) {
static function (string $direction, string $original) {
$key = sprintf('firefly.%s_%s', $original, $direction);
$translation = trans($key);
if ($key === $translation) {

View File

@ -77,9 +77,8 @@ return [
FireflyIII\Providers\RouteServiceProvider::class,
// own stuff:
// TwigBridge\ServiceProvider::class,
PragmaRX\Google2FALaravel\ServiceProvider::class,
TwigBridge\ServiceProvider::class,
/*
* More service providers.
@ -135,7 +134,6 @@ return [
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'Twig' => TwigBridge\Facade\Twig::class,
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
'Preferences' => \FireflyIII\Support\Facades\Preferences::class,
@ -149,6 +147,7 @@ return [
'PiggyBankForm' => \FireflyIII\Support\Facades\PiggyBankForm::class,
'RuleForm' => \FireflyIII\Support\Facades\RuleForm::class,
'Google2FA' => PragmaRX\Google2FALaravel\Facade::class,
'Twig' => TwigBridge\Facade\Twig::class,
],

View File

@ -1,38 +1,4 @@
<?php
/**
* twigbridge.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program 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 (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
use TwigBridge\Extension\Laravel\Auth;
use TwigBridge\Extension\Laravel\Config;
use TwigBridge\Extension\Laravel\Dump;
use TwigBridge\Extension\Laravel\Input;
use TwigBridge\Extension\Laravel\Session;
use TwigBridge\Extension\Laravel\Str;
use TwigBridge\Extension\Laravel\Translator;
use TwigBridge\Extension\Laravel\Url;
use TwigBridge\Extension\Loader\Facades;
use TwigBridge\Extension\Loader\Filters;
use TwigBridge\Extension\Loader\Functions;
use TwigBridge\Twig\Template;
/**
* This file is part of the TwigBridge package.
@ -43,6 +9,21 @@ use TwigBridge\Twig\Template;
* file that was distributed with this source code.
*/
use FireflyIII\Support\Twig\AmountFormat;
use FireflyIII\Support\Twig\General;
use FireflyIII\Support\Twig\Rule;
use FireflyIII\Support\Twig\TransactionGroupTwig;
use FireflyIII\Support\Twig\Translation;
use TwigBridge\Extension\Laravel\Dump;
use TwigBridge\Extension\Laravel\Input;
use TwigBridge\Extension\Laravel\Model;
use TwigBridge\Extension\Laravel\Str;
use TwigBridge\Extension\Laravel\Translator;
use TwigBridge\Extension\Laravel\Url;
use TwigBridge\Extension\Loader\Facades;
use TwigBridge\Extension\Loader\Filters;
use TwigBridge\Extension\Loader\Functions;
/**
* Configuration options for Twig.
*/
@ -57,7 +38,7 @@ return [
| File extension for Twig view files.
|
*/
'extension' => 'twig',
'extension' => 'twig',
/*
|--------------------------------------------------------------------------
@ -72,39 +53,39 @@ return [
// When set to true, the generated templates have a __toString() method
// that you can use to display the generated nodes.
// default: false
'debug' => env('APP_DEBUG', false),
'debug' => env('APP_DEBUG', false),
// The charset used by the templates.
// default: utf-8
'charset' => 'utf-8',
'charset' => 'utf-8',
// The base template class to use for generated templates.
// default: TwigBridge\Twig\Template
'base_template_class' => Template::class,
'base_template_class' => 'TwigBridge\Twig\Template',
// An absolute path where to store the compiled templates, or false to disable caching. If null
// then the cache file path is used.
// default: cache file storage path
'cache' => null,
'cache' => null,
// When developing with Twig, it's useful to recompile the template
// whenever the source code changes. If you don't provide a value
// for the auto_reload option, it will be determined automatically based on the debug value.
'auto_reload' => true,
'auto_reload' => true,
// If set to false, Twig will silently ignore invalid variables
// (variables and or attributes/methods that do not exist) and
// replace them with a null value. When set to true, Twig throws an exception instead.
// default: false
'strict_variables' => false,
'strict_variables' => false,
// If set to true, auto-escaping will be enabled by default for all templates.
// default: 'html'
'autoescape' => 'html',
'autoescape' => 'html',
// A flag that indicates which optimizations to apply
// (default to -1 -- all optimizations are enabled; set it to 0 to disable)
'optimizations' => -1,
'optimizations' => -1,
],
/*
@ -116,7 +97,7 @@ return [
| NOTE: these will be overwritten if you pass data into the view with the same key.
|
*/
'globals' => [],
'globals' => [],
],
'extensions' => [
@ -131,24 +112,31 @@ return [
| `Twig\Extension\DebugExtension` is enabled automatically if twig.debug is TRUE.
|
*/
'enabled' => [
'enabled' => [
Facades::class,
Filters::class,
Functions::class,
Auth::class,
Config::class,
\TwigBridge\Extension\Laravel\Auth::class,
\TwigBridge\Extension\Laravel\Config::class,
Dump::class,
Input::class,
Session::class,
\TwigBridge\Extension\Laravel\Session::class,
Str::class,
Translator::class,
Url::class,
Model::class,
// 'TwigBridge\Extension\Laravel\Gate',
// 'TwigBridge\Extension\Laravel\Form',
// 'TwigBridge\Extension\Laravel\Html',
// 'TwigBridge\Extension\Laravel\Legacy\Facades',
AmountFormat::class,
General::class,
Rule::class,
TransactionGroupTwig::class,
Translation::class,
],
/*
@ -290,7 +278,7 @@ return [
| </code>
|
*/
'filters' => [
'filters' => [
'get' => 'data_get',
],
],