Skip to content

Commit f28a28d

Browse files
committed
Review changes
1 parent 125838b commit f28a28d

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

classes/snippets.php

+58-30
Original file line numberDiff line numberDiff line change
@@ -48,63 +48,66 @@ class snippets {
4848

4949
/**
5050
* Base path CSS snippets that are shipped with boost_union.
51+
*
5152
* @var string
5253
*/
5354
const BUILTIN_SNIPPETS_BASE_PATH = '/theme/boost_union/snippets/builtin/';
5455

5556
/**
5657
* Gets the snippet file based on the meta information.
5758
*
58-
* @param mixed $path
59-
* @param mixed $source
59+
* @param string $path The snippet's path.
60+
* @param string $source The snippet's source.
6061
*
6162
* @return string|null
6263
*/
6364
public static function get_snippet_file($path, $source): string|null {
6465
global $CFG;
66+
6567
// Get the snippet file based on the different sources.
6668
if ('theme_boost_union' === $source) {
6769
// Builtin CSS SNippets.
6870
$file = $CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . $path;
6971
} else {
70-
// Other snippet sources.
72+
// Other snippet sources (which are currently not supported).
7173
return null;
7274
}
7375
return is_readable($file) ? $file : null;
7476
}
7577

7678
/**
77-
* Loads the Snippets SCSS content.
79+
* Get the SCSS of a snippet based on its path and source.
7880
*
7981
* Returns an empty string as a fallback if the snippet is not found.
8082
*
81-
* @param mixed $path
82-
* @param mixed $source
83+
* @param string $path The snippet's path.
84+
* @param string $source The snippet's source.
8385
*
8486
* @return string
8587
*/
8688
public static function get_snippet_scss($path, $source): string {
8789
// Get the snippets file, based on the source.
8890
$file = self::get_snippet_file($path, $source);
8991

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.
9193
if (is_null($file)) {
9294
return '';
9395
}
9496

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);
9699

97100
// Return the SCSS or an empty string if reading the file has failed.
98101
return $scss ?: '';
99102
}
100103

101104
/**
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.
103106
*
104-
* @param string $path
105-
* @param string $source
107+
* @param string $path The snippet's path.
108+
* @param string $source The snippet's source.
106109
*
107-
* @return stdClass|null
110+
* @return stdClass|null The snippet metadata object, or null if the snippet was not found.
108111
*/
109112
public static function get_snippet_meta($path, $source): stdClass|null {
110113
// Get the snippets file, based on the source.
@@ -115,21 +118,22 @@ public static function get_snippet_meta($path, $source): stdClass|null {
115118
return null;
116119
}
117120

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.
119122
$headers = self::get_snippet_meta_from_file($file);
120123

121124
// The title is the only required meta-key that actually must be set.
125+
// If it is not present, we can not proceed.
122126
if (!array_key_exists('Snippet Title', $headers)) {
123127
return null;
124128
}
125129

126-
// Create an object containing the information.
130+
// Create an object containing the metadata.
127131
$snippet = new stdClass();
128132
$snippet->title = $headers['Snippet Title'];
129133
$snippet->description = $headers['Description'];
130134
$snippet->scope = $headers['Scope'];
131135
$snippet->goal = $headers['Goal'];
132-
$snippet->source = 'theme_boost_union';
136+
$snippet->source = $source;
133137

134138
return $snippet;
135139
}
@@ -139,17 +143,19 @@ public static function get_snippet_meta($path, $source): stdClass|null {
139143
*
140144
* This is currently used for create the view for the settings table.
141145
*
142-
* @param \moodle_recordset $snippetrecordset
146+
* @param \moodle_recordset $snippetrecordset The recordset of snippets which are present in the database.
143147
*
144148
* @return array An array of snippet objects.
145149
*/
146150
public static function compose_snippets_data($snippetrecordset): array {
151+
// Initialize snippets array for returning later.
147152
$snippets = [];
148153

154+
// Iterate over the snippets which are present in the database.
149155
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.
151157
$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.
153159
if ($snippet) {
154160
// Merge the two objects.
155161
$snippets[] = (object) array_merge((array) $snippetrecord, (array) $snippet);
@@ -176,14 +182,19 @@ public static function get_enabled_snippet_scss(): string {
176182
// Get records.
177183
$data = $DB->get_recordset_sql($sql);
178184

185+
// Initialize SCSS code.
179186
$scss = '';
180187

188+
// Iterate over all records.
181189
foreach ($data as $snippet) {
190+
// And add the SCSS code to the stack.
182191
$scss .= self::get_snippet_scss($snippet->path, $snippet->source);
183192
}
184193

194+
// Close the recordset.
185195
$data->close();
186196

197+
// Return the SCSS code.
187198
return $scss;
188199
}
189200

@@ -243,7 +254,7 @@ public static function get_snippet_meta_from_file($file): array {
243254
}
244255

245256
/**
246-
* Retrieve all builtin CSS snippets via the actual scss files.
257+
* Retrieve all builtin CSS snippetsfrom the actual scss files on disk.
247258
*
248259
* @return string[]
249260
*/
@@ -252,12 +263,22 @@ private static function get_builtin_snippet_paths(): array {
252263
// Get an array of all .scss files in the directory.
253264
$files = glob($CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . '*.scss');
254265

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+
}
257278
}
258279

259280
/**
260-
* Make sure builtin snippets are in the database.
281+
* Make sure that the builtin snippets are in the database.
261282
*
262283
* @return void
263284
*/
@@ -269,14 +290,14 @@ public static function add_builtin_snippets(): void {
269290

270291
// Get builtin snippets which are known in the database.
271292
$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'
276297
);
277298

278299
// Get the highest sortorder present.
279-
$sortorder = empty($snippets) ? 0 : intval(reset($snippets)->sortorder) + 1;
300+
$sortorder = empty($snippets) ? 0 : intval(reset($snippets)->sortorder);
280301

281302
// Prepare an array with all the present builtin snippet paths.
282303
$presentpaths = array_map(function($snippet) {
@@ -285,21 +306,28 @@ public static function add_builtin_snippets(): void {
285306

286307
$transaction = $DB->start_delegated_transaction();
287308

309+
// Iterate over the builtin snippets that are present on disk.
288310
foreach ($paths as $path) {
311+
// If the snippet is not in the database yet.
289312
if (!in_array($path, $presentpaths)) {
313+
// Add it to the database (raising the sort order).
290314
$DB->insert_record(
291315
'theme_boost_union_snippets',
292316
[
293317
'path' => $path,
294318
'source' => 'theme_boost_union',
295-
'sortorder' => $sortorder,
319+
'sortorder' => $sortorder + 1,
296320
]
297321
);
298-
// We add each record with incrementing sortorder.
299-
$sortorder += 1;
300322
}
301323
}
302324

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+
303331
$transaction->allow_commit();
304332
}
305333
}

tests/snippets_test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use theme_boost_union\snippets;
2121

2222
/**
23-
* Tests for Boost Union SCSS Snippets.
23+
* Theme Boost Union - Tests for SCSS Snippets.
2424
*
2525
* @package theme_boost_union
2626
* @category test

0 commit comments

Comments
 (0)