-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.php
More file actions
171 lines (152 loc) · 4.36 KB
/
Bootstrap.php
File metadata and controls
171 lines (152 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Definition of class Bootstrap
*
* @copyright 2014-today Justso GmbH, Frankfurt, Germany
* @author j.schirrmacher@justso.de
*
* @package justso
*/
namespace justso\justapi;
require_once("Autoloader.php");
require_once("InvalidParameterException.php");
/**
* Handles global configuration
*
* @package justso
*/
class Bootstrap
{
private $appRoot;
private $config;
private $environment;
private $info;
private static $instance;
/**
* Initializes the Bootstrap configuration.
* @param string|null $appRoot
* @param array $config
* @throws InvalidParameterException
*/
public function __construct($appRoot = null, array $config = null)
{
$this->appRoot = $appRoot ?: dirname(dirname(dirname(__DIR__)));
$this->config = $config ?: json_decode(file_get_contents($this->appRoot . '/config.json'), true);
$this->setEnvironmentInfo();
self::$instance = $this;
}
/**
* Sets the configuration in an existing Bootstrap config for testing purposes - don't use for other things.
*
* @param string $appRoot Simulated application root folder
* @param array $config New test configuration
*/
public function setTestConfiguration($appRoot = null, array $config = null)
{
if ($appRoot !== null) {
$this->appRoot = $appRoot;
}
if ($config !== null) {
$this->config = $config;
}
$this->setEnvironmentInfo();
}
/**
* Resets the configuration to the one specified by config.json
*/
public function resetConfiguration()
{
$this->appRoot = dirname(dirname(dirname(__DIR__)));
$this->config = json_decode(file_get_contents($this->appRoot . '/config.json'), true);
$this->setEnvironmentInfo();
}
/**
* Returns the Bootstrap instance.
*
* @return Bootstrap
* @deprecated Use new Bootstrap() instead.
*/
public static function getInstance()
{
return self::$instance;
}
/**
* Returns the installation type, which is the name of the current environment, e.g. "development" or "production".
*
* @return string
*/
public function getInstallationType()
{
return $this->environment;
}
/**
* Returns allowed origins for CORS.
*
* @return string
*/
public function getAllowedOrigins()
{
return empty($this->info['origins']) ? '' : $this->info['origins'];
}
/**
* Returns the URL of the corresponding API
*/
public function getApiUrl()
{
return empty($this->info['apiurl']) ? $this->getWebAppUrl() . '/api' : $this->info['apiurl'];
}
/**
* Returns the URL of the corresponding web application
*/
public function getWebAppUrl()
{
return empty($this->info['appurl']) ? 'http://localhost' : $this->info['appurl'];
}
/**
* Returns the application root directory path
*
* @return string
*/
public function getAppRoot()
{
return $this->appRoot;
}
/**
* Returns the site configuration.
*
* @return mixed
*/
public function getConfiguration()
{
return $this->config;
}
/**
* Sets a new site configuration
* @param $config
*/
public function setConfiguration($config)
{
$this->config = $config;
$encoded = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
file_put_contents($this->appRoot . '/config.json', $encoded);
}
private function setEnvironmentInfo()
{
if (empty($this->config['environments'])) {
throw new InvalidParameterException('config.json should contain information about environments');
}
foreach ($this->config['environments'] as $environment => $info) {
if (empty($info['approot'])) {
throw new InvalidParameterException(
"config.json environment '$environment' should contain at least 'approot'"
);
}
if ($info['approot'] === $this->appRoot) {
$this->environment = $environment;
$this->info = $info;
return;
}
}
throw new InvalidParameterException("Environment for cwd='" . $this->appRoot . "' not found");
}
}