Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
50.00% covered (warning)
50.00%
1 / 2
25.00% covered (danger)
25.00%
1 / 4
CRAP
52.63% covered (warning)
52.63%
10 / 19
Path
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
17.61
52.63% covered (warning)
52.63%
10 / 19
 __construct($pathString, $createNonexisting = false)
0.00% covered (danger)
0.00%
0 / 1
3.71
57.14% covered (warning)
57.14%
4 / 7
 __toString()
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 resolve($path)
0.00% covered (danger)
0.00%
0 / 1
5.40
55.56% covered (warning)
55.56%
5 / 9
 _create($pathString)
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
FileDoesNotExistException
100.00% covered (success)
100.00%
1 / 1
 
 
0  
 
<?php
/**
 * Path is a utility to simplify working with directories.
 */
namespace aae\fs {
    /**
     * @author Axel Ancona Esselmann
     * @package aae\cdt
     */
    class Path {
        protected $_pathString = null;
        public function __construct($pathString, $createNonexisting = false) {
            $pathString = $this->resolve($pathString);
            if (!file_exists($pathString)) {
                if ($createNonexisting) $this->_create($pathString);
                else throw new FileDoesNotExistException("Error: '$pathString' is not a valid path.", 213141057);
            }
            $this->_pathString = $pathString;
        }
        public function __toString() {
            return strval($this->_pathString);
        }
        public static function resolve($path) {
            if (substr($path, 0, 1) != "/") {
                $docRoot = $_SERVER["DOCUMENT_ROOT"];
                if (substr($docRoot, -1) != "/") $docRoot .= "/";
                $path    = $docRoot."../".$path;
            }
            $regex  = "/(.?)(\/[^\/]*\/\.\.)(.*)/";
            $result = preg_replace($regex, "$1$3", $path);
            if ($result != $path) $result = self::resolve($result);
            return $result;
        }
        protected function _create($pathString) {
            mkdir($pathString, 0777, true);
        }
    }
    class FileDoesNotExistException extends \Exception {}
}