承接 entropyphp/utils 相关项目开发

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

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

entropyphp/utils

最新稳定版本:0.1.5

Composer 安装命令:

composer require entropyphp/utils

包简介

Utility classes for PHP

README 文档

README

License: MIT PHP Version Coverage Status Continuous Integration

Utility classes for PHP. A collection of PHP utility classes for file operations, attribute handling, and PHP code parsing.

Requirements

  • PHP 8.2 or higher
  • Composer

Installation

Install via Composer:

composer require entropyphp/utils

Components

AttributeLoader

A utility class for working with PHP 8 attributes (annotations). It provides methods to retrieve attributes from classes and methods.

Usage

use Koriym\Attributes\AttributeReader;
use Pg\Utils\Attribute\AttributeLoader;
use ReflectionClass;
use ReflectionMethod;

// Initialize the AttributeLoader
$attributeLoader = new AttributeLoader(new AttributeReader());

// Get a single attribute from a method
$reflectionMethod = new ReflectionMethod(MyClass::class, 'myMethod');
$attribute = $attributeLoader->getMethodAttribute($reflectionMethod, MyAttribute::class);

// Get multiple attributes from a method
$attributes = $attributeLoader->getMethodAttributes($reflectionMethod, MyAttribute::class);
foreach ($attributes as $attribute) {
    // Process each attribute
}

// Get a single attribute from a class
$reflectionClass = new ReflectionClass(MyClass::class);
$attribute = $attributeLoader->getClassAttribute($reflectionClass, MyAttribute::class);

// Get multiple attributes from a class
$attributes = $attributeLoader->getClassAttributes($reflectionClass, MyAttribute::class);
foreach ($attributes as $attribute) {
    // Process each attribute
}

// Access the underlying AttributeReader
$reader = $attributeLoader->getReader();

FileUtils

A utility class for file system operations.

Usage

use Pg\Utils\File\FileUtils;

// Get all PHP files in a directory (recursively)
$files = FileUtils::getFiles('/path/to/directory', 'php');
foreach ($files as $file) {
    echo $file->getPathname() . PHP_EOL;
}

// Get all PHP files excluding those containing 'test' in the filename
$files = FileUtils::getFiles('/path/to/directory', 'php', 'test');

// Find the project root directory (where composer.json is located)
$projectDir = FileUtils::getProjectDir(__DIR__);

// Get the root path of the installed package
$rootPath = FileUtils::getRootPath();

RequestUtil

a utility class for working with HTTP requests.

Usage

use Pg\Utils\HttpUtils\RequestUtils;

// Get if the request Ajax call?
$isAjax = RequestUtils::IsAjax($request);

// Get if Request is a JSON request
$isJson = RequestUtils::isJson($request);

// Get the request body as an array if it is a JSON or std POST request, if JSON is malformed, returns an empty array
$body = RequestUtils::getPostParams($request);

// Get the request accept format, defaults to 'html' if not set, or 'json' if the request is a JSON request
$acceptFormat = RequestUtils::getAcceptFormat($request);

// Get if the request accepts JSON
$acceptsJson = RequestUtils::wantJson($request);

// Get the absolute domain URL of the request, with eventually the port number
$url = RequestUtils::getDomain($request);

PhpTokenParser

A utility class for parsing PHP code to extract class information.

Usage

use Pg\Utils\Parser\PhpTokenParser;

// Get the fully qualified class name from PHP code
$phpCode = file_get_contents('/path/to/file.php');
$className = PhpTokenParser::findClass($phpCode);

if ($className) {
    echo "Found class: " . $className . PHP_EOL;
} else {
    echo "No class found in the file." . PHP_EOL;
}

Testing

Run the test suite:

composer run tests

Generate code coverage report:

composer run coverage

License

This project is licensed under the MIT License - see the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-29