sourcequartet/visitor-log 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sourcequartet/visitor-log

最新稳定版本:0.1.2

Composer 安装命令:

composer require sourcequartet/visitor-log

包简介

A package for Laravel 5 to log all visitors

README 文档

README

Scrutinizer Code Quality Build Status Code Climate

A K.I.S.S package to log your Visitor for your Laravel 5 apps.

This package allows you to log your visitor into the database with their useragent and has multiples habilities, it can find if an user is currently on your website, if a visitor is a authentificated user and others habilities 👍

It also includes jenssegers/laravel-agent based from Mobile-Detect library to allow you to identify the Useragent of a Visitor.

It is based on the excellent base from JN-Jones/visitor-log, completely reworked for Laravel 5 using Repositories pattern and Middleware.

Installation

Little thing before anything, the Laravel 'file' driver is known to have no-persistency in session storing, you should store your session through another driver, however, this package has been made to work and override session sid by IP in order to make file driver persistant, it's not ideal.. but it works.

$ composer require sourcequartet/visitor-log
$ php composer update

Configuration on Laravel 5+

Add providers to config/app.php

'providers' => [

    SourceQuartet\VisitorLog\VisitorLogServiceProvider::class,

],

Also you can add the Facade for Visitor class :

'aliases' => [

	'Visitor' => SourceQuartet\VisitorLog\VisitorLogFacade::class,

],

Publish migration and package config:

$ php artisan vendor:publish --provider="SourceQuartet\VisitorLog\VisitorLogServiceProvider"

Add the Middleware (you must)

Add this line to your App/Http/Kernel.php

protected $middleware = [
        \SourceQuartet\VisitorLog\Middleware\VisitorMiddleware::class,
    ];

You can also register it to as a route Middleware but as the package can ignore route within his own configuration, it's all you choice, I recommand to not 😋

Configuration

  • onlinetime: The time (in minutes) while a visitor is still saved
  • usermodel: Set this to the Auth Provider you're using:
    • Laravel: The package will use any package that extend Laravel Guard or will use the basic Auth Guard
    • Sentinel: Visitor-Log will try to get the User with Sentinel -- NOT READY --
  • ignore: This is an array of pages that will be ignored by Visitor-Log. Example "admin/online"

API and VisitorLog classes

The Visitor class is an interface binded and included into the Service Container as a Singleton :

  • SourceQuartet\VisitorLog\Visitor\Visitor is the VisitorManager Interface
  • SourceQuartet\VisitorLog\Visitor\VisitorManager is the direct instance of the VisitorManager, where the logic before sending data to Repository is stored, require Illuminate\Config\Repository, Illuminate\Http\Request and Jenssegers\Agent\Agent as dependencies to be constructed.
  • SourceQuartet\VisitorLog\Contracts\Visitor\VisitorContract is the VisitorRepository Interface-Contract
  • SourceQuartet\VisitorLog\Visitor\VisitorRepository is the SourceQuartet\VisitorLog\VisitorModel repository handling all the database layer, it requires SourceQuartet\VisitorLog\VisitorModel and Illuminate\Database\DatabaseManager to be constructed and is bind with his Contract called : SourceQuartet\VisitorLog\Contracts\Visitor\VisitorContract
  • SourceQuartet\VisitorLog\VisitorModel is the Visitor table Model, it manages the logging of the sid attributes and is extended by Illuminate\Database\Eloquent\Model

Facade and Visitor class methods

/** Will check whether the user with $id is online and registered as a visitor
 *  return false if the user is not an authentificated User, else, it returns true
 */
Visitor::checkOnline($id);

/** Find the current visitor using his address IP and retriving his unique SID,
 *  The SID is also stored into the session through Illuminate\Http\Request accessor
 */
Visitor::findCurrent();

/** Clear all visitor where the created_at timestamp is inferior at created_at minus config(visitor-log::onlinetime)
 *  Should not be called out of the scope of a custom Middleware.
 */
Visitor::clear($time);

