pax_global_header00006660000000000000000000000064125660564240014524gustar00rootroot0000000000000052 comment=5145afabdff12b5871f4a647e86ab2f05059d742 Kit-PathJoin-1.1.2/000077500000000000000000000000001256605642400137665ustar00rootroot00000000000000Kit-PathJoin-1.1.2/CHANGES.md000066400000000000000000000007511256605642400153630ustar00rootroot00000000000000# Changelog # ## v1.1.2 (2015-08-22) ## * Slightly reworked how the paths are built ## v1.1.1 (2015-08-09) ## * Maintenance release that simply addresses some coding standards issues ## v1.1.0 (2015-03-25) ## * Added `Path::normalize()` method for normalizing a single path. * The `Path::join()` method now correctly returns '.' instead of an empty path, similar to the `dirname()` function. ## v1.0.1 (2015-01-24) ## * Improvements in code quality and documentation Kit-PathJoin-1.1.2/LICENSE000066400000000000000000000020541256605642400147740ustar00rootroot00000000000000Copyright (c) 2014 - 2015 Riikka Kalliomäki Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Kit-PathJoin-1.1.2/README.md000066400000000000000000000130721256605642400152500ustar00rootroot00000000000000# Path joiner and normalizer # *PathJoin* is PHP library for normalizing and joining file system paths. The purpose of this library is to make easier to work with file system paths irregardless of the platform and the system directory separator. The purpose of file path normalization is to provide a single consistent file path representation. In other words, the normalization in this library will resolve `.` and `..` directory references and also condense multiple directory separators into one. This makes it much easier to avoid common problems when comparing paths against each other. While PHP provides a built in function `realpath()`, it is not usable in every case since it works by using the file system. This library simply combines and normalizes the paths using string handling. There is no requirement for the files or directories to be readable or even exist. The API documentation, which can be generated using Apigen, can be read online at: http://kit.riimu.net/api/pathjoin/ [![Build Status](https://img.shields.io/travis/Riimu/Kit-PathJoin.svg?style=flat)](https://travis-ci.org/Riimu/Kit-PathJoin) [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/Riimu/Kit-PathJoin.svg?style=flat)](https://scrutinizer-ci.com/g/Riimu/Kit-PathJoin/) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/Riimu/Kit-PathJoin.svg?style=flat)](https://scrutinizer-ci.com/g/Riimu/Kit-PathJoin/) ## Requirements ## In order to use this library, the following requirements must be met: * PHP version 5.4 ## Installation ## This library can be installed by using [Composer](http://getcomposer.org/). In order to do this, you must download the latest Composer version and run the `require` command to add this library as a dependency to your project. The easiest way to complete these two tasks is to run the following two commands in your terminal: ``` php -r "readfile('https://getcomposer.org/installer');" | php php composer.phar require "riimu/kit-pathjoin:1.*" ``` If you already have Composer installed on your system and you know how to use it, you can also install this library by adding it as a dependency to your `composer.json` file and running the `composer install` command. Here is an example of what your `composer.json` file could look like: ```json { "require": { "riimu/kit-pathjoin": "1.*" } } ``` After installing this library via Composer, you can load the library by including the `vendor/autoload.php` file that was generated by Composer during the installation. ### Manual installation ### You can also install this library manually without using Composer. In order to do this, you must download the [latest release](https://github.com/Riimu/Kit-PathJoin/releases/latest) and extract the `src` folder from the archive to your project folder. To load the library, you can simply include the `src/autoload.php` file that was provided in the archive. ## Usage ## This library provides two convenient methods, `Path::normalize()` and `Path::join()`. Both of these methods work in a very similar fashion. The main difference is that while the `join()` method can accept multiple paths to join, the `normalize()` will only accept a single path. Both of the methods will return a normalized path as the result. The following example will contain numerous different use cases of the library: ```php =5.4.0" }, "autoload": { "psr-4": { "Riimu\\Kit\\PathJoin\\": "src/" } } } Kit-PathJoin-1.1.2/examples/000077500000000000000000000000001256605642400156045ustar00rootroot00000000000000Kit-PathJoin-1.1.2/examples/example.php000066400000000000000000000031151256605642400177500ustar00rootroot00000000000000 * @copyright Copyright (c) 2014, Riikka Kalliomäki * @license http://opensource.org/licenses/mit-license.php MIT License */ class Path { /** * Normalizes the provided file system path. * * Normalizing file system paths means that all forward and backward * slashes in the path will be replaced with the system directory separator * and multiple directory separators will be condensed into one. * Additionally, all `.` and `..` directory references will be resolved in * the returned path. * * Note that if the normalized path is not an absolute path, the resulting * path may begin with `..` directory references if it is not possible to * resolve them simply by using string handling. You should also note that * if the resulting path would result in an empty string, this method will * return `.` instead. * * If the `$prependDrive` option is enabled, the normalized path will be * prepended with the drive name on Windows platforms using the current * working directory, if the path is an absolute path that does not include * a drive name. * * @param string $path File system path to normalize * @param bool $prependDrive True to prepend drive name to absolute paths * @return string The normalizes file system path */ public static function normalize($path, $prependDrive = true) { $path = self::join((string) $path); if ($path[0] === DIRECTORY_SEPARATOR && $prependDrive) { return strstr(getcwd(), DIRECTORY_SEPARATOR, true) . $path; } return $path; } /** * Joins the provided file systems paths together and normalizes the result. * * The paths can be provided either as multiple arguments to this method * or as an array. The paths will be joined using the system directory * separator and the result will be normalized similar to the normalization * method (the drive letter will not be prepended however). * * Note that unless the first path in the list is an absolute path, the * entire resulting path will be treated as a relative path. * * @param string[]|string $paths File system paths to join * @return string The joined file system paths */ public static function join($paths) { $paths = array_map('strval', is_array($paths) ? $paths : func_get_args()); $parts = self::getParts($paths); $absolute = self::isAbsolute($paths[0]); $root = $absolute ? array_shift($parts) . DIRECTORY_SEPARATOR : ''; $parts = self::resolve($parts, $absolute); return self::buildPath($root, $parts); } /** * Builds the final path from the root and path parts. * @param string $root Root path * @param string[] $parts The path parts * @return string The final built path */ private static function buildPath($root, array $parts) { if ($parts === []) { return $root === '' ? '.' : $root; } return $root . implode(DIRECTORY_SEPARATOR, $parts); } /** * Merges the paths and returns the individual parts. * @param string[] $paths Array of paths * @return string[] Parts in the paths merged into a single array */ private static function getParts(array $paths) { if ($paths === []) { throw new \InvalidArgumentException('You must provide at least one path'); } return array_map('trim', explode('/', str_replace('\\', '/', implode('/', $paths)))); } /** * Tells if the path is an absolute path. * @param string $path The file system path to test * @return bool True if the path is an absolute path, false if not */ private static function isAbsolute($path) { $path = trim($path); if ($path === '') { return false; } $length = strcspn($path, '/\\'); return $length === 0 || $path[$length - 1] === ':'; } /** * Resolves parent directory references and removes redundant entries. * @param string[] $parts List of parts in the the path * @param bool $absolute Whether the path is an absolute path or not * @return string[] Resolved list of parts in the path */ private static function resolve(array $parts, $absolute) { $resolved = []; foreach ($parts as $path) { if ($path === '..') { self::resolveParent($resolved, $absolute); } elseif (self::isValidPath($path)) { $resolved[] = $path; } } return $resolved; } /** * Tells if the part of the path is valid and not empty. * @param string $path Part of the path to check for redundancy * @return bool True if the path is valid and not empty, false if not * @throws \InvalidArgumentException If the path contains invalid characters */ private static function isValidPath($path) { if (strpos($path, ':') !== false) { throw new \InvalidArgumentException('Invalid path character ":"'); } return $path !== '' && $path !== '.'; } /** * Resolves the relative parent directory for the path. * @param string[] $parts Path parts to modify * @param bool $absolute True if dealing with absolute path, false if not * @return string|null The removed parent or null if nothing was removed */ private static function resolveParent(& $parts, $absolute) { if ($absolute || !in_array(end($parts), ['..', false], true)) { return array_pop($parts); } $parts[] = '..'; } } Kit-PathJoin-1.1.2/src/autoload.php000066400000000000000000000006451256605642400171030ustar00rootroot00000000000000