tomaskraus / path-utils
一个简单的PHP文件系统路径库 - 智能路径连接、根路径等...
0.1.0
2017-01-30 00:45 UTC
Requires
- php: ^5.3|^7.0
Requires (Dev)
- phpunit/phpunit: ~4.2
This package is not auto-updated.
Last update: 2024-09-18 21:35:22 UTC
README
一个简单的PHP文件系统路径库 - 智能路径连接、根路径等...
特性
智能路径连接,修复缺失或过多的路径分隔符,连接不存在路径
Path::join("myapp/", "/dist/app.zip") ); // => "myapp/dist/app.zip"
应用根路径,使用智能连接
$pth = new Path("/var/www/myApp/"); $pth->root(); // => "/var/www/myApp" $pth->root("conf/local.php"); // => "/var/www/myapp/conf/local.php"
安装
通过composer
composer require tomaskraus/path-utils
,或者将以下代码片段添加到composer.json
"require": { "tomaskraus/path-utils": "^0.1" },
使用示例
假设我们的PHP应用位于/var/www/myApp。一个/var/www/myApp是我们的应用根路径。
<?php require "vendor/autoload.php"; use Tomaskraus\PathUtils\Path; /* remember our web application root */ $pth = new Path("/var/www/myApp/"); /* one can use even a non-existent root path */ /* just acts as a prefix path */ $pth2 = new Path("foo"); var_dump( $pth->root() ); // => "/var/www/myApp" //everything is returned without the trailing path separator var_dump( $pth2->root() ); // => "foo" /* we can create new, non-existing path strings, based on our web application root */ var_dump( $pth->root("conf/file-to-be-created.php") ); // => "/var/www/myapp/conf/file-to-be-created.php" var_dump( $pth->root("/conf/file-to-be-created.php") ); // => "/var/www/myapp/conf/file-to-be-created.php" // returns the same... smart path separator handling /* another instance with the different root */ var_dump( $pth2->root("/conf/file-to-be-created.php") ); // => "foo/conf/file-to-be-created.php" /* root() does not normalize paths */ var_dump( $pth->root("/../conf/file-to-be-created.php") ); // => "/var/www/myApp/../conf/file-to-be-created.php" /* path-safe include, wherever you are */ include $pth->root("myLib/utils.php"); //includes /var/www/myapp/myLib/utils.php /* Path::join does not normalize paths */ var_dump( Path::join("/var/www/myProject/", "./../otherProject") ); // => "/var/www/myProject/./../otherProject" /* smart path join, fixes missing or too many path separators */ var_dump( Path::join("myapp/", "/dist/app.zip") ); // => "myapp/dist/app.zip" var_dump( Path::join("/var/www", "dist/app.zip") ); // => "/var/www/dist/app.zip", //preserves a root slash from the first parameter /* join Windows path */ var_dump( Path::join("C:\\www\\", "/dist/app.zip") ); // => "C:\www/dist/app.zip", // mixed result for Windows path (still works in PHP) var_dump( Path::join("C:\\www", "dist/app.zip") ); // => the same "C:\www/dist/app.zip" /* foldable */ var_dump( Path::join("a/b", Path::join("c/d", "e/f")) ); // => "a/b/c/d/e/f" var_dump( Path::join("a/b/", Path::join("/c/d/", "/e/f/")) ); // => "a/b/c/d/e/f" // smart path separator handling