From d3a22b51fa896dd1929dac5774c1cd654ea45515 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 12 Sep 2019 18:41:32 +0100 Subject: [PATCH] Fixing the log checker and compact() error --- .gitattributes | 2 +- .travis.yml | 6 +-- composer.json | 1 + resources/views/show.blade.php | 30 ++++++++++----- src/Commands/CheckCommand.php | 6 ++- src/Http/Controllers/LogViewerController.php | 39 ++++++++++++-------- src/Utilities/LogChecker.php | 34 ++++++++--------- 7 files changed, 69 insertions(+), 49 deletions(-) diff --git a/.gitattributes b/.gitattributes index a56c67c9..c64e4fb4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,4 +12,4 @@ .travis.yml export-ignore CONTRIBUTING.md export-ignore CODE_OF_CONDUCT.md export-ignore -phpunit.xml export-ignore +phpunit.xml.dist export-ignore diff --git a/.travis.yml b/.travis.yml index 24784a13..ac7620d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: php -sudo: false - php: - 5.6 - 7.0 @@ -24,5 +22,5 @@ script: - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - - if [ "$TRAVIS_PHP_VERSION" != "7.2" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [ "$TRAVIS_PHP_VERSION" != "7.2" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi + - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi + - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/composer.json b/composer.json index e0bd5217..44b557d7 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "license": "MIT", "require": { "php": ">=5.6", + "ext-json": "*", "psr/log": "~1.0", "arcanedev/support": "~4.1.0" }, diff --git a/resources/views/show.blade.php b/resources/views/show.blade.php index 989cb32f..68e5573f 100644 --- a/resources/views/show.blade.php +++ b/resources/views/show.blade.php @@ -1,3 +1,10 @@ + @extends('log-viewer::_template.master') @section('content') @@ -57,12 +64,16 @@
- + - @if (request()->has('query')) - - @endif - + @unless (is_null($query)) + + + + @endunless +
@@ -74,10 +85,10 @@
@if ($entries->hasPages())
- {!! $entries->appends(compact('query'))->render() !!} + {{ $entries->appends(compact('query'))->render() }} - Page {!! $entries->currentPage() !!} of {!! $entries->lastPage() !!} + Page {{ $entries->currentPage() }} of {{ $entries->lastPage() }}
@endif @@ -95,6 +106,7 @@ @forelse($entries as $key => $entry) + {{ $entry->env }} @@ -142,10 +154,10 @@ @if ($entries->hasPages()) @endif diff --git a/src/Commands/CheckCommand.php b/src/Commands/CheckCommand.php index aca4acd9..21f5f389 100644 --- a/src/Commands/CheckCommand.php +++ b/src/Commands/CheckCommand.php @@ -99,7 +99,9 @@ private function displayMessages() $rows[] = [$file, $message]; } - $this->frame('LogViewer messages'); - $this->table(['File', 'Message'], $rows); + if ( ! empty($rows)) { + $this->frame('LogViewer messages'); + $this->table(['File', 'Message'], $rows); + } } } diff --git a/src/Http/Controllers/LogViewerController.php b/src/Http/Controllers/LogViewerController.php index d40d1bcf..65015fde 100644 --- a/src/Http/Controllers/LogViewerController.php +++ b/src/Http/Controllers/LogViewerController.php @@ -8,6 +8,7 @@ use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Routing\Controller; use Illuminate\Support\Arr; +use Illuminate\Support\Collection; use Illuminate\Support\Str; /** @@ -90,38 +91,42 @@ public function listLogs(Request $request) /** * Show the log. * - * @param string $date + * @param string $date + * @param \Illuminate\Http\Request $request * * @return \Illuminate\View\View */ - public function show($date) + public function show($date, Request $request) { + $level = 'all'; $log = $this->getLogOrFail($date); + $query = $request->get('query'); $levels = $this->logViewer->levelsNames(); - $entries = $log->entries($level = 'all')->paginate($this->perPage); + $entries = $log->entries($level)->paginate($this->perPage); - return $this->view('show', compact('log', 'levels', 'level', 'search', 'entries')); + return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries')); } /** * Filter the log entries by level. * - * @param string $date - * @param string $level + * @param string $date + * @param string $level + * @param \Illuminate\Http\Request $request * * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse */ - public function showByLevel($date, $level) + public function showByLevel($date, $level, Request $request) { - $log = $this->getLogOrFail($date); - if ($level === 'all') return redirect()->route($this->showRoute, [$date]); + $log = $this->getLogOrFail($date); + $query = $request->get('query'); $levels = $this->logViewer->levelsNames(); $entries = $this->logViewer->entries($date, $level)->paginate($this->perPage); - return $this->view('show', compact('log', 'levels', 'level', 'search', 'entries')); + return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries')); } /** @@ -133,18 +138,20 @@ public function showByLevel($date, $level) * * @return \Illuminate\View\View */ - public function search($date, $level = 'all', Request $request) { - $log = $this->getLogOrFail($date); + public function search($date, $level = 'all', Request $request) + { + $query = $request->get('query'); - if (is_null($query = $request->get('query'))) - return redirect()->route('log-viewer::logs.show', [$date]); + if (is_null($query)) + return redirect()->route($this->showRoute, [$date]); + $log = $this->getLogOrFail($date); $levels = $this->logViewer->levelsNames(); $entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) { return Str::contains($value->header, $query); })->paginate($this->perPage); - return $this->view('show', compact('log', 'levels', 'level', 'query', 'entries')); + return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries')); } /** @@ -207,7 +214,7 @@ protected function view($view, $data = [], $mergeData = []) */ protected function paginate(array $data, Request $request) { - $data = collect($data); + $data = new Collection($data); $page = $request->get('page', 1); $path = $request->url(); diff --git a/src/Utilities/LogChecker.php b/src/Utilities/LogChecker.php index 40720b1b..7a510a85 100644 --- a/src/Utilities/LogChecker.php +++ b/src/Utilities/LogChecker.php @@ -265,22 +265,23 @@ private function checkLogFiles() */ private function checkLogFile($path) { - $status = true; - $file = basename($path); - $message = "The log file [$file] is valid."; - - if ($this->isSingleLogFile($file)) { - $this->status = $status = false; - $this->messages['files'][$file] = $message = - "You have a single log file in your application, you should split the [$file] into seperate log files."; + $status = true; + $filename = basename($path); + $message = "The log file [$filename] is valid."; + $pattern = $this->filesystem->getPattern(); + + if ($this->isSingleLogFile($filename)) { + $this->status = $status = false; + $this->messages['files'][$filename] = $message = + "You have a single log file in your application, you should split the [$filename] into separate log files."; } - elseif ($this->isInvalidLogDate($file)) { - $this->status = $status = false; - $this->messages['files'][$file] = $message = - "The log file [$file] has an invalid date, the format must be like laravel-YYYY-MM-DD.log."; + elseif ($this->isInvalidLogPattern($filename, $pattern)) { + $this->status = $status = false; + $this->messages['files'][$filename] = $message = + "The log file [$filename] has an invalid date, the format must be like {$pattern}."; } - $this->files[$file] = compact('filename', 'status', 'message', 'path'); + $this->files[$filename] = compact('filename', 'status', 'message', 'path'); } /** @@ -299,13 +300,12 @@ private function isSingleLogFile($file) * Check the date of the log file. * * @param string $file + * @param string $pattern * * @return bool */ - private function isInvalidLogDate($file) + private function isInvalidLogPattern($file, $pattern) { - $pattern = '/laravel-(\d){4}-(\d){2}-(\d){2}.log/'; - - return ((bool) preg_match($pattern, $file, $matches)) === false; + return ((bool) preg_match("/{$pattern}/", $file, $matches)) === false; } }