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
|
|
|
use stdClass; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Moodle installation. |
21
|
|
|
* |
22
|
|
|
* Facilitates interaction with a Moodle installation. This class should be |
23
|
|
|
* used only by the MoodleCommand class. Elsewhere, use the Moodle class |
24
|
|
|
* instead. */ |
25
|
|
|
class MoodleInstallation { |
26
|
|
|
/** |
27
|
|
|
* Instance state: new. |
28
|
|
|
* |
29
|
|
|
* The instance has not yet been configured, and is not ready to use. |
30
|
|
|
* |
31
|
|
|
* @var integer |
32
|
|
|
*/ |
33
|
|
|
const STATE_NEW = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Instance state: ready for use. |
37
|
|
|
* |
38
|
|
|
* The instance has been configured and is ready for use. |
39
|
|
|
* |
40
|
|
|
* @var integer |
41
|
|
|
*/ |
42
|
|
|
const STATE_READY = 1; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Instance state: disposed. |
46
|
|
|
* |
47
|
|
|
* The instance was configured, but has since been disposed. It is no longer |
48
|
|
|
* usable, and a new instance will need to be created to proceed. |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
const STATE_DISPOSED = 2; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The value of the Moodle $CFG object. |
56
|
|
|
* |
57
|
|
|
* We have to cache the result of the first call to getConfig() as |
58
|
|
|
* constants can be declared within this file, causing nasty warnings. |
59
|
|
|
* |
60
|
|
|
* @var stdClass |
61
|
|
|
*/ |
62
|
|
|
protected $config; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Platform support library. |
66
|
|
|
* |
67
|
|
|
* @var Platform |
68
|
|
|
*/ |
69
|
|
|
protected $platform; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Root directory of the Moodle installation. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $rootDirectory; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Instance state. |
80
|
|
|
* |
81
|
|
|
* @var integer |
82
|
|
|
*/ |
83
|
|
|
protected $state; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialiser. |
87
|
|
|
* |
88
|
|
|
* @param Platform $platform |
89
|
|
|
* @param string $rootDirectory |
90
|
|
|
*/ |
91
|
|
|
public function __construct(Platform $platform, $rootDirectory) { |
92
|
|
|
$this->state = static::STATE_NEW; |
93
|
|
|
|
94
|
|
|
$this->platform = $platform; |
95
|
|
|
$this->rootDirectory = $rootDirectory; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Assert that the instance's state is as expected. |
100
|
|
|
* |
101
|
|
|
* @param $expected |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
* |
105
|
|
|
* @throws MoodleException |
106
|
|
|
*/ |
107
|
|
|
protected function assertState($expected) { |
108
|
|
|
if ($this->state !== $expected) { |
109
|
|
|
throw new MoodleException( |
110
|
|
|
sprintf('Expected state "%d", actual state was "%d"', $expected, $this->state), |
111
|
|
|
MoodleException::CODE_INVALID_STATE); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Require the configuration file. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* |
120
|
|
|
* @throws MoodleException |
121
|
|
|
*/ |
122
|
|
|
public function configure() { |
123
|
|
|
$this->assertState(static::STATE_NEW); |
124
|
|
|
|
125
|
|
|
/* This value should be overwritten shortly, either by the definition in |
126
|
|
|
* the Moodle instance's configuration or by our own "fake" |
127
|
|
|
* configuration used to load the plugin manager. */ |
128
|
|
|
$CFG = null; |
129
|
|
|
|
130
|
|
|
$constants = [ |
131
|
|
|
'ABORT_AFTER_CONFIG', |
132
|
|
|
'CLI_SCRIPT', |
133
|
|
|
'IGNORE_COMPONENT_CACHE', |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
foreach ($constants as $constant) { |
137
|
|
|
define($constant, true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/* Only an extremely slim portion of Moodle will function correctly in |
141
|
|
|
* this state, as we've not actually done most of the Moodle setup |
142
|
|
|
* dance. It seems to be enough to run the plugin manager. */ |
143
|
|
|
|
144
|
|
|
global $CFG; |
|
|
|
|
145
|
|
|
$CFG = (object) [ |
146
|
|
|
'dirroot' => $this->rootDirectory, |
147
|
|
|
'dataroot' => $this->platform->createTempDirectory(), |
148
|
|
|
'wwwroot' => 'http://localhost', |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
require_once "{$CFG->dirroot}/lib/setup.php"; |
|
|
|
|
152
|
|
|
|
153
|
|
|
$this->config = $CFG; |
154
|
|
|
$this->state = static::STATE_READY; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function dispose() { |
158
|
|
|
$this->assertState(static::STATE_READY); |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
$this->platform->removeTempDirectory($this->config->dataroot); |
162
|
|
|
} catch (PlatformException $e) { |
163
|
|
|
if ($e->getCode() !== PlatformException::CODE_UNKNOWN_TEMP_DIRECTORY) { |
164
|
|
|
throw $e; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->state = static::STATE_DISPOSED; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get plugin types. |
173
|
|
|
* |
174
|
|
|
* @return string[] |
175
|
|
|
*/ |
176
|
|
|
public function getPluginTypes() { |
177
|
|
|
$this->assertState(static::STATE_READY); |
178
|
|
|
|
179
|
|
|
$CFG = $this->config; |
180
|
|
|
require_once "{$CFG->dirroot}/lib/classes/plugin_manager.php"; |
|
|
|
|
181
|
|
|
|
182
|
|
|
$pluginMgr = core_plugin_manager::instance(); |
183
|
|
|
|
184
|
|
|
return $pluginMgr->get_plugin_types(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state