定制 elegantmedia/php-toolkit 二次开发

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

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

elegantmedia/php-toolkit

Composer 安装命令:

composer require elegantmedia/php-toolkit

包简介

Utility and helper toolkit based on PHP best practises. No dependencies.

README 文档

README

Latest Version on Packagist Software License Quality Score Build Status Coverage Status

This library holds commonly used helper functions used in many business applications. All features used PHP's native APIs and there are no third party dependencies.

Installation

Install via composer

composer require elegantmedia/php-toolkit

Available Commands

Array Helpers

use \ElegantMedia\PHPToolkit\Arr;

// Check if an array is an associative array
Arr::isAssoc($array);

// get matching subset of array. Similar to `array_intersect`, but does recursively.
Arr::intersectRecursive($source, $subset);

// Implode, but exclude empty values
Arr::implodeIgnoreEmpty($glue, $array);

// Sway a key on an associative array
Arr::swapKey($array, $oldKey, $newKey, $recursive = false);

// Key values of an array to another key
Arr::keyBy($array, $key);

Conversions

use \ElegantMedia\PHPToolkit\Conversion;

// Convert bytes to a human readble format
// Example output: '200 KB', '1 MB', '3 TB' etc
Conversion::bytesToHumans($bytes, $precision = 2);

// Converts a string with numbers to a full number
// Example: 1,232.12 -> becomes -> (int) 1232
Conversion::stringToInt($string);

// Convert a string numeric to a float
// Example: 1,232.12 -> becomes -> (float) 1232.12
Conversion::stringToFloat($value);

Directory Handling

use \ElegantMedia\PHPToolkit\Dir;

// Create a directory if it doesn't exist
Dir::makeDirectoryIfNotExists($dirPath, $permissions = 0775, $recursive = true);

// Delete a directory
Dir::deleteDirectory($dirPath);

// Delete files in a directory by extension
Dir::cleanDirectoryByWildcard('/my-dir-path/', 'txt');

// Delete files in a directory by (glob) wildcard
Dir::cleanDirectoryByWildcard('/my-dir-path/', '*.txt');

File Editor

use \ElegantMedia\PHPToolkit\FileEditor;

// Append a stub to a file, identified by the unique first line
FileEditor::appendStubIfSectionNotFound($filePath, $stubPath)

// Append a stub to a file
FileEditor::appendStub($filePath, $stubPath, $verifyPathsExists = true)

// Check if a string is in file
FileEditor::isTextInFile($filePath, $string, $caseSensitive = true)

// Find and replace text in a file
$bytes = FileEditor::findAndReplace($filePath, $search, $replace)

// Find and replace text in a file by regex
$text = FileEditor::findAndReplaceRegEx($filePath, $searchRegEx, $replaceRegEx)

// Check if two files are similar (by size and hash)
FileEditor::areFilesSimilar($path1, $path2)

// Read the first line from a file
FileEditor::readFirstLine($filePath, $trim = true);

// Get the classname and namespace from a while (by reading the file)
FileEditor::getClassname('/my-path/file.php');

Loader

use \ElegantMedia\PHPToolkit\Loader;

// Include all php files from a given directory
Loader::includeAllFilesFromDir($dirPath);

// Include all files recursively
Loader::includeAllFilesFromDirRecursive($dirPath);

Path

use \ElegantMedia\PHPToolkit\Path;

Path::withEndingSlash($path);
Path::withStartingSlash($path);
Path::withoutEndingSlash($path);
Path::withoutStartingSlash($path);

// remove dot segments from paths
// Example: Convert `/folder1/folder2/./../folder3` to `/folder1/folder3`
Path::canonical($path);

Reflector

use ElegantMedia\PHPToolkit\Reflector;

// Get an inherited class' directory path
Reflector::classPath($object, $pathSuffix = null);

Text

use \ElegantMedia\PHPToolkit\Text;

// Convert a block of text and split it into lines
/*
Example:
one, two,   three
four
five

Returns:
['one', 'two', 'three', 'four', 'five']
*/
Text::textToArray($text, $delimiters = null);

// Convert an 'existing_snake_case' to 'existing snake case'
Text::reverseSnake($string);

// Generate a random string without any ambiguous characters
// So it'll be easier to spell or read. For example, `AIilO01` will not be generated.
Text::randomUnambiguous($length = 16)

Timing

use \ElegantMedia\PHPToolkit\Timing;

// Get formatted microtimestamp. Example: `2008_07_14_010813.98232`
Timing::microTimestamp()

Global Functions

Validation

// Check all values are !empty. Throws an exception if at least one value is empty
// if APP_DEBUG=true ENV variable is set, you'll be able to see which variable was missing
validate_all_present();

// Example: validate_all_present($var1, $var2, $var5)

Development Setup

Running Tests Locally

Run the test suite:

composer test

Code Quality Tools

This project uses GitHub Actions for CI/CD instead of external services. You can run the same checks locally:

Code Style Fixing

Fix code style issues automatically:

# Install PHP CS Fixer globally (first time only)
composer global require friendsofphp/php-cs-fixer

# Run the fixer
php-cs-fixer fix --config=.php-cs-fixer.php

Check for style issues without fixing:

php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php

Static Analysis

Run PHPStan for static analysis:

# Install PHPStan (included in composer dev dependencies)
composer install

# Run analysis
vendor/bin/phpstan analyse

Code Coverage

Generate code coverage report:

# Run tests with coverage (requires Xdebug)
vendor/bin/phpunit --coverage-html coverage/

# View the report
open coverage/index.html

GitHub Actions CI

The project includes GitHub Actions workflows that automatically:

  • Run tests on multiple PHP versions (7.3, 7.4, 8.0, 8.1, 8.2)
  • Check and fix code style issues
  • Perform static analysis with PHPStan
  • Generate and upload code coverage reports to Codecov

These checks run on every push and pull request to the master or main branches.

Contributing

Please see CONTRIBUTING for details.

License

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

Copyright (c) 2025 Elegant Media.

elegantmedia/php-toolkit 适用场景与选型建议

elegantmedia/php-toolkit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.53k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 elegantmedia/php-toolkit 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-21