/lib/internal/Magento/Framework/App/SetupInfo.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 165 lines · 73 code · 15 blank · 77 comment · 9 complexity · a49c4318208893b263d63df69d641bd6 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use \Magento\Framework\Setup\BackendFrontnameGenerator;
  8. /**
  9. * A model for determining information about setup application
  10. */
  11. class SetupInfo
  12. {
  13. /**#@+
  14. * Initialization parameters for redirecting if the application is not installed
  15. */
  16. const PARAM_NOT_INSTALLED_URL_PATH = 'MAGE_NOT_INSTALLED_URL_PATH';
  17. const PARAM_NOT_INSTALLED_URL = 'MAGE_NOT_INSTALLED_URL';
  18. /**#@-*/
  19. /**
  20. * Default path relative to the project root
  21. */
  22. const DEFAULT_PATH = 'setup';
  23. /**
  24. * Environment variables
  25. *
  26. * @var array
  27. */
  28. private $server;
  29. /**
  30. * Current document root directory
  31. *
  32. * @var string
  33. */
  34. private $docRoot;
  35. /**
  36. * Project root directory
  37. *
  38. * @var string
  39. */
  40. private $projectRoot;
  41. /**
  42. * Constructor
  43. *
  44. * @param array $server
  45. * @param string $projectRoot
  46. * @throws \InvalidArgumentException
  47. */
  48. public function __construct($server, $projectRoot = '')
  49. {
  50. $this->server = $server;
  51. if (empty($server['DOCUMENT_ROOT'])) {
  52. throw new \InvalidArgumentException('DOCUMENT_ROOT variable is unavailable.');
  53. }
  54. $this->docRoot = rtrim(str_replace('\\', '/', $server['DOCUMENT_ROOT']), '/');
  55. $this->projectRoot = $projectRoot ?: $this->detectProjectRoot();
  56. $this->projectRoot = str_replace('\\', '/', $this->projectRoot);
  57. }
  58. /**
  59. * Automatically detects project root from current environment
  60. *
  61. * Assumptions:
  62. * if the current setup application relative path is at the end of script path, then it is setup application
  63. * otherwise it is the "main" application
  64. *
  65. * @return mixed
  66. * @throws \InvalidArgumentException
  67. */
  68. private function detectProjectRoot()
  69. {
  70. if (empty($this->server['SCRIPT_FILENAME'])) {
  71. throw new \InvalidArgumentException('Project root cannot be automatically detected.');
  72. }
  73. $haystack = str_replace('\\', '/', dirname($this->server['SCRIPT_FILENAME']));
  74. $needle = '/' . $this->getPath();
  75. $isSetupApp = preg_match('/^(.+?)' . preg_quote($needle, '/') . '$/', $haystack, $matches);
  76. if ($isSetupApp) {
  77. return $matches[1];
  78. }
  79. return $haystack;
  80. }
  81. /**
  82. * Gets setup application URL
  83. *
  84. * @return string
  85. */
  86. public function getUrl()
  87. {
  88. if (isset($this->server[self::PARAM_NOT_INSTALLED_URL])) {
  89. return $this->server[self::PARAM_NOT_INSTALLED_URL];
  90. }
  91. return Request\Http::getDistroBaseUrlPath($this->server) . $this->getPath() . '/';
  92. }
  93. /**
  94. * Gets the "main" application URL
  95. *
  96. * @return string
  97. */
  98. public function getProjectUrl()
  99. {
  100. $isProjectInDocRoot = false !== strpos($this->projectRoot . '/', $this->docRoot . '/');
  101. if (empty($this->server['HTTP_HOST'])) {
  102. return '';
  103. } else if (!$isProjectInDocRoot) {
  104. return 'http://' . $this->server['HTTP_HOST'] . '/';
  105. }
  106. return 'http://' . $this->server['HTTP_HOST'] . substr($this->projectRoot . '/', strlen($this->docRoot));
  107. }
  108. /**
  109. * Get the admin area path
  110. *
  111. * @return string
  112. */
  113. public function getProjectAdminPath()
  114. {
  115. return BackendFrontnameGenerator::generate();
  116. }
  117. /**
  118. * Gets setup application directory path in the filesystem
  119. *
  120. * @param string $projectRoot
  121. * @return string
  122. */
  123. public function getDir($projectRoot)
  124. {
  125. return rtrim($projectRoot, '/') . '/' . $this->getPath();
  126. }
  127. /**
  128. * Checks if the setup application is available in current document root
  129. *
  130. * @return bool
  131. */
  132. public function isAvailable()
  133. {
  134. $setupDir = $this->getDir($this->projectRoot);
  135. $isSubDir = false !== strpos($setupDir . '/', $this->docRoot . '/');
  136. return $isSubDir && realpath($setupDir);
  137. }
  138. /**
  139. * Gets relative path to setup application
  140. *
  141. * @return string
  142. */
  143. private function getPath()
  144. {
  145. if (isset($this->server[self::PARAM_NOT_INSTALLED_URL_PATH])) {
  146. return trim($this->server[self::PARAM_NOT_INSTALLED_URL_PATH], '/');
  147. }
  148. return self::DEFAULT_PATH;
  149. }
  150. }