Presenting the PHP 5.3 compatible version of my PHP path resolution class!
Why there should be a difference? Because my original version used create_function, which… created a whole new function object every time you used one of the class’s methods. This was very memory expensive and even incurred a slight performance hit.
The new version uses PHP 5.3′s anonymous functions to create more readable code in addition to reducing memory consumption and execution time.
As usual, the source code is under the WTFPL for you to enjoy without any restrictions.
<?php /** * @class Path * * @brief Utility class that handles file and directory pathes * * This class handles basic important operations done to file system paths. * It safely renders relative pathes and removes all ambiguity from a relative path. * * @author Liran Nuna */ final class Path { /** * Returns the parent path of this path. * "/path/to/directory" will return "/path/to" * * @arg $path The path to retrieve the parent path from */ public static function dirname($path) { return dirname(self::normalize($path)); } /** * Returns the last item on the path. * "/path/to/directory" will return "directory" * * @arg $path The path to retrieve the base from */ public static function basename($path) { return basename(self::normalize($path)); } /** * Normalizes the path for safe usage * This function does several operations to the given path: * * Removes unnecessary slashes (///path//to/////directory////) * * Removes current directory references (/path/././to/./directory/./././) * * Renders relative pathes (/path/from/../to/somewhere/in/../../directory) * * @arg $path The path to normalize */ public static function normalize($path) { return array_reduce(explode('/', $path), function($a, $b) { if($a === 0) $a = "/"; if($b === "" || $b === ".") return $a; if($b === "..") return dirname($a); return preg_replace("/\/+/", "/", "$a/$b"); }, 0); } /** * Combines a list of pathes to one safe path * * @arg $root The path or array with values to combine into a single path * @arg ... Relative pathes to root or arrays * * @note This function works with multi-dimentional arrays recursively. */ public static function combine($root, $rel1) { $arguments = func_get_args(); return self::normalize(array_reduce($arguments, function($a,$b) { if(is_array($a)) $a = array_reduce($a, 'Path::combine'); if(is_array($b)) $b = array_reduce($b, 'Path::combine'); return "$a/$b"; })); } /** * Empty, private constructor, to prevent instantiation */ private function __construct() { // Prevents instantiation } } |