/** loggedIn() - Will return all visitor that are currently authentificated through Auth or Sentinel
 *  guests() - Will return all visitor that are currently not authentificated through Auth or Sentinel
 */
Visitor::loggedIn();
Visitor::guests();

/** Find a visitor by his User id, if the user is online and authentificated, it'll return VisitorModel Collection
 *  If the $id isn't passed as an attribute, it'll throw an SourceQuartet\Exception\InvalidArgumentException, 
 *  If the user corresponding to the $id is not a visitor (offline), it'll return a null
 */
Visitor::findUser($id);

/** Find a visitor by his IP, if the user is online and authentificated, it'll return VisitorModel Collection
 *  If the $ip isn't passed as an attribute/invalid IP, it'll throw an SourceQuartet\Exception\InvalidArgumentException,
 */
Visitor::findByIp($id);

/** isUser() - Check if current Visitor is an Authentificated User, return bool (true if authentificated, false if not)
 *  isGuest() - Opposite method to isUser()
 *  getUseragent() - Get current Visitor stored Useragent
 */
Visitor::isUser();
Visitor::isGuest();
Visitor::getUseragent();

// Fetch all visitor logged into the database.
Visitor::all();

// Should not be used out of the scope of a Custom Middleware of for testing purposes.
Visitor::create(array $attributes);
Visitor::updateOrCreate(array $attributes);

// Find Visitor directly with his SID
Visitor::create($id);

// Should not be used out of the VisitorManager constructor or Custom Middleware
Visitor::setAgentDetector();

Using jenssegers/laravel-agent

  • getAgentDetector() - This method will return an Instance of Jenssegers\Agent\Agent

is('Agent') method :

Visitor::getAgentDetector()->is('Windows'); // Check if UserAgent is Windows
Visitor::getAgentDetector()->is('Firefox'); // Check if UserAgent is Firefox
Visitor::getAgentDetector()->is('iPhone'); // Check if UserAgent is iPhone
Visitor::getAgentDetector()->is('OS X'); // Check if UserAgent is OS X

Magic is-Methods :

Visitor::getAgentDetector()->isAndroidOS();
Visitor::getAgentDetector()->isNexus();
Visitor::getAgentDetector()->isSafari();
Visitor::getAgentDetector()->isMobile();
Visitor::getAgentDetector()->isTablet();

Setting up an UserAgent

Sometime, you may need to foreach or to set manually the Useragent to analyze, so it's quite simple :

$visitors = Visitor::all();
foreach($visitors as $visitor)
{
    Visitor::getAgentDetector()->setAgentToDetector($visitor->useragent);
    Visitor::getAgentDetector()->isAndroidOS();
}

Or in the case if you need to load the current Useragent :

// Just leave it empty
Visitor::getAgentDetector()->setAgentToDetector();
Visitor::getAgentDetector()->isAndroidOS();
```php

Or in the case of a single loading
```php
$visitor = Visitor::findUser($id);
Visitor::getAgentDetector()->setAgentToDetector($visitor->useragent);
Visitor::getAgentDetector()->isAndroidOS();

Others functionnality of the AgentDetector using MobileDetect and jenssegers\laravel-agent :

Please check out jenssenger\laravel-agent docs for a more complete documentation on how to use this UserAgent detector, if you want to use inbuild of VisitorLog, just replace Agent::method() by Visitor::getAgentDetector()->method() it's not simpler, and if you want use directely the Agent Facade, register it as jenssenger\laravel-agent is a dependency of this package and so, you just need to follow the installation inscruction to use it out of the scope of this package 👍

Visitor Model

The Visitor Model also provides some attributes:

  • sid: A random String which is used to identicate the visitor
  • ip: The IP of the visitor
  • page: The Page where the visitor is
  • useragent: The useragent of the visitor
  • user: The UserID of the visitor

sourcequartet/visitor-log 适用场景与选型建议

sourcequartet/visitor-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 103 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「laravel」 「visitor」 「sentry」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sourcequartet/visitor-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sourcequartet/visitor-log 我们能提供哪些服务?
定制开发 / 二次开发

基于 sourcequartet/visitor-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 103
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-06