PathJoin是用于标准化和连接文件系统路径的PHP库。该库的目的是使与平台和系统目录分隔符的文件系统路径更易于使用。
文件路径归一化的目的是提供单个一致的文件路径表示。换句话说,该库中的归一化将解决.和..目录参考,还将多个目录分离器凝结为一个。这使得在相互比较路径时避免常见问题变得更加容易。
虽然PHP提供了内置函数realpath() ,但在每种情况下都不可用,因为它可以使用文件系统工作。该库只需使用字符串处理将路径归一化并归一化。不需要文件或目录是可读甚至存在的。
API文档可在以下网址获得:http://kit.riimu.net/api/pathjoin/
安装此库的最简单方法是使用作曲家来处理您的依赖项。为了通过作曲家安装此库,只需按照以下两个步骤操作:
通过在项目root中运行作曲家命令行安装来获取composer.phar 。
运行安装脚本后,您应该在项目root中有composer.phar文件,并且可以运行以下命令:
php composer.phar require "riimu/kit-pathjoin:^1.2"
通过作曲家安装此库后,您可以通过包括由作曲家在安装过程中生成的vendor/autoload.php文件加载库。
如果您已经熟悉如何使用Composer,则可以通过将以下composer.json文件添加到项目中并运行composer install命令来添加库作为依赖项:
{
"require" : {
"riimu/kit-pathjoin" : " ^1.2 "
}
}如果您不希望使用作曲家加载库,则可以通过下载最新版本并将src文件夹提取到您的项目中来手动下载库。然后,您可以包括提供的src/autoload.php文件以加载库类。
该库提供了两种方便的方法, Path::normalize()和Path::join() 。这两种方法都以非常相似的方式工作。主要区别在于,虽然join()方法可以接受多个路径加入, normalize()只能接受单个路径。两种方法将返回归一化路径的结果。
以下示例将包含库的许多不同用例:
<?php
require ' vendor/autoload.php ' ;
use Riimu Kit PathJoin Path ;
// Both of the following will output 'foo/bar' on Unix and 'foobar' on Windows
echo Path:: normalize ( ' foo/bar ' ) . PHP_EOL ;
echo Path:: join ( ' foo ' , ' bar ' ) . PHP_EOL ;
// The join method accepts multiple arguments or a single array
echo Path:: join ( ' foo ' , ' bar ' , ' baz ' ) . PHP_EOL ; // outputs 'foo/bar/baz'
echo Path:: join ([ ' foo ' , ' bar ' , ' baz ' ]) . PHP_EOL ; // outputs 'foo/bar/baz'
// The '.' and '..' directory references will be resolved in the paths
echo Path:: normalize ( ' foo/./bar/../baz ' ) . PHP_EOL ; // outputs 'foo/baz'
echo Path:: join ([ ' foo/./ ' , ' bar ' , ' ../baz ' ]) . PHP_EOL ; // outputs 'foo/baz'
// Only the first path can denote an absolute path in the join method
echo Path:: join ( ' /foo ' , ' /bar/baz ' ) . PHP_EOL ; // outputs '/foo/bar/baz'
echo Path:: join ( ' foo ' , ' /bar ' ) . PHP_EOL ; // outputs 'foo/bar'
echo Path:: join ( ' foo ' , ' ../bar ' , ' baz ' ) . PHP_EOL ; // outputs 'bar/baz'
echo Path:: join ( '' , ' /bar ' , ' baz ' ) . PHP_EOL ; // outputs 'bar/baz'
// Relative paths can start with a '..', but absolute paths cannot
echo Path:: join ( ' /foo ' , ' ../../bar ' , ' baz ' ) . PHP_EOL ; // outputs '/bar/baz'
echo Path:: join ( ' foo ' , ' ../../bar ' , ' baz ' ) . PHP_EOL ; // outputs '../bar/baz'
// Empty paths will result in a '.'
echo Path:: normalize ( ' foo/.. ' ) . PHP_EOL ;
echo Path:: join ( ' foo ' , ' bar ' , ' ../.. ' ) . PHP_EOL ; Path::normalize()还接受第二个参数$prependDrive ,该$ prependive占用布尔值并默认为true。在Windows平台上,驱动器字母是绝对路径的重要组成部分。因此,如果将参数设置为true时,如果绝对路径不提供一个本身,则该方法将预先将当前工作目录的驱动字母预先为绝对路径。
对于Windows系统,下面的示例是正确的,如果工作目录位于C:驱动器:
<?php
require ' vendor/autoload.php ' ;
use Riimu Kit PathJoin Path ;
echo Path:: normalize ( ' /foo/bar ' ) . PHP_EOL ; // outputs 'C:fooBar'
echo Path:: normalize ( ' D:/foo/bar ' ) . PHP_EOL ; // outputs 'D:fooBar'
echo Path:: normalize ( ' /foo/bar ' , false ) . PHP_EOL ; // outputs 'fooBar' 该图书馆是版权(C)2014-2017 RiikkaKalliomäki。
有关许可证和复制信息,请参见许可证。