Add explicit `return null;` statements
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / devtools / DevtoolsSetup.class.php
1 <?php
2
3 namespace wcf\system\devtools;
4
5 use wcf\system\SingletonFactory;
6 use wcf\util\FileUtil;
7 use wcf\util\JSON;
8
9 /**
10 * Enables the rapid deployment of new installations using a central configuration file
11 * in the document root. Requires the developer mode to work.
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Devtools\Package
17 * @since 3.1
18 */
19 class DevtoolsSetup extends SingletonFactory
20 {
21 /**
22 * configuration file in the server's document root
23 * @var string
24 */
25 const CONFIGURATION_FILE = 'wsc-dev-config-53.json';
26
27 /**
28 * configuration data
29 * @var array
30 */
31 protected $configuration = [];
32
33 /**
34 * @inheritDoc
35 */
36 protected function init()
37 {
38 if (empty($_SERVER['DOCUMENT_ROOT'])) {
39 return;
40 }
41
42 $docRoot = FileUtil::addTrailingSlash(FileUtil::unifyDirSeparator($_SERVER['DOCUMENT_ROOT']));
43 if (!\file_exists($docRoot . self::CONFIGURATION_FILE)) {
44 return;
45 }
46
47 $contents = \file_get_contents($docRoot . self::CONFIGURATION_FILE);
48
49 // allow the exception to go rampage
50 $this->configuration = JSON::decode($contents);
51 }
52
53 /**
54 * Returns the database configuration.
55 *
56 * @return array|null
57 */
58 public function getDatabaseConfig()
59 {
60 if (!isset($this->configuration['setup']) || !isset($this->configuration['setup']['database'])) {
61 return null;
62 }
63
64 // dirname return a single backslash on Windows if there are no parent directories
65 $dir = \dirname($_SERVER['SCRIPT_NAME']);
66 $dir = ($dir === '\\') ? '/' : FileUtil::addTrailingSlash($dir);
67 if ($dir === '/') {
68 throw new \RuntimeException("Refusing to install in the document root.");
69 }
70
71 $dir = FileUtil::removeLeadingSlash(FileUtil::removeTrailingSlash($dir));
72 $dbName = \implode('_', \explode('/', $dir));
73
74 $dbConfig = $this->configuration['setup']['database'];
75
76 return [
77 'auto' => $dbConfig['auto'],
78 'host' => $dbConfig['host'],
79 'password' => $dbConfig['password'],
80 'username' => $dbConfig['username'],
81 'dbName' => $dbName,
82 'dbNumber' => $dbConfig['dbNumber'],
83 ];
84 }
85
86 /**
87 * Returns true if the suggested default paths for the Core and, if exists,
88 * the bundled app should be used.
89 *
90 * @return bool
91 */
92 public function useDefaultInstallPath()
93 {
94 return isset($this->configuration['setup']) && isset($this->configuration['setup']['useDefaultInstallPath']) && $this->configuration['setup']['useDefaultInstallPath'] === true;
95 }
96
97 /**
98 * Returns true if a static cookie prefix should be used, instead of the randomized
99 * value used for non-dev-mode installations.
100 *
101 * @return bool
102 */
103 public function forceStaticCookiePrefix()
104 {
105 return isset($this->configuration['setup']) && isset($this->configuration['setup']['forceStaticCookiePrefix']) && $this->configuration['setup']['forceStaticCookiePrefix'] === true;
106 }
107
108 /**
109 * List of option values that will be set after the setup has completed.
110 *
111 * @return string[]
112 */
113 public function getOptionOverrides()
114 {
115 if (!isset($this->configuration['configuration']) || empty($this->configuration['configuration']['option'])) {
116 return [];
117 }
118
119 if (isset($this->configuration['configuration']['option']['cookie_prefix'])) {
120 throw new \DomainException("The 'cookie_prefix' option cannot be set during the setup, consider using the 'forceStaticCookiePrefix' setting instead.");
121 }
122
123 return $this->configuration['configuration']['option'];
124 }
125
126 /**
127 * Returns a list of users that should be automatically created during setup.
128 *
129 * @return array|\Generator
130 */
131 public function getUsers()
132 {
133 if (empty($this->configuration['user'])) {
134 return;
135 }
136
137 foreach ($this->configuration['user'] as $user) {
138 if ($user['username'] === 'root') {
139 throw new \LogicException("The 'root' user is automatically created.");
140 }
141
142 yield [
143 'username' => $user['username'],
144 'password' => $user['password'],
145 'email' => $user['email'],
146 ];
147 }
148 }
149
150 /**
151 * Returns the base path for projects that should be automatically imported.
152 *
153 * @return string
154 */
155 public function getDevtoolsImportPath()
156 {
157 return (isset($this->configuration['configuration']['devtools']) && !empty($this->configuration['configuration']['devtools']['importFromPath'])) ? $this->configuration['configuration']['devtools']['importFromPath'] : '';
158 }
159
160 /**
161 * Returns the raw configuration data.
162 *
163 * @return array
164 */
165 public function getRawConfiguration()
166 {
167 return $this->configuration;
168 }
169 }