@@ -48,63 +48,66 @@ class snippets {
48
48
49
49
/**
50
50
* Base path CSS snippets that are shipped with boost_union.
51
+ *
51
52
* @var string
52
53
*/
53
54
const BUILTIN_SNIPPETS_BASE_PATH = '/theme/boost_union/snippets/builtin/ ' ;
54
55
55
56
/**
56
57
* Gets the snippet file based on the meta information.
57
58
*
58
- * @param mixed $path
59
- * @param mixed $source
59
+ * @param string $path The snippet's path.
60
+ * @param string $source The snippet's source.
60
61
*
61
62
* @return string|null
62
63
*/
63
64
public static function get_snippet_file ($ path , $ source ): string |null {
64
65
global $ CFG ;
66
+
65
67
// Get the snippet file based on the different sources.
66
68
if ('theme_boost_union ' === $ source ) {
67
69
// Builtin CSS SNippets.
68
70
$ file = $ CFG ->dirroot . self ::BUILTIN_SNIPPETS_BASE_PATH . $ path ;
69
71
} else {
70
- // Other snippet sources.
72
+ // Other snippet sources (which are currently not supported) .
71
73
return null ;
72
74
}
73
75
return is_readable ($ file ) ? $ file : null ;
74
76
}
75
77
76
78
/**
77
- * Loads the Snippets SCSS content .
79
+ * Get the SCSS of a snippet based on its path and source .
78
80
*
79
81
* Returns an empty string as a fallback if the snippet is not found.
80
82
*
81
- * @param mixed $path
82
- * @param mixed $source
83
+ * @param string $path The snippet's path.
84
+ * @param string $source The snippet's source.
83
85
*
84
86
* @return string
85
87
*/
86
88
public static function get_snippet_scss ($ path , $ source ): string {
87
89
// Get the snippets file, based on the source.
88
90
$ file = self ::get_snippet_file ($ path , $ source );
89
91
90
- // Return an empty string if the file is not found/ readable.
92
+ // If the file does not exist or is not readable, we simply return an empty string .
91
93
if (is_null ($ file )) {
92
94
return '' ;
93
95
}
94
96
95
- $ scss = file_get_contents ( $ file , false , null , 0 );
97
+ // Get and return the whole file content (which will contain the SCSS comments as well).
98
+ $ scss = file_get_contents ($ file );
96
99
97
100
// Return the SCSS or an empty string if reading the file has failed.
98
101
return $ scss ?: '' ;
99
102
}
100
103
101
104
/**
102
- * Get a snippet defined in the code based on path.
105
+ * Get the meta data of a snippet defined in the snippet code based on its path and source .
103
106
*
104
- * @param string $path
105
- * @param string $source
107
+ * @param string $path The snippet's path.
108
+ * @param string $source The snippet's source.
106
109
*
107
- * @return stdClass|null
110
+ * @return stdClass|null The snippet metadata object, or null if the snippet was not found.
108
111
*/
109
112
public static function get_snippet_meta ($ path , $ source ): stdClass |null {
110
113
// Get the snippets file, based on the source.
@@ -115,21 +118,22 @@ public static function get_snippet_meta($path, $source): stdClass|null {
115
118
return null ;
116
119
}
117
120
118
- // Extract the meta from the SCSS files top level multiline comment in WordPress style.
121
+ // Extract the meta from the SCSS file's top level multiline comment in WordPress style.
119
122
$ headers = self ::get_snippet_meta_from_file ($ file );
120
123
121
124
// The title is the only required meta-key that actually must be set.
125
+ // If it is not present, we can not proceed.
122
126
if (!array_key_exists ('Snippet Title ' , $ headers )) {
123
127
return null ;
124
128
}
125
129
126
- // Create an object containing the information .
130
+ // Create an object containing the metadata .
127
131
$ snippet = new stdClass ();
128
132
$ snippet ->title = $ headers ['Snippet Title ' ];
129
133
$ snippet ->description = $ headers ['Description ' ];
130
134
$ snippet ->scope = $ headers ['Scope ' ];
131
135
$ snippet ->goal = $ headers ['Goal ' ];
132
- $ snippet ->source = ' theme_boost_union ' ;
136
+ $ snippet ->source = $ source ;
133
137
134
138
return $ snippet ;
135
139
}
@@ -139,17 +143,19 @@ public static function get_snippet_meta($path, $source): stdClass|null {
139
143
*
140
144
* This is currently used for create the view for the settings table.
141
145
*
142
- * @param \moodle_recordset $snippetrecordset
146
+ * @param \moodle_recordset $snippetrecordset The recordset of snippets which are present in the database.
143
147
*
144
148
* @return array An array of snippet objects.
145
149
*/
146
150
public static function compose_snippets_data ($ snippetrecordset ): array {
151
+ // Initialize snippets array for returning later.
147
152
$ snippets = [];
148
153
154
+ // Iterate over the snippets which are present in the database.
149
155
foreach ($ snippetrecordset as $ snippetrecord ) {
150
- // Get the meta information from the SCSS files top multiline comment.
156
+ // Get the meta information from the SCSS files' top multiline comment.
151
157
$ snippet = self ::get_snippet_meta ($ snippetrecord ->path , $ snippetrecord ->source );
152
- // If snippets meta is not found, it will no be added to the returned snippet array.
158
+ // Only if snippet metadata is found, it will be added to the returned snippets array.
153
159
if ($ snippet ) {
154
160
// Merge the two objects.
155
161
$ snippets [] = (object ) array_merge ((array ) $ snippetrecord , (array ) $ snippet );
@@ -176,14 +182,19 @@ public static function get_enabled_snippet_scss(): string {
176
182
// Get records.
177
183
$ data = $ DB ->get_recordset_sql ($ sql );
178
184
185
+ // Initialize SCSS code.
179
186
$ scss = '' ;
180
187
188
+ // Iterate over all records.
181
189
foreach ($ data as $ snippet ) {
190
+ // And add the SCSS code to the stack.
182
191
$ scss .= self ::get_snippet_scss ($ snippet ->path , $ snippet ->source );
183
192
}
184
193
194
+ // Close the recordset.
185
195
$ data ->close ();
186
196
197
+ // Return the SCSS code.
187
198
return $ scss ;
188
199
}
189
200
@@ -243,7 +254,7 @@ public static function get_snippet_meta_from_file($file): array {
243
254
}
244
255
245
256
/**
246
- * Retrieve all builtin CSS snippets via the actual scss files.
257
+ * Retrieve all builtin CSS snippetsfrom the actual scss files on disk .
247
258
*
248
259
* @return string[]
249
260
*/
@@ -252,12 +263,22 @@ private static function get_builtin_snippet_paths(): array {
252
263
// Get an array of all .scss files in the directory.
253
264
$ files = glob ($ CFG ->dirroot . self ::BUILTIN_SNIPPETS_BASE_PATH . '*.scss ' );
254
265
255
- // Return an array of the basenames of the files.
256
- return is_array ($ files ) ? array_map (fn ($ file ) => basename ($ file ), $ files ) : [];
266
+ // If there are files.
267
+ if (is_array ($ files )) {
268
+ // Return an array of the basenames of the files.
269
+ $ basenames = array_map (function ($ file ) {
270
+ return basename ($ file );
271
+ }, $ files );
272
+ return $ basenames ;
273
+
274
+ // Otherwise.
275
+ } else {
276
+ return [];
277
+ }
257
278
}
258
279
259
280
/**
260
- * Make sure builtin snippets are in the database.
281
+ * Make sure that the builtin snippets are in the database.
261
282
*
262
283
* @return void
263
284
*/
@@ -269,14 +290,14 @@ public static function add_builtin_snippets(): void {
269
290
270
291
// Get builtin snippets which are known in the database.
271
292
$ snippets = $ DB ->get_records (
272
- 'theme_boost_union_snippets ' ,
273
- ['source ' => 'theme_boost_union ' ],
274
- 'sortorder DESC ' ,
275
- 'id,path,sortorder '
293
+ 'theme_boost_union_snippets ' ,
294
+ ['source ' => 'theme_boost_union ' ],
295
+ 'sortorder DESC ' ,
296
+ 'id,path,sortorder '
276
297
);
277
298
278
299
// Get the highest sortorder present.
279
- $ sortorder = empty ($ snippets ) ? 0 : intval (reset ($ snippets )->sortorder ) + 1 ;
300
+ $ sortorder = empty ($ snippets ) ? 0 : intval (reset ($ snippets )->sortorder );
280
301
281
302
// Prepare an array with all the present builtin snippet paths.
282
303
$ presentpaths = array_map (function ($ snippet ) {
@@ -285,21 +306,28 @@ public static function add_builtin_snippets(): void {
285
306
286
307
$ transaction = $ DB ->start_delegated_transaction ();
287
308
309
+ // Iterate over the builtin snippets that are present on disk.
288
310
foreach ($ paths as $ path ) {
311
+ // If the snippet is not in the database yet.
289
312
if (!in_array ($ path , $ presentpaths )) {
313
+ // Add it to the database (raising the sort order).
290
314
$ DB ->insert_record (
291
315
'theme_boost_union_snippets ' ,
292
316
[
293
317
'path ' => $ path ,
294
318
'source ' => 'theme_boost_union ' ,
295
- 'sortorder ' => $ sortorder ,
319
+ 'sortorder ' => $ sortorder + 1 ,
296
320
]
297
321
);
298
- // We add each record with incrementing sortorder.
299
- $ sortorder += 1 ;
300
322
}
301
323
}
302
324
325
+ // Note: snippets which exist in the database and do not exist on disk won't be removed from the database in
326
+ // in this process.
327
+ // They will stay there until the theme is removed. This is to make sure that snippet settings do not get lost
328
+ // even if they appear from disk temporarily.
329
+ // But such snippets will be ignored later when the snippet table is processed and when the SCSS is composed.
330
+
303
331
$ transaction ->allow_commit ();
304
332
}
305
333
}
0 commit comments