Skip to content

Commit

Permalink
Added developer setting to show error messages in production mode
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
MasterZydra committed Feb 27, 2024
1 parent cd9e59a commit 5841689
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Types of changes: `Added`, `Changed`, `Deprecate`, `Removed`, `Fixed`, `Secruity

## [Unreleased]

## v2.2.1 - 27.02.2024 - Added developer setting

### Added
- Added developer setting to show error messages in production mode

## v2.2.0 - 27.02.2024 - New "orderBy" property for default sort order

### Added
Expand Down
30 changes: 30 additions & 0 deletions app/Http/Controllers/DeveloperToolsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers;

use Framework\Authentication\Auth;
use Framework\Facades\DeveloperTools;
use Framework\Facades\Http;
use Framework\Routing\BaseController;
use Framework\Routing\ControllerInterface;

class DeveloperToolsController extends BaseController implements ControllerInterface
{
public function execute(): void
{
Auth::checkRole('Developer');

if (Http::requestMethod() === 'GET') {
view('settings.devTools');
return;
}

if (Http::requestMethod() === 'POST') {
DeveloperTools::setShowErrorMessages(Http::param('showErrorMessages') === '1');

Http::redirect('/');
}

Http::redirect('/');
}
}
3 changes: 3 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use App\Http\Controllers\ActiveSuppliersController;
use App\Http\Controllers\DeliveryNoteController;
use App\Http\Controllers\DeveloperToolsController;
use App\Http\Controllers\EditImprintSettingsController;
use App\Http\Controllers\EditInvoiceSettingsController;
use App\Http\Controllers\EditVolumeDistributionController;
Expand Down Expand Up @@ -77,6 +78,8 @@
}

if (Auth::hasRole('Developer')) {
Router::addController('devTools', new DeveloperToolsController());
Router::addController('devTools', new DeveloperToolsController(), 'POST');
Router::addController('cli', new WebCliController());
Router::addController('cli', new WebCliController(), 'POST');
}
Expand Down
4 changes: 2 additions & 2 deletions framework/Authentication/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static function start(): void
setcookie('PHPSESSID', session_id());
}

public static function getValue(string $key, mixed $value = null): mixed
public static function getValue(string $key, mixed $default = null): mixed
{
if (!array_key_exists($key, $_SESSION)) {
return $value;
return $default;
}

return $_SESSION[$key];
Expand Down
5 changes: 3 additions & 2 deletions framework/Error/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Framework\Cli\Cli;
use Framework\Facades\DeveloperTools;
use Framework\Facades\Env;
use Framework\Facades\Http;
use Throwable;
Expand All @@ -14,7 +15,7 @@ public static function handleError(
int $errno, string $errstr, string $errfile = null, int $errline = null, array $errcontext = null
): bool {
ob_end_clean();
if (Env::isDev()) {
if (Env::isDev() || DeveloperTools::showErrorMessages()) {
self::handleDevError($errno, $errstr, $errfile, $errline, $errcontext);
exit();
} else {
Expand All @@ -26,7 +27,7 @@ public static function handleError(
public static function handleException(Throwable $exception): void
{
ob_end_clean();
if (Env::isDev()) {
if (Env::isDev() || DeveloperTools::showErrorMessages()) {
self::handleDevException($exception);
exit();
} else {
Expand Down
3 changes: 2 additions & 1 deletion framework/Error/RegisterErrorHandler.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use Framework\Error\ErrorHandler;
use Framework\Facades\DeveloperTools;
use Framework\Facades\Env;

set_error_handler(fn(int $errno, string $errstr, ?string $errfile = null, ?int $errline = null, ?array $errcontext = null) => ErrorHandler::handleError($errno, $errstr, $errfile, $errline, $errcontext));

set_exception_handler(fn(Throwable $exception) => ErrorHandler::handleException($exception));

if (Env::isDev()) {
if (Env::isDev() || DeveloperTools::showErrorMessages()) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
31 changes: 31 additions & 0 deletions framework/Facades/DeveloperTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Framework\Facades;

use Framework\Authentication\Auth;
use Framework\Authentication\Session;

class DeveloperTools
{
public static function showErrorMessages(): bool
{
if (!Auth::hasRole('Developer')) {
return false;
}

return Session::getValue('showErrorMessages') === 'true';
}

public static function setShowErrorMessages(bool $value): void
{
if (!Auth::hasRole('Developer')) {
return;
}

if ($value) {
Session::setValue('showErrorMessages', 'true');
} else {
Session::setValue('showErrorMessages', null);
}
}
}
3 changes: 2 additions & 1 deletion framework/i18n/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Framework\i18n;

use Framework\Config\Config;
use Framework\Facades\DeveloperTools;
use Framework\Facades\Env;
use Framework\Facades\File;
use Framework\Facades\Path;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static function translate(string $label, ...$values): string
}

if (!array_key_exists($lang, self::$labels) || !(array_key_exists($label, self::$labels[$lang]))) {
if (Env::isDev()) {
if (Env::isDev() || DeveloperTools::showErrorMessages()) {
throw new LabelNotFoundException($lang, $label);
}
return $label;
Expand Down
3 changes: 3 additions & 0 deletions resources/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'DeliveryNote' => 'Lieferschein',
'DeliveryNotes' => 'Lieferscheine',
'Developer' => 'Entwickler',
'DeveloperTools' => 'Entwicklungswerkzeuge',
'Edit' => 'Bearbeiten',
'EditDeliveryNote' => 'Lieferschein bearbeiten',
'EditImprint' => 'Impressum bearbeiten',
Expand Down Expand Up @@ -112,12 +113,14 @@
'ShowAllSettings' => 'Alle Einstellungen anzeigen',
'ShowAllSuppliers' => 'Alle Lieferanten anzeigen',
'ShowAllUsers' => 'Alle Benutzer anzeigen',
'ShowErrorMessages' => 'Fehlermeldungen anzeigen',
'ShowPayouts' => 'Auszahlungen anzeigen',
'ShowVolumeDistribution' => 'Mengenverteilung anzeigen',
'Street' => 'Straße',
'Subdistrict' => 'Gemarkung',
'Supplier' => 'Lieferant',
'SystemSettings' => 'Systemeinstellungen',
'Tools' => 'Werkzeuge',
'UpdatedAt' => 'Aktualisiert am',
'User' => 'Benutzer',
'UserManagement' => 'Benutzerverwaltung',
Expand Down
3 changes: 3 additions & 0 deletions resources/Lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'DeliveryNote' => 'Delivery note',
'DeliveryNotes' => 'Delivery notes',
'Developer' => 'Developer',
'DeveloperTools' => 'Developer tools',
'Edit' => 'Edit',
'EditDeliveryNote' => 'Edit delivery note',
'EditImprint' => 'Edit imprint',
Expand Down Expand Up @@ -113,12 +114,14 @@
'ShowAllSuppliers' => 'Show all suppliers',
'ShowAllUsers' => 'Show all users',
'ShowDeliveryNotes' => 'Show all delivery notes',
'ShowErrorMessages' => 'Show error messages',
'ShowPayouts' => 'Show payouts',
'ShowVolumeDistribution' => 'Show volume distribution',
'Street' => 'Street',
'Subdistrict' => 'Subdistrict',
'Supplier' => 'Supplier',
'SystemSettings' => 'System settings',
'Tools' => 'Tools',
'UpdatedAt' => 'Updated at',
'User' => 'User',
'UserManagement' => 'User management',
Expand Down
2 changes: 1 addition & 1 deletion resources/Views/about.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<table class="scrollable">
<tr>
<td>Bio-Manager Version</td>
<td class="right">2.1.0</td>
<td class="right">2.2.1</td>
</tr>
<tr>
<td><?= __('Developer') ?></td>
Expand Down
1 change: 1 addition & 0 deletions resources/Views/auth/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<h2><?= __('Developer') ?></h2>

<div class="box">
<a href="devTools"><?= __('Tools') ?></a><br>
<a href="cli">CLI</a>
</div>

Expand Down
1 change: 0 additions & 1 deletion resources/Views/entities/plot/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
use Framework\Authentication\Auth;
use Framework\Facades\Convert;
?>
<?= component('layout.header') ?>
Expand Down
19 changes: 19 additions & 0 deletions resources/Views/settings/devTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php use Framework\Authentication\Session; ?>
<?= component('layout.header') ?>

<h1><?= __('DeveloperTools') ?></h1>

<form method="post">
<label>
<input type="hidden" name="showErrorMessages" value="0">
<input type="checkbox" name="showErrorMessages" value="1"
<?php if (Session::getValue('showErrorMessages') === 'true') {
echo ' checked';
} ?>>
<?= __('ShowErrorMessages') ?>
</label><br>

<button><?= __('Save') ?></button>
</form>

<?= component('layout.footer') ?>

0 comments on commit 5841689

Please sign in to comment.