Resolves a url adds path if missing

@param string $url THe Url to internalize
@return string A well formed internalized URL
357511
public function internalize($url = '')
    {

        //Are we dealing with an array of parts?
        if (is_array($url)) {
            $url = implode('/', $url);
        }

        //@TODO make sure that this url does not have the scheme
        if (preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url))
            return $url;
        //@TODO make sure that this url does not have the host already
        //@TODO make sure we are internalizing a path and nothing else
        //die;
        //Do we have the path info included?
        $sPath = $this->request->getBasePath() . "/";


        if (!empty($url) && $sPath <> "/") {

            $parts = explode("/", $sPath);
            $segments = explode("/", $url);

            //Remove all empty elements
            $segments = array_filter($segments, 'strlen');
            $parts = array_filter($parts, 'strlen');
            //die;
            //This is in case we have a system deep in multiple supdirectories
            array_unshift($parts, null);
            $fragment = implode("/", $parts);

            if (is_array($segments)) {

                array_unshift($segments, null); //Adds the / to the start of the url
                $url = implode("/", $segments);

                //now look for $fragment at the start of $url
                $pos = strpos($url, $fragment);
                if ($pos !== 0 || $pos === FALSE) {
                    $url = $fragment . $url;
                }
            }
        }
        return $url;
    }
279701
private function getPath($url)
    {
        $url = trim($url);
        $parsed = $this->parseURL($url);
        if ($parsed !== false) {
            $this->url = $url;
            return $parsed['custom'];
        }
        return $url;
    }
1107572
static public function uri($url)
    {
        $res = parse_url($url);
        $return['protocol'] = $res['scheme'];
        $return['host'] = $res['host'];
        $return['port'] = $res['port'];
        $return['user'] = $res['user'];
        $return['pass'] = $res['pass'];
        $return['path'] = $res['path'];
        $return['id'] = $res['fragment'];
        parse_str($res['query'], $return['params']);
        return $return;
    }
679241
private function cleanUrl($url)
    {
        if (strlen($url) > 1 && strrpos($url, '/') === strlen($url) - 1) {
            $url = substr($url, 0, -1);
        }
        if (strrpos($url, '.php') === strlen($url) - 4) {
            $url = substr($url, 0, -4);
        }
        return $url;
    }
1359054
protected function guessInternalPath($url)
    {
        $urlParts = preg_split('~/~', $url->basepath, -1, PREG_SPLIT_NO_EMPTY);
        $app = \T4\Mvc\Application::instance();

        if (0 == count($urlParts)) {
            return new Route([
                'module' => '',
                'controller' => self::DEFAULT_CONTROLLER,
                'action' => self::DEFAULT_ACTION,
                'params' => [],
                'format' => $url->extension ?: 'html',
            ]);
        }

        if (1 == count($urlParts)) {
            if ($app->existsModule($urlParts[0]))
                return new Route([
                    'module' => ucfirst($urlParts[0]),
                    'controller' => self::DEFAULT_CONTROLLER,
                    'action' => self::DEFAULT_ACTION,
                    'params' => [],
                    'format' => $url->extension ?: 'html',
                ]);
            elseif ($app->existsController(null, $urlParts[0]))
                return new Route([
                    'module' => '',
                    'controller' => ucfirst($urlParts[0]),
                    'action' => self::DEFAULT_ACTION,
                    'params' => [],
                    'format' => $url->extension ?: 'html',
                ]);
            else
                return new Route([
                    'module' => '',
                    'controller' => self::DEFAULT_CONTROLLER,
                    'action' => ucfirst($urlParts[0]),
                    'params' => [],
                    'format' => $url->extension ?: 'html',
                ]);
        }

        if (2 == count($urlParts)) {
            if ($app->existsModule($urlParts[0])) {
                if ($app->existsController($urlParts[0], $urlParts[1])) {
                    return new Route([
                        'module' => ucfirst($urlParts[0]),
                        'controller' => ucfirst($urlParts[1]),
                        'action' => self::DEFAULT_ACTION,
                        'params' => [],
                        'format' => $url->extension ?: 'html',
                    ]);
                } else {
                    return new Route([
                        'module' => ucfirst($urlParts[0]),
                        'controller' => self::DEFAULT_CONTROLLER,
                        'action' => ucfirst($urlParts[1]),
                        'params' => [],
                        'format' => $url->extension ?: 'html',
                    ]);
                }
            } elseif ($app->existsController(null, $urlParts[0])) {
                return new Route([
                    'module' => '',
                    'controller' => ucfirst($urlParts[0]),
                    'action' => ucfirst($urlParts[1]),
                    'params' => [],
                    'format' => $url->extension ?: 'html',
                ]);
            }
        }

        if (3 == count($urlParts)) {
            if ($app->existsModule($urlParts[0]) && $app->existsController($urlParts[0], $urlParts[1])) {
                return new Route([
                    'module' => ucfirst($urlParts[0]),
                    'controller' => ucfirst($urlParts[1]),
                    'action' => ucfirst($urlParts[2]),
                    'params' => [],
                    'format' => $url->extension ?: 'html',
                ]);
            }
        }

        if ($app->existsController(null, implode('\\', $urlParts))) {
            return new Route([
                'module' => '',
                'controller' => implode('\\', array_map('ucfirst', $urlParts)),
                'action' => self::DEFAULT_ACTION,
                'params' => [],
                'format' => $url->extension ?: 'html',
            ]);
        }

        if ($app->existsController(null, implode('\\', array_slice($urlParts, 0, -1)))) {
            return new Route([
                'module' => '',
                'controller' => implode('\\', array_map('ucfirst', array_slice($urlParts, 0, -1))),
                'action' => ucfirst(array_pop($urlParts)),
                'params' => [],
                'format' => $url->extension ?: 'html',
            ]);
        }

        throw new RouterException('Route to path \'' . $url->basepath . '\' is not found');
    }
