Go\Instrument\PathResolver::realpath PHP Method

realpath() public static method

Custom replacement for realpath() and stream_resolve_include_path()
public static realpath ( string | array $somePath, boolean $shouldCheckExistence = false ) : array | boolean | string
$somePath string | array Path without normalization or array of paths
$shouldCheckExistence boolean Flag for checking existence of resolved filename
return array | boolean | string
    public static function realpath($somePath, $shouldCheckExistence = false)
    {
        // Do not resolve empty string/false/arrays into the current path
        if (!$somePath) {
            return $somePath;
        }
        if (is_array($somePath)) {
            return array_map(array(__CLASS__, __FUNCTION__), $somePath);
        }
        // Trick to get scheme name and path in one action. If no scheme, then there will be only one part
        $components = explode('://', $somePath, 2);
        list($pathScheme, $path) = isset($components[1]) ? $components : array(null, $components[0]);
        // Optimization to bypass complex logic for simple paths (eg. not in phar archives)
        if (!$pathScheme && ($fastPath = stream_resolve_include_path($somePath))) {
            return $fastPath;
        }
        $isRelative = !$pathScheme && $path[0] !== '/' && $path[1] !== ':';
        if ($isRelative) {
            $path = getcwd() . DIRECTORY_SEPARATOR . $path;
        }
        // resolve path parts (single dot, double dot and double delimiters)
        $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
        if (strpos($path, '.') !== false) {
            $parts = explode(DIRECTORY_SEPARATOR, $path);
            $absolutes = [];
            foreach ($parts as $part) {
                if ('.' == $part) {
                    continue;
                } elseif ('..' == $part) {
                    array_pop($absolutes);
                } else {
                    $absolutes[] = $part;
                }
            }
            $path = implode(DIRECTORY_SEPARATOR, $absolutes);
        }
        if ($pathScheme) {
            $path = "{$pathScheme}://{$path}";
        }
        if ($shouldCheckExistence && !file_exists($path)) {
            return false;
        }
        return $path;
    }

Usage Example

 /**
  * Replace source path with correct one
  *
  * This operation can check for cache, can rewrite paths, add additional filters and much more
  *
  * @param string $originalResource Initial resource to include
  * @param string $originalDir Path to the directory from where include was called for resolving relative resources
  *
  * @return string Transformed path to the resource
  */
 public static function rewrite($originalResource, $originalDir = '')
 {
     static $appDir, $cacheDir, $debug, $usePrebuiltCache;
     if (!$appDir) {
         extract(self::$options, EXTR_IF_EXISTS);
         $usePrebuiltCache = self::$options['features'] & Features::PREBUILT_CACHE;
     }
     $resource = (string) $originalResource;
     if ($resource['0'] !== '/') {
         $resource = PathResolver::realpath($resource, $checkExistence = true) ?: PathResolver::realpath("{$originalDir}/{$resource}", $checkExistence = true) ?: $originalResource;
     }
     // If the cache is disabled, then use on-fly method
     if (!$cacheDir || $debug) {
         return self::PHP_FILTER_READ . self::$filterName . "/resource=" . $resource;
     }
     $newResource = str_replace($appDir, $cacheDir, $resource);
     // Trigger creation of cache, this will create a cache file with $newResource name
     if (!$usePrebuiltCache && !file_exists($newResource)) {
         // Workaround for https://github.com/facebook/hhvm/issues/2485
         $file = fopen($resource, 'r');
         stream_filter_append($file, self::$filterName);
         stream_get_contents($file);
         fclose($file);
     }
     return $newResource;
 }
All Usage Examples Of Go\Instrument\PathResolver::realpath
PathResolver