From 27815d031163ef2a3e67d2e15affb2216d46cd16 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 31 Jan 2021 20:29:17 +0100 Subject: [PATCH] Make it easier to switch between v1/v2 layout. --- .../Controllers/Budget/IndexController.php | 2 +- app/Http/Controllers/HomeController.php | 2 +- bootstrap/app.php | 37 +++++++++++++++++++ config/breadcrumbs.php | 2 +- config/view.php | 11 +----- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.php index b7de0793ec..8a90506f15 100644 --- a/app/Http/Controllers/Budget/IndexController.php +++ b/app/Http/Controllers/Budget/IndexController.php @@ -133,7 +133,7 @@ class IndexController extends Controller // get all inactive budgets, and simply list them: $inactive = $this->repository->getInactiveBudgets(); - return view( + return prefixView( 'budgets.index', compact( 'availableBudgets', 'budgeted', 'spent', 'prevLoop', 'nextLoop', 'budgets', 'currencies', 'enableAddButton', 'periodTitle', 'defaultCurrency', 'activeDaysPassed', 'activeDaysLeft', 'inactive', 'budgets', 'start', 'end', 'sums' diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 4146d5972c..b15ec8b399 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -148,6 +148,6 @@ class HomeController extends Controller $user = auth()->user(); event(new RequestedVersionCheckStatus($user)); - return view('index', compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today')); + return prefixView('index', compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today')); } } diff --git a/bootstrap/app.php b/bootstrap/app.php index 54e123593d..2ba4fb5aa6 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -21,6 +21,7 @@ declare(strict_types=1); +use Illuminate\Contracts\View\Factory as ViewFactory; /* |-------------------------------------------------------------------------- | Create The Application @@ -56,6 +57,7 @@ if (!function_exists('str_is_equal')) { /** * @param string $left * @param string $right + * * @return bool */ function str_is_equal(string $left, string $right): bool @@ -64,6 +66,41 @@ if (!function_exists('str_is_equal')) { } } +if (!function_exists('prefixView')) { + /** + * Get the evaluated view contents for the given view. + * + * @param string|null $view + * @param \Illuminate\Contracts\Support\Arrayable|array $data + * @param array $mergeData + * + * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory + */ + function prefixView($view = null, $data = [], $mergeData = []) + { + $factory = app(ViewFactory::class); + + if (func_num_args() === 0) { + return $factory; + } + $prefixView = $view; + if ( + true === env('APP_DEBUG', false) + && null !== $view + && is_string($view)) { + // do something with view: + $layout = env('FIREFLY_III_LAYOUT', 'v1'); + $prefixView = sprintf('%s/%s', $layout, $view); + if(false ===$factory->exists($view)) { + // fall back to v1. + $prefixView = sprintf('%s/%s', 'v1', $view); + } + } + + return $factory->make($prefixView, $data, $mergeData); + } +} + $app = new Illuminate\Foundation\Application( realpath(__DIR__ . '/../') ); diff --git a/config/breadcrumbs.php b/config/breadcrumbs.php index 1764fcd984..f0c0008a81 100644 --- a/config/breadcrumbs.php +++ b/config/breadcrumbs.php @@ -39,7 +39,7 @@ return [ | */ - 'view' => 'partials/layout/breadcrumbs', + 'view' => sprintf('%s/partials/layout/breadcrumbs', env('FIREFLY_III_LAYOUT', 'v1')), /* |-------------------------------------------------------------------------- diff --git a/config/view.php b/config/view.php index 88dcd694b2..309f703e82 100644 --- a/config/view.php +++ b/config/view.php @@ -21,13 +21,6 @@ declare(strict_types=1); -// simple hack to force v2. Used for demo until next release. -$layout = env('FIREFLY_III_LAYOUT', 'v1'); -if (isset($_GET['layout']) && 'v2' === $_GET['layout'] && 'demo@firefly' === env('DEMO_USERNAME')) { - $layout = 'v2'; -} - - return [ /* |-------------------------------------------------------------------------- @@ -41,7 +34,7 @@ return [ */ 'paths' => [ - realpath(base_path(sprintf('resources/views/%s', $layout))), + realpath(base_path('resources/views')), ], /* @@ -55,6 +48,6 @@ return [ | */ - 'compiled' => realpath(storage_path(sprintf('framework/views/%s', $layout))), + 'compiled' => realpath(storage_path('framework/views')), ];