weevers/path

This package is abandoned and no longer maintained. No replacement package was suggested.

PHP 5.4 port of the node/iojs path module

v0.2.0 2015-04-02 00:12 UTC

This package is not auto-updated.

Last update: 2020-08-08 17:28:15 UTC


README

PHP 5.4 port of the node/iojs path module, because PHP is like a family holiday dinner and I want to survive. Description from the iojs documentation:

This module contains utilities for handling and transforming file paths. Almost all these methods perform only string transformations. The file system is not consulted to check whether paths are valid.

packagist status Travis build status AppVeyor build status Dependency status

Jump to: usage / install / license

examples

Example output on a posix system, assuming "/please" is the current working directory:

<?php
use Weevers\Path\Path;

// "../../tolerate/php"
Path::relative('i/love/node', 'i/tolerate/php');

// "/alright" (resolve works like a sequence of cd commands in a shell)
Path::resolve('ah', '/okay', '../alright');

// true
Path::isInside('parent/child', 'parent');

// "/please/start/dancing"
Path::resolve('start', 'dancing');

?>

usage

Usage docs pending. In the meantime, consult the iojs documentation, because the function signatures are almost the same and the code (including unit tests) copied.

Ported so far (for posix and windows), including unit tests

  • relative($from, $to)
  • isAbsolute($path)
  • resolve($path, ..)
  • join($path, ..)
  • normalize($path)
  • separator()
  • delimiter()

PHP-specific additions

resolve() also accepts stream wrapper URI's. This is implemented outside of the (ported) posix and Windows adapters, to ensure it doesn't interfere with the original behavior (which remains well tested and has a frozen stability).

  • file schemes are stripped, unless combined with other schemes
  • A scheme change is interpreted like a root change. E.g. going from http://x to glob://x acts like a cd from c:\x to d:\x.
  • For remote streams (like http) and vfs streams, the arguments passed to resolve() are joined (instead of resolved to an absolute path) with forward slashes (regardless of OS).

Some examples (for Windows, so you can see the difference between local and remote streams), assuming "C:\project" is the current working directory:

<?php

// Relative input: "c:\project\beep"
Path::resolve('file://beep');
Path::resolve('beep');

// Absolute input: "c:\beep"
Path::resolve('file:///beep');
Path::resolve('/beep');

// Combined wrappers: "zip://file://c:\beep"
Path::resolve('zip://file:///beep');

// Remote stream, so forward slashes: "zip://http://example.com/blog/2015"
Path::resolve('zip://http://example.com\blog', '2015');

// "zip://c:\project\bar"
Path::resolve('zip://dir', '..', 'bar');
Path::resolve('dir', 'zip://..', 'bar');

// A VFS URI stays relative: "vfs://root/virtual.file"
Path::resolve('vfs://root', 'virtual.file');

// A scheme change: "glob://c:\project\*.js"
Path::resolve('http://example.com', 'glob://*.js');

// No scheme change: "glob://c:\project\dir\*.js"
Path::resolve('glob://dir', 'glob://*.js');

// UNC (a network path): "compress.zlib://\\server\share\resource"
// Note that "\\server\share" is the root, so ".." has no effect
Path::resolve('compress.zlib:////server/share', '..', 'resource');

?>

Additional methods

  • isInside($path, $parent) - similar to sindresorhus/is-path-inside for node
  • getPrefix($path) - returns "zip://" for "zip://2015.zip#april/02.log"

install

With composer do:

composer require weevers/path

license

MIT © Vincent Weevers and iojs/node authors.