Skip to content

Commit

Permalink
refactor: fix PHP styling
Browse files Browse the repository at this point in the history
  • Loading branch information
SpeedyD authored and github-actions[bot] committed Aug 19, 2024
1 parent 59f4a68 commit 189e3db
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 50 deletions.
100 changes: 52 additions & 48 deletions app/Http/Controllers/Admin/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,127 +2,131 @@

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use Auth;
use Config;

use App\Services\FileManager;

use App\Http\Controllers\Controller;
use App\Services\FileManager;
use Illuminate\Http\Request;

class LogController extends Controller
{
class LogController extends Controller {
/**
* Shows the Logs index.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getIndex()
{
$logsDirectory = storage_path() . '/logs';

if($logsDirectory && !file_exists($logsDirectory)) abort(404);
$logs = scandir($logsDirectory);
public function getIndex() {
$logsDirectory = storage_path().'/logs';

if ($logsDirectory && !file_exists($logsDirectory)) {
abort(404);
}
$logs = scandir($logsDirectory);
$logList = [];
foreach($logs as $log)
{
if(is_file($logsDirectory. '/'.$log) && str_contains($log, '.log')) $logList[] = $log;
foreach ($logs as $log) {
if (is_file($logsDirectory.'/'.$log) && str_contains($log, '.log')) {
$logList[] = $log;
}
}

return view('admin.logs.index', [
'logs' => array_reverse($logList)
'logs' => array_reverse($logList),
]);
}


/**
* Shows a specific log.
*
* @param string $name
* @param string $name
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getLog($name)
{
$logsDirectory = storage_path() . '/logs';
if(is_file($logsDirectory. '/'.$name)) {
$log = file($logsDirectory. '/'.$name, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
public function getLog($name) {
$logsDirectory = storage_path().'/logs';
if (is_file($logsDirectory.'/'.$name)) {
$log = file($logsDirectory.'/'.$name, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);

if(str_starts_with($name, 'laravel')){
if (str_starts_with($name, 'laravel')) {
$logLines = $this->parseLaravelLog($log);
}else{
} else {
$logLines = $this->parseLog($log);
}

return view('admin.logs.log', [
'name' => $name,
'log' => array_reverse($logLines)
'log' => array_reverse($logLines),
]);
}
}
abort(404);
}


/**
* Deletes a log in the log directory.
*
* @param \Illuminate\Http\Request $request
* @param App\Services\FileManager $service
* @param App\Services\FileManager $service
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postDeleteLog(Request $request, FileManager $service)
{
public function postDeleteLog(Request $request, FileManager $service) {
$request->validate(['filename' => 'required']);
$name = $request->get('filename');

if($service->deleteFile(storage_path() . '/logs/' . $name)) {
if ($service->deleteFile(storage_path().'/logs/'.$name)) {
flash('Log deleted successfully.')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}
else {
foreach($service->errors()->getMessages()['error'] as $error) flash($error)->error();
}

return redirect()->back();
}


/**
* Parse laravel logs to build collapsible stacktraces in the view.
* This probably won't work with custom formatted logs.
*
* @param mixed $log
*/
private function parseLaravelLog($log){
private function parseLaravelLog($log) {
$index = 0;
$logLines = [];
$stacktrace = [];
foreach($log as $line){
foreach ($log as $line) {
$line = trim($line);
if(str_contains($line, '{main}')) {
if(count($stacktrace) > 0) {
if (str_contains($line, '{main}')) {
if (count($stacktrace) > 0) {
$logLines[$index]['stacktrace'] = $stacktrace;
$stacktrace = [];
$index += 1;
}
} elseif (str_ends_with($line, '"}') && strlen($line) > 2){
} elseif (str_ends_with($line, '"}') && strlen($line) > 2) {
$logLines[$index]['line'] = $line;
$index += 1;
} elseif (str_starts_with($line, '#')) {
$stacktrace[] = $line;
} else {
// some log lines are sorta useless we cut them out.
if(!str_contains($line, '[stacktrace]') && !str_contains($line, 'Stack trace') && strlen($line) > 2) $logLines[$index]['line'] = $line;
if (!str_contains($line, '[stacktrace]') && !str_contains($line, 'Stack trace') && strlen($line) > 2) {
$logLines[$index]['line'] = $line;
}
}
}

return $logLines;
}

/**
* Simply parse all lines without creating stacktraces for collapse views.
* Should work for any logs.
*
* @param mixed $log
*/
private function parseLog($log){
private function parseLog($log) {
$index = 0;
$logLines = [];
foreach($log as $line){
foreach ($log as $line) {
$logLines[$index]['line'] = $line;
$index += 1;
}

return $logLines;
}
}
2 changes: 1 addition & 1 deletion config/lorekeeper/admin_sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
],
[
'name' => 'Log Viewer',
'url' => 'admin/logs'
'url' => 'admin/logs',
],
],
],
Expand Down
2 changes: 1 addition & 1 deletion routes/lorekeeper/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
});

// LOG VIEWER
Route::group(['prefix' => 'logs', 'middleware' => 'power:edit_site_settings'], function() {
Route::group(['prefix' => 'logs', 'middleware' => 'power:edit_site_settings'], function () {
Route::get('/', 'LogController@getIndex');
Route::get('/{name}', 'LogController@getLog');
Route::post('/delete', 'LogController@postDeleteLog');
Expand Down

0 comments on commit 189e3db

Please sign in to comment.