diff --git a/.travis.yml b/.travis.yml index 4b0fcb4..d772ef9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ install: - travis_retry composer install --no-interaction --prefer-source script: - - vendor/bin/phpunit + - XDEBUG_MODE=coverage vendor/bin/phpunit after_script: - wget https://scrutinizer-ci.com/ocular.phar diff --git a/README.md b/README.md index 1b9888e..54689ac 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class, Add a route in your web routes file: ```php Route::get('logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index'); + +or +// Laravel 8+ +Route::get('logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']); ``` Go to `http://myapp/logs` or some other route diff --git a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php index 3077f87..8b31ca0 100644 --- a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php +++ b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php @@ -55,23 +55,27 @@ public function __construct() public function setFolder($folder) { if (app('files')->exists($folder)) { + $this->folder = $folder; } - if(is_array($this->storage_path)) { + else if(is_array($this->storage_path)) { + foreach ($this->storage_path as $value) { + $logsPath = $value . '/' . $folder; + if (app('files')->exists($logsPath)) { $this->folder = $folder; break; } } } else { - if ($this->storage_path) { + $logsPath = $this->storage_path . '/' . $folder; if (app('files')->exists($logsPath)) { $this->folder = $folder; } - } + } } @@ -97,9 +101,11 @@ public function pathToLogFile($file) { if (app('files')->exists($file)) { // try the absolute path + return $file; } if (is_array($this->storage_path)) { + foreach ($this->storage_path as $folder) { if (app('files')->exists($folder . '/' . $file)) { // try the absolute path $file = $folder . '/' . $file; @@ -114,8 +120,9 @@ public function pathToLogFile($file) $file = $logsPath . '/' . $file; // check if requested file is really in the logs directory if (dirname($file) !== $logsPath) { - throw new \Exception('No such log file'); + throw new \Exception('No such log file: '.$file); } + return $file; } @@ -228,29 +235,51 @@ public function all() return array_reverse($log); } - /** - * @return array - */ - public function getFolders() + /**Creates a multidimensional array + * of subdirectories and files + * + * @param null $path + * + * @return array + */ + public function foldersAndFiles($path = null) { - $folders = glob($this->storage_path . '/*', GLOB_ONLYDIR); - if (is_array($this->storage_path)) { - foreach ($this->storage_path as $value) { - $folders = array_merge( - $folders, - glob($value . '/*', GLOB_ONLYDIR) - ); - } - } + $contents = array(); + $dir = $path ? $path : $this->storage_path; + foreach (scandir($dir) as $node) { + if ($node == '.' || $node == '..') continue; + $path = $dir . '\\' . $node; + if (is_dir($path)) { + $contents[$path] = $this->foldersAndFiles($path); + } else { + $contents[] = $path; + } + } + + return $contents; + } - if (is_array($folders)) { - foreach ($folders as $k => $folder) { - $folders[$k] = basename($folder); - } - } - return array_values($folders); + /**Returns an array of + * all subdirectories of specified directory + * + * @param string $folder + * + * @return array + */ + public function getFolders($folder = '') + { + $folders = []; + $listObject = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($this->storage_path.'/'.$folder, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($listObject as $fileinfo) { + if($fileinfo->isDir()) $folders[] = $fileinfo->getRealPath(); + } + return $folders; } + /** * @param bool $basename * @return array @@ -267,30 +296,81 @@ public function getFolderFiles($basename = false) */ public function getFiles($basename = false, $folder = '') { - $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log'; - $files = glob( - $this->storage_path . '/' . $folder . '/' . $pattern, - preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0 - ); - if (is_array($this->storage_path)) { - foreach ($this->storage_path as $value) { - $files = array_merge( - $files, - glob( - $value . '/' . $folder . '/' . $pattern, - preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0 - ) - ); - } - } + $files = []; + $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log'; + $fullPath = $this->storage_path.'/'.$folder; - $files = array_reverse($files); - $files = array_filter($files, 'is_file'); - if ($basename && is_array($files)) { - foreach ($files as $k => $file) { - $files[$k] = basename($file); - } - } - return array_values($files); + $listObject = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($fullPath, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($listObject as $fileinfo) { + if(!$fileinfo->isDir() && strtolower(pathinfo($fileinfo->getRealPath(), PATHINFO_EXTENSION)) == explode('.', $pattern)[1]) + $files[] = $basename ? basename($fileinfo->getRealPath()) : $fileinfo->getRealPath(); + } + return $files; + + } + + /** + * @return string + */ + public function getStoragePath() + { + return $this->storage_path; } + + /** + * @param $path + * + * @return void + */ + public function setStoragePath($path) + { + $this->storage_path = $path; + } + + public static function directoryTreeStructure($storage_path, array $array) + { + foreach ($array as $k => $v) { + if(is_dir( $k )) { + + $exploded = explode( "\\", $k ); + $show = last( $exploded ); + + echo '