server = $server; if (empty($server['DOCUMENT_ROOT'])) { throw new \InvalidArgumentException('DOCUMENT_ROOT variable is unavailable.'); } $this->docRoot = rtrim(str_replace('\\', '/', $server['DOCUMENT_ROOT']), '/'); $this->projectRoot = $projectRoot ?: $this->detectProjectRoot(); $this->projectRoot = str_replace('\\', '/', $this->projectRoot); } /** * Automatically detects project root from current environment * * Assumptions: * if the current setup application relative path is at the end of script path, then it is setup application * otherwise it is the "main" application * * @return mixed * @throws \InvalidArgumentException */ private function detectProjectRoot() { if (empty($this->server['SCRIPT_FILENAME'])) { throw new \InvalidArgumentException('Project root cannot be automatically detected.'); } $haystack = str_replace('\\', '/', dirname($this->server['SCRIPT_FILENAME'])); $needle = '/' . $this->getPath(); $isSetupApp = preg_match('/^(.+?)' . preg_quote($needle, '/') . '$/', $haystack, $matches); if ($isSetupApp) { return $matches[1]; } return $haystack; } /** * Gets setup application URL * * @return string */ public function getUrl() { if (isset($this->server[self::PARAM_NOT_INSTALLED_URL])) { return $this->server[self::PARAM_NOT_INSTALLED_URL]; } return Request\Http::getDistroBaseUrlPath($this->server) . $this->getPath() . '/'; } /** * Gets the "main" application URL * * @return string */ public function getProjectUrl() { $isProjectInDocRoot = false !== strpos($this->projectRoot . '/', $this->docRoot . '/'); if (empty($this->server['HTTP_HOST'])) { return ''; } else if (!$isProjectInDocRoot) { return 'http://' . $this->server['HTTP_HOST'] . '/'; } return 'http://' . $this->server['HTTP_HOST'] . substr($this->projectRoot . '/', strlen($this->docRoot)); } /** * Get the admin area path * * @return string */ public function getProjectAdminPath() { return BackendFrontnameGenerator::generate(); } /** * Gets setup application directory path in the filesystem * * @param string $projectRoot * @return string */ public function getDir($projectRoot) { return rtrim($projectRoot, '/') . '/' . $this->getPath(); } /** * Checks if the setup application is available in current document root * * @return bool */ public function isAvailable() { $setupDir = $this->getDir($this->projectRoot); $isSubDir = false !== strpos($setupDir . '/', $this->docRoot . '/'); return $isSubDir && realpath($setupDir); } /** * Gets relative path to setup application * * @return string */ private function getPath() { if (isset($this->server[self::PARAM_NOT_INSTALLED_URL_PATH])) { return trim($this->server[self::PARAM_NOT_INSTALLED_URL_PATH], '/'); } return self::DEFAULT_PATH; } }