定制 williamug/versioning 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

williamug/versioning

Composer 安装命令:

composer require williamug/versioning

包简介

A PHP package that helps you display the version of your Laravel or vanilla PHP application by leveraging git version tags

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads License

A robust PHP package that helps you display your application's version by leveraging Git tags. Features include caching, multiple format options, error handling, and support for both Laravel and vanilla PHP applications.

Works with Laravel and vanilla PHP!

Quick Links:

Features

  • Multiple Version Formats: Tag, full, commit hash, or tag with commit
  • Performance: Built-in caching support to minimize Git command executions
  • Secure: Proper input sanitization and error handling
  • Laravel Integration: Seamless integration with Laravel's facade and Blade directives
  • Vanilla PHP Support: Works standalone without any framework
  • Configurable: Extensive configuration options
  • FTP Deployment Support: Works with deployments that don't include git history
  • Well-tested: Comprehensive test coverage

Requirements

  • PHP 8.2 or higher
  • Laravel 10.x, 11.x, or 12.x (for Laravel integration) or vanilla PHP
  • Git installed on your system
  • Optional: Laravel Cache for caching (automatically available in Laravel)

Installation

Install the package via Composer:

composer require williamug/versioning

Laravel Configuration (Optional)

Publish the configuration file:

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

This creates config/versioning.php where you can customize:

return [
    'repository_path' => base_path(),
    'cache' => [
        'enabled' => true,
        'ttl' => 3600, // 1 hour
        'key' => 'app_version',
    ],
    'fallback_version' => env('APP_VERSION', 'dev'),
    'format' => 'tag',
    'include_prefix' => true,
];

Usage

Vanilla PHP

Option 1: Using the Helper Function (Simplest)

require __DIR__ . '/vendor/autoload.php';

// Simple usage
echo app_version(); // v1.0.0

// Different formats
echo app_version('tag');        // v1.0.0
echo app_version('full');       // v1.0.0-5-g123abc
echo app_version('commit');     // 123abc
echo app_version('tag-commit'); // v1.0.0-123abc

Option 2: Using the Standalone Class (More Features)

require __DIR__ . '/vendor/autoload.php';

use Williamug\Versioning\StandaloneVersioning;

// Configure (optional)
StandaloneVersioning::setRepositoryPath(__DIR__);
StandaloneVersioning::setFallbackVersion('1.0.0');
StandaloneVersioning::setCaching(true, 3600);
StandaloneVersioning::setIncludePrefix(true);

// Get version
echo StandaloneVersioning::tag();           // v1.0.0
echo StandaloneVersioning::full();          // v1.0.0-5-g123abc
echo StandaloneVersioning::commit();        // 123abc
echo StandaloneVersioning::tagWithCommit(); // v1.0.0-123abc

// Clear cache when needed
StandaloneVersioning::clearCache();

See VANILLA-PHP-USAGE.md for complete standalone usage guide.

Laravel

Using the Facade

use Williamug\Versioning\Versioning;

// Get version tag
Versioning::tag(); // v1.0.0

// Get full version info
Versioning::full(); // v1.0.0-5-g123abc

// Get commit hash
Versioning::commit(); // 123abc

// Get tag with commit
Versioning::tagWithCommit(); // v1.0.0-123abc

// Clear version cache
Versioning::clearCache();

Using Blade Directives

{{-- Simple tag version --}}
<footer>
    Version: @app_version_tag
</footer>

{{-- Full version info --}}
<div>
    Build: @app_version_full
</div>

{{-- Just the commit hash --}}
<div>
    Commit: @app_version_commit
</div>

{{-- Custom format --}}
<div>
    Version: @app_version('tag-commit')
</div>

Using the Helper Function

// In your controllers or views
$version = app_version();
$commit = app_version('commit');

Configuration Options

Repository Path

Specify where your .git directory is located:

'repository_path' => base_path(), // or any absolute path

Caching

Enable caching to improve performance:

'cache' => [
    'enabled' => true,
    'ttl' => 3600, // Cache for 1 hour
    'key' => 'app_version',
],

Fallback Version

Set a default version when Git is unavailable:

'fallback_version' => env('APP_VERSION', 'dev'),

You can set this in your .env:

APP_VERSION=v1.0.0

Version Format

Choose default format:

'format' => 'tag', // Options: 'tag', 'full', 'commit', 'tag-commit'

Version Prefix

Control whether to include 'v' prefix:

'include_prefix' => false, // Displays: 1.0.0 instead of v1.0.0

Error Handling

The package gracefully handles errors:

  • Returns fallback version if Git is not installed
  • Returns fallback version if not in a Git repository
  • Returns fallback version if no tags exist
  • Catches and handles all exceptions

Testing

# Run tests
composer test

# Run tests with coverage
composer test-coverage

# Run static analysis
composer analyse

# Format code
composer format

Development

# Install dependencies
composer install

# Run Pint (code formatting)
vendor/bin/pint

# Run PHPStan (static analysis)
vendor/bin/phpstan analyse

# Run Pest (tests)
vendor/bin/pest

Common Use Cases

Display Version in Footer

<footer class="text-center py-4">
    <p>MyApp @app_version_tag | Build @app_version_commit</p>
</footer>

API Response

public function version()
{
    return response()->json([
        'version' => Versioning::tag(),
        'commit' => Versioning::commit(),
        'build_date' => now(),
    ]);
}

Admin Dashboard

public function dashboard()
{
    return view('admin.dashboard', [
        'app_version' => Versioning::full(),
        'git_commit' => Versioning::commit(),
    ]);
}

Clear Cache After Deployment

// In your deployment script
Artisan::call('cache:clear');
Versioning::clearCache();

Troubleshooting

"dev" is always displayed

  • Ensure you're in a Git repository
  • Ensure Git is installed: git --version
  • Ensure you have tags: git tag
  • Check your repository path in config

Create a tag if none exist

git tag v1.0.0
git push origin v1.0.0

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

williamug/versioning 适用场景与选型建议

williamug/versioning 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.64k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-10