Skip to content

Commit

Permalink
remove static
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed Jul 16, 2018
1 parent 1d17068 commit a9ba2e8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
62 changes: 31 additions & 31 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Rap2hpoutre\LaravelLogViewer;

use Psr\Log\LogLevel;

/**
* Class LaravelLogViewer
* @package Rap2hpoutre\LaravelLogViewer
Expand All @@ -12,14 +10,14 @@ class LaravelLogViewer
/**
* @var string file
*/
private static $file;
private $file;

/**
* @var string folder
*/
private static $folder;
private $folder;

private static $levels_classes = [
private $levels_classes = [
'debug' => 'info',
'info' => 'info',
'notice' => 'info',
Expand All @@ -32,7 +30,7 @@ class LaravelLogViewer
'failed' => 'warning',
];

private static $levels_imgs = [
private $levels_imgs = [
'debug' => 'info-circle',
'info' => 'info-circle',
'notice' => 'info-circle',
Expand All @@ -49,7 +47,7 @@ class LaravelLogViewer
* Log levels that are used
* @var array
*/
private static $log_levels = [
private $log_levels = [
'emergency',
'alert',
'critical',
Expand All @@ -67,24 +65,25 @@ class LaravelLogViewer
/**
* @param string $folder
*/
public static function setFolder($folder)
public function setFolder($folder)
{
$logsPath = storage_path('logs') . '/' . $folder;

if (app('files')->exists($logsPath)) {
self::$folder = $folder;
$this->folder = $folder;
}
}

/**
* @param string $file
* @throws \Exception
*/
public static function setFile($file)
public function setFile($file)
{
$file = self::pathToLogFile($file);
$file = $this->pathToLogFile($file);

if (app('files')->exists($file)) {
self::$file = $file;
$this->file = $file;
}
}

Expand All @@ -93,10 +92,10 @@ public static function setFile($file)
* @return string
* @throws \Exception
*/
public static function pathToLogFile($file)
public function pathToLogFile($file)
{
$logsPath = storage_path('logs');
$logsPath .= (self::$folder) ? '/' . self::$folder : '' ;
$logsPath .= ($this->folder) ? '/' . $this->folder : '' ;

if (app('files')->exists($file)) { // try the absolute path
return $file;
Expand All @@ -115,39 +114,39 @@ public static function pathToLogFile($file)
/**
* @return string
*/
public static function getFolderName()
public function getFolderName()
{
return self::$folder;
return $this->folder;
}

/**
* @return string
*/
public static function getFileName()
public function getFileName()
{
return basename(self::$file);
return basename($this->file);
}

/**
* @return array
*/
public static function all()
public function all()
{
$log = array();

$pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/';

if (!self::$file) {
$log_file = (!self::$folder) ? self::getFiles() : self::getFolderFiles();
if (!$this->file) {
$log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
if(!count($log_file)) {
return [];
}
self::$file = $log_file[0];
$this->file = $log_file[0];
}

if (app('files')->size(self::$file) > self::MAX_FILE_SIZE) return null;
if (app('files')->size($this->file) > self::MAX_FILE_SIZE) return null;

$file = app('files')->get(self::$file);
$file = app('files')->get($this->file);

preg_match_all($pattern, $file, $headings);

Expand All @@ -163,7 +162,7 @@ public static function all()

foreach ($headings as $h) {
for ($i=0, $j = count($h); $i < $j; $i++) {
foreach (self::$log_levels as $level) {
foreach ($this->log_levels as $level) {
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {

preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)' . $level . ': (.*?)( in .*?:[0-9]+)?$/i', $h[$i], $current);
Expand All @@ -172,8 +171,8 @@ public static function all()
$log[] = array(
'context' => $current[3],
'level' => $level,
'level_class' => self::$levels_classes[$level],
'level_img' => self::$levels_imgs[$level],
'level_class' => $this->levels_classes[$level],
'level_img' => $this->levels_imgs[$level],
'date' => $current[1],
'text' => $current[4],
'in_file' => isset($current[5]) ? $current[5] : null,
Expand Down Expand Up @@ -209,7 +208,7 @@ public static function all()
/**
* @return array
*/
public static function getFolders()
public function getFolders()
{
$folders = glob(storage_path() . '/logs/*', GLOB_ONLYDIR);
if (is_array($folders)) {
Expand All @@ -224,16 +223,17 @@ public static function getFolders()
* @param bool $basename
* @return array
*/
public static function getFolderFiles($basename = false)
public function getFolderFiles($basename = false)
{
return self::getFiles($basename, self::$folder);
return $this->getFiles($basename, $this->folder);
}

/**
* @param bool $basename
* @param string $folder
* @return array
*/
public static function getFiles($basename = false, $folder = '')
public function getFiles($basename = false, $folder = '')
{
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
$files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0);
Expand Down
30 changes: 19 additions & 11 deletions src/controllers/LogViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ class BaseController extends \Laravel\Lumen\Routing\Controller {}
*/
class LogViewerController extends BaseController
{
/**
* @var
*/
protected $request;
/**
* @var LaravelLogViewer
*/
private $log_viewer;

/**
* LogViewerController constructor.
*/
public function __construct ()
{
$this->log_viewer = new LaravelLogViewer();
$this->request = app('request');
}

Expand All @@ -32,24 +40,24 @@ public function index()
{
$folderFiles = [];
if ($this->request->input('f')) {
LaravelLogViewer::setFolder(Crypt::decrypt($this->request->input('f')));
$folderFiles = LaravelLogViewer::getFolderFiles(true);
$this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f')));
$folderFiles = $this->log_viewer->getFolderFiles(true);
}
if ($this->request->input('l')) {
LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l')));
$this->log_viewer->setFile(Crypt::decrypt($this->request->input('l')));
}

if ($early_return = $this->earlyReturn()) {
return $early_return;
}

$data = [
'logs' => LaravelLogViewer::all(),
'folders' => LaravelLogViewer::getFolders(),
'current_folder' => LaravelLogViewer::getFolderName(),
'logs' => $this->log_viewer->all(),
'folders' => $this->log_viewer->getFolders(),
'current_folder' => $this->log_viewer->getFolderName(),
'folder_files' => $folderFiles,
'files' => LaravelLogViewer::getFiles(true),
'current_file' => LaravelLogViewer::getFileName(),
'files' => $this->log_viewer->getFiles(true),
'current_file' => $this->log_viewer->getFileName(),
'standardFormat' => true,
];

Expand Down Expand Up @@ -80,8 +88,8 @@ private function earlyReturn()
app('files')->delete($this->pathFromInput('del'));
return $this->redirect($this->request->url());
} elseif ($this->request->has('delall')) {
foreach(LaravelLogViewer::getFiles(true) as $file){
app('files')->delete(LaravelLogViewer::pathToLogFile($file));
foreach($this->log_viewer->getFiles(true) as $file){
app('files')->delete($this->log_viewer->pathToLogFile($file));
}
return $this->redirect($this->request->url());
}
Expand All @@ -95,7 +103,7 @@ private function earlyReturn()
*/
private function pathFromInput($input_string)
{
return LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
return $this->log_viewer->pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
}

/**
Expand Down

0 comments on commit a9ba2e8

Please sign in to comment.