internal/path
Composer 安装命令:
composer require internal/path
包简介
Type-safe, immutable file path library with cross-platform support and automatic normalization.
README 文档
README
Path helper for PHP
A type-safe library for working with file system paths. Handles normalization, cross-platform compatibility, and all the edge cases with slashes and separators.
Path is an immutable value object – all methods return new instances, so it's safe to use as a DTO, pass between layers, or store in your domain models without worrying about side effects.
Installation
composer require internal/path
Usage
Creating paths
use Internal\Path; // Create from string $path = Path::create('/var/www/app'); $path = Path::create('src/helpers/utils.php');
Joining paths
$base = Path::create('/var/www'); $full = $base->join('app', 'src', 'Controller.php'); // Result: /var/www/app/src/Controller.php // Works with Path objects too $subdir = Path::create('logs'); $logFile = $base->join($subdir, 'app.log');
Working with path components
$file = Path::create('/home/user/documents/report.pdf'); $file->name(); // 'report.pdf' $file->stem(); // 'report' $file->extension(); // 'pdf' $file->parent(); // Path('/home/user/documents')
Path checks
$path = Path::create('config/app.php'); $path->isAbsolute(); // false $path->isRelative(); // true $path->exists(); // checks if file/directory exists $path->isFile(); // checks if it's a file $path->isDir(); // checks if it's a directory $path->isWriteable(); // checks if writable
Converting paths
$relative = Path::create('src/Path.php'); $absolute = $relative->absolute(); // Result: /current/working/directory/src/Path.php // Resolve against custom directory $absolute = $relative->absolute('/var/www/app'); // Result: /var/www/app/src/Path.php // Use as string echo $path; // Path implements Stringable
Pattern matching
$path = Path::create('/var/www/app/Controller.php'); $path->match('*.php'); // true $path->match('/var/www/*/Con*'); // true // Supports wildcards $path->match('file?.txt'); // matches file1.txt, fileA.txt, etc. $path->match('file[123].txt'); // matches file1.txt, file2.txt, file3.txt $path->match('test/*/*.php'); // matches test/any/file.php // Control case sensitivity with optional parameter $file = Path::create('File.TXT'); $file->match('*.txt'); // OS default (case-insensitive on Windows, case-sensitive on Unix) $file->match('*.txt', false); // case-insensitive (matches on any OS) $file->match('*.txt', true); // case-sensitive (won't match - different case) $file->match('*.TXT', true); // case-sensitive (matches - exact case)
Edge cases and special handling
The library handles common edge cases automatically:
Hidden files and multiple extensions
// Hidden files (Unix-style) $hidden = Path::create('.gitignore'); $hidden->stem(); // '.gitignore' $hidden->extension(); // 'gitignore' // Files with multiple dots $config = Path::create('app.config.json'); $config->stem(); // 'app.config' $config->extension(); // 'json' (only the last extension)
Windows paths
// Automatically normalizes Windows backslashes $winPath = Path::create('C:\Users\Admin\Documents'); echo $winPath; // 'C:/Users/Admin/Documents' // Windows drive letters are recognized as absolute Path::create('C:/Program Files')->isAbsolute(); // true
Path normalization
// Removes redundant separators Path::create('path//to///file.txt'); // 'path/to/file.txt' // Resolves . and .. segments Path::create('path/./to/../file.txt'); // 'path/file.txt' // Empty path becomes current directory Path::create(''); // '.'
Safety checks
// Cannot join absolute paths (prevents common mistakes) $base = Path::create('/var/www'); $base->join('/etc/config'); // throws LogicException // Cannot navigate above root in absolute paths Path::create('/var/../../root'); // throws LogicException
Converting to absolute with custom base directory
// Relative base directory is converted to absolute first $path = Path::create('lib/helper.php'); $path->absolute('project/app'); // resolves 'project/app/lib/helper.php' against current directory first // Absolute path with matching base - validation passes $absolutePath = Path::create('/var/www/app/src/file.php'); $absolutePath->absolute('/var/www/app'); // returns same path (validation OK) $absolutePath->absolute('/var/www'); // returns same path (validation OK - parent directory) // Absolute path with non-matching base - throws exception $absolutePath = Path::create('/var/www/app/file.php'); $absolutePath->absolute('/home/user'); // throws LogicException - path doesn't start with base
internal/path 适用场景与选型建议
internal/path 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 125.05k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2025 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「path」 「filesystem」 「windows」 「helper」 「unix」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 internal/path 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 internal/path 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 internal/path 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
The flysystem adapter for yandex disk rest api.
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
统计信息
- 总下载量: 125.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 39
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2025-12-02