16
16
17
17
namespace theme_boost_union ;
18
18
19
+ use moodle_recordset ;
20
+
19
21
/**
20
22
* Class snippets
21
23
*
24
26
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
27
*/
26
28
class snippets {
29
+ /**
30
+ * Constant for how many a Kilobyte are in bytes.
31
+ *
32
+ * @var int
33
+ */
34
+ const KB_IN_BYTES = 1024 ;
35
+
36
+ /**
37
+ * List of all available Snippet Meta File headers.
38
+ *
39
+ * @var array
40
+ */
41
+ const SNIPPET_HEADERS = [
42
+ 'Snippet Title ' ,
43
+ 'Goal ' ,
44
+ 'Domain ' ,
45
+ 'Description ' ,
46
+ 'Scope ' ,
47
+ ];
48
+
49
+ /**
50
+ * Gets the snippet file based on the meta information.
51
+ * @param mixed $key
52
+ * @param mixed $source
53
+ * @param mixed $domain
54
+ * @return string|null
55
+ */
56
+ public static function get_snippet_file ($ key , $ source , $ domain = '' ) {
57
+ global $ CFG ;
58
+ if ('theme_boost_union ' === $ source ) {
59
+ $ file = $ CFG ->dirroot . sprintf ('/theme/boost_union/snippets/builtin/%s.scss ' , $ key );
60
+ } else {
61
+ return null ;
62
+ }
63
+ return is_readable ($ file ) ? $ file : null ;
64
+ }
65
+
66
+ /**
67
+ * TODO
68
+ * @param mixed $key
69
+ * @param mixed $source
70
+ * @param mixed $domain
71
+ * @return bool|string
72
+ */
73
+ public static function get_snippet_css ($ key , $ source , $ domain = '' ) {
74
+ // Get the snippets file, based on the source and domain.s
75
+ $ file = self ::get_snippet_file ($ key , $ source );
76
+
77
+ if (is_null ($ file )) {
78
+ return ;
79
+ }
80
+ $ scss = file_get_contents ( $ file , false , null , 0 );
81
+
82
+ return $ scss ;
83
+ }
84
+
27
85
/**
28
86
* Get a snippet defined in the code based on key and domain.
29
87
* @param string $key
30
88
* @param string $domain
31
89
* @return mixed
32
90
*/
33
- public static function get_snippet ($ key , $ domain = ' theme_boost_union ' ) {
91
+ public static function get_snippet_meta ($ key , $ source ) {
34
92
global $ CFG ;
35
- if ('theme_boost_union ' == $ domain ) {
36
- return require_once ($ CFG ->dirroot . sprintf ('/theme/boost_union/snippets/builtin/%s.php ' , $ key ));
93
+
94
+ // Get the snippets file, based on the source and domain.
95
+ $ file = self ::get_snippet_file ($ key , $ source );
96
+
97
+ if (is_null ($ file )) {
98
+ return ;
99
+ }
100
+
101
+ $ headers = self ::get_snippet_meta_from_file ($ file );
102
+
103
+ if (!array_key_exists ('Snippet Title ' , $ headers )) {
104
+ return null ;
37
105
}
106
+
107
+ $ snippet = new \stdClass ();
108
+ $ snippet ->title = $ headers ['Snippet Title ' ];
109
+ $ snippet ->description = $ headers ['Description ' ];
110
+ $ snippet ->scope = $ headers ['Scope ' ];
111
+ $ snippet ->goal = $ headers ['Goal ' ];
112
+ $ snippet ->source = 'theme_boost_union ' ;
113
+
114
+ return $ snippet ;
38
115
}
39
116
40
117
/**
@@ -45,14 +122,9 @@ public static function get_snippet($key, $domain = 'theme_boost_union') {
45
122
public static function compose_snippets_data ($ snippetrecordset ) {
46
123
$ snippets = [];
47
124
foreach ($ snippetrecordset as $ snippetrecord ) {
48
- if ('code ' === $ snippetrecord ->source ) {
49
- $ snippetcontent = self ::get_snippet ($ snippetrecord ->key , $ snippetrecord ->domain );
50
- $ snippetrecord ->title = $ snippetcontent ['title ' ];
51
- $ snippetrecord ->description = $ snippetcontent ['description ' ];
52
- $ snippetrecord ->css = $ snippetcontent ['css ' ];
53
- $ snippetrecord ->goal = $ snippetcontent ['goal ' ];
54
- $ snippetrecord ->scope = $ snippetcontent ['scope ' ];
55
- $ snippets [] = $ snippetrecord ;
125
+ $ snippet = self ::get_snippet_meta ($ snippetrecord ->key , $ snippetrecord ->source );
126
+ if ($ snippet ) {
127
+ $ snippets [] = (object ) array_merge ((array ) $ snippetrecord , (array ) $ snippet );
56
128
}
57
129
}
58
130
return $ snippets ;
@@ -76,14 +148,75 @@ public static function get_enabled_snippet_css() {
76
148
77
149
$ css = '' ;
78
150
79
- foreach ($ data as $ snippet ) {
80
- if ($ snippet ->enabled ) {
81
- $ css .= self ::get_snippet ($ snippet ->key , $ snippet ->domain )['css ' ] . ' ' ;
82
- }
151
+ // foreach ($data as $snippet) {
152
+ // if ($snippet->enabled) {
153
+ // $css .= self::get_snippet($snippet->key, $snippet->domain)['css'] . ' ';
154
+ // }
155
+ // }
156
+
157
+ return $ css ;
158
+ }
159
+
160
+ /**
161
+ * Strips close comment and close php tags from file headers.
162
+ *
163
+ * @param string $str Header comment to clean up.
164
+ * @return string
165
+ */
166
+ private static function cleanup_header_comment ($ str ) {
167
+ return trim (preg_replace ('/\s*(?:\*\/|\?>).*/ ' , '' , $ str ));
168
+ }
169
+
170
+ /**
171
+ * Retrieves metadata from a file.
172
+ *
173
+ * Searches for metadata in the first 8 KB of a file, such as a plugin or theme.
174
+ * Each piece of metadata must be on its own line. Fields can not span multiple
175
+ * lines, the value will get cut at the end of the first line.
176
+ *
177
+ * If the file data is not within that first 8 KB, then the author should correct
178
+ * the snippet.
179
+ *
180
+ * @param string $file Absolute path to the file.
181
+ *
182
+ * @return string[] Array of file header values keyed by header name.
183
+ */
184
+ public static function get_snippet_meta_from_file ($ file ,) {
185
+ // Pull only the first 8 KB of the file in.
186
+ $ filedata = file_get_contents ( $ file , false , null , 0 , 8 * self ::KB_IN_BYTES );
187
+
188
+ if ( false === $ filedata ) {
189
+ $ filedata = '' ;
83
190
}
84
191
85
- $ data ->close ();
192
+ // Make sure we catch CR-only line endings.
193
+ $ filedata = str_replace ( "\r" , "\n" , $ filedata );
86
194
87
- return $ css ;
195
+ $ headers = [];
196
+
197
+ foreach (self ::SNIPPET_HEADERS as $ regex ) {
198
+ if (preg_match ('/^(?:[ \t]*)?[ \t\/*#@]* ' . preg_quote ($ regex , '/ ' ) . ':(.*)$/mi ' , $ filedata , $ match )
199
+ && $ match [1 ]) {
200
+ $ headers [$ regex ] = self ::cleanup_header_comment ($ match [1 ]);
201
+ } else {
202
+ $ headers [$ regex ] = '' ;
203
+ }
204
+ }
205
+
206
+ return $ headers ;
88
207
}
208
+
209
+ // /**
210
+ // * Checks if there are any snippets not tracked in the database.
211
+ // *
212
+ // * If they are not they will be added to the database.
213
+ // *
214
+ // * @param moodle_recordset $data The snippets dataset currently present in the DB.
215
+ // *
216
+ // * @return void
217
+ // */
218
+ // public static function check_for_missing_snippets_int_the_database($data) {
219
+
220
+ // }
221
+
89
222
}
0 commit comments