@@ -41,41 +41,48 @@ class snippets {
41
41
const SNIPPET_HEADERS = [
42
42
'Snippet Title ' ,
43
43
'Goal ' ,
44
- 'Domain ' ,
45
44
'Description ' ,
46
45
'Scope ' ,
47
46
];
48
47
48
+ /**
49
+ * Base path CSS snippets that are shipped with boost_union.
50
+ * @var string
51
+ */
52
+ const BUILTIN_SNIPPETS_BASE_PATH = '/theme/boost_union/snippets/builtin/ ' ;
53
+
49
54
/**
50
55
* Gets the snippet file based on the meta information.
51
- * @param mixed $key
56
+ *
57
+ * @param mixed $path
52
58
* @param mixed $source
53
- * @param mixed $domain
59
+ *
54
60
* @return string|null
55
61
*/
56
- public static function get_snippet_file ($ key , $ source, $ domain ) {
62
+ public static function get_snippet_file ($ path , $ source ) {
57
63
global $ CFG ;
64
+ // Get the snippet file based on the different sources.
58
65
if ('theme_boost_union ' === $ source ) {
59
- $ file = $ CFG ->dirroot . sprintf ('/theme/boost_union/snippets/builtin/%s.scss ' , $ key );
66
+ // Builtin CSS SNippets.
67
+ $ file = $ CFG ->dirroot . self ::BUILTIN_SNIPPETS_BASE_PATH . $ path ;
60
68
} else {
61
- // TODO: other sources.
69
+ // Other snippet sources.
62
70
return null ;
63
71
}
64
-
65
72
return is_readable ($ file ) ? $ file : null ;
66
73
}
67
74
68
75
/**
69
76
* Loads the Snippets SCSS content.
70
77
*
71
- * @param mixed $key
78
+ * @param mixed $path
72
79
* @param mixed $source
73
- * @param mixed $domain
80
+ *
74
81
* @return boolean|string
75
82
*/
76
- public static function get_snippet_scss ($ key , $ source, $ domain = '' ) {
77
- // Get the snippets file, based on the source and domain .
78
- $ file = self ::get_snippet_file ($ key , $ source, $ domain );
83
+ public static function get_snippet_scss ($ path , $ source ) {
84
+ // Get the snippets file, based on the source.
85
+ $ file = self ::get_snippet_file ($ path , $ source );
79
86
80
87
if (is_null ($ file )) {
81
88
return null ;
@@ -86,16 +93,17 @@ public static function get_snippet_scss($key, $source, $domain = '') {
86
93
}
87
94
88
95
/**
89
- * Get a snippet defined in the code based on key and domain.
90
- * @param string $key
91
- * @param string $domain
96
+ * Get a snippet defined in the code based on path.
97
+ *
98
+ * @param string $path
99
+ *
92
100
* @return mixed
93
101
*/
94
- public static function get_snippet_meta ($ key , $ source, $ domain = '' ) {
102
+ public static function get_snippet_meta ($ path , $ source ) {
95
103
global $ CFG ;
96
104
97
- // Get the snippets file, based on the source and domain .
98
- $ file = self ::get_snippet_file ($ key , $ source, $ domain );
105
+ // Get the snippets file, based on the source.
106
+ $ file = self ::get_snippet_file ($ path , $ source );
99
107
100
108
if (is_null ($ file )) {
101
109
return ;
@@ -119,14 +127,16 @@ public static function get_snippet_meta($key, $source, $domain = '') {
119
127
120
128
/**
121
129
* Compose snippets file data to record.
130
+ *
122
131
* @param mixed $data
132
+ *
123
133
* @return array
124
134
*/
125
135
public static function compose_snippets_data ($ snippetrecordset ) {
126
136
$ snippets = [];
127
137
128
138
foreach ($ snippetrecordset as $ snippetrecord ) {
129
- $ snippet = self ::get_snippet_meta ($ snippetrecord ->key , $ snippetrecord ->source );
139
+ $ snippet = self ::get_snippet_meta ($ snippetrecord ->path , $ snippetrecord ->source );
130
140
if ($ snippet ) {
131
141
$ snippets [] = (object ) array_merge ((array ) $ snippetrecord , (array ) $ snippet );
132
142
}
@@ -137,6 +147,7 @@ public static function compose_snippets_data($snippetrecordset) {
137
147
138
148
/**
139
149
* Checks which snippets are active and returns their css.
150
+ *
140
151
* @return string
141
152
*/
142
153
public static function get_enabled_snippet_css () {
@@ -151,21 +162,22 @@ public static function get_enabled_snippet_css() {
151
162
// Get records.
152
163
$ data = $ DB ->get_recordset_sql ($ sql );
153
164
154
- $ css = '' ;
165
+ $ scss = '' ;
155
166
156
167
foreach ($ data as $ snippet ) {
157
- if ($ snippet ->enabled ) {
158
- $ css .= self ::get_snippet_scss ($ snippet ->key , $ snippet ->domain )['css ' ] . ' ' ;
159
- }
168
+ $ scss .= self ::get_snippet_scss ($ snippet ->path , $ snippet ->source );
160
169
}
161
170
162
- return $ css ;
171
+ $ data ->close ();
172
+
173
+ return $ scss ;
163
174
}
164
175
165
176
/**
166
177
* Strips close comment and close php tags from file headers.
167
178
*
168
179
* @param string $str Header comment to clean up.
180
+ *
169
181
* @return string
170
182
*/
171
183
private static function cleanup_header_comment ($ str ) {
@@ -186,7 +198,7 @@ private static function cleanup_header_comment($str) {
186
198
*
187
199
* @return string[] Array of file header values keyed by header name.
188
200
*/
189
- public static function get_snippet_meta_from_file ($ file, ) {
201
+ public static function get_snippet_meta_from_file ($ file ) {
190
202
// Pull only the first 8 KB of the file in.
191
203
$ filedata = file_get_contents ( $ file , false , null , 0 , 8 * self ::KB_IN_BYTES );
192
204
@@ -211,17 +223,55 @@ public static function get_snippet_meta_from_file($file,) {
211
223
return $ headers ;
212
224
}
213
225
214
- // /**
215
- // * Checks if there are any snippets not tracked in the database.
216
- // *
217
- // * If they are not they will be added to the database.
218
- // *
219
- // * @param moodle_recordset $data The snippets dataset currently present in the DB.
220
- // *
221
- // * @return void
222
- // */
223
- // public static function check_for_missing_snippets_int_the_database($data) {
224
-
225
- // }
226
+ /**
227
+ * Retrieve all builtin CSS snippets via the actual scss files.
228
+ *
229
+ * @return string[]
230
+ */
231
+ private static function get_builtin_snippet_paths () {
232
+ global $ CFG ;
233
+ // Get an array of all .scss files in the directory.
234
+ $ files = glob ($ CFG ->dirroot . self ::BUILTIN_SNIPPETS_BASE_PATH . '*.scss ' );
235
+
236
+ // Get the basenames.
237
+ $ basenames = array_map (fn ($ file ) => basename ($ file ), $ files );
238
+
239
+ return $ basenames ;
240
+ }
241
+
242
+ /**
243
+ * Make sure builtin snippets are in the database.
244
+ *
245
+ * @return void
246
+ */
247
+ public static function add_builtin_snippets () {
248
+ global $ DB ;
249
+ $ paths = self ::get_builtin_snippet_paths ();
250
+
251
+ $ present = $ DB ->get_records (
252
+ 'theme_boost_union_snippets ' ,
253
+ ['source ' => 'theme_boost_union ' ],
254
+ 'sortorder DESC ' ,
255
+ 'id,path,sortorder ' ,
256
+ 0 ,
257
+ 0 ,
258
+ );
259
+
260
+ $ sortorder = empty ($ present ) ? 0 : $ present [0 ]->sortorder + 1 ;
261
+
262
+ foreach ($ paths as $ path ) {
263
+ if (!array_key_exists ($ path , $ present )) {
264
+ $ DB ->insert_record (
265
+ 'theme_boost_union_snippets ' ,
266
+ [
267
+ 'path ' => $ path ,
268
+ 'source ' => 'theme_boost_union ' ,
269
+ 'sortorder ' => $ sortorder ,
270
+ ]
271
+ );
272
+ $ sortorder += 1 ;
273
+ }
274
+ }
275
+ }
226
276
227
277
}
0 commit comments