Skip to content

Commit

Permalink
Log + documentation
Browse files Browse the repository at this point in the history
- Created the service provider
- created the Facade
- created the migration file
- created a config file so the user can configure it’s own db table
- created the helper methods
- Wrote the docs
  • Loading branch information
Joren Van Hocht authored and Joren Van Hocht committed May 12, 2015
1 parent 68ddc79 commit e4087fc
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Config/Tracert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'table' => 'history',

'char_sets' => [
'hash' => 'ABCDEFGHIJKLMNOPQRSTUVWabcdefghijklmnopqrstuvw0123456789',
],

];
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
# Tracert
Laravel package to log the users actions in the database

Tracert is a package for Laravel to log actions of your users to your database.

## Installation

### Composer
You can require this package through composer, just run the following line in your terminal.

```php
composer require jorenvanhocht/tracert 1.*
```

### Service Provider
Add the following line to the providers array in config/app.php

```php
'jorenvanhocht\Tracert\TracertServiceProvider',
```

If you want you can also add the facade to the aliases array in config/app.php

>**Note**: this is not required, you can make use of available helper method wich is faster.
```php
'Tracert' => 'jorenvanhocht\Blogify\Facades\Tracert',
```

### Composer update
To be sure everything is loaded properly run ```composer update``` from your terminal.

### Publish config file
If you want you can publish the config file by running the following command from your terminal

```php
php artisan vendor:publish --tag="config"
```

### Migrations
This package contains a migration file to create the table where all actions will be logged into. Run it by the following command:

```php
php artisan migrate --path="jorenvanhocht/Tracert/Migrations"
```

## Configuration
When you have published the config file you can change the name of the database table where all actions will be logged into.

The config file is located at config/Tracert.php.

## Usage

### Log an action

```php
tracert()->log('Model', 'row', 'user_id', 'Action');
```

### Retrieve actions for your activity feed
This package contains a model so you can just retrieve data like you would always do using Eloquent.

```php
use jorenvanhocht\Tracert\Models\History;

History::all();
History::whereUserId(1);
History::whereTable('table_name');

...

```

## Issues

If you find any issues pleas report them so I can fix them.


7 changes: 7 additions & 0 deletions src/Facades/Tracert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace jorenvanhocht\Tracert\Facades;

use Illuminate\Support\Facades\Facade;

class Tracert extends Facade{
protected static function getFacadeAccessor() { return 'jorenvanhocht.tracert'; }
}
28 changes: 28 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

if ( ! function_exists('tracert'))
{
/**
* Get the Blogify binding
*
* @return \Illuminate\Foundation\Application|mixed
*/
function tracert()
{
return app('jorenvanhocht.tracert');
}
}

if ( ! function_exists('objectify') )
{
/**
* Make an object of the given var
*
* @param $var
* @return mixed
*/
function objectify($var)
{
return json_decode(json_encode($var));
}
}
45 changes: 45 additions & 0 deletions src/Migrations/2015_05_12_121200_create_history_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php namespace jorenvanhocht\Tracert\Migrations;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateHistoryTable extends Migration {

protected $config;

public function __construct()
{
$this->config = objectify(config('tracert'));
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create($this->config->table, function($table)
{
$table->increments('id');
$table->string('hash', 80)->unique();
$table->string('crud_action', 10);
$table->string('table', 30);
$table->integer('row');
$table->boolean('last');
$table->integer('user_id')->unsigned();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists($this->config->table);
}

}
28 changes: 28 additions & 0 deletions src/Models/History.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace jorenvanhocht\Tracert\Models;

use Illuminate\Database\Eloquent\Model;

class History extends Model{

/**
* The database table used by the model
*
* @var string
*/
protected $table = 'history';


/**
* Set or unset the timestamps for the model
*
* @var bool
*/
public $timestamps = true;

public function __construct()
{
parent::__construct([]);
$this->table = config('tracert')['table'];
}

}
98 changes: 98 additions & 0 deletions src/Tracert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php namespace jorenvanhocht\Tracert;

use Illuminate\Database\DatabaseManager;
use jorenvanhocht\Tracert\Models\History;

class Tracert
{

/**
* @var DatabaseManager
*/
protected $db;

/**
* @var object
*/
protected $config;

/**
* @var History
*/
protected $history;

/**
* Construct the class
*
* @param History $history
* @param DatabaseManager $db
*/
public function __construct( $db, History $history)
{
$this->db = $db;
$this->history = $history;
$this->config = objectify(config('tracert'));
}


/**
* Log an action to the database
*
* @param $model
* @param $row
* @param $user_id
* @param string $action
*/
public function log($model, $row, $user_id, $action = 'created')
{
$history = new History;

$history->hash = $this->makeUniqueHash($this->config->table, 'hash');
$history->table = $model;
$history->row = $row;
$history->user_id = $user_id;
$history->crud_action = $action;

$history->save();
}

///////////////////////////////////////////////////////////////////////////
// Helper methods
///////////////////////////////////////////////////////////////////////////

/**
* @param $table
* @param $field
* @param int $min_length
* @param int $max_length
* @return string
*/
public function makeUniqueHash( $table, $field, $min_length = 5, $max_length = 20 )
{
$hash = '';
$minus = 0;

// Generate a random length for the hash between the given min and max length
$rand = rand($min_length, $max_length);

for ( $i = 0; $i < $rand; $i++ )
{
$char = rand( 0, strlen( $this->config->char_sets->hash));

// When it's not the first char from the char_set make $minus equal to 1
if( $char != 0 ? $minus = 1 : $minus = 0 );

// Add the character to the hash
$hash .= $this->config->char_sets->hash[ $char - $minus ];
}

// Check if the hash doest not exist in the given table and column
if ( ! $this->db->table($table)->where($field, '=', $hash)->get() )
{
return $hash;
}

$this->makeUniqueHash($table, $field, $min_length, $max_length);
}

}
39 changes: 39 additions & 0 deletions src/TracertServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php namespace jorenvanhocht\Tracert;


use Illuminate\Support\ServiceProvider;
use jorenvanhocht\Tracert\Models\History;

class TracertServiceProvider extends ServiceProvider {

/**
* Register the service provider
*
* @return Tracert
*/
public function register()
{
$this->app->bind('jorenvanhocht.tracert', function()
{
$db = $this->app['db'];
$model = new History;
return new Tracert($db, $model);
});
}

/**
* Load the resources
*
*/
public function boot()
{
// Publish the config file
$this->publishes([
__DIR__.'/../Config' => config_path(),
], 'config');

// Make the config file accessible even when the files are not published
$this->mergeConfigFrom(__DIR__.'/../Config/Tracert.php', 'tracert');
}

}

0 comments on commit e4087fc

Please sign in to comment.