Skip to content
This repository has been archived by the owner on Mar 21, 2018. It is now read-only.

Commit

Permalink
Merge pull request #52 from GrahamCampbell/psr-2
Browse files Browse the repository at this point in the history
PSR-2 Coding Style
  • Loading branch information
mikemand committed Dec 16, 2013
2 parents d3b2037 + 0dfbfc1 commit 0c8f070
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 221 deletions.
2 changes: 1 addition & 1 deletion provides.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"providers": [
"Kmd\Logviewer\LogviewerServiceProvider"
]
}
}
69 changes: 30 additions & 39 deletions src/Kmd/Logviewer/Logviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use Psr\Log\LogLevel;
use ReflectionClass;

class Logviewer {
class Logviewer
{
public $path;
public $sapi;
public $date;
public $level;
public $empty;

/**
* Create a new Logviewer.
*
Expand All @@ -31,7 +31,7 @@ public function __construct($app, $sapi, $date, $level = 'all')
$this->date = $date;
$this->level = $level;
}

/**
* Check if the log is empty.
*
Expand All @@ -42,7 +42,7 @@ public function isEmpty()
{
return $this->empty;
}

/**
* Open and parse the log.
*
Expand All @@ -53,56 +53,49 @@ public function log()
{
$this->empty = true;
$log = array();

$pattern = "/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\].*/";

$log_levels = $this->getLevels();

$log_file = glob($this->path . '/log-' . $this->sapi . '*-' . $this->date . '.txt');

if ( ! empty($log_file))
{

$log_file = glob($this->path.'/log-'.$this->sapi.'*-'.$this->date.'.txt');

if (!empty($log_file)) {
$this->empty = false;
$file = File::get($log_file[0]);

// There has GOT to be a better way of doing this...
preg_match_all($pattern, $file, $headings);
$log_data = preg_split($pattern, $file);

if ($log_data[0] < 1)
{

if ($log_data[0] < 1) {
$trash = array_shift($log_data);
unset($trash);
}

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

foreach ($headings as $h) {
for ($i=0, $j = count($h); $i < $j; $i++) {
foreach ($log_levels as $ll) {
if ($this->level == $ll || $this->level == 'all') {
if (strpos(strtolower($h[$i]), strtolower('.'.$ll))) {
$log[] = array('level' => $ll, 'header' => $h[$i], 'stack' => $log_data[$i]);
}
}
}
}
}
}

unset($headings);
unset($log_data);
if(strtolower(Config::get('logviewer::log_order')) == "desc"){

if (strtolower(Config::get('logviewer::log_order')) == 'desc') {
$log = array_reverse($log);
}

return $log;
}

/**
* Delete the log.
*
Expand All @@ -111,14 +104,13 @@ public function log()
*/
public function delete()
{
$log_file = glob($this->path . '/log-' . $this->sapi . '*-' . $this->date . '.txt');

if ( ! empty($log_file))
{
$log_file = glob($this->path.'/log-'.$this->sapi.'*-'.$this->date.'.txt');

if (!empty($log_file)) {
return File::delete($log_file[0]);
}
}

/**
* Get the log levels from psr/log.
*
Expand All @@ -128,7 +120,6 @@ public function delete()
public function getLevels()
{
$class = new ReflectionClass(new LogLevel);
return $constants = $class->getConstants();
return $class->getConstants();
}

}
10 changes: 6 additions & 4 deletions src/Kmd/Logviewer/LogviewerFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

use Illuminate\Support\Facades\Facade;

class LogviewerFacade extends Facade {
class LogviewerFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @access protected
* @return string
*/
protected static function getFacadeAccessor() { return 'logviewer'; }

protected static function getFacadeAccessor()
{
return 'logviewer';
}
}
108 changes: 53 additions & 55 deletions src/Kmd/Logviewer/LogviewerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
<?php namespace Kmd\Logviewer;

use Illuminate\Support\ServiceProvider;

class LogviewerServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @access protected
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @access public
* @return void
*/
public function boot()
{
$this->package('kmd/logviewer');

include __DIR__ . '/../../routes.php';
include __DIR__ . '/../../filters.php';
include __DIR__ . '/../../macros.php';
}

/**
* Register the service provider.
*
* @access public
* @return void
*/
public function register()
{
$this->app['logviewer'] = $this->app->share(function($app)
{
return new Logviewer;
});
}

/**
* Get the services provided by the provider.
*
* @access public
* @return array
*/
public function provides()
{
return array('logviewer');
}

}
<?php namespace Kmd\Logviewer;

use Illuminate\Support\ServiceProvider;

class LogviewerServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @access protected
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @access public
* @return void
*/
public function boot()
{
$this->package('kmd/logviewer');

include __DIR__.'/../../routes.php';
include __DIR__.'/../../filters.php';
include __DIR__.'/../../macros.php';
}

/**
* Register the service provider.
*
* @access public
* @return void
*/
public function register()
{
$this->app['logviewer'] = $this->app->share(function ($app) {
return new Logviewer;
});
}

/**
* Get the services provided by the provider.
*
* @access public
* @return array
*/
public function provides()
{
return array('logviewer');
}
}
6 changes: 3 additions & 3 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

return array(

'base_url' => 'logviewer',
'filters' => array(
'global' => array(),
'view' => array(),
'delete' => array(),
'delete' => array()
),
'log_dirs' => array('app' => storage_path().'/logs'),
'log_order' => 'asc', // Change to 'desc' for the latest entries first
'per_page' => 10,
'view' => 'logviewer::viewer',
'p_view' => 'pagination::slider',
'p_view' => 'pagination::slider'

);
33 changes: 11 additions & 22 deletions src/filters.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
<?php

Route::filter('logviewer.logs', function()
{
Route::filter('logviewer.logs', function () {
$logs = array();
if( ! Lang::has('logviewer::logviewer.sapi')){
if (!Lang::has('logviewer::logviewer.sapi')) {
App::setLocale('en');
}
foreach (Lang::get('logviewer::logviewer.sapi') as $sapi => $human)
{
foreach (Lang::get('logviewer::logviewer.sapi') as $sapi => $human) {
$logs[$sapi]['sapi'] = $human;
$dirs = Config::get('logviewer::log_dirs');

$files = array();

foreach ($dirs as $app => $dir)
{
$files[$app] = glob($dir . '/log-' . $sapi . '*', GLOB_BRACE);
if (is_array($files[$app]))
{
foreach ($dirs as $app => $dir) {
$files[$app] = glob($dir.'/log-'.$sapi.'*', GLOB_BRACE);
if (is_array($files[$app])) {
$files[$app] = array_reverse($files[$app]);
foreach ($files[$app] as &$file)
{
foreach ($files[$app] as &$file) {
$file = preg_replace('/.*(\d{4}-\d{2}-\d{2}).*/', '$1', basename($file));
}
}
else
{
} else {
$files[$app] = array();
}
}
Expand All @@ -36,14 +29,10 @@
View::share('logs', $logs);
});

Route::filter('logviewer.messages', function()
{
if (Session::has('success') OR Session::has('error') OR Session::has('info'))
{
Route::filter('logviewer.messages', function () {
if (Session::has('success') || Session::has('error') || Session::has('info')) {
View::share('has_messages', true);
}
else
{
} else {
View::share('has_messages', false);
}
});
21 changes: 8 additions & 13 deletions src/macros.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<?php

// Inspired by: http://forums.laravel.io/viewtopic.php?id=827
HTML::macro('nav_item', function($url, $text, $a_attr = array(), $active_class = 'active', $li_attrs = array())
{
HTML::macro('nav_item', function ($url, $text, $a_attr = array(), $active_class = 'active', $li_attrs = array()) {
$href = HTML::link($url, $text, $a_attr);
$response = '';

if( Request::is($url) || Request::is($url . '/*') )
{
if (isset($li_attrs['class']))
{
$li_attrs['class'] .= ' ' . $active_class;
}
else
{

if (Request::is($url) || Request::is($url.'/*')) {
if (isset($li_attrs['class'])) {
$li_attrs['class'] .= ' '.$active_class;
} else {
$li_attrs['class'] = $active_class;
}
}
return '<li ' . HTML::attributes($li_attrs) . '>' . $href . '</li>';

return '<li '.HTML::attributes($li_attrs).'>'.$href.'</li>';
});
Loading

0 comments on commit 0c8f070

Please sign in to comment.