Skip to content

Commit

Permalink
Merge pull request #12 from cyrildewit/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
cyrildewit authored Jul 19, 2017
2 parents 8dc96cf + b873107 commit ce06003
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 126 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to `laravel-page-visits-counter` will be documented in this
### Changed
- Improved `README.md`.
- Updated the `CHANGELOG.md` file.
- Improved the comments inside the following files: `PageVisitsCounterServiceProvider.php`, `HasPageVisitsCounter.php` and `SessionHistory.php`.

## Removed
- Removing `string` and `int` type hints from the code.
Expand Down
4 changes: 2 additions & 2 deletions LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)
# The MIT License (MIT)

Copyright (c) 2016 Nicolas Brosy
Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
50 changes: 33 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# Laravel Page Visit Counter

[![Packagist](https://img.shields.io/packagist/v/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)
[![Travis branch](https://img.shields.io/travis/cyrildewit/laravel-page-visits-counter/master.svg?style=flat-square)](https://travis-ci.org/cyrildewit/laravel-page-visits-counter)
[![StyleCI](https://styleci.io/repos/94131608/shield?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)
[![Build Status](https://travis-ci.org/cyrildewit/laravel-page-visits-counter.svg?branch=master)](https://travis-ci.org/cyrildewit/laravel-page-visits-counter)
[![Packagist](https://img.shields.io/packagist/v/cyrildewit/laravel-page-visits-counter.svg)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)
[![Total Downloads](https://img.shields.io/packagist/dt/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://packagist.org/packages/cyrildewit/laravel-page-visits-counter)
[![license](https://img.shields.io/github/license/cyrildewit/laravel-page-visits-counter.svg?style=flat-square)](https://github.com/cyrildewit/laravel-page-visits-counter/blob/master/LICENSE.md)

This package allows you to store page visits of different models into the database.

Once installed you can do stuff like this:

```php
// Return total visits of the article
$article->total_visits_count->formatted
$article->page_visits
$article->page_visits_formatted

// Return total visits of last 24 hours
$article->last_24h_visits_count->formatted
$article->page_visits_24h

// Store new visit in the databae
$article->addVisit();
Expand Down Expand Up @@ -50,6 +52,7 @@ In this documention you will find some helpful information about the use of this
* [Making a Elqouent model visitable](#making-a-eloquent-model-visitable)
* [Retrieving page visits count](#retrieving-page-visits-count)
* [Storing new visits](#storing-new-visits)
* [Sorting Model items by visit count](#sorting-model-items-by-visits-count)
3. [Configuration](#configuration)
* [Configuring the formatted number format](#configuring-the-formatted-number-format)

Expand Down Expand Up @@ -113,13 +116,18 @@ class Article extends Model

### Retrieving page visits count

Our attributes always return an object. This object contains two properties: `number` & `formatted`. As the names maybe suggests `total_visits_count->number` will return the whole number and `total_visits_count->formatted` will return a formatted string (120000 -> 120.000).

```php
$article->total_visits_count // Retrieve all counted visits
$article->last_24h_visits_count // Retrieve all counted visits from the past 24 hours
$article->last_7d_visits_count // Retrieve all counted visits from the past 7 days
$article->last_14d_visits_count // Retrieve all counted visits from the past 14 days
$article->page_visits
$article->page_visits_formatte

$article->page_visits_24h
$article->page_visits_24h_formatted

$article->page_visits_7d
$article->page_visits_7d_formatted

$article->page_visits_14d
$article->page_visits_14d_formatted

// Retrieve visits from date (past 2 weeks)
$article->retrievePageVisitsFrom(Carbon::now()->subWeeks(2));
Expand All @@ -139,6 +147,15 @@ $article->addVisit()
$article->addVisitThatExpiresAt(Carbon::now()->addHours(2))
```

### Sorting Model items by visit count

```php
// Example 1
$articles = Article::all()->sortBy('page_visits_14d');
$articles = Article::with('relatedModel')->get()->sortBy('page_visits_7d');
$articles = Article::where('status', 'published')->get()->sortBy('page_visits_24h');
```

## Configuration

### Configuring the formatted number format
Expand All @@ -152,16 +169,15 @@ return [

// ...

/*
* Number format output settings.
*/
'output-settings' => [

/*
* Set true for aut number output.
*/
'formatted-output-enabled' => true,

/*
* The following optiosn will be used inside the
* `number_format`function.
* The configured option values will be used
* inside the official php `number_format()` function.
*
* Example: 120000 ==> 120.000
* Example: 500206000 ==> 502.006.000
*/
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@
"config": {
"sort-packages": true,
"optimize-autoloader": true
},
"extra": {
"laravel": {
"providers": [
"Cyrildewit\\PageVisitsCounter\\PageVisitsCounterServiceProvider"
]
}
}
}
56 changes: 23 additions & 33 deletions config/page-visits-counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,32 @@

return [

'models' => [

/*
* This model is used by default to store
* the page visits into the database.
*/
'page-visit' => Cyrildewit\PageVisitsCounter\Models\PageVisit::class,

],

'table_names' => [

/*
* This table is used by creating the migrations
* files and default model.
*/
'page-visits' => 'page-visits',

],

'sessions' => [

'primary-session-key' => 'page-visits-counter.history',

],

/*
* The class name of the page visit Eloquent model to be used.
*/
'page_visit_model' => Cyrildewit\PageVisitsCounter\Models\PageVisit::class,

/*
* The table name of the page visits database table.
* It is used by creating the migrations files and default Eloquent model.
*/
'page_visits_table_name' => 'page-visits',

/*
* The key thas used to store page visits into the session. This is used by
* the SessionHistory class that handles the visits with expiry dates.
*/
'page_visits_history_session_key' => 'page-visits-counter.history',

/*
* Number format output settings.
*/
'output-settings' => [

/*
* Set true for formatted number output.
*/
'formatted-output-enabled' => true,

/*
* The following optiosn will be used inside the
* `number_format`function.
* The configured option values will be used
* inside the official php `number_format()` function.
*
* Example: 120000 ==> 120.000
* Example: 500206000 ==> 502.006.000
*/
Expand Down
8 changes: 4 additions & 4 deletions database/migrations/create_page_visits_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class CreatePageVisitsTable extends Migration
*/
public function up()
{
$tableNames = config('page-visits-counter.table_names');
$pageVisitsTableName = config('page-visits-counter.page_visits_table_name', 'page-visits');

Schema::create($tableNames['page-visits'], function (Blueprint $table) {
Schema::create($pageVisitsTableName, function (Blueprint $table) {
$table->increments('id')->unsigned();

$table->bigInteger('visitable_id')->unsigned();
Expand All @@ -32,8 +32,8 @@ class CreatePageVisitsTable extends Migration
*/
public function down()
{
$tableNames = config('page-visits-counter.table_names');
$pageVisitsTableName = config('page-visits-counter.page_visits_table_name', 'page-visits');

Schema::dropIfExists($tableNames['page-visits']);
Schema::dropIfExists(pageVisitsTableName);
}
}
5 changes: 1 addition & 4 deletions src/Classes/SessionHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Session;

/**
* Laravel wrapper for SessionHistory.
* Class SessionHistory.
*
* @copyright Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl)
* @author Cyril de Wit ([email protected])
Expand All @@ -27,7 +27,6 @@ class SessionHistory
*/
public function __construct()
{
// Set primary Session Key
$this->primarySessionKey = config('page-visits-counter.sessions.primary-session-key', 'page-visits-counter.history');
}

Expand All @@ -43,12 +42,10 @@ public function addToSession(Model $model, Carbon $expires_at)
// Make unique key from the inserted model
$uniqueKey = $this->fromCamelCaseToDashes(class_basename($model));

// Remove expired visits froms session
$this->removeExpiredVisitsFromSession($uniqueKey);

// Check if the item is visited, if not add to session
if (! $this->isItemVisited($uniqueKey, $model->id)) {
// Push it to the session
Session::push($this->primarySessionKey.'.'.$uniqueKey, [
'visitable_id' => $model->id,
'expires_at' => $expires_at,
Expand Down
2 changes: 1 addition & 1 deletion src/Models/PageVisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->setTable(config('page-visits-counter.table_names.page-visits', 'page-visits'));
$this->setTable(config('page-visits-counter.page_visits_table_name', 'page-visits'));
}
}
45 changes: 25 additions & 20 deletions src/PageVisitsCounterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,27 @@
use Illuminate\Support\ServiceProvider;
use Cyrildewit\PageVisitsCounter\Contracts\PageVisit as PageVisitContract;

/**
* Class PageVisitsCounterServiceProvider.
*
* @copyright Copyright (c) 2017 Cyril de Wit (http://www.cyrildewit.nl)
* @author Cyril de Wit ([email protected])
* @license https://opensource.org/licenses/MIT MIT License
*/
class PageVisitsCounterServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
// Merge the config file
$this->mergeConfigFrom(
__DIR__.'/../config/page-visits-counter.php',
'page-visits-counter'
);
}

/**
* Bootstrap PageVisitsCounter application services.
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// Publish config file
$this->publishes([
__DIR__.'/../config/page-visits-counter.php' => $this->app->configPath('page-visits-counter.php'),
], 'config');

// Publish migration file only if it doesn't exist
// Publish migration file only if it doesn't exists
if (! class_exists('CreatePageVisitsTable')) {
$timestamp = date('Y_m_d_His', time());

Expand All @@ -42,19 +34,32 @@ public function boot()
], 'migrations');
}

// Register Model Bindings
$this->registerModelBindings();
}

/**
* Regiser the application services.
*
* @return void
*/
public function register()
{
// Merge the config file
$this->mergeConfigFrom(
__DIR__.'/../config/page-visits-counter.php',
'page-visits-counter'
);
}

/**
* Register Model Bindings.
*
* @return void
*/
protected function registerModelBindings()
{
$config = $this->app->config['page-visits-counter.models'];
$config = $this->app->config['page-visits-counter'];

$this->app->bind(PageVisitContract::class, $config['page-visit']);
$this->app->bind(PageVisitContract::class, $config['page_visit_model']);
}
}
Loading

0 comments on commit ce06003

Please sign in to comment.