firhatsungkar
2/20/2018 - 4:17 AM

Valet Driver for Themosis (https://framework.themosis.com/). Modified from https://gist.github.com/ramon-villain/fa6a3098944d06f2d714e7457eb

<?php
class ThemosisValetDriver extends BasicValetDriver
{
    /**
     * Determine if the driver serves the request.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return bool
     */
    public function serves($sitePath, $siteName, $uri)
    {
        return file_exists($sitePath . "/$siteName" . '/library/Thms/Config/Environment.php');
    }
    /**
     * Determine if the incoming request is for a static file.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string|false
     */
    public function isStaticFile($sitePath, $siteName, $uri)
    {
        if (file_exists($staticFilePath = $sitePath . '/$siteName' . $uri)) {
            header('Content-Type:text/plain');
            return $staticFilePath;
        } elseif ($this->isActualFile($staticFilePath = $sitePath . "/$siteName" . $uri)) {
            header('Content-Type:text/plain');
            return $staticFilePath;
        }
        return false;
    }
    /**
     * Get the fully resolved path to the application's front controller.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string
     */
    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        if (strpos($uri, '/cms/') !== false) {
            $dynamicCandidates = [
                $this->asActualFile($sitePath . "/$siteName", $uri),
                $this->asPhpIndexFileInDirectory($sitePath . "/$siteName", $uri),
                $this->asHtmlIndexFileInDirectory($sitePath . "/$siteName", $uri),
            ];
            foreach ($dynamicCandidates as $id => $candidate) {

                if ($this->isActualFile($candidate)) {
                    $_SERVER['PHP_SELF'] = $uri;
                    $_SERVER['SCRIPT_FILENAME'] = $candidate;
                    $_SERVER['SCRIPT_NAME'] = str_replace($sitePath . "/$siteName", '', $candidate);
                    $_SERVER['DOCUMENT_ROOT'] = $sitePath . "/$siteName";
                    return $candidate;
                }
            }
        }
        return $sitePath . "/$siteName" . '/htdocs/index.php';
    }
    /**
     * Redirect to uri with trailing slash.
     *
     * @param  string $uri
     * @return string
     */
    private function forceTrailingSlash($uri)
    {
        if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') {
            header('Location: ' . $uri . '/');
            die;
        }
        return $uri;
    }
    /**
     * Mutate the incoming URI.
     *
     * @param  string  $uri
     * @return string
     */
    public function mutateUri($uri)
    {
        return rtrim('/htdocs' . $uri, '/');
    }
}