2019-02-17 11:34:46 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-02-17 11:34:46 +01:00
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
use App\Models\Account;
|
2019-12-10 21:25:54 +01:00
|
|
|
use App\Models\Language;
|
2020-03-01 11:18:13 +01:00
|
|
|
use App\Utils\CurlUtils;
|
2019-12-10 21:25:54 +01:00
|
|
|
use Closure;
|
2019-02-17 11:34:46 +01:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-10 21:25:54 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2019-02-17 11:34:46 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2019-12-10 21:25:54 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-09-26 12:49:52 +02:00
|
|
|
use Illuminate\Support\Facades\Request as Input;
|
2019-02-17 11:34:46 +01:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class StartupCheck.
|
|
|
|
*/
|
|
|
|
class StartupCheck
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
// $start = microtime(true);
|
|
|
|
// Log::error('start up check');
|
2019-02-17 11:34:46 +01:00
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
if ($request->has('clear_cache')) {
|
2019-02-17 11:34:46 +01:00
|
|
|
Session::flash('message', 'Cache cleared');
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-09-11 01:31:55 +02:00
|
|
|
|
2020-05-19 14:54:22 +02:00
|
|
|
|
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
/* Make sure our cache is built */
|
|
|
|
$cached_tables = config('ninja.cached_tables');
|
|
|
|
|
2019-02-17 11:34:46 +01:00
|
|
|
foreach ($cached_tables as $name => $class) {
|
2020-03-01 11:18:13 +01:00
|
|
|
if ($request->has('clear_cache') || ! Cache::has($name)) {
|
2019-02-17 11:34:46 +01:00
|
|
|
// check that the table exists in case the migration is pending
|
|
|
|
if (! Schema::hasTable((new $class())->getTable())) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-11 01:31:55 +02:00
|
|
|
if ($name == 'payment_terms') {
|
2019-02-17 11:34:46 +01:00
|
|
|
$orderBy = 'num_days';
|
|
|
|
} elseif ($name == 'fonts') {
|
|
|
|
$orderBy = 'sort_order';
|
|
|
|
} elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) {
|
|
|
|
$orderBy = 'name';
|
|
|
|
} else {
|
|
|
|
$orderBy = 'id';
|
|
|
|
}
|
|
|
|
$tableData = $class::orderBy($orderBy)->get();
|
|
|
|
if ($tableData->count()) {
|
|
|
|
Cache::forever($name, $tableData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 13:46:45 +01:00
|
|
|
|
2019-02-17 11:34:46 +01:00
|
|
|
$response = $next($request);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|