From 900e8202e6906094469cdbf72c2862e9a330b360 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 16 Apr 2018 20:21:28 +0200 Subject: [PATCH] Remove some deprecated functions. --- app/Providers/FireflyServiceProvider.php | 2 + app/Support/Twig/Extension/Account.php | 51 ++++++++++++++++++++++ app/Support/Twig/General.php | 4 +- app/Support/Twig/Loader/AccountLoader.php | 52 +++++++++++++++++++++++ resources/views/list/accounts.twig | 2 +- resources/views/reports/index.twig | 4 +- 6 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 app/Support/Twig/Extension/Account.php create mode 100644 app/Support/Twig/Loader/AccountLoader.php diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 6bea0cca5b..842103b20e 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -61,6 +61,7 @@ use FireflyIII\Support\Steam; use FireflyIII\Support\Twig\AmountFormat; use FireflyIII\Support\Twig\General; use FireflyIII\Support\Twig\Journal; +use FireflyIII\Support\Twig\Loader\AccountLoader; use FireflyIII\Support\Twig\Loader\TransactionJournalLoader; use FireflyIII\Support\Twig\Loader\TransactionLoader; use FireflyIII\Support\Twig\PiggyBank; @@ -93,6 +94,7 @@ class FireflyServiceProvider extends ServiceProvider $config = app('config'); Twig::addExtension(new Functions($config)); Twig::addRuntimeLoader(new TransactionLoader); + Twig::addRuntimeLoader(new AccountLoader); Twig::addRuntimeLoader(new TransactionJournalLoader); Twig::addExtension(new PiggyBank); Twig::addExtension(new General); diff --git a/app/Support/Twig/Extension/Account.php b/app/Support/Twig/Extension/Account.php new file mode 100644 index 0000000000..c3e61b32ba --- /dev/null +++ b/app/Support/Twig/Extension/Account.php @@ -0,0 +1,51 @@ +. + */ +declare(strict_types=1); + +namespace FireflyIII\Support\Twig\Extension; + +use FireflyIII\Models\Account as AccountModel; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use Twig_Extension; + +/** + * Class Account. + */ +class Account extends Twig_Extension +{ + /** + * @param AccountModel $account + * @param string $field + * + * @return string + */ + public function getMetaField(AccountModel $account, string $field): string + { + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + $result = $repository->getMetaValue($account, $field); + if (null === $result) { + return ''; + } + + return $result; + } +} diff --git a/app/Support/Twig/General.php b/app/Support/Twig/General.php index eb53c2311d..ca06db2ada 100644 --- a/app/Support/Twig/General.php +++ b/app/Support/Twig/General.php @@ -29,6 +29,7 @@ use Route; use Twig_Extension; use Twig_SimpleFilter; use Twig_SimpleFunction; +use FireflyIII\Support\Twig\Extension\Account as AccountExtension; /** * Class TwigSupport. @@ -58,12 +59,11 @@ class General extends Twig_Extension $this->getCurrencySymbol(), $this->phpdate(), $this->env(), - //$this->getAmountFromJournal(), $this->activeRouteStrict(), - //$this->steamPositive(), $this->activeRoutePartial(), $this->activeRoutePartialWhat(), $this->formatDate(), + new Twig_SimpleFunction('accountGetMetaField', [AccountExtension::class, 'getMetaField']), ]; } diff --git a/app/Support/Twig/Loader/AccountLoader.php b/app/Support/Twig/Loader/AccountLoader.php new file mode 100644 index 0000000000..be92e6586d --- /dev/null +++ b/app/Support/Twig/Loader/AccountLoader.php @@ -0,0 +1,52 @@ +. + */ +declare(strict_types=1); + +namespace FireflyIII\Support\Twig\Loader; + +use FireflyIII\Support\Twig\Extension\Account; +use Twig_RuntimeLoaderInterface; + +/** + * Class AccountLoader. + */ +class AccountLoader implements Twig_RuntimeLoaderInterface +{ + /** + * Creates the runtime implementation of a Twig element (filter/function/test). + * + * @param string $class A runtime class + * + * @return object|null The runtime instance or null if the loader does not know how to create the runtime for this class + */ + public function load($class) + { + // implement the logic to create an instance of $class + // and inject its dependencies + // most of the time, it means using your dependency injection container + + if (Account::class === $class) { + return app(Account::class); + } + + return null; + } +} diff --git a/resources/views/list/accounts.twig b/resources/views/list/accounts.twig index 90c69be280..7d016cbf77 100644 --- a/resources/views/list/accounts.twig +++ b/resources/views/list/accounts.twig @@ -40,7 +40,7 @@ {% endfor %} {% endif %} - {{ account.iban }}{% if account.iban == '' %}{{ account.getMeta('accountNumber') }}{% endif %} + {{ account.iban }}{% if account.iban == '' %}{{ accountGetMetaField(account, 'accountNumber') }}{% endif %} {{ formatAmountByAccount(account, account.endBalance) }} diff --git a/resources/views/reports/index.twig b/resources/views/reports/index.twig index fac9b2b1b8..ea2ef75733 100644 --- a/resources/views/reports/index.twig +++ b/resources/views/reports/index.twig @@ -41,8 +41,8 @@ {% for account in accounts %} {% endfor %}