1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moodle component manager. |
5
|
|
|
* |
6
|
|
|
* @author Luke Carrier <[email protected]> |
7
|
|
|
* @copyright 2016 Luke Carrier |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ComponentManager; |
12
|
|
|
|
13
|
|
|
use ComponentManager\Exception\MoodleException; |
14
|
|
|
use ComponentManager\Exception\PlatformException; |
15
|
|
|
use ComponentManager\Platform\Platform; |
16
|
|
|
use core_plugin_manager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Moodle installation. |
20
|
|
|
* |
21
|
|
|
* Facilitates interaction with a Moodle installation. This class should be |
22
|
|
|
* used only by the MoodleCommand class. Elsewhere, use the Moodle class |
23
|
|
|
* instead. */ |
24
|
|
|
class MoodleInstallation { |
25
|
|
|
/** |
26
|
|
|
* Configuration file filename. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
const CONFIG_FILENAME = 'config.php'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Instance state: new. |
34
|
|
|
* |
35
|
|
|
* The instance has not yet been configured, and is not ready to use. |
36
|
|
|
* |
37
|
|
|
* @var integer |
38
|
|
|
*/ |
39
|
|
|
const STATE_NEW = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Instance state: ready for use. |
43
|
|
|
* |
44
|
|
|
* The instance has been configured and is ready for use. |
45
|
|
|
* |
46
|
|
|
* @var integer |
47
|
|
|
*/ |
48
|
|
|
const STATE_READY = 1; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Instance state: disposed. |
52
|
|
|
* |
53
|
|
|
* The instance was configured, but has since been disposed. It is no longer |
54
|
|
|
* usable, and a new instance will need to be created to proceed. |
55
|
|
|
* |
56
|
|
|
* @var integer |
57
|
|
|
*/ |
58
|
|
|
const STATE_DISPOSED = 2; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The value of the Moodle $CFG object. |
62
|
|
|
* |
63
|
|
|
* We have to cache the result of the first call to getConfig() as |
64
|
|
|
* constants can be declared within this file, causing nasty warnings. |
65
|
|
|
* |
66
|
|
|
* @var \stdClass |
67
|
|
|
*/ |
68
|
|
|
protected $config; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Platform support library. |
72
|
|
|
* |
73
|
|
|
* @var \ComponentManager\Platform\Platform |
74
|
|
|
*/ |
75
|
|
|
protected $platform; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Root directory of the Moodle installation. |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $rootDirectory; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Instance state. |
86
|
|
|
* |
87
|
|
|
* @var integer |
88
|
|
|
*/ |
89
|
|
|
protected $state; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Initialiser. |
93
|
|
|
* |
94
|
|
|
* @param \ComponentManager\Platform\Platform $platform |
95
|
|
|
* @param string $rootDirectory |
96
|
|
|
*/ |
97
|
|
|
public function __construct(Platform $platform, $rootDirectory) { |
98
|
|
|
$this->state = static::STATE_NEW; |
99
|
|
|
|
100
|
|
|
$this->platform = $platform; |
101
|
|
|
$this->rootDirectory = $rootDirectory; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Assert that the instance's state is as expected. |
106
|
|
|
* |
107
|
|
|
* @param $expected |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
* |
111
|
|
|
* @throws \ComponentManager\Exception\MoodleException |
112
|
|
|
*/ |
113
|
|
|
protected function assertState($expected) { |
114
|
|
|
if ($this->state !== $expected) { |
115
|
|
|
throw new MoodleException( |
116
|
|
|
sprintf('Expected state "%d", actual state was "%d"', $expected, $this->state), |
|
|
|
|
117
|
|
|
MoodleException::CODE_INVALID_STATE); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Require the configuration file. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
* |
126
|
|
|
* @throws \ComponentManager\Exception\MoodleException |
127
|
|
|
*/ |
128
|
|
|
public function configure() { |
129
|
|
|
$this->assertState(static::STATE_NEW); |
130
|
|
|
|
131
|
|
|
/* This value should be overwritten shortly, either by the definition in |
132
|
|
|
* the Moodle instance's configuration or by our own "fake" |
133
|
|
|
* configuration used to load the plugin manager. */ |
134
|
|
|
$CFG = null; |
135
|
|
|
|
136
|
|
|
$constants = [ |
137
|
|
|
'ABORT_AFTER_CONFIG', |
138
|
|
|
'CLI_SCRIPT', |
139
|
|
|
'IGNORE_COMPONENT_CACHE', |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
foreach ($constants as $constant) { |
143
|
|
|
define($constant, true); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$path = $this->platform->joinPaths([ |
147
|
|
|
$this->rootDirectory, |
148
|
|
|
static::CONFIG_FILENAME, |
149
|
|
|
]); |
150
|
|
|
if (is_file($path)) { |
151
|
|
|
require_once $path; |
152
|
|
|
|
153
|
|
|
if (!is_object($CFG)) { |
154
|
|
|
throw new MoodleException( |
155
|
|
|
"The Moodle configuration file \"{$path}\" did not define \$CFG", |
|
|
|
|
156
|
|
|
MoodleException::CODE_NOT_CONFIGURED); |
157
|
|
|
} |
158
|
|
|
} else { |
159
|
|
|
/* We don't have a configured site, so we'll have to fake it. |
160
|
|
|
* Only an extremely slim portion of Moodle will function |
161
|
|
|
* correctly in this state, as we've not actually done most of |
162
|
|
|
* the Moodle setup dance. It seems to be enough to run the |
163
|
|
|
* plugin manager. */ |
164
|
|
|
|
165
|
|
|
global $CFG; |
|
|
|
|
166
|
|
|
$CFG = (object) [ |
167
|
|
|
'dirroot' => $this->rootDirectory, |
168
|
|
|
'dataroot' => $this->platform->createTempDirectory(), |
169
|
|
|
'wwwroot' => 'http://localhost', |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
require_once "{$CFG->dirroot}/lib/setup.php"; |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->config = $CFG; |
176
|
|
|
$this->state = static::STATE_READY; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function dispose() { |
180
|
|
|
$this->assertState(static::STATE_READY); |
181
|
|
|
|
182
|
|
|
try { |
183
|
|
|
$this->platform->removeTempDirectory($this->config->dataroot); |
184
|
|
|
} catch (PlatformException $e) { |
185
|
|
|
if ($e->getCode() !== PlatformException::CODE_UNKNOWN_TEMP_DIRECTORY) { |
|
|
|
|
186
|
|
|
throw $e; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->state = static::STATE_DISPOSED; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get plugin types. |
195
|
|
|
* |
196
|
|
|
* @return string[] |
197
|
|
|
*/ |
198
|
|
|
public function getPluginTypes() { |
199
|
|
|
$this->assertState(static::STATE_READY); |
200
|
|
|
|
201
|
|
|
$CFG = $this->config; |
202
|
|
|
require_once "{$CFG->dirroot}/lib/classes/plugin_manager.php"; |
|
|
|
|
203
|
|
|
|
204
|
|
|
$pluginMgr = core_plugin_manager::instance(); |
205
|
|
|
|
206
|
|
|
return $pluginMgr->get_plugin_types(); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.