承接 esi/simple_counter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

esi/simple_counter

Composer 安装命令:

composer require esi/simple_counter

包简介

A simple web hit counter.

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality Tests PHPStan Psalm Static analysis Type Coverage Psalm Level Quality Gate Status Latest Stable Version Downloads per Month License

Simple Counter is a simple PHP counter that counts your website visitors. It has the ability to either show the count as plain text or images; and whether to count only unique hits, or all hits. (IP Based)

Important Note

  • As of v6.0.0, Simple Counter is no longer licensed under the GNU LGPLv3 license.
  • v6.0.0 is a complete rewrite of the library, and no GNU LGPLv3 licensed code remains.
  • v5.0.1 and previous are still licensed under the GNU LGPLv3 license.

With that being said, it is important to read the Upgrading section below if you are coming from an older version.

Acknowledgements

The icons used for the default image set (0-9 'png' images found in counter/images/) are licensed under the CC BY 4.0 DEED license, and were designed by StreamlineHQ.

Upgrading

Pre-v6 -> v6 is not a simple upgrade. Several things have changed in this rewrite, and there are breaking changes.

Please read UPGRADING.md first before attempting an upgrade.

Installation

To install Simple Counter, first install via composer:

composer require esi/simple_counter:^6.0

There are several options defined by default, however you will likely run into issues if you do not change some of them. More information can be found in Usage below.

  • Copy the counter directory from vendor/esi/simple_counter to your webroot
    • The counter directory contains the logs and images directory.
    • You can change the name of either directory if you wish, or skip using the counter directory all together and just move logs and images to your webroot.
      • However:
        • The ip file and counter file must remain ips.json and counter.json
        • The images must be named 0-9.
  • Make sure the ips.json and counter.json files within your logs directory are writable.

Usage

More detailed documentation is a work in progress.

Usage is fairly simple once installed. There is currently one option for the type of counter you wish to use, and that is the FlatfileStorage. A DatabaseStorage is slated for an upcoming release.

Simply add the following code to the page where you want the counter to be shown:

<?php

// Load the composer autoload file, if not already loaded
require_once 'vendor/autoload.php';

use Esi\SimpleCounter\Counter;
use Esi\SimpleCounter\Storage\FlatfileStorage;
use Esi\SimpleCounter\Configuration\FlatfileConfiguration;

/**
 * $options is an array of:
 *
 * array{
 *     logDir: string,
 *     countFile: string,
 *     ipFile: string,
 *     imageDir: string,
 *     imageExt: string,
 *     uniqueOnly: bool,
 *     asImage: bool,
 *     honorDnt: bool,
 *     visitorTextString?: string
 * }
 *
 * Default values are:
 *
 * [
 *      'logDir'            => dirname(__DIR__, 2) . '/counter/logs/',
 *      'countFile'         => 'counter.json',
 *      'ipFile'            => 'ips.json',
 *      'imageDir'          => dirname(__DIR__, 2) . '/counter/images/',
 *      'imageExt'          => '.png',
 *      'uniqueOnly'        => true,
 *      'asImage'           => false,
 *      'honorDnt'          => false,
 *      'visitorTextString' => 'You are visitor #%s',
 * ] 
 */
// Valid options are:
$options = [
    'logDir'            => '/path/to/some/dir/logs',
    'countFile'         => 'counter.json',
    'ipFile'            => 'ips.json',
    'imageDir'          => '/path/to/some/dir/images',
    'imageExt'          => '.png', // '.png', '.jpg' etc. default images are PNG images
    'asImage'           => true,   // true = images, false = plain text
    'uniqueOnly'        => true,   // true = counts only unique ip's, false = counts all,
    'honorDnt'          => false,
    'visitorTextString' => 'You are visitor #%s',
];

/**
 * Important note regarding the 'visitorTextString'. This is the text that is shown if 'asImage' is false.
 *
 * For example, by default, it would show: You are visitor #123.
 * If you wanted to change it to something like: Counter: #123,
 * you would set the 'visitorTextString' option to:
 * 
 * 'Counter: #%s'
 */

/**
 * When creating the counter instance, a Storage implementation with a valid Configuration is required.
 * Currently, Simple Counter ships with one Storage implementation, and it's corresponding Configuration:
 *
 * \Esi\SimpleCounter\Storage\FlatfileStorage
 * \Esi\SimpleCounter\Configuration\FlatfileConfiguration
 *
 * \Esi\SimpleCounter\Counter can be used as a wrapper, but it is not necessary. For example:
 *
 * $counter = new Counter(
 *     new FlatfileStorage(
 *         FlatfileConfiguration::initOptions($options)
 *     )
 * );
 */
// Pass custom options
$counter = new FlatfileStorage(
    FlatfileConfiguration::initOptions($options)
);

// ... or if you wish to use defaults
$counter = new FlatfileStorage(
    FlatfileConfiguration::initOptions()
);

// ... or maybe you only want to switch to using images, for example
$counter = new FlatfileStorage(
    FlatfileConfiguration::initOptions(['asImage' => true])
);

// Finally, call display(). You can either output it directly or save it to a variable if needed
echo $counter->display();

// ... or ...

$hitCount = $counter->display();

// ... do some stuff
echo $hitCount;

?>

Handling Errors

Simple Counter uses Exceptions for various issues that may arise throughout its process.

Currently, most exceptions fall under \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException.

These exceptions can be thrown if:

  • You pass an option that is not defined.
  • A given option's value type does not match the allowed types.
  • When reading/writing to a file encounters an error, since it is likely due to a file name/location issue.
  • For invalid directories or files, in terms of logDir, imageDir, countFile, ipFile

About

Requirements

  • PHP >= 8.2

Credits

Contributing

See CONTRIBUTING.

Bugs and feature requests are tracked on GitHub.

Contributor Covenant Code of Conduct

See CODE_OF_CONDUCT.md

Backward Compatibility Promise

See backward-compatibility.md for more information on Backwards Compatibility.

Changelog

See the CHANGELOG for more information on what has changed recently.

License

See the LICENSE for more information on the license that applies to this project.

Important Note

Simple Counter switched to the MIT license as of v6.0.

Version 5.0.1 and older is licensed under the GNU LGPL v3 License. See the <= 5.x LICENSE.md file for details.

Security

See SECURITY for more information on the security disclosure process.

esi/simple_counter 适用场景与选型建议

esi/simple_counter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40 次下载、GitHub Stars 达 26, 最近一次更新时间为 2024 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 esi/simple_counter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 40
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-11