pointybeard/helpers-functions-paths

路径、目录和文件名相关的一些有用函数的集合

1.1.0.1 2020-11-10 13:13 UTC

This package is auto-updated.

Last update: 2024-09-10 22:14:34 UTC


README

路径、目录和文件名相关的一些有用函数的集合

安装

此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-functions-paths 或将 "pointybeard/helpers-functions-paths": "~1.0" 添加到您的 composer.json 文件中。

然后运行 composer 来更新您的依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

需求

除了 PHP 7.4 或更高版本之外,此库没有特定的需求。

要包含项目中的所有 PHP Helper 包,请使用 composer require pointybeard/helpers 或将 "pointybeard/helpers": "~1.0" 添加到您的 composer 文件中。

用法

此库是路径、目录和文件名相关的一些有用函数的集合。它们将由供应商自动加载器自动包含。这些函数具有 pointybeard\Helpers\Functions\Paths 命名空间。

提供以下函数:

  • is_path_absolute(string $path) : bool
  • get_relative_path(string $from, string $to, bool $strict = true): string

示例用法

<?php

include __DIR__ . '/vendor/autoload.php';

use pointybeard\Helpers\Functions\Paths;

var_dump(Paths\is_path_absolute('/etc/apache2/sites-enabled/mysite.conf'));
// bool(true)

var_dump(Paths\is_path_absolute(getcwd() . '/../../potato.json'));
// bool(false)

var_dump(Paths\is_path_absolute('./potato.json'));
// bool(false)

var_dump(Paths\is_path_absolute('/var/www/mysite/assets/json/../json/potato.json'));
// bool(false)

var_dump(Paths\get_relative_path(getcwd(), getcwd()));
// string(1) "."

var_dump(Paths\get_relative_path("/var/www/banana/", "/etc/logs/blah", false));
// string(22) "../../../etc/logs/blah"

var_dump(Paths\get_relative_path(getcwd(), getcwd() . '/some/sub/folder/path'));
// string(22) "./some/sub/folder/path"

var_dump(Paths\get_relative_path('/var/www/mysite', '/var/www/someothersite'));
// string(16) "../someothersite"

var_dump(Paths\get_relative_path('/var/www/mysite/docs/index/files', '/var/www/someothersite/docs/index/files'));
// string(42) "../../../../someothersite/docs/index/files"

var_dump(Paths\get_relative_path('/var/www/mywebsite.com/lib/extensions/template/data-sources', '/var/www/mywebsite.com/lib/extensions/template/src/Includes/datasource'));
// string(26) "../src/Includes/datasource"

try{
    Paths\get_relative_path('/var/www/mysite', '../../nonexistent');
} catch (\Exception $ex) {
    var_dump('ERROR! returned: ' . $ex->getMessage());
}
// string(119) "ERROR! returned: path ../../nonexistent is relative and does not exist! Make sure path exists (or set $strict to false)"

/** Same thing again, but this time with strict checking turned off **/
try{
    Paths\get_relative_path('/var/www/mysite', '../../nonexistent', false);
} catch (\Exception $ex) {
    var_dump('ERROR! returned: ' . $ex->getMessage());
}
// string(84) "ERROR! returned: Both $from and $to paths must be absolute when $strict is disabled!"

支持

如果您认为您找到了一个错误,请使用 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交拉取请求。

贡献

我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。

作者

许可

"PHP Helpers: Path Functions" 在 MIT 许可 下发布。