jackiedo/path-helper
Composer 安装命令:
composer require jackiedo/path-helper
包简介
Helper class for working with local paths in PHP
README 文档
README
Helper class for working with local paths in PHP.
Compatibility
This package requires PHP 5.4.0 or later.
Overview
Look at one of the following topics to learn more about Path Helper
- Path Helper
- Compatibility
- Overview
- License
Installation
Download a latest package or use Composer:
$ composer require jackiedo/path-helper
Usage
After requiring composer autoloader, you can use the package in the following ways:
Using static method
use Jackiedo\PathHelper\Path; // ... $return = Path::doSomething();
Using method of instance
use Jackiedo\PathHelper\Path; // ... $helper = new Path; $return = $helper->doSomething();
Using built-in function
See here for more details.
Available methods
Normalize the directory separators of the path
Formats the directory separators of a given path with a specific string.
Syntax:
/** * Formats the directory separators of a given path with a specific string. * * @param string $path the path want to normalize * @param string $separator the directory separator want to use * * @return string */ public static function normalize($path, $separator = DIRECTORY_SEPARATOR);
Example:
$return1 = Path::normalize('path\\to/specific/file/or\\directory'); // The result returned will depend on the operating system // On Windows -> path\to\specific\file\or\directory // On Unix -> path/to/specific/file/or/directory $return2 = Path::normalize('path\\to/specific/file/or\\directory', '/'); // path/to/specific/file/or/directory $return3 = Path::normalize('path\\to/specific/file/or\\directory', ' > '); // path > to > specific > file > or > directory
Restyle the path
Restyle to Unix style
Alternative to normalize($path, '/') method.
Syntax:
/** * Normalize directory separators of a given path according to Unix OS style. * * @param string $path the path want to normalize * * @return string */ public static function unixStyle($path);
Example:
$return = Path::unixStyle('path\\to/specific/file/or\\directory'); // path/to/specific/file/or/directory
Restyle to Windows style
Alternative to normalize($path, '\\') method.
Syntax:
/** * Normalize directory separators of a given path according to Windows OS style. * * @param string $path the path want to normalize * * @return string */ public static function winStyle($path);
Example:
$return = Path::winStyle('path\\to/specific/file/or\\directory'); // path\to\specific\file\or\directory
Restyle belong to current OS
Alternative to normalize($path, DIRECTORY_SEPARATOR) method.
Syntax:
/** * Normalize directory separators of a given path according to the current OS style. * * @param string $path the path want to normalize * * @return string */ public static function osStyle($path);
Example:
$return = Path::osStyle('path\\to/specific/file/or\\directory'); // The result returned will depend on the operating system // On Windows -> path\to\specific\file\or\directory // On Unix -> path/to/specific/file/or/directory
Create the absolute path
Create the absolute path from a given path.
Syntax:
/** * Return absolute path from a given path. * * This method is an alternative to `realpath()` function for non-existent paths. * * @param string $path the path want to format * @param string $separator the directory separator want to use in the result * * @return string */ public static function absolute($path, $separator = DIRECTORY_SEPARATOR);
Example:
$return = Path::absolute('./this\\is/../sample/path'); // The result returned will depend on the operating system and current working directory // You will probably get the following result: /home/www/public_html/this/sample/path
Note:
This method looks like PHP's realpath() function at first glance, but it actually works in a different way.
The
realpath()function returns the absolute path to theexistingdirectory or file, while this methoddoes not checkfor actual existence.
Create the relative path
Create the relative path from a given file or directory to another location.
Syntax:
/** * Return relative path from a given file or directory to another location. * * @param string $from the path of departure file or directory * @param string $to the path of destination file or directory * @param string $separator the directory separator want to use in the result * * @return string */ public static function relative($from, $to, $separator = DIRECTORY_SEPARATOR);
Example:
$return = Path::absolute('./this\\is/../sample/path', '/home/www/another/directory'); // The result returned will depend on the operating system and current working directory // You will probably get the following result: ../../../../another/directory
Check the form of the path
Check the absolute form
Syntax:
/** * Check if a given path is an absolute path. * * @param string $path the path want to check * * @return bool */ public static function isAbsolute($path);
Example:
$return = Path::isAbsolute('/home/www/public_html'); // true $return = Path::isAbsolute('sample/../path'); // false $return = Path::isAbsolute('D:\\home\\www\\public_html'); // true $return = Path::isAbsolute('sample\\..\\path'); // false
Check the relative form
Syntax:
/** * Check if a given path is a relative path. * * @param string $path the path want to check * * @return bool */ public static function isRelative($path);
Example:
$return = Path::isRelative('/home/www/public_html'); // false $return = Path::isRelative('sample/../path'); // true $return = Path::isRelative('D:\\home\\www\\public_html'); // false $return = Path::isRelative('sample\\..\\path'); // true
Check the mutual wrapping of paths
Check if the path is ancestor of another
Syntax:
/** * Check if a given path is ancestor of the another path. * * Return true if the input path is ancestor of the comparison * * @param string $path the path want to check * @param string $comparison the target path used for comparison * * @return bool */ public static function isAncestor($path, $comparison);
Example:
$return = Path::isAncestor('/home/www', '/home/www/public/assets/css/../images/sample.png'); // true $return = Path::isAncestor('/home/www', '/another_home/public/assets/css/../images/sample.png'); // false
Check if the path is descendant of another
Syntax:
/** * Check if a given path is descendant of the another path. * * Return true if the input path is descendant of the comparison * * @param string $path the path want to check * @param string $comparison the target path used for comparison * * @return bool */ public static function isDescendant($path, $comparison);
Example:
$return = Path::isDescendant('/home/www/public/assets/css/../images/sample.png', '/home/www'); // true $return = Path::isDescendant('/another_home/public/assets/css/../images/sample.png', '/home/www'); // false
Available functions
This package contains several built-in functions to alternative using the methods of the Path class. However, the use of these functions is not recommended, because they may conflict with the functions of other packages.
| Function | Class method |
|---|---|
| absolute_path() | Path::absolute() |
| relative_path() | Path::relative() |
| winstyle_path() | Path::winStyle() |
| unixstyle_path() | Path::unixStyle() |
| osstyle_path() | Path::osStyle() |
| normalize_path() | Path::normalize() |
| is_absolute_path() | Path::isAbsolute() |
| is_relative_path() | Path::isRelative() |
| is_descendant_path() | Path::isDescendant() |
| is_ancestor_path() | Path::isAncestor() |
License
MIT © Jackie Do
jackiedo/path-helper 适用场景与选型建议
jackiedo/path-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 293.1k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「path」 「helpers」 「library」 「helper」 「paths」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jackiedo/path-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jackiedo/path-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jackiedo/path-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
The InstantConfigurationCopy module provides easy way to copy configuration field information for admin in back office Magento 2.
HTML and form generation
Falsy helps you manage half-truths with PHP
simple agnostic package for IO releted (files, dir, etc..)
Collections of Path generators for Spatie laravel-medialibrary pacakge
统计信息
- 总下载量: 293.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-06