loilo/node-path

Node.js `path` 模块,移植到 PHP

1.0.0 2019-08-10 23:27 UTC

This package is auto-updated.

Last update: 2024-09-24 07:52:40 UTC


README

Tests Version on packagist.org

本软件包是将 Node.js 的 path 模块移植到 PHP。

移植代码和文档是从 Node.js v12.8.0 生成的。

安装

composer require loilo/node-path

用法

示例

在 Unix 系统上执行(见 Windows 与 POSIX

use Loilo\NodePath\Path;

Path::basename('/foo/bar/baz/asdf/quux.html') === 'quux.html';
Path::basename('/foo/bar/baz/asdf/quux.html', '.html') === 'quux';

Path::dirname('/foo/bar/baz/asdf/quux') === '/foo/bar/baz/asdf';

Path::extname('index.html') === '.html';
Path::extname('index.coffee.md') === '.md';
Path::extname('index.') === '.';
Path::extname('index') === '';
Path::extname('.index') === '';
Path::extname('.index.md') === '.md';

// If $dir, $root and $base are provided,
// $dir . Path::getSeparator() . $base
// will be returned. $root is ignored.
Path::format([
  'root' => '/ignored',
  'dir' => '/home/user/dir',
  'base' => 'file.txt'
]) === '/home/user/dir/file.txt';

// $root will be used if $dir is not specified.
// If only $root is provided or $dir is equal to $root then the
// platform separator will not be included. $ext will be ignored.
Path::format([
  'root' => '/',
  'base' => 'file.txt',
  'ext' => 'ignored'
]) === '/file.txt';

// $name . $ext will be used if $base is not specified.
Path::format([
  'root' => '/',
  'name' => 'file',
  'ext' => '.txt'
]) === '/file.txt';

Path::getDelimiter() === ':';

Path::getSeparator() === '/';

Path::isAbsolute('/foo/bar') === true;
Path::isAbsolute('/baz/..') === true;
Path::isAbsolute('qux/') === false;
Path::isAbsolute('.') === false;

Path::join('/foo', 'bar', 'baz/asdf', 'quux', '..') === '/foo/bar/baz/asdf';

Path::normalize('/foo/bar//baz/asdf/quux/..') === '/foo/bar/baz/asdf';

Path::parse('/home/user/dir/file.txt');
// Returns an instance of Loilo\NodePath\PathObjectInterface
// representing path components.
// Can be cast to an array by calling toArray() on it.

Path::relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb') === '../../impl/bbb';

Path::resolve('/foo/bar', './baz') === '/foo/bar/baz';
Path::resolve('/foo/bar', '/tmp/file/') === '/tmp/file';
Path::resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If the current working directory is /home/myself/node,
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'

Windows 与 POSIX

本软件包的默认操作取决于您的 PHP 应用程序运行的操作系统。具体来说,当在 Windows 操作系统上运行时,它将假定正在使用 Windows 风格的路径。

因此,使用 Path::basename() 在 POSIX 和 Windows 上可能会有不同的结果

在 POSIX 上

Path::basename('C:\\temp\\myfile.html') === 'C:\\temp\\myfile.html';

在 Windows 上

Path::basename('C:\\temp\\myfile.html') === 'myfile.html';

要在任何操作系统上处理 Windows 文件路径时获得一致的结果,请使用 Loilo\NodePath\WindowsPath

在 POSIX 和 Windows 上

use Loilo\NodePath\WindowsPath;

WindowsPath::basename('C:\\temp\\myfile.html') === 'myfile.html';

要在任何操作系统上处理 POSIX 文件路径时获得一致的结果,请使用 Loilo\NodePath\PosixPath

在 POSIX 和 Windows 上

use Loilo\NodePath\PosixPath;

PosixPath::basename('/tmp/myfile.html') === 'myfile.html';

在 Windows 上,本软件包遵循每个驱动器工作目录的概念。当使用不带反斜杠的驱动器路径时,可以观察到这种行为。例如,Path::resolve('c:\\') 可能会返回与 Path::resolve('c:') 不同的结果。有关更多信息,请参阅 此 MSDN 页面

API

这是由 Loilo\NodePath\PathInterface 实现的完整 API,该接口由 WindowsPathPosixPath 类实现。

注意:除非另有说明,否则假定 PathInterface 方法是在 Unix 环境下执行的。

basename ( string $path [, ext: $suffix ] ) : string

返回 $path 的最后一部分,类似于 Unix 的 basename 命令。忽略尾随目录分隔符,见 getSeparator()

dirname ( string $path ) : string

返回路径的目录名,类似于 Unix 的 dirname 命令。忽略尾随目录分隔符,见 getSeparator()

extname ( string $path ) : string

返回 $path 的扩展名,从最后一个 .(点)字符到最后一个 $path 的最后一部分。如果 $path 的最后一部分中没有 .,或者如果除了 $path 基名中的第一个字符之外没有其他 . 字符(见 basename()),则返回空字符串。

format ( Loilo\NodePath\PathObjectInterface|array $pathData ) : string

从一个关联数组或一个 PathObjectInterface 实例返回一个路径字符串。这是 parse() 的相反操作。

当为$pathData提供属性时,请记住有些情况下一个属性会优先于另一个属性。

  • 如果提供了$pathData['dir'],则忽略$pathData['root']
  • 如果存在$pathData['base'],则忽略$pathData['ext']$pathData['name']

getDelimiter ( void ) : string

提供平台特定的路径分隔符

  • 在Windows上为;
  • 在POSIX上为:

getSeparator ( void ) : string

提供平台特定的路径段分隔符

  • 在Windows上为\
  • 在POSIX上为/

isAbsolute ( string $path ) : bool

确定$path是否为绝对路径。

如果给定的路径是一个零长度字符串,则返回false

join ([ array $... ] ) : string

使用平台特定的分隔符将所有给定的路径段连接在一起,然后规范化结果路径。

忽略零长度路径段。如果连接的路径字符串是零长度字符串,则返回'.',表示当前工作目录。

normalize ( string $path ) : string

规范化给定的$path,解析...段。

当找到多个连续的路径段分隔符时(例如,在POSIX上的/和Windows上的\/),它们将被替换为平台特定的路径段分隔符的单个实例(POSIX上的/和Windows上的\)。保留尾随分隔符。

如果路径是零长度字符串,则返回'.',表示当前工作目录。

parse ( string $path ) : Loilo\NodePath\PathObjectInterface

返回一个对象,其属性表示路径的显著元素。忽略尾随目录分隔符,请参阅getSeparator()

relative ( string $from, string $to ) : string

根据当前工作目录返回从$from$to的相对路径。如果$from$to分别解析到相同的路径(在调用resolve()后),则返回零长度字符串。

如果将零长度字符串传递为$from$to,则使用当前工作目录而不是零长度字符串。

resolve ([ array $... ] ) : string

将一系列路径或路径段解析为绝对路径。

从右到左处理给定的路径序列,直到构建一个绝对路径。例如,给定路径段序列:/foo/barbaz,调用PathInterface::resolve('/foo', '/bar', 'baz')将返回/bar/baz

如果在处理所有给定的路径段之后尚未生成绝对路径,则使用当前工作目录。

结果路径被规范化,并且移除尾随反斜杠,除非路径解析到根目录。

忽略零长度路径段。

如果没有传递路径段,则PathInterface::resolve()将返回当前工作目录的绝对路径。

toNamespacedPath ([ mixed $path = null ] ) : mixed

仅在Windows系统上,返回给定路径的等效命名空间前缀路径。如果$path不是字符串,则将其返回而不进行修改。

此方法仅在Windows系统上有意义。在POSIX系统上,该方法不可操作,并且始终返回未修改的$path