Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetupInfo.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework\App;
8 
10 
14 class SetupInfo
15 {
19  const PARAM_NOT_INSTALLED_URL_PATH = 'MAGE_NOT_INSTALLED_URL_PATH';
20  const PARAM_NOT_INSTALLED_URL = 'MAGE_NOT_INSTALLED_URL';
26  const DEFAULT_PATH = 'setup';
27 
33  private $server;
34 
40  private $docRoot;
41 
47  private $projectRoot;
48 
56  public function __construct($server, $projectRoot = '')
57  {
58  $this->server = $server;
59  if (empty($server['DOCUMENT_ROOT'])) {
60  throw new \InvalidArgumentException('DOCUMENT_ROOT variable is unavailable.');
61  }
62  $this->docRoot = rtrim(str_replace('\\', '/', $server['DOCUMENT_ROOT']), '/');
63  $this->projectRoot = $projectRoot ?: $this->detectProjectRoot();
64  $this->projectRoot = str_replace('\\', '/', $this->projectRoot);
65  }
66 
77  private function detectProjectRoot()
78  {
79  if (empty($this->server['SCRIPT_FILENAME'])) {
80  throw new \InvalidArgumentException('Project root cannot be automatically detected.');
81  }
82  $haystack = str_replace('\\', '/', dirname($this->server['SCRIPT_FILENAME']));
83  $needle = '/' . $this->getPath();
84  $isSetupApp = preg_match('/^(.+?)' . preg_quote($needle, '/') . '$/', $haystack, $matches);
85  if ($isSetupApp) {
86  return $matches[1];
87  }
88  return $haystack;
89  }
90 
96  public function getUrl()
97  {
98  if (isset($this->server[self::PARAM_NOT_INSTALLED_URL])) {
99  return $this->server[self::PARAM_NOT_INSTALLED_URL];
100  }
101  return Request\Http::getDistroBaseUrlPath($this->server) . $this->getPath() . '/';
102  }
103 
109  public function getProjectUrl()
110  {
111  $isProjectInDocRoot = false !== strpos($this->projectRoot . '/', $this->docRoot . '/');
112  if (empty($this->server['HTTP_HOST'])) {
113  return '';
114  } elseif (!$isProjectInDocRoot) {
115  return 'http://' . $this->server['HTTP_HOST'] . '/';
116  }
117  return 'http://' . $this->server['HTTP_HOST'] . substr($this->projectRoot . '/', strlen($this->docRoot));
118  }
119 
125  public function getProjectAdminPath()
126  {
128  }
129 
136  public function getDir($projectRoot)
137  {
138  return rtrim($projectRoot, '/') . '/' . $this->getPath();
139  }
140 
146  public function isAvailable()
147  {
148  $setupDir = $this->getDir($this->projectRoot);
149  $isSubDir = false !== strpos($setupDir . '/', $this->docRoot . '/');
150  // Setup is not accessible from pub folder
151  $setupDir = rtrim($setupDir, '/');
152  $lastOccurrence = strrpos($setupDir, '/pub/setup');
153 
154  if (false !== $lastOccurrence) {
155  $setupDir = substr_replace($setupDir, '/setup', $lastOccurrence, strlen('/pub/setup'));
156  }
157 
158  return $isSubDir && realpath($setupDir);
159  }
160 
166  private function getPath()
167  {
168  if (isset($this->server[self::PARAM_NOT_INSTALLED_URL_PATH])) {
169  return trim($this->server[self::PARAM_NOT_INSTALLED_URL_PATH], '/');
170  }
171  return self::DEFAULT_PATH;
172  }
173 }
static getDistroBaseUrlPath($server)
Definition: Http.php:362
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct($server, $projectRoot='')
Definition: SetupInfo.php:56