forked from SuiteCRM/SuiteCRM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionManager.php
More file actions
249 lines (213 loc) · 6.77 KB
/
Copy pathExtensionManager.php
File metadata and controls
249 lines (213 loc) · 6.77 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace SuiteCRM\ModuleInstall;
use \LoggerManager;
/**
* Class ExtensionManager
*/
class ExtensionManager
{
/**
* @var array
*/
protected static $moduleList = [];
/**
* @var
*/
protected static $logger;
/**
* Checks if authenticated and dies if not.
*
* @return void
*/
protected static function handleAuth()
{
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
}
/**
* Sets the static module and extension lists.
*
* @return void
*/
protected static function initialise()
{
static::$moduleList = get_module_dir_list();
static::$logger = LoggerManager::getLogger();
}
/**
* Compiles source files for given extension to targeted file applying any given filters.
*
* @param string $extension Name of Extension i.e. 'Language'
* @param string $targetFileName Name of target file
* @param string $filter To filter file names such as language prefixes
* @param bool $applicationOnly Whether or not to only compile application extensions
* @return void
*/
public static function compileExtensionFiles(
$extension,
$targetFileName,
$filter = '',
$applicationOnly = false
) {
static::handleAuth();
static::initialise();
if ($extension === 'Language' && strpos($targetFileName, $filter) !== 0) {
$targetFileName = $filter . $targetFileName;
}
if (!$applicationOnly) {
static::compileModuleExtensions($extension, $targetFileName, $filter);
}
static::compileApplicationExtensions($extension, $targetFileName, $filter);
}
/**
* @param $extension
* @param $targetFileName
* @param string $filter
* @return void
*/
protected static function compileApplicationExtensions(
$extension,
$targetFileName,
$filter = ''
) {
static::$logger->{'debug'}("Merging application files for $targetFileName in $extension");
$extensionContents = '<?php' . PHP_EOL . '// WARNING: The contents of this file are auto-generated' . PHP_EOL;
$extPath = "application/Ext/$extension";
$moduleInstall = "custom/Extension/$extPath";
$shouldSave = false;
if (is_dir($moduleInstall)) {
$dir = dir($moduleInstall);
while ($entry = $dir->read()) {
if (static::shouldSkipFile($entry, $moduleInstall, $filter)) {
continue;
}
$shouldSave = true;
$file = file_get_contents("$moduleInstall/$entry");
$extensionContents .= PHP_EOL . static::removePhpTagsFromString($file);
}
}
if ($shouldSave) {
static::saveFile($extPath, $targetFileName, $extensionContents);
return;
}
static::unlinkFile($extPath, $targetFileName);
}
/**
* @param $extension
* @param $targetFileName
* @param string $filter
* @return void
*/
protected static function compileModuleExtensions(
$extension,
$targetFileName,
$filter = ''
) {
static::$logger->{'debug'}(
self::class . "::compileExtensionFiles() : Merging module files in " .
"custom/Extension/modules/<module>/$extension to custom/modules/<module>/$extension/$targetFileName"
);
foreach (static::$moduleList as $module) {
$extensionContents = '<?php'
. PHP_EOL
. '// WARNING: The contents of this file are auto-generated'
. PHP_EOL;
$extPath = "modules/$module/Ext/$extension";
$moduleInstall = "custom/Extension/$extPath";
$shouldSave = false;
if (is_dir($moduleInstall)) {
$dir = dir($moduleInstall);
$shouldSave = true;
$override = [];
while ($entry = $dir->read()) {
if (static::shouldSkipFile($entry, $moduleInstall, $filter)) {
continue;
}
if (strpos($entry, '_override') === 0) {
$override[] = $entry;
continue;
}
$file = file_get_contents("$moduleInstall/$entry");
static::$logger->{'debug'}(
self::class . "->compileExtensionFiles(): found {$moduleInstall}{$entry}"
);
$extensionContents .= PHP_EOL . static::removePhpTagsFromString($file);
}
foreach ($override as $entry) {
$file = file_get_contents("$moduleInstall/$entry");
$extensionContents .= PHP_EOL . static::removePhpTagsFromString($file);
}
}
if ($shouldSave) {
static::saveFile($extPath, $targetFileName, $extensionContents);
continue;
}
static::unlinkFile($extPath, $targetFileName);
}
}
/**
* @param string $string
* @return string
*/
protected static function removePhpTagsFromString($string)
{
return str_replace(
['<?php', '?>', '<?PHP', '<?'],
'',
$string
);
}
/**
* @param $entry
* @param $moduleInstall
* @param $filter
* @return bool
*/
protected static function shouldSkipFile(
$entry,
$moduleInstall,
$filter
) {
if ($entry === '.' || $entry === '..' || strtolower(substr($entry, -4)) !== '.php') {
return true;
}
if (!is_file("$moduleInstall/$entry")) {
return true;
}
if (!empty($filter) && substr_count($entry, $filter) <= 0) {
return true;
}
return false;
}
/**
* @param string $extPath
* @param string $targetFileName
* @param string $extensionContents
* @return void
*/
protected static function saveFile(
$extPath,
$targetFileName,
$extensionContents
) {
if (!file_exists("custom/$extPath")) {
mkdir_recursive("custom/$extPath", true);
}
$extensionContents .= PHP_EOL;
$out = sugar_fopen("custom/$extPath/$targetFileName", 'wb');
fwrite($out, $extensionContents);
fclose($out);
}
/**
* @param $extPath
* @param $targetFileName
* @return void
*/
protected static function unlinkFile($extPath, $targetFileName)
{
if (file_exists("custom/$extPath/$targetFileName")) {
unlink("custom/$extPath/$targetFileName");
}
}
}