Shortens file paths by replacing the application base path with 'APP', and the CakePHP core
path with 'CORE'.

@param string $path Path to shorten.
@return string Normalized path
500101
public static function trimPath($path)
    {
        if (defined('APP') && strpos($path, APP) === 0) {
            return str_replace(APP, 'APP/', $path);
        }
        if (defined('CAKE_CORE_INCLUDE_PATH') && strpos($path, CAKE_CORE_INCLUDE_PATH) === 0) {
            return str_replace(CAKE_CORE_INCLUDE_PATH, 'CORE', $path);
        }
        if (defined('ROOT') && strpos($path, ROOT) === 0) {
            return str_replace(ROOT, 'ROOT', $path);
        }

        return $path;
    }
1082603
protected function merge($path)
    {
        // 5.2.3.  Merge Paths
        if ($this->host !== null && $this->path == '') {
            return '/'.$path;
        }

        $pos = strrpos($this->path, '/');
        if ($pos === false) {
            return $path;
        }
        return substr($this->path, 0, $pos + 1).$path;
    }
321431
public static function normalize(string $path): string {
		if (!(false === strpos($path, '://'))) {
			$ex = explode("://", $path);

			return $ex[0] . "://" . "/" . ltrim(Util::normalizePath("/" . ltrim($ex[1], "/")));
		}

		return "/" . ltrim(Util::normalizePath($path), "/");
	}
1332184
protected function normalizePath($path)
    {
        if (preg_match("/^id:.*|^rev:.*|^(ns:[0-9]+(\/.*)?)/", $path) === 1) {
            return $path;
        }

        $path = (trim($path, '/') === '') ? '' : '/'.$path;

        return str_replace('//', '/', $path);
    }
221030
private function normalizeAssetPath($path)
    {
        $pattern = '~@(\w+)~';
        $replacement = sprintf('/%s/$1/%s', ViewManager::TEMPLATE_PARAM_MODULES_DIR, ViewManager::TEMPLATE_PARAM_ASSETS_DIR);

        return preg_replace($pattern, $replacement, $path);
    }
300866
protected function canonicalizePath($path)
    {
        if ('' === $path || '/' === $path) {
            return $path;
        }

        if ('.' === substr($path, -1)) {
            $path .= '/';
        }

        $output = [];

        foreach (explode('/', $path) as $segment) {
            if ('..' === $segment) {
                array_pop($output);
            } elseif ('.' !== $segment) {
                $output[] = $segment;
            }
        }

        return implode('/', $output);
    }
1144400
protected function splitPath($path)
    {
        $lastSlashPos = strrpos($path, '/');
        if (false === $lastSlashPos) {
            return array('dirname' => '', 'basename' => $path);
        }

        return array('dirname'  => substr($path, 0, $lastSlashPos),
                     'basename' => substr($path, $lastSlashPos + 1)
               );
    }
590451
private function removeFileExtensionFromPath($path)
    {
        if (strrpos($path, '.') !== false) {
            $path = substr($path, 0, strrpos($path, '.'));
        }

        return $path;
    }
1203071
public static function shortenPath($file)
    {
        $file = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $file)), 'strlen'));

        $longest_match = 0;
        $match = '/';

        foreach (self::$app_root_dirs as $path => $alias) {
            if (empty($path)) {
                continue;
            }

            $path = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $path)), 'strlen'));

            if (\array_slice($file, 0, \count($path)) === $path && \count($path) > $longest_match) {
                $longest_match = \count($path);
                $match = $alias;
            }
        }

        if ($longest_match) {
            $file = \array_merge(array($match), \array_slice($file, $longest_match));

            return \implode('/', $file);
        }

        // fallback to find common path with Kint dir
        $kint = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', KINT_DIR)), 'strlen'));

        foreach ($file as $i => $part) {
            if (!isset($kint[$i]) || $kint[$i] !== $part) {
                return ($i ? '.../' : '/').\implode('/', \array_slice($file, $i));
            }
        }

        return '/'.\implode('/', $file);
    }
272596
public function optionPath($path)
    {
        $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
        $path = PATH_ROOT . str_replace(PATH_ROOT, '', $path);

        if (pathinfo($path, PATHINFO_EXTENSION)) {
            $this->optionFilename(pathinfo($path, PATHINFO_FILENAME));
            $path = dirname($path);
        }

        $this->optionPath = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

        return $this;
    }
642781
private function fileRelativePath($path)
    {
        $path = $this->fileWebpath($path);
        // @todo This wont work for files not located on the current server...
        $path = str_replace_first(env('APP_URL'), '', $path);
        $path = str_replace(' ', '%20', $path);

        return $path;
    }
1115439
protected function normalize($path)
    {
        // deal with different operating systems' directory structure
        $path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/');

        /*
         * Example:
         *     /home/forkcms/frontend/cache/compiled_templates/../../core/layout/css/../images/img.gif
         * to
         *     /home/forkcms/frontend/core/layout/images/img.gif
         */
        do {
            $path = preg_replace('/[^\/]+(?<!\.\.)\/\.\.\//', '', $path, -1, $count);
        } while ($count);

        return $path;
    }
857711
public static function getPath($path = '')
    {
        $path = ltrim($path, '/\\');
        $path = sprintf('%s%s%s', self::$applicationRoot, DIRECTORY_SEPARATOR, $path);
        $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
        return $path;
    }
961370
public static function normalizeRelativePath($path)
	{
		// Path remove self referring paths ("/./").
		$path = preg_replace('#/\.(?=/)|^\./|/\./?$#', '', $path);

		// Regex for resolving relative paths
		$regex = '#/*[^/\.]+/\.\.#Uu';

		while (preg_match($regex, $path))
		{
			$path = preg_replace($regex, '', $path);
		}

		return $path;
	}
26951
public static function normalizePath($path, $endSlash = true)
    {
        $path = rtrim($path, '/') . DIRECTORY_SEPARATOR;
        $path = rtrim($path, DIRECTORY_SEPARATOR);

        $path = $endSlash ? $path . DIRECTORY_SEPARATOR : $path;

        return $path;
    }
210192
public static function normalizePath(?string $path): string
  {
    // With thanks to monkeysuffrage, see https://github.com/monkeysuffrage/phpuri/blob/master/phpuri.php.

    if ($path===null || $path==='')
    {
      return '';
    }

    $normalized_path = $path;
    $normalized_path = preg_replace('`//+`', '/', $normalized_path, -1, $c0);
    $normalized_path = preg_replace('`^/\\.\\.?/`', '/', $normalized_path, -1, $c1);
    $normalized_path = preg_replace('`/\\.(/|$)`', '/', $normalized_path, -1, $c2);
    $normalized_path = preg_replace('`/[^/]*?/\\.\\.(/|$)`', '/', $normalized_path, 1, $c3);
    $_num_matches    = $c0 + $c1 + $c2 + $c3;

    return ($_num_matches>0) ? self::normalizePath($normalized_path) : $normalized_path;
  }
1075945
protected function formatPath($path)
    {
        $path = trim($path, '/') . '/';
        if (strlen($path) > 1) {
            $path = '/' . $path;
        }

        return $path;
    }