475216
public function generateUrl($url)
    {
        $extern = false;
        if ((0 === strrpos($url, 'http://'))
            || (0 === strrpos($url, 'https://'))) {
            $extern = true;
        }

        $http = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';

        if (!$extern) {
            return ($this->getRoot() == '/')
                ? (($this->getRoot() == $url) ? '' : $url) : $http.$_SERVER['SERVER_NAME'].$this->getRoot().$url;
        } else {
            return $url;
        }
    }
393603
protected function createUrl($url)
    {
        // is absolute URL?
        if (strpos($url, '://')) {
            return Url::createFromUrl($url);
        }

        // only query and path matters in relative url
        $url = Url::createFromUrl($url);
        $query = $this->baseUrl->getQuery();
        $path = $this->baseUrl->getPath();

        $query->modify($url->getQuery());
        $path->append($url->getPath());

        return $this->baseUrl->setQuery($query)->setPath($path);
    }
1039619
public function getPathFromUrl($url)
    {
        $basename = $this->storageUrl ? basename($this->storageUrl) : basename($this->storageDir);
        $basename && $url = preg_replace("/(.*$basename\/)/", '', $url);
        return $url;
    }
311770
protected function formatUrl($url)
    {
        $uri = !is_null($this->prefixUrl) ? $this->prefixUrl . $url : $url;

        return $uri == '/' ? '/' : ltrim(rtrim($uri, '/'), '/');
    }
304716
public function url($url)
    {
        if (\is_string($url)) {
            if ('/' == $url[0]) {
                return $this->getPathToRoot().ltrim($url, '/');
            } elseif (\is_string($url)) {
                return $url;
            }
        } elseif ($url instanceof ClassDefinition) {
            return $this->url(Documentation::classUrl($url));
        } elseif ($url instanceof CompilerFile) {
            return $this->url(Documentation::classUrl($url->getClassDefinition()));
        }

        return '';
    }
743236
public static function resolveHttp($url = null)
    {
        if (is_null($url)) {
            return '';
        }
        $url = preg_replace(',^feed://,i', 'http://', $url);
        if (!preg_match(',^[a-z]+://,i', $url)) {
            $url = 'http://'.$url;
        }
        return $url;
    }
1037351
public function setUrl($url)
    {
        if (! empty($url)) {
            $segments = parse_url($url);

            foreach ($segments as $key => $value) {
                $this->$key = $value;
            }

            if ($this->format = pathinfo($this->path, PATHINFO_EXTENSION)) {
                $this->path = str_replace('.'.$this->format, '', $this->path);
            }
        }

        return $this;
    }
142174
private function parseUrl($url)
	{
		$newUrl = preg_replace($this->patterns, $this->replacements, $url);
		$newUrl = trim($newUrl, '\/?');
		$newUrl = trim($newUrl, '\/');

		return $newUrl;
	}
439810
public function relative($url)
    {
        if (!$url || $url == '/') {
            return '.';
        }

        if (strpos($url, '..') !== false) {
            throw new InvalidArgumentException('Double dots in paths are not allowed');
        }

        $baseUrl = $this->getBaseUrl();

        if ($baseUrl && strpos($url, $baseUrl) === 0) {
            return trim(str_replace($baseUrl, '', $url), '/');
        }

        if ($this->startsWithBasePath($url) || !$this->checkFilesystem) {
            return trim(str_replace($this->basePath, '', $url), '/');
        }

        return static::getRelativePath($this->basePath, $url);
    }
674495
public static function fixUri($url) {
        $url = preg_replace('#/+|\\\+#', '/', $url);

        if ($url[0] == '/') {
            $url = substr($url, 1, strlen($url));
        } if (substr($url, -1) == '/') {
            $url = substr($url, 0, strlen($url) - 1);
        }

        return $url;
    }
1189820
public function url($url)
    {
        if (is_string($url)) {
            $url = $this->localePrefix($url);
        }

        try {
            $url = Router::url($url, true);
        } catch (\Exception $ex) {
            $url = '';
        }

        return $url;
    }
717076
public static function parseUrl($url)
    {
        $parseUrlResult = parse_url($url);
        if ($parseUrlResult === false) {
            return false;
        }

        if (empty($parseUrlResult['scheme'])) {
            $parseUrlResult['scheme'] = 'http';
        }

        if (empty($parseUrlResult['host'])) {
            return false;
        }

        $parseUrlResult['url'] = $url;

        if (empty($parseUrlResult['port'])) {
            if ($parseUrlResult['scheme'] == 'http') {
                $parseUrlResult['port'] = 80;
                $parseUrlResult['ssl']  = false;
            } else {
                $parseUrlResult['port'] = 443;
                $parseUrlResult['ssl']  = true;
            }
        } else {
            if ($parseUrlResult['scheme'] == 'http') {
                $parseUrlResult['ssl']  = false;
            } else {
                $parseUrlResult['ssl']  = true;
            }
        }

        if (empty($parseUrlResult['path'])) {
            $parseUrlResult['path'] = '/';
        }

        if (empty($parseUrlResult['query'])) {
            $parseUrlResult['query'] = '';
        } else {
            $parseUrlResult['query'] = '?' . $parseUrlResult['query'];
        }

        return $parseUrlResult;
    }