2017 © Pedro Peláez
 

library path-utils

A simple PHP library for file system paths - smart path join, root path, and so on...

image

tomaskraus/path-utils

A simple PHP library for file system paths - smart path join, root path, and so on...

  • Monday, January 30, 2017
  • by tomaskraus
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

path-utils

A simple PHP library for file system paths - smart path join, root path, and so on..., (*1)

features

smart path join, fixes missing or too many path separators, joins even non-existent paths, (*2)

Path::join("myapp/", "/dist/app.zip") );
// => "myapp/dist/app.zip"

application root path, with smart join, (*3)

$pth = new Path("/var/www/myApp/");
$pth->root(); // => "/var/www/myApp"

$pth->root("conf/local.php");
// => "/var/www/myapp/conf/local.php"

Installation

Via composer:, (*4)

composer require tomaskraus/path-utils

, or add this snippet to composer.json, (*5)

    "require": {
        "tomaskraus/path-utils": "^0.1"
    },

Usage example

Assume we have our php application in /var/www/myApp. A /var/www/myApp is our application root path., (*6)

<?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


The Versions

30/01 2017

dev-master

9999999-dev https://github.com/tomaskraus/path-utils

A simple PHP library for file system paths - smart path join, root path, and so on...

  Sources   Download

MIT

The Requires

  • php ^5.3|^7.0

 

The Development Requires

by Avatar tomaskraus

php path

30/01 2017

dev-develop

dev-develop https://github.com/tomaskraus/path-utils

A simple PHP library for file system paths - smart path join, root path, and so on...

  Sources   Download

MIT

The Requires

  • php ^5.3|^7.0

 

The Development Requires

by Avatar tomaskraus

php path

30/01 2017

0.1.0

0.1.0.0 https://github.com/tomaskraus/path-utils

A simple PHP library for file system paths - smart path join, root path, and so on...

  Sources   Download

MIT

The Requires

  • php ^5.3|^7.0

 

The Development Requires

by Avatar tomaskraus

php path