Skip to content

Firefly III: a personal finances manager

Refactor configuration methods into trait

James Cole2ed433c96df0
diff --git a/app/Http/Controllers/DebugController.php b/app/Http/Controllers/DebugController.php
index ab401b393d..7d66276b23 100644
--- a/app/Http/Controllers/DebugController.php
+++ b/app/Http/Controllers/DebugController.php
@@ -29,6 +29,7 @@ use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Middleware\IsDemoUser;
+use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Log;
@@ -42,6 +43,8 @@ use Route as RouteFacade;
*/
class DebugController extends Controller
{
+ use GetConfigurationData;
+
/**
* HomeController constructor.
*/
@@ -241,54 +244,6 @@ class DebugController extends Controller
return redirect(route('home'));
}
- /**
- * All packages that are installed.
- *
- * @return array
- */
- protected function collectPackages(): array // get configuration
- {
- $packages = [];
- $file = \dirname(__DIR__, 3) . '/vendor/composer/installed.json';
- if (file_exists($file)) {
- // file exists!
- $content = file_get_contents($file);
- $json = json_decode($content, true);
- foreach ($json as $package) {
- $packages[]
- = [
- 'name' => $package['name'],
- 'version' => $package['version'],
- ];
- }
- }
-
- return $packages;
- }
- /**
- * Some common combinations.
- *
- * @param int $value
- *
- * @return string
- */
- protected function errorReporting(int $value): string // get configuration
- {
- $array = [
- -1 => 'ALL errors',
- E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED => 'E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED',
- E_ALL => 'E_ALL',
- E_ALL & ~E_DEPRECATED & ~E_STRICT => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
- E_ALL & ~E_NOTICE => 'E_ALL & ~E_NOTICE',
- E_ALL & ~E_NOTICE & ~E_STRICT => 'E_ALL & ~E_NOTICE & ~E_STRICT',
- E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR => 'E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR',
- ];
- $result = (string)$value;
- if (isset($array[$value])) {
- $result = $array[$value];
- }
- return $result;
- }
}
diff --git a/app/Http/Controllers/JavascriptController.php b/app/Http/Controllers/JavascriptController.php
index 7b6c5e8f13..faf5a7872a 100644
--- a/app/Http/Controllers/JavascriptController.php
+++ b/app/Http/Controllers/JavascriptController.php
@@ -22,21 +22,22 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
-use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
+use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
-use Log;
/**
* Class JavascriptController.
*/
class JavascriptController extends Controller
{
+ use GetConfigurationData;
+
/**
* Show info about accounts.
*
@@ -140,79 +141,4 @@ class JavascriptController extends Controller
->header('Content-Type', 'text/javascript');
}
- /**
- * Get config for date range.
- *
- * @return array
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function getDateRangeConfig(): array // get configuration + get preferences.
- {
- $viewRange = app('preferences')->get('viewRange', '1M')->data;
- /** @var Carbon $start */
- $start = session('start');
- /** @var Carbon $end */
- $end = session('end');
- /** @var Carbon $first */
- $first = session('first');
- $title = sprintf('%s - %s', $start->formatLocalized($this->monthAndDayFormat), $end->formatLocalized($this->monthAndDayFormat));
- $isCustom = true === session('is_custom_range', false);
- $today = new Carbon;
- $ranges = [
- // first range is the current range:
- $title => [$start, $end],
- ];
- Log::debug(sprintf('viewRange is %s', $viewRange));
- Log::debug(sprintf('isCustom is %s', var_export($isCustom, true)));
-
- // when current range is a custom range, add the current period as the next range.
- if ($isCustom) {
- Log::debug('Custom is true.');
- $index = app('navigation')->periodShow($start, $viewRange);
- $customPeriodStart = app('navigation')->startOfPeriod($start, $viewRange);
- $customPeriodEnd = app('navigation')->endOfPeriod($customPeriodStart, $viewRange);
- $ranges[$index] = [$customPeriodStart, $customPeriodEnd];
- }
- // then add previous range and next range
- $previousDate = app('navigation')->subtractPeriod($start, $viewRange);
- $index = app('navigation')->periodShow($previousDate, $viewRange);
- $previousStart = app('navigation')->startOfPeriod($previousDate, $viewRange);
- $previousEnd = app('navigation')->endOfPeriod($previousStart, $viewRange);
- $ranges[$index] = [$previousStart, $previousEnd];
-
- $nextDate = app('navigation')->addPeriod($start, $viewRange, 0);
- $index = app('navigation')->periodShow($nextDate, $viewRange);
- $nextStart = app('navigation')->startOfPeriod($nextDate, $viewRange);
- $nextEnd = app('navigation')->endOfPeriod($nextStart, $viewRange);
- $ranges[$index] = [$nextStart, $nextEnd];
-
- // today:
- /** @var Carbon $todayStart */
- $todayStart = app('navigation')->startOfPeriod($today, $viewRange);
- /** @var Carbon $todayEnd */
- $todayEnd = app('navigation')->endOfPeriod($todayStart, $viewRange);
- if ($todayStart->ne($start) || $todayEnd->ne($end)) {
- $ranges[ucfirst((string)trans('firefly.today'))] = [$todayStart, $todayEnd];
- }
-
- // everything
- $index = (string)trans('firefly.everything');
- $ranges[$index] = [$first, new Carbon];
-
- $return = [
- 'title' => $title,
- 'configuration' => [
- 'apply' => (string)trans('firefly.apply'),
- 'cancel' => (string)trans('firefly.cancel'),
- 'from' => (string)trans('firefly.from'),
- 'to' => (string)trans('firefly.to'),
- 'customRange' => (string)trans('firefly.customRange'),
- 'start' => $start->format('Y-m-d'),
- 'end' => $end->format('Y-m-d'),
- 'ranges' => $ranges,
- ],
- ];
-
- return $return;
- }
}
diff --git a/app/Http/Controllers/Json/IntroController.php b/app/Http/Controllers/Json/IntroController.php
index b52e7a1ea7..9ad233c2b1 100644
--- a/app/Http/Controllers/Json/IntroController.php
+++ b/app/Http/Controllers/Json/IntroController.php
@@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
+use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\JsonResponse;
use Log;
@@ -30,6 +31,8 @@ use Log;
*/
class IntroController
{
+ use GetConfigurationData;
+
/**
* Returns the introduction wizard for a page.
*
@@ -135,66 +138,4 @@ class IntroController
return response()->json(['result' => sprintf('Reported demo watched for route "%s".', $route)]);
}
- /**
- * Get the basic steps from config.
- *
- * @param string $route
- *
- * @return array
- */
- protected function getBasicSteps(string $route): array // get config values
- {
- $routeKey = str_replace('.', '_', $route);
- $elements = config(sprintf('intro.%s', $routeKey));
- $steps = [];
- if (\is_array($elements) && \count($elements) > 0) {
- foreach ($elements as $key => $options) {
- $currentStep = $options;
-
- // get the text:
- $currentStep['intro'] = (string)trans('intro.' . $route . '_' . $key);
-
- // save in array:
- $steps[] = $currentStep;
- }
- }
- Log::debug(sprintf('Total basic steps for %s is %d', $routeKey, \count($steps)));
-
- return $steps;
- }
-
- /**
- * Get specific info for special routes.
- *
- * @param string $route
- * @param string $specificPage
- *
- * @return array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- protected function getSpecificSteps(string $route, string $specificPage): array // get config values
- {
- $steps = [];
- $routeKey = '';
-
- // user is on page with specific instructions:
- if (\strlen($specificPage) > 0) {
- $routeKey = str_replace('.', '_', $route);
- $elements = config(sprintf('intro.%s', $routeKey . '_' . $specificPage));
- if (\is_array($elements) && \count($elements) > 0) {
- foreach ($elements as $key => $options) {
- $currentStep = $options;
-
- // get the text:
- $currentStep['intro'] = (string)trans('intro.' . $route . '_' . $specificPage . '_' . $key);
-
- // save in array:
- $steps[] = $currentStep;
- }
- }
- }
- Log::debug(sprintf('Total specific steps for route "%s" and page "%s" (routeKey is "%s") is %d', $route, $specificPage, $routeKey, \count($steps)));
-
- return $steps;
- }
}
diff --git a/app/Http/Controllers/System/InstallController.php b/app/Http/Controllers/System/InstallController.php
index 9ea63c37d9..3c7a2039b1 100644
--- a/app/Http/Controllers/System/InstallController.php
+++ b/app/Http/Controllers/System/InstallController.php
@@ -27,6 +27,7 @@ namespace FireflyIII\Http\Controllers\System;
use Artisan;
use Exception;
use FireflyIII\Http\Controllers\Controller;
+use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\JsonResponse;
use Laravel\Passport\Passport;
use Log;
@@ -39,6 +40,7 @@ use phpseclib\Crypt\RSA;
*/
class InstallController extends Controller
{
+ use GetConfigurationData;
/** @var string Forbidden error */
public const FORBIDDEN_ERROR = 'Internal PHP function "proc_close" is disabled for your installation. Auto-migration is not possible.';
/** @var string Basedir error */
@@ -178,29 +180,5 @@ class InstallController extends Controller
return response()->json(['error' => false, 'message' => 'OK']);
}
- /**
- * Check if forbidden functions are set.
- *
- * @return bool
- */
- protected function hasForbiddenFunctions(): bool // validate system config
- {
- $list = ['proc_close'];
- $forbidden = explode(',', ini_get('disable_functions'));
- $trimmed = array_map(
- function (string $value) {
- return trim($value);
- }, $forbidden
- );
- foreach ($list as $entry) {
- if (\in_array($entry, $trimmed, true)) {
- Log::error('Method "%s" is FORBIDDEN, so the console command cannot be executed.');
-
- return true;
- }
- }
-
- return false;
- }
}
diff --git a/app/Support/Http/Controllers/GetConfigurationData.php b/app/Support/Http/Controllers/GetConfigurationData.php
new file mode 100644
index 0000000000..9e5da73394
--- /dev/null
+++ b/app/Support/Http/Controllers/GetConfigurationData.php
@@ -0,0 +1,251 @@
+<?php
+/**
+ * GetConfigurationData.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Support\Http\Controllers;
+
+
+use Carbon\Carbon;
+use Log;
+
+/**
+ * Trait GetConfigurationData
+ *
+ * @package FireflyIII\Support\Http\Controllers
+ */
+trait GetConfigurationData
+{
+ /**
+ * All packages that are installed.
+ *
+ * @return array
+ */
+ protected function collectPackages(): array // get configuration
+ {
+ $packages = [];
+ $file = \dirname(__DIR__, 4) . '/vendor/composer/installed.json';
+ if (file_exists($file)) {
+ // file exists!
+ $content = file_get_contents($file);
+ $json = json_decode($content, true);
+ foreach ($json as $package) {
+ $packages[]
+ = [
+ 'name' => $package['name'],
+ 'version' => $package['version'],
+ ];
+ }
+ }
+
+ return $packages;
+ }
+
+ /**
+ * Some common combinations.
+ *
+ * @param int $value
+ *
+ * @return string
+ */
+ protected function errorReporting(int $value): string // get configuration
+ {
+ $array = [
+ -1 => 'ALL errors',
+ E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED => 'E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED',
+ E_ALL => 'E_ALL',
+ E_ALL & ~E_DEPRECATED & ~E_STRICT => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
+ E_ALL & ~E_NOTICE => 'E_ALL & ~E_NOTICE',
+ E_ALL & ~E_NOTICE & ~E_STRICT => 'E_ALL & ~E_NOTICE & ~E_STRICT',
+ E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR => 'E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR',
+ ];
+ $result = (string)$value;
+ if (isset($array[$value])) {
+ $result = $array[$value];
+ }
+
+ return $result;
+ }
+
+ /**
+ * Get the basic steps from config.
+ *
+ * @param string $route
+ *
+ * @return array
+ */
+ protected function getBasicSteps(string $route): array // get config values
+ {
+ $routeKey = str_replace('.', '_', $route);
+ $elements = config(sprintf('intro.%s', $routeKey));
+ $steps = [];
+ if (\is_array($elements) && \count($elements) > 0) {
+ foreach ($elements as $key => $options) {
+ $currentStep = $options;
+
+ // get the text:
+ $currentStep['intro'] = (string)trans('intro.' . $route . '_' . $key);
+
+ // save in array:
+ $steps[] = $currentStep;
+ }
+ }
+ Log::debug(sprintf('Total basic steps for %s is %d', $routeKey, \count($steps)));
+
+ return $steps;
+ }
+
+ /**
+ * Get config for date range.
+ *
+ * @return array
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
+ */
+ protected function getDateRangeConfig(): array // get configuration + get preferences.
+ {
+ $viewRange = app('preferences')->get('viewRange', '1M')->data;
+ /** @var Carbon $start */
+ $start = session('start');
+ /** @var Carbon $end */
+ $end = session('end');
+ /** @var Carbon $first */
+ $first = session('first');
+ $title = sprintf('%s - %s', $start->formatLocalized($this->monthAndDayFormat), $end->formatLocalized($this->monthAndDayFormat));
+ $isCustom = true === session('is_custom_range', false);
+ $today = new Carbon;
+ $ranges = [
+ // first range is the current range:
+ $title => [$start, $end],
+ ];
+ Log::debug(sprintf('viewRange is %s', $viewRange));
+ Log::debug(sprintf('isCustom is %s', var_export($isCustom, true)));
+
+ // when current range is a custom range, add the current period as the next range.
+ if ($isCustom) {
+ Log::debug('Custom is true.');
+ $index = app('navigation')->periodShow($start, $viewRange);
+ $customPeriodStart = app('navigation')->startOfPeriod($start, $viewRange);
+ $customPeriodEnd = app('navigation')->endOfPeriod($customPeriodStart, $viewRange);
+ $ranges[$index] = [$customPeriodStart, $customPeriodEnd];
+ }
+ // then add previous range and next range
+ $previousDate = app('navigation')->subtractPeriod($start, $viewRange);
+ $index = app('navigation')->periodShow($previousDate, $viewRange);
+ $previousStart = app('navigation')->startOfPeriod($previousDate, $viewRange);
+ $previousEnd = app('navigation')->endOfPeriod($previousStart, $viewRange);
+ $ranges[$index] = [$previousStart, $previousEnd];
+
+ $nextDate = app('navigation')->addPeriod($start, $viewRange, 0);
+ $index = app('navigation')->periodShow($nextDate, $viewRange);
+ $nextStart = app('navigation')->startOfPeriod($nextDate, $viewRange);
+ $nextEnd = app('navigation')->endOfPeriod($nextStart, $viewRange);
+ $ranges[$index] = [$nextStart, $nextEnd];
+
+ // today:
+ /** @var Carbon $todayStart */
+ $todayStart = app('navigation')->startOfPeriod($today, $viewRange);
+ /** @var Carbon $todayEnd */
+ $todayEnd = app('navigation')->endOfPeriod($todayStart, $viewRange);
+ if ($todayStart->ne($start) || $todayEnd->ne($end)) {
+ $ranges[ucfirst((string)trans('firefly.today'))] = [$todayStart, $todayEnd];
+ }
+
+ // everything
+ $index = (string)trans('firefly.everything');
+ $ranges[$index] = [$first, new Carbon];
+
+ $return = [
+ 'title' => $title,
+ 'configuration' => [
+ 'apply' => (string)trans('firefly.apply'),
+ 'cancel' => (string)trans('firefly.cancel'),
+ 'from' => (string)trans('firefly.from'),
+ 'to' => (string)trans('firefly.to'),
+ 'customRange' => (string)trans('firefly.customRange'),
+ 'start' => $start->format('Y-m-d'),
+ 'end' => $end->format('Y-m-d'),
+ 'ranges' => $ranges,
+ ],
+ ];
+
+ return $return;
+ }
+
+ /**
+ * Get specific info for special routes.
+ *
+ * @param string $route
+ * @param string $specificPage
+ *
+ * @return array
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
+ */
+ protected function getSpecificSteps(string $route, string $specificPage): array // get config values
+ {
+ $steps = [];
+ $routeKey = '';
+
+ // user is on page with specific instructions:
+ if (\strlen($specificPage) > 0) {
+ $routeKey = str_replace('.', '_', $route);
+ $elements = config(sprintf('intro.%s', $routeKey . '_' . $specificPage));
+ if (\is_array($elements) && \count($elements) > 0) {
+ foreach ($elements as $key => $options) {
+ $currentStep = $options;
+
+ // get the text:
+ $currentStep['intro'] = (string)trans('intro.' . $route . '_' . $specificPage . '_' . $key);
+
+ // save in array:
+ $steps[] = $currentStep;
+ }
+ }
+ }
+ Log::debug(sprintf('Total specific steps for route "%s" and page "%s" (routeKey is "%s") is %d', $route, $specificPage, $routeKey, \count($steps)));
+
+ return $steps;
+ }
+
+ /**
+ * Check if forbidden functions are set.
+ *
+ * @return bool
+ */
+ protected function hasForbiddenFunctions(): bool // validate system config
+ {
+ $list = ['proc_close'];
+ $forbidden = explode(',', ini_get('disable_functions'));
+ $trimmed = array_map(
+ function (string $value) {
+ return trim($value);
+ }, $forbidden
+ );
+ foreach ($list as $entry) {
+ if (\in_array($entry, $trimmed, true)) {
+ Log::error('Method "%s" is FORBIDDEN, so the console command cannot be executed.');
+
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
\ No newline at end of file