24
24
25
25
namespace theme_boost_union ;
26
26
27
+ use stdClass ;
28
+
27
29
/**
28
- * Class snippets
30
+ * Library class containing solely static functions for dealing with SCSS snippets.
29
31
*
30
32
* @package theme_boost_union
31
33
* @copyright 2024 André Menrath, University of Graz <[email protected] >
32
34
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
35
*/
34
36
class snippets {
35
- /**
36
- * Constant for how many a Kilobyte are in bytes.
37
- *
38
- * @var int
39
- */
40
- const KB_IN_BYTES = 1024 ;
41
-
42
37
/**
43
38
* List of all available Snippet Meta File headers.
44
39
*
@@ -65,7 +60,7 @@ class snippets {
65
60
*
66
61
* @return string|null
67
62
*/
68
- public static function get_snippet_file ($ path , $ source ) {
63
+ public static function get_snippet_file ($ path , $ source ): string | null {
69
64
global $ CFG ;
70
65
// Get the snippet file based on the different sources.
71
66
if ('theme_boost_union ' === $ source ) {
@@ -81,21 +76,26 @@ public static function get_snippet_file($path, $source) {
81
76
/**
82
77
* Loads the Snippets SCSS content.
83
78
*
79
+ * Returns an empty string as a fallback if the snippet is not found.
80
+ *
84
81
* @param mixed $path
85
82
* @param mixed $source
86
83
*
87
- * @return boolean| string
84
+ * @return string
88
85
*/
89
- public static function get_snippet_scss ($ path , $ source ) {
86
+ public static function get_snippet_scss ($ path , $ source ): string {
90
87
// Get the snippets file, based on the source.
91
88
$ file = self ::get_snippet_file ($ path , $ source );
92
89
90
+ // Return an empty string if the file is not found/readable.
93
91
if (is_null ($ file )) {
94
92
return '' ;
95
93
}
96
94
97
95
$ scss = file_get_contents ( $ file , false , null , 0 );
98
- return $ scss ;
96
+
97
+ // Return the SCSS or an empty string if reading the file has failed.
98
+ return $ scss ?: '' ;
99
99
}
100
100
101
101
/**
@@ -104,9 +104,9 @@ public static function get_snippet_scss($path, $source) {
104
104
* @param string $path
105
105
* @param string $source
106
106
*
107
- * @return mixed
107
+ * @return stdClass|null
108
108
*/
109
- public static function get_snippet_meta ($ path , $ source ) {
109
+ public static function get_snippet_meta ($ path , $ source ): stdClass | null {
110
110
// Get the snippets file, based on the source.
111
111
$ file = self ::get_snippet_file ($ path , $ source );
112
112
@@ -124,7 +124,7 @@ public static function get_snippet_meta($path, $source) {
124
124
}
125
125
126
126
// Create an object containing the information.
127
- $ snippet = new \ stdClass ();
127
+ $ snippet = new stdClass ();
128
128
$ snippet ->title = $ headers ['Snippet Title ' ];
129
129
$ snippet ->description = $ headers ['Description ' ];
130
130
$ snippet ->scope = $ headers ['Scope ' ];
@@ -141,9 +141,9 @@ public static function get_snippet_meta($path, $source) {
141
141
*
142
142
* @param \moodle_recordset $snippetrecordset
143
143
*
144
- * @return array
144
+ * @return array An array of snippet objects.
145
145
*/
146
- public static function compose_snippets_data ($ snippetrecordset ) {
146
+ public static function compose_snippets_data ($ snippetrecordset ): array {
147
147
$ snippets = [];
148
148
149
149
foreach ($ snippetrecordset as $ snippetrecord ) {
@@ -164,7 +164,7 @@ public static function compose_snippets_data($snippetrecordset) {
164
164
*
165
165
* @return string
166
166
*/
167
- public static function get_enabled_snippet_scss () {
167
+ public static function get_enabled_snippet_scss (): string {
168
168
global $ DB ;
169
169
170
170
// Compose SQL base query.
@@ -190,6 +190,8 @@ public static function get_enabled_snippet_scss() {
190
190
/**
191
191
* Strips close comment and close php tags from file headers.
192
192
*
193
+ * @copyright WordPress https://developer.wordpress.org/reference/functions/_cleanup_header_comment/
194
+ *
193
195
* @param string $str Header comment to clean up.
194
196
*
195
197
* @return string
@@ -208,14 +210,15 @@ private static function cleanup_header_comment($str) {
208
210
* If the file data is not within that first 8 KB, then the author should correct
209
211
* the snippet.
210
212
*
213
+ * @copyright forked from https://developer.wordpress.org/reference/functions/get_file_data/
214
+ *
211
215
* @param string $file Absolute path to the file.
212
216
*
213
- * @copyright forked from https://developer.wordpress.org/reference/functions/get_file_data/
214
217
* @return string[] Array of file header values keyed by header name.
215
218
*/
216
- public static function get_snippet_meta_from_file ($ file ) {
219
+ public static function get_snippet_meta_from_file ($ file ): array {
217
220
// Pull only the first 8 KB of the file in.
218
- $ filedata = file_get_contents ( $ file , false , null , 0 , 8 * self :: KB_IN_BYTES );
221
+ $ filedata = file_get_contents ( $ file , false , null , 0 , 8192 );
219
222
220
223
if ( false === $ filedata ) {
221
224
$ filedata = '' ;
@@ -226,6 +229,7 @@ public static function get_snippet_meta_from_file($file) {
226
229
227
230
$ headers = [];
228
231
232
+ // Scan for each snippet header meta information in the files top scss comment.
229
233
foreach (self ::SNIPPET_HEADERS as $ regex ) {
230
234
if (preg_match ('/^(?:[ \t]*)?[ \t\/*#@]* ' . preg_quote ($ regex , '/ ' ) . ':(.*)$/mi ' , $ filedata , $ match )
231
235
&& $ match [1 ]) {
@@ -243,23 +247,21 @@ public static function get_snippet_meta_from_file($file) {
243
247
*
244
248
* @return string[]
245
249
*/
246
- private static function get_builtin_snippet_paths () {
250
+ private static function get_builtin_snippet_paths (): array {
247
251
global $ CFG ;
248
252
// Get an array of all .scss files in the directory.
249
253
$ files = glob ($ CFG ->dirroot . self ::BUILTIN_SNIPPETS_BASE_PATH . '*.scss ' );
250
254
251
- // Get the basenames.
252
- $ basenames = array_map (fn ($ file ) => basename ($ file ), $ files );
253
-
254
- return $ basenames ;
255
+ // Return an array of the basenames of the files.
256
+ return is_array ($ files ) ? array_map (fn ($ file ) => basename ($ file ), $ files ) : [];
255
257
}
256
258
257
259
/**
258
260
* Make sure builtin snippets are in the database.
259
261
*
260
262
* @return void
261
263
*/
262
- public static function add_builtin_snippets () {
264
+ public static function add_builtin_snippets (): void {
263
265
global $ DB ;
264
266
265
267
// Get builtin snippets that are present on disk.
@@ -281,6 +283,8 @@ public static function add_builtin_snippets() {
281
283
return $ snippet ->path ;
282
284
}, $ snippets );
283
285
286
+ $ transaction = $ DB ->start_delegated_transaction ();
287
+
284
288
foreach ($ paths as $ path ) {
285
289
if (!in_array ($ path , $ presentpaths )) {
286
290
$ DB ->insert_record (
@@ -295,5 +299,7 @@ public static function add_builtin_snippets() {
295
299
$ sortorder += 1 ;
296
300
}
297
301
}
302
+
303
+ $ transaction ->allow_commit ();
298
304
}
299
305
}
0 commit comments