-
Notifications
You must be signed in to change notification settings - Fork 247
/
crayon_resource.class.php
478 lines (401 loc) · 12.1 KB
/
crayon_resource.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
require_once ('global.php');
require_once (CRAYON_LANGS_PHP);
require_once (CRAYON_THEMES_PHP);
require_once (CRAYON_FONTS_PHP);
class CrayonResources {
private static $langs = NULL;
private static $themes = NULL;
private static $fonts = NULL;
private function __construct() {}
public static function langs() {
if (self::$langs == NULL) {
self::$langs = new CrayonLangs();
}
return self::$langs;
}
public static function themes() {
if (self::$themes == NULL) {
self::$themes = new CrayonThemes();
}
return self::$themes;
}
public static function fonts() {
if (self::$fonts == NULL) {
self::$fonts = new CrayonFonts();
}
return self::$fonts;
}
}
class CrayonResourceCollection {
// Properties and Constants ===============================================
// Loaded resources
private $collection = array();
// Loading state
private $state = self::UNLOADED;
// Directory containing resources
private $dir = '';
private $default_id = '';
private $default_name = '';
const UNLOADED = -1;
const LOADING = 0;
const LOADED = 1;
// Methods ================================================================
/* Override in subclasses. Returns the absolute path for a given resource. Does not check for its existence. */
public function path($id) {
return '';
}
/* Verifies a resource exists. */
public function exists($id) {
return file_exists($this->path($id));
}
/* Load all the available languages. Doesn't parse them for their names and regex. */
public function load() {
// Load only once
if (!$this->is_state_unloaded()) {
return;
}
$this->state = self::LOADING;
$this->load_process();
$this->state = self::LOADED;
}
public function load_resources($dir = NULL) {
if ($dir === NULL) {
$dir = $this->dir;
}
if (!$this->is_state_loading()) {
// Load only once
return;
}
try {
// Look in directory for resources
if (!is_dir($dir)) {
CrayonLog::syslog('The resource directory is missing, should be at \'' . $dir . '\'.');
} else if (($handle = @opendir($dir)) != FALSE) {
// Loop over directory contents
while (($file = readdir($handle)) !== FALSE) {
if ($file != "." && $file != "..") {
// Check if $file is directory, remove extension when checking for existence.
if (!is_dir($dir . $file)) {
$file = CrayonUtil::path_rem_ext($file);
}
if ($this->exists($file)) {
$this->add_resource($this->resource_instance($file));
}
}
}
closedir($handle);
}
} catch (Exception $e) {
CrayonLog::syslog('An error occured when trying to load resources: ' . $e->getFile() . $e->getLine());
}
}
/* Override in subclasses. */
public function load_process() {
if (!$this->is_state_loading()) {
return;
}
$this->load_resources();
$this->add_default();
}
/* Override in subclasses */
public function add_default() {
if (!$this->is_state_loading()) {
return FALSE;
} else if (!$this->is_loaded($this->default_id)) {
CrayonLog::syslog('The default resource could not be loaded from \'' . $this->dir . '\'.');
// Add the default, but it will not be functionable
$default = $this->resource_instance($this->default_id, $this->default_name);
$this->add($this->default_id, $default);
return TRUE;
}
return FALSE;
}
/* Returns the default resource */
public function set_default($id, $name) {
$this->default_id = $id;
$this->default_name = $name;
}
/* Returns the default resource */
public function get_default() {
return $this->get($this->default_id);
}
/* Override in subclasses to create subclass object if needed */
public function resource_instance($id, $name = NULL) {
return new CrayonResource($id, $name);
}
public function add($id, $resource) {
if (is_string($id) && !empty($id)) {
$this->collection[$id] = $resource;
asort($this->collection);
CrayonLog::debug('Added resource: ' . $this->path($id));
} else {
CrayonLog::syslog('Could not add resource: ', $id);
}
}
public function add_resource($resource) {
$this->add($resource->id(), $resource);
}
public function remove($name) {
if (is_string($name) && !empty($name) && array_key_exists($name, $this->collection)) {
unset($this->collection[$name]);
}
}
public function remove_all() {
$this->collection = array();
}
/* Returns the resource for the given id or NULL if it can't be found */
public function get($id = NULL) {
$this->load();
if ($id === NULL) {
return $this->collection;
} else if (is_string($id) && $this->is_loaded($id)) {
return $this->collection[$id];
}
return NULL;
}
public function get_array() {
$array = array();
foreach ($this->get() as $resource) {
$array[$resource->id()] = $resource->name();
}
return $array;
}
public function is_loaded($id) {
if (is_string($id)) {
return array_key_exists($id, $this->collection);
}
return FALSE;
}
public function get_state() {
return $this->state;
}
public function is_state_loaded() {
return $this->state == self::LOADED;
}
public function is_state_loading() {
return $this->state == self::LOADING;
}
public function is_state_unloaded() {
return $this->state == self::UNLOADED;
}
public function directory($dir = NULL) {
if ($dir === NULL) {
return $this->dir;
} else {
$this->dir = CrayonUtil::path_slash($dir);
}
}
public function url($id) {
return '';
}
public function get_css($id, $ver = NULL) {
$resource = $this->get($id);
return '<link rel="stylesheet" type="text/css" href="' . $this->url($resource->id()) . ($ver ? "?ver=$ver" : '') . '" />' . CRAYON_NL;
}
}
class CrayonUsedResourceCollection extends CrayonResourceCollection {
// Checks if any resoruces are being used
public function is_used($id = NULL) {
if ($id === NULL) {
foreach ($this->get() as $resource) {
if ($resource->used()) {
return TRUE;
}
}
return FALSE;
} else {
$resource = $this->get($id);
if (!$resource) {
return FALSE;
} else {
return $resource->used();
}
}
}
public function set_used($id, $value = TRUE) {
$resource = $this->get($id);
if ($resource !== NULL && !$resource->used()) {
$resource->used($value == TRUE);
return TRUE;
}
return FALSE;
}
public function get_used() {
$used = array();
foreach ($this->get() as $resource) {
if ($resource->used()) {
$used[] = $resource;
}
}
return $used;
}
// XXX Override
public function resource_instance($id, $name = NULL) {
return new CrayonUsedResource($id, $name);
}
public function get_used_css() {
$used = $this->get_used();
$css = array();
foreach ($used as $resource) {
$url = $this->url($resource->id());
$css[$resource->id()] = $url;
}
return $css;
}
}
class CrayonUserResourceCollection extends CrayonUsedResourceCollection {
private $user_dir = '';
private $curr_dir = NULL;
// TODO better to use a base dir and relative
private $relative_directory = NULL;
// TODO move this higher up inheritance
private $extension = '';
// XXX Override
public function resource_instance($id, $name = NULL) {
$resource = $this->create_user_resource_instance($id, $name);
$resource->user($this->curr_dir == $this->user_directory());
return $resource;
}
public function create_user_resource_instance($id, $name = NULL) {
return new CrayonUserResource($id, $name);
}
public function user_directory($dir = NULL) {
if ($dir === NULL) {
return $this->user_dir;
} else {
$this->user_dir = CrayonUtil::path_slash($dir);
}
}
public function relative_directory($relative_directory = NULL) {
if ($relative_directory == NULL) {
return $this->relative_directory;
}
$this->relative_directory = $relative_directory;
}
public function extension($extension = NULL) {
if ($extension == NULL) {
return $this->extension;
}
$this->extension = $extension;
}
public function load_resources($dir = NULL) {
$this->curr_dir = $this->directory();
parent::load_resources($this->curr_dir);
$this->curr_dir = $this->user_directory();
parent::load_resources($this->curr_dir);
$this->curr_dir = NULL;
}
public function current_directory() {
return $this->curr_dir;
}
public function dir_is_user($id, $user = NULL) {
if ($user === NULL) {
if ($this->is_state_loading()) {
// We seem to be loading resources - use current directory
$user = $this->current_directory() == $this->user_directory();
} else {
$theme = $this->get($id);
if ($theme) {
$user = $theme->user();
} else {
$user = FALSE;
}
}
}
return $user;
}
public function dirpath($user = NULL) {
$path = $user ? $this->user_directory() : $this->directory();
return CrayonUtil::path_slash($path);
}
public function dirpath_for_id($id, $user = NULL) {
$user = $this->dir_is_user($id, $user);
return $this->dirpath($user) . $id;
}
public function dirurl($user = NULL) {
$path = $user ? CrayonGlobalSettings::upload_url() : CrayonGlobalSettings::plugin_path();
return CrayonUtil::path_slash($path . $this->relative_directory());
}
// XXX Override
public function path($id, $user = NULL) {
$user = $this->dir_is_user($id, $user);
return $this->dirpath($user) . $this->filename($id, $user);
}
// XXX Override
public function url($id, $user = NULL) {
$user = $this->dir_is_user($id, $user);
return $this->dirurl($user) . $this->filename($id, $user);
}
public function filename($id, $user = NULL) {
return "$id.$this->extension";
}
}
class CrayonResource {
private $id = '';
private $name = '';
function __construct($id, $name = NULL) {
$id = $this->clean_id($id);
CrayonUtil::str($this->id, $id);
( empty($name) ) ? $this->name( self::clean_name($this->id) ) : $this->name($name);
}
function __tostring() {
return $this->name;
}
function id() {
return $this->id;
}
function name($name = NULL) {
if ($name === NULL) {
return $this->name;
} else {
$this->name = $name;
}
}
function clean_id($id) {
$id = CrayonUtil::space_to_hyphen( strtolower(trim($id)) );
return preg_replace('#[^\w-]#msi', '', $id);
}
public static function clean_name($id) {
$id = CrayonUtil::hyphen_to_space( strtolower(trim($id)) );
return CrayonUtil::ucwords($id);
}
}
class CrayonUsedResource extends CrayonResource {
// Keeps track of usage
private $used = FALSE;
function used($used = NULL) {
if ($used === NULL) {
return $this->used;
} else {
$this->used = ($used ? TRUE : FALSE);
}
}
}
class CrayonUserResource extends CrayonUsedResource {
// Keeps track of user modifications
private $user = FALSE;
function user($user = NULL) {
if ($user === NULL) {
return $this->user;
} else {
$this->user = ($user ? TRUE : FALSE);
}
}
}
class CrayonVersionResource extends CrayonUserResource {
// Adds version
private $version = '';
function __construct($id, $name = NULL, $version = NULL) {
parent::__construct($id, $name);
$this->version($version);
}
function version($version = NULL) {
if ($version === NULL) {
return $this->version;
} else if (is_string($version)) {
$this->version = $version;
}
}
}
?>