Habari\Site::get_dir PHP Method

get_dir() public static method

get_dir returns a complete filesystem path to the requested item 'config_file' returns the complete path to the config.php file, including the filename 'config' returns the path of the directory containing config.php 'user' returns the path of the user directory 'theme' returns the path of the site's active theme 'admin_theme' returns the path to the admin directory 'vendor' returns the path to the vendor directory
public static get_dir ( string $name, boolean | string $trail = false ) : string
$name string the name of the path item to return
$trail boolean | string whether to include a trailing slash, or a string to use as the trailing value. Default: false
return string Path
    public static function get_dir($name, $trail = false)
    {
        $path = '';
        switch (strtolower($name)) {
            case 'config_file':
                $path = Site::get_dir('config') . '/config.php';
                break;
            case 'config':
                if (self::$config_path) {
                    return self::$config_path;
                }
                self::$config_path = HABARI_PATH;
                $config_dirs = preg_replace('/^' . preg_quote(HABARI_PATH, '/') . '\\/user\\/sites\\/(.*)/', '$1', Utils::glob(HABARI_PATH . '/user/sites/*', GLOB_ONLYDIR));
                if (empty($config_dirs)) {
                    return self::$config_path;
                }
                // Collect host parts
                $server = InputFilter::parse_url(Site::get_url('habari'));
                $request = array();
                if (isset($server['port']) && $server['port'] != '' && $server['port'] != '80') {
                    $request[] = $server['port'];
                }
                $request = array_merge($request, explode('.', $server['host']));
                // Collect the subdirectory(s) the core is located in to determine the base path later
                // Don't add them to $request, they will be added with $_SERVER['REQUEST_URI']
                $basesegments = count($request);
                if (!empty($server['path'])) {
                    $coresubdir = explode('/', trim($server['path'], '/'));
                    $basesegments += count($coresubdir);
                }
                // Preserve $request without directories for fallback
                $request_base = $request;
                // Add directory part
                if (!empty(trim($_SERVER['REQUEST_URI'], '/'))) {
                    $subdirectories = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
                    $request = array_merge($request, $subdirectories);
                }
                // Now cut parts until we found a matching site directory
                $cuts = 0;
                $dir_cuts = 0;
                do {
                    $match = implode('.', $request);
                    if (in_array($match, $config_dirs)) {
                        self::$config_dir = $match;
                        self::$config_path = HABARI_PATH . '/user/sites/' . self::$config_dir;
                        self::$config_type = isset($subdirectories) && array_intersect($subdirectories, $request) == $subdirectories ? Site::CONFIG_SUBDIR : Site::CONFIG_SUBDOMAIN;
                        self::$config_urldir = implode('/', array_slice($request, $basesegments + $cuts));
                        break;
                    }
                    // Cut a part from the beginning
                    array_shift($request);
                    $cuts--;
                    // Do not fallback to only the directory part, instead, cut one and start over
                    if ($basesegments + $cuts <= 0 && isset($subdirectories) && $dir_cuts < count($subdirectories)) {
                        $cuts = 0;
                        $dir_cuts++;
                        $request = array_merge($request_base, array_slice($subdirectories, 0, count($subdirectories) - $dir_cuts));
                    }
                    // What does the following check do?!
                    if ($cuts < -10) {
                        echo $cuts;
                        var_dump($request);
                        die('too many ');
                    }
                } while (count($request) > 0);
                $path = self::$config_path;
                break;
            case 'user':
                if (Site::get_dir('config') == HABARI_PATH) {
                    $path = HABARI_PATH . '/user';
                } else {
                    $path = Site::get_dir('config');
                }
                break;
            case 'theme':
                $theme = Themes::get_theme_dir();
                if (file_exists(Site::get_dir('config') . '/themes/' . $theme)) {
                    $path = Site::get_dir('user') . '/themes/' . $theme;
                } elseif (file_exists(HABARI_PATH . '/user/themes/' . $theme)) {
                    $path = HABARI_PATH . '/user/themes/' . $theme;
                } elseif (file_exists(HABARI_PATH . '/3rdparty/themes/' . $theme)) {
                    $path = Site::get_dir('habari') . '/3rdparty/themes/' . $theme;
                } else {
                    $path = HABARI_PATH . '/system/themes/' . $theme;
                }
                break;
            case 'admin_theme':
                $path = HABARI_PATH . '/system/admin';
                break;
            case 'vendor':
                $path = HABARI_PATH . '/system/vendor';
                break;
        }
        $path .= Utils::trail($trail);
        $path = Plugins::filter('site_dir_' . $name, $path);
        return $path;
    }