-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMoodle.php
153 lines (129 loc) · 4.1 KB
/
Moodle.php
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
<?php
/*
* This file is part of the Moodle Plugin CI package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Copyright (c) 2018 Blackboard Inc. (http://www.blackboard.com)
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace MoodlePluginCI\Bridge;
use MoodlePluginCI\Parser\CodeParser;
use MoodlePluginCI\Parser\StatementFilter;
use PhpParser\Node\Scalar\String_;
/**
* Bridge to Moodle.
*/
class Moodle
{
/**
* Absolute path to Moodle directory.
*/
public string $directory;
/**
* Moodle config.
*/
protected ?object $cfg;
/**
* @param string $directory Absolute path to Moodle directory
*/
public function __construct(string $directory)
{
$this->directory = $directory;
}
/**
* Load Moodle config so we can use Moodle APIs.
*/
public function requireConfig(): void
{
global $CFG;
if (!defined('CLI_SCRIPT')) {
define('CLI_SCRIPT', true);
}
if (!defined('IGNORE_COMPONENT_CACHE')) {
define('IGNORE_COMPONENT_CACHE', true);
}
if (!defined('CACHE_DISABLE_ALL')) {
define('CACHE_DISABLE_ALL', true);
}
if (!defined('ABORT_AFTER_CONFIG')) {
// Need this since Moodle will not be fully installed.
define('ABORT_AFTER_CONFIG', true);
}
$path = $this->directory . '/config.php';
if (!is_file($path)) {
throw new \RuntimeException('Failed to find Moodle config file');
}
/** @noinspection PhpIncludeInspection */
require_once $path;
// Save a local reference to Moodle config.
if (empty($this->cfg)) {
$this->cfg = $CFG;
}
}
/**
* Normalize the component into the type and plugin name.
*
* @param string $component
*
* @return array
*/
public function normalizeComponent(string $component): array
{
$this->requireConfig();
/* @noinspection PhpUndefinedClassInspection */
return \core_component::normalize_component($component);
}
/**
* Get the absolute install directory path within Moodle.
*
* @param string $component Moodle component, EG: mod_forum
*
* @return string Absolute path, EG: /path/to/mod/forum
*/
public function getComponentInstallDirectory(string $component): string
{
list($type, $name) = $this->normalizeComponent($component);
// Must use reflection to avoid using static cache.
/* @noinspection PhpUndefinedClassInspection */
$method = new \ReflectionMethod(\core_component::class, 'fetch_plugintypes');
$method->setAccessible(true);
$result = $method->invoke(null);
if (!array_key_exists($type, $result[0])) {
throw new \InvalidArgumentException(sprintf('The component %s has an unknown plugin type of %s', $component, $type));
}
return $result[0][$type] . '/' . $name;
}
/**
* Get the branch number, EG: 29, 30, etc.
*
* @return int
*/
public function getBranch(): int
{
$filter = new StatementFilter();
$parser = new CodeParser();
$statements = $parser->parseFile($this->directory . '/version.php');
$assign = $filter->findFirstVariableAssignment($statements, 'branch', 'Failed to find $branch in Moodle version.php');
if ($assign->expr instanceof String_) {
return (int) $assign->expr->value;
}
throw new \RuntimeException('Failed to find Moodle branch version');
}
/**
* Get a Moodle config value.
*
* @param string $name the config name
*
* @return string
*/
public function getConfig(string $name): string
{
$this->requireConfig();
if (null === $this->cfg || !property_exists($this->cfg, $name)) {
throw new \RuntimeException(sprintf('Failed to find $CFG->%s in Moodle config file', $name));
}
return $this->cfg->$name;
}
}