Skip to content

Commit

Permalink
add middleware log
Browse files Browse the repository at this point in the history
  • Loading branch information
khanzadimahdi committed Oct 14, 2019
1 parent ee16a08 commit 3e117e3
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 0 deletions.
40 changes: 40 additions & 0 deletions database/migrations/2019_10_14_110318_create_visits_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class CreateVisitsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('visits', function (Blueprint $table) {
$table->bigIncrements('id');
$table->mediumText('request')->nullable();
$table->text('languages')->nullable();
$table->text('useragent')->nullable();
$table->text('headers')->nullable();
$table->text('device')->nullable();
$table->text('platform')->nullable();
$table->text('browser')->nullable();
$table->text('ip')->nullable();
$table->nullableMorphs('visitable');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('visits');
}
}
Empty file modified resources/images/visitor.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/Contracts/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

interface Driver
{
/**
* Retrieve request's data
*
* @return array
*/
public function request() : array;

/**
* Retrieve agent.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Drivers/JenssegersAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function __construct(Request $request)
$this->parser = $this->initParser();
}

/**
* Retrieve request's data
*
* @return array
*/
public function request() : array
{
return $this->request->all();
}

/**
* Retrieve agent.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Drivers/UAParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function __construct(Request $request)
$this->parser = $this->initParser();
}

/**
* Retrieve request's data
*
* @return array
*/
public function request() : array
{
return $this->request->all();
}

/**
* Retrieve agent.
*
Expand Down
40 changes: 40 additions & 0 deletions src/Middlewares/LogVisits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Shetabit\Visitor\Middlewares;

use Closure;
use Illuminate\Database\Eloquent\Model;

class LogVisits
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$logHasSaved = false;

// create log for first binded model
foreach ($request->route()->parameters() as $parameter) {
if ($parameter instanceof Model) {
visitor()->visit($parameter);

$logHasSaved = true;

break;
}
}

// create log for normal visits
if (!$logHasSaved) {
visitor()->visit();
}

return $next($request);
}
}
45 changes: 45 additions & 0 deletions src/Models/Visit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Shetabit\Visitor\Models;

use Illuminate\Database\Eloquent\Model;

class Visit extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'visits';

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $fillable = [
'request', 'languages', 'useragent',
'headers', 'device', 'platform',
'browser', 'ip',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'request' => 'array',
'languages' => 'array',
'headers' => 'array',
];

/**
* Get the owning visitable model.
*/
public function visitable()
{
return $this->morphTo();
}
}
10 changes: 10 additions & 0 deletions src/Provider/VisitorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public function boot()
'config'
);

/**
* Migrations that needs to be done by user.
*/
$this->publishes(
[
__DIR__.'/../../database/migrations/' => database_path('migrations')
],
'migrations'
);

$this->registerMacroHelpers();
}

Expand Down
59 changes: 59 additions & 0 deletions src/Traits/Visitable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Shetabit\Visitor\Traits;

use Shetabit\Visitor\Models\Visit;

trait Visitable
{
/**
* Get all of the post's comments.
*
* @return mixed
*/
public function visits()
{
return $this->morphMany(Visit::class, 'visitable');
}

/**
* Create a visit log
*
* @return mixed
*/
public function visit()
{
$visitor = app('shetabit-visitor');

return $this->visits()->create([
'request' => $visitor->request(),
'languages' => $visitor->languages(),
'useragent' => $visitor->userAgent(),
'headers' => $visitor->httpHeaders(),
'device' => $visitor->device(),
'platform' => $visitor->platform(),
'browser' => $visitor->browser(),
'ip' => $visitor->ip(),
]);
}

/**
* Alias for visits.
*
* @return mixed
*/
public function views()
{
return $this->visits();
}

/**
* Alias for visit
*
* @return mixed
*/
public function view()
{
return $this->visit();
}
}
47 changes: 47 additions & 0 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Shetabit\Visitor;

use Illuminate\Database\Eloquent\Model;
use Shetabit\Visitor\Contracts\Driver;
use Shetabit\Visitor\Exceptions\DriverNotFoundException;
use Shetabit\Visitor\Models\Visit;

class Visitor implements Driver
{
Expand Down Expand Up @@ -59,6 +61,16 @@ public function via($driver)
return $this;
}

/**
* Retrieve request's data
*
* @return array
*/
public function request() : array
{
return $this->getDriverInstance()->request();
}

/**
* Retrieve agent.
*
Expand Down Expand Up @@ -143,6 +155,41 @@ public function ip() : string
return $this->getDriverInstance()->ip();
}

/**
* Create a visit log.
*
* @param Model $model
*/
public function visit(Model $model = null)
{
if (method_exists($model, 'visit')) {
$visit = $model->visit();
} else {
$visit = Visit::create([
'request' => $this->request(),
'languages' => $this->languages(),
'useragent' => $this->userAgent(),
'headers' => $this->httpHeaders(),
'device' => $this->device(),
'platform' => $this->platform(),
'browser' => $this->browser(),
'ip' => $this->ip(),
]);
}

return $visit;
}

/**
* Alias for visit.
*
* @param Model $model
*/
public function view(Model $model)
{
return $this->visit($model);
}

/**
* Retrieve current driver instance or generate new one.
*
Expand Down
Empty file modified src/helpers.php
100644 → 100755
Empty file.

0 comments on commit 3e117e3

Please sign in to comment.