1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Selim; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
class SelimConfig |
9
|
|
|
{ |
10
|
|
|
private $sites = array(); |
11
|
|
|
private static $uniqueInstance = null; |
12
|
|
|
private $path_config; |
13
|
|
|
|
14
|
|
|
public static function getInstance() |
15
|
|
|
{ |
16
|
|
|
if (self::$uniqueInstance === null) { |
17
|
|
|
self::$uniqueInstance = new SelimConfig(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return self::$uniqueInstance; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function sitePathExists($path) { |
24
|
|
|
foreach($this->sites as $site){ |
25
|
|
|
if(dirname($path) === dirname($site->path)){ |
26
|
|
|
return true; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
final private function __clone() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function __construct() |
37
|
|
|
{ |
38
|
|
|
$request = Request::createFromGlobals(); |
39
|
|
|
$server = $request->server; |
40
|
|
|
$selim_foldername = "/.selim/"; |
41
|
|
|
if($server->get("HOME")){ |
42
|
|
|
$conf_dir = $server->get("HOME").$selim_foldername; |
43
|
|
|
}else if($server->get("HOMEDRIVE")){ |
44
|
|
|
$conf_dir = "{$server->get("HOMEDRIVE")}{$server->get("HOMEPATH")}{$selim_foldername}"; |
45
|
|
|
}else{ |
46
|
|
|
Util::reportError("Can't find Homedir. Aborting..."); |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if(!file_exists($conf_dir)){ |
51
|
|
|
mkdir($conf_dir); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->path_config = $conf_dir."config.json"; |
55
|
|
|
|
56
|
|
|
if (file_exists($this->path_config)) { |
57
|
|
|
self::load(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function load() |
62
|
|
|
{ |
63
|
|
|
$sites = array(); |
64
|
|
|
$json = json_decode(file_get_contents($this->path_config), true); |
65
|
|
|
|
66
|
|
|
if (!isset($json["sites"])) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
foreach ($json["sites"] as $s) { |
70
|
|
|
array_push($sites, new SiteConfig($s["name"], $s["path"])); |
71
|
|
|
} |
72
|
|
|
$this->sites = $sites; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function write() |
76
|
|
|
{ |
77
|
|
|
file_put_contents($this->path_config, json_encode(array( |
78
|
|
|
"sites" => $this->sites, |
79
|
|
|
))); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* |
85
|
|
|
* @return SiteConfig |
86
|
|
|
*/ |
87
|
|
|
public function getSite($name) |
88
|
|
|
{ |
89
|
|
|
foreach ($this->sites as $s) { |
90
|
|
|
if ($s->name === $name) { |
91
|
|
|
return $s; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getSites() |
98
|
|
|
{ |
99
|
|
|
return $this->sites; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* checks if a site with specific name already exists in this config. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
|
|
public function siteExists($name) |
110
|
|
|
{ |
111
|
|
|
$exists = false; |
112
|
|
|
foreach ($this->sites as $s) { |
113
|
|
|
if ($s->name === $name) { |
114
|
|
|
$exists = true; |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $exists; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Adds the path of a Silverstripe project directory (e.g. mysite) to this config. |
124
|
|
|
* |
125
|
|
|
* @param string $name |
126
|
|
|
* @param string $path |
127
|
|
|
* |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
|
|
public function addSite($name, $path) |
131
|
|
|
{ |
132
|
|
|
array_push($this->sites, new SiteConfig($name, $path)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function removeSite($name) |
136
|
|
|
{ |
137
|
|
|
$size = count($this->sites); |
138
|
|
|
for ($i = 0; $i < $size;$i++) { |
139
|
|
|
$s = $this->sites[$i]; |
140
|
|
|
if ($s->name === $name) { |
141
|
|
|
array_splice($this->sites, $i, 1); |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function setPath($config_path = "",$is_cli = true) { |
148
|
|
|
$dir = dirname($config_path); |
149
|
|
|
if(file_exists($dir)) { |
150
|
|
|
if(!file_exists($config_path) && $is_cli){ |
151
|
|
|
echo "The file $config_path doesn't exist. Do you want to create it? yes/[no]"; |
152
|
|
|
$line = fgets(STDIN); |
153
|
|
|
if(preg_match("/^y|yes/", $line)) { |
154
|
|
|
file_put_contents($config_path, '{"sites":{}}'); |
155
|
|
|
}else{ |
156
|
|
|
Util::reportError("Aborting..."); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
$this->path_config = $config_path; |
160
|
|
|
$this->load(); |
161
|
|
|
}else{ |
162
|
|
|
Util::reportError("The directory \"$dir\" doesnt exist."); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|