/
casset.php
1322 lines (1191 loc) · 41.4 KB
/
casset.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
* @version v1.18
* @author Antony Male
* @license MIT License
* @copyright 2012 Antony Male
* @link http://github.com/canton7/fuelphp-casset
*/
namespace Casset;
class Casset {
/**
* @var array Array of paths in which the css, js, img directory structure
* can be found, relative to $asset_url
*/
protected static $asset_paths = array(
'core' => 'assets/',
);
/*
* @var string The key in $asset_paths to use if no key is given
*/
protected static $default_path_key = 'core';
/**
* @var string The URL to be prepanded to all assets.
*/
protected static $asset_url = null;
/**
* @var array The folders in which css, js, and images can be found.
*/
protected static $default_folders = array(
'css' => 'css/',
'js' => 'js/',
'img' => 'img/',
);
/**
* @var string The directory, relative to public/, where cached minified failes
* are stored.
*/
protected static $cache_path = 'assets/cache/';
/**
* @var array Holds groups of assets. Is documenented fully in the config file.
*/
protected static $groups = array(
'css' => array(),
'js' => array(),
);
/**
* @var array Holds inline js and css.
*/
protected static $inline_assets = array(
'css' => array(),
'js' => array(),
);
/**
*
* @var array Defaults for a group
*/
protected static $default_options = array(
'enabled' => true,
'combine' => true,
'min' => true,
'inline' => false,
'attr' => array(),
'deps' => array(),
);
/**
* @var int How deep to go when resolving deps
*/
protected static $deps_max_depth = 5;
/**
* @var bool Whether to show comments above the <script>/<link> tags showing
* which files have been minified into that file.
*/
protected static $show_files = false;
/**
* @var bool Whether to show comments inside minified files showing which
* original file is where.
*/
protected static $show_files_inline = false;
/**
* @var function If given, the function to call when we've read a file, before
* minifying.
* Note that it's only called if $combine for the file is true
* Prototype: callback(content, filename, type, group_name);
*/
protected static $post_load_callback = null;
/*
* @var function If given, the function to call when we've decided on the name
* for a file, but want to allow the user to tweak it before we write it to the
* page.
* Prototype: callback($filepath, $type, $remote);
*/
protected static $filepath_callback = null;
/**
* @var array Keeps a record of which groups have been rendered.
* We then check this when deciding whether to render a dep.
*/
protected static $rendered_groups = array('js' => array(), 'css' => array());
/**
* @var string which css uri rewriter we want to use. Options are 'absolute', 'relative', 'none'
*/
protected static $css_uri_rewriter = 'absolute';
/**
* @var bool Wether we've been initialized.
*/
public static $initialized = false;
/**
* Loads in the config and sets the variables
*/
public static function _init()
{
// Prevent multiple initializations
if (static::$initialized)
{
return;
}
\Config::load('casset', true);
static::$asset_url = \Config::get('casset.url', \Config::get('base_url'));
static::$default_folders = array(
'css' => \Config::get('casset.css_dir', static::$default_folders['css']),
'js' => \Config::get('casset.js_dir', static::$default_folders['js']),
'img' => \Config::get('casset.img_dir', static::$default_folders['img']),
);
$paths = \Config::get('casset.paths', static::$asset_paths);
foreach($paths as $key => $path)
{
static::add_path($key, $path);
}
static::$cache_path = \Config::get('casset.cache_path', static::$cache_path);
static::$default_options['min'] = \Config::get('casset.min', static::$default_options['min']);
static::$default_options['combine'] = \Config::get('casset.combine', static::$default_options['combine']);
static::$deps_max_depth = \Config::get('casset.deps_max_depth', static::$deps_max_depth);
$group_sets = \Config::get('casset.groups', array());
foreach ($group_sets as $group_type => $groups)
{
foreach ($groups as $group_name => $group)
{
$options = static::prep_new_group_options($group);
static::add_group($group_type, $group_name, $group['files'], $options);
}
}
// Add the global group if it doesn't already exist.
// This is so that set_group_option() can be used on it. This function will
// throw an exception if the named group doesn't exist.
if (!static::group_exists('js', 'global'))
static::add_group_base('js', 'global');
if (!static::group_exists('css', 'global'))
static::add_group_base('css', 'global');
static::$show_files = \Config::get('casset.show_files', static::$show_files);
static::$show_files_inline = \Config::get('casset.show_files_inline', static::$show_files_inline);
static::$post_load_callback = \Config::get('casset.post_load_callback', static::$post_load_callback);
static::$filepath_callback = \Config::get('casset.filepath_callback', static::$filepath_callback);
static::$css_uri_rewriter = \Config::get('casset.css_uri_rewriter', static::$css_uri_rewriter);
static::$initialized = true;
}
/**
* Sets up options for new groups setup via casset/config.php.
* Abstracts away from _init method. Also easier if options are
* added in future as iterates through defaults to do checking.
*
* @param array $options Options as defined in group in config.php
* @return void
*/
protected static function prep_new_group_options($group_options)
{
$options = array();
foreach (static::$default_options as $key => $option_val) {
if (array_key_exists($key, $group_options)) {
$options[$key] = $group_options[$key];
}
}
return $options;
}
/**
* Parses oen of the 'paths' config keys into the format used internally.
* Config file format:
* 'paths' => array(
* 'assets/',
* array(
* 'path' => 'assets_2/',
* 'js_dir' => 'js/',
* 'css_dir' => 'css/',
* ),
* ),
* In the event that the value isn't an array, it is turned into one.
* If js_dir, css_dir or img_dir are not given, they are populated with
* the defaults, giving in the 'js_dir', 'css_dir' and 'img_dir' config keys.
* @param string $path_key the key of the path
* @param mixed $path_attr the path attributes, as described above
*/
public static function add_path($path_key, $path_attr)
{
$path_val = array();
if (!is_array($path_attr))
$path_attr = array('path' => $path_attr, 'dirs' => array());
elseif (!array_key_exists('dirs', $path_attr))
$path_attr['dirs'] = array();
$path_val['path'] = $path_attr['path'];
$path_val['dirs'] = array(
'js' => array_key_exists('js_dir', $path_attr) ? $path_attr['js_dir'] : static::$default_folders['js'],
'css' => array_key_exists('css_dir', $path_attr) ? $path_attr['css_dir'] : static::$default_folders['css'],
'img' => array_key_exists('img_dir', $path_attr) ? $path_attr['img_dir'] : static::$default_folders['img'],
);
static::$asset_paths[$path_key] = $path_val;
}
/**
* Set the current default path
*
* @param $path_key the path key to set the default to.
*/
public static function set_path($path_key = 'core')
{
if (!array_key_exists($path_key, static::$asset_paths))
throw new Casset_Exception("Asset path key $path_key doesn't exist");
static::$default_path_key = $path_key;
}
/**
* Adds a group of assets. If a group of this name exists, the function returns.
*
* @param string $group_type 'js' or 'css'
* @param string $group_name The name of the group
* @param array $options. An array of options. array(
* 'enabled' => true/false,
* 'combine' => true/false,
* 'min' => true/false,
* 'inline' => true/false,
* 'deps' => array(),
* );
*/
protected static function add_group_base($group_type, $group_name, $options = array())
{
// Insert defaults
$options += static::$default_options;
if (!is_array($options['deps']))
$options['deps'] = array($options['deps']);
$options['files'] = array();
// If it already exists, don't overwrite it
if (array_key_exists($group_name, static::$groups[$group_type]))
throw new Casset_Exception("Group $group_name already exists: can't create it.");
static::$groups[$group_type][$group_name] = $options;
}
/**
* Adds a group for assets, and adds assets to that group.
*
* @param string $group_type 'js' or 'css'
* @param string $group_name The name of the group
* @param array $options. An array of options. array(
* 'enabled' => true/false,
* 'combine' => true/false,
* 'min' => true/false,
* 'inline' => true/false,
* 'attr' => array(),
* 'deps' => array(),
* );
* To maintain backwards compatibility, you can also pass $enabled here.
* @param bool $combine_dep DEPRECATED. Whether to combine files in this group. Default (null) means use config setting
* @param boo $min_dep DEPRECATED/ Whether to minify files in this group. Default (null) means use config setting
*/
public static function add_group($group_type, $group_name, $files, $options = array(), $combine_dep = null, $min_dep = null)
{
// Bit of backwards compatibity.
// Order used to be add_group(group_type, group_name, files, enabled, combine, min)
if (!is_array($options))
{
$options = array(
'enabled' => $options,
'combine' => $combine_dep,
'min' => $min_dep,
);
}
// We're basically faking the old add_group. However, the approach has changed since those days
// Therefore we create the group it it doesn't already exist, then add the files to it
static::add_group_base($group_type, $group_name, $options);
foreach ($files as $file) {
if (!is_array($file))
$file = array($file, false);
static::add_asset($group_type, $file[0], $file[1], $group_name);
}
}
/**
* Returns true if the given group exists
*
* @param string $group_type 'js' or 'css
* @param type $group_name the nam eof the group
*/
public static function group_exists($group_type, $group_name)
{
return array_key_exists($group_name, static::$groups[$group_type]);
}
/**
* Enables both js and css groups of the given name.
*
* @param mixed $group The group to enable, or array of groups
*/
public static function enable($groups)
{
static::asset_enabled('js', $groups, true);
static::asset_enabled('css', $groups, true);
}
/**
* Disables both js and css groups of the given name.
*
* @param string $group The group to disable, or array of groups
*/
public static function disable($groups)
{
static::asset_enabled('js', $groups, false);
static::asset_enabled('css', $groups, false);
}
/**
* Enable a group of javascript assets.
*
* @param string $group The group to enable, or array of groups
*/
public static function enable_js($groups)
{
static::asset_enabled('js', $groups, true);
}
/**
* Disable a group of javascript assets.
*
* @param string $group The group to disable, or array of groups
*/
public static function disable_js($groups)
{
static::asset_enabled('js', $groups, false);
}
/**
* Enable a group of css assets.
*
* @param string $group The group to enable, or array of groups
*/
public static function enable_css($groups)
{
static::asset_enabled('css', $groups, true);
}
/**
* Disable a group of css assets.
*
* @param string $group The group to disable, or array of groups
*/
public static function disable_css($groups)
{
static::asset_enabled('css', $groups, false);
}
/**
* Enables / disables an asset.
*
* @param string $type 'css' / 'js'
* @param string $group The group to enable/disable, or array of groups
* @param bool $enabled True to enabel to group, false odisable
*/
protected static function asset_enabled($type, $groups, $enabled)
{
if (!is_array($groups))
$groups = array($groups);
foreach ($groups as $group)
{
// If the group doesn't exist it's of no consequence
if (!array_key_exists($group, static::$groups[$type]))
continue;
static::$groups[$type][$group]['enabled'] = $enabled;
}
}
/**
* Set group options on-the-fly.
*
* @param string $type 'js' / 'css
* @param mixed $group_names Group name to change, or array of groups to change,
* or '' for global group, or '*' for all groups.
* @param string $option_key The name of the option to change
* @param mixed $option_value What to set the option to
*/
public static function set_group_option($type, $group_names, $option_key, $option_value)
{
if ($group_names == '')
$group_names = array('global');
else if ($group_names == '*')
{
// Change the default
static::$default_options[$option_key] = $option_value;
$group_names = array_keys(static::$groups[$type]);
}
else if (!is_array($group_names))
$group_names = array($group_names);
// Allow them to specify a single string dep
if ($option_key == 'deps' && !is_array($option_value))
$option_value = array($option_value);
foreach ($group_names as $group_name)
{
// If the group doesn't exist, throw a fuss
if (!static::group_exists($type, $group_name))
throw new Casset_Exception("Can't set option for group '$group_name' ($type), as it doesn't exist.");
static::$groups[$type][$group_name][$option_key] = $option_value;
}
}
/**
* Set group options on-the-fly, js version
*
* @param mixed $group_names Group name to change, or array of groups to change,
* or '' for global group, or '*' for all groups.
* @param string $option_key The name of the option to change
* @param mixed $option_value What to set the option to
*/
public static function set_js_option($group_names, $option_key, $option_value)
{
static::set_group_option('js', $group_names, $option_key, $option_value);
}
/**
* Set group options on-the-fly, css version
*
* @param mixed $group_names Group name to change, or array of groups to change,
* or '' for global group, or '*' for all groups.
* @param string $option_key The name of the option to change
* @param mixed $option_value What to set the option to
*/
public static function set_css_option($group_names, $option_key, $option_value)
{
static::set_group_option('css', $group_names, $option_key, $option_value);
}
/**
* Add a javascript asset.
*
* @param string $script The script to add.
* @param string $script_min If given, will be used when $min = true
* If omitted, $script will be minified internally
* @param string $group The group to add this asset to. Defaults to 'global'
*/
public static function js($script, $script_min = false, $group = 'global')
{
static::add_asset('js', $script, $script_min, $group);
}
/**
* Add a css asset.
*
* @param string $sheet The script to add
* @param string $sheet_min If given, will be used when $min = true
* If omitted, $script will be minified internally
* @param string $group The group to add this asset to. Defaults to 'global'
*/
public static function css($sheet, $sheet_min = false, $group = 'global')
{
static::add_asset('css', $sheet, $sheet_min, $group);
}
/**
* Abstraction of js() and css().
*
* @param string $type 'css' / 'js'
* @param string $script The script to add.
* @param string $script_min If given, will be used when $min = true
* If omitted, $script will be minified internally
* @param string $group The group to add this asset to
*/
protected static function add_asset($type, $script, $script_min, $group)
{
// Allow the user to specify any non-string value for an asset, and it
// will be ignore. This can be handy when using ternary operators
// in the groups config.
if (!is_string($script))
return;
// Don't force the user to remember that 'false' is used when not supplying
// a pre-minified file.
if (!is_string($script_min))
$script_min = false;
$files = array($script, $script_min);
// If the user hasn't specified a path key, add $default_path_key
foreach ($files as &$file)
{
if ($file != false && strpos($file, '::') === false)
$file = static::$default_path_key.'::'.$file;
}
if (!array_key_exists($group, static::$groups[$type]))
{
// Assume they want the group enabled
static::add_group_base($type, $group);
}
array_push(static::$groups[$type][$group]['files'], $files);
}
/**
* Add a string containing javascript, which can be printed inline with
* js_render_inline().
*
* @param string $content The javascript to add
*/
public static function js_inline($content)
{
static::add_asset_inline('js', $content);
}
/**
* Add a string containing css, which can be printed inline with
* css_render_inline().
*
* @param string $content The css to add
*/
public static function css_inline($content)
{
static::add_asset_inline('css', $content);
}
/**
* Abstraction of js_inline() and css_inline().
*
* @param string $type 'css' / 'js'
* @param string $content The css / js to add
*/
protected static function add_asset_inline($type, $content)
{
array_push(static::$inline_assets[$type], $content);
}
/**
* Return the path for the given JS asset. Ties into find_files, so supports
* everything that, say, Casset::js() does.
* Throws an exception if the file isn't found.
* @param string $script the name of the asset to find
* @param bool $add_url whether to add the 'url' config key to the filename
* @param bool $force_array by default, when one file is found a string is
* returned. Setting this to true causes a single-element array to be returned.
*/
public static function get_filepath_js($filename, $add_url = false, $force_array = false)
{
return static::get_filepath($filename, 'js', $add_url, $force_array);
}
/**
* Return the path for the given CSS asset. Ties into find_files, so supports
* everything that, say, Casset::js() does.
* Throws an exception if the file isn't found.
* @param string $script the name of the asset to find
* @param bool $add_url whether to add the 'url' config key to the filename
* @param bool $force_array by default, when one file is found a string is
* returned. Setting this to true causes a single-element array to be returned.
*/
public static function get_filepath_css($filename, $add_url = false, $force_array = false)
{
return static::get_filepath($filename, 'css', $add_url, $force_array);
}
/**
* Return the path for the given img asset. Ties into find_files, so supports
* everything that, say, Casset::js() does.
* Throws an exception if the file isn't found.
* @param string $script the name of the asset to find
* @param bool $add_url whether to add the 'url' config key to the filename
* @param bool $force_array by default, when one file is found a string is
* returned. Setting this to true causes a single-element array to be returned.
*/
public static function get_filepath_img($filename, $add_url = false, $force_array = false)
{
return static::get_filepath($filename, 'img', $add_url, $force_array);
}
/**
* Return the path for the given asset. Ties into find_files, so supports
* everything that, say, Casset::js() does.
* Throws an exception if the file isn't found.
* @param string $script the name of the asset to find
* @param string $type js, css or img
* @param bool $add_url whether to add the 'url' config key to the filename
* @param bool $force_array by default, when one file is found a string is
* returned. Setting this to true causes a single-element array to be returned.
*/
public static function get_filepath($filename, $type, $add_url = false, $force_array = false)
{
if (strpos($filename, '::') === false)
$filename = static::$default_path_key.'::'.$filename;
$files = static::find_files($filename, $type);
foreach ($files as &$file)
{
$remote = (strpos($file, '//') !== false);
$file = static::process_filepath($file, $type, $remote);
if ($remote)
continue;
if ($add_url)
$file = static::$asset_url.$file;
}
if (count($files) == 1 && !$force_array)
return $files[0];
return $files;
}
/**
* Can be used to add deps to a group.
*
* @param string $type 'js' / 'css
* @param string $group The group name to add deps to
* @param array $deps An array of group names to add as deps.
*/
public static function add_deps($type, $group, $new_deps)
{
if (!is_array($new_deps))
$new_deps = array($new_deps);
if (!array_key_exists($group, static::$groups[$type]))
throw new Casset_Exception("Group $group ($type) doesn't exist, so can't add deps to it.");
// Avoid duplicates in deps array
$deps = &static::$groups[$type][$group]['deps'];
$deps = array_unique(array_merge($deps, $new_deps));
}
/**
* Sugar for add_deps(), for js groups
* @param string $group The group name to add deps to
* @param array $deps An array of group names to add as deps.
*/
public static function add_js_deps($group, $deps)
{
static::add_deps('js', $group, $deps);
}
/**
* Sugar for add_deps(), for css groups
* @param string $group The group name to add deps to
* @param array $deps An array of group names to add as deps.
*/
public static function add_css_deps($group, $deps)
{
static::add_deps('css', $group, $deps);
}
/**
* Sticks the given filename through the filepath callback, if given.
*
* @param string $filepath The filepath to process
* @param string $type The type of asset, passed to the callback
* @param bool $remote Whether the asset is on another machine, passed to the callback
*/
protected static function process_filepath($filepath, $type, $remote = null)
{
if (static::$filepath_callback)
{
if ($remote === null)
$remote = (strpos($filepath, '//') !== false);
$func = static::$filepath_callback;
$filepath = $func($filepath, $type, $remote);
}
return $filepath;
}
/**
* Shortcut to render_js() and render_css().
*
* @param string $group Which group to render. If omitted renders all groups
* @param bool $inline_dep DEPRECATED. If true, the result is printed inline. If false, is
* written to a file and linked to. In fact, $inline = true also causes
* a cache file to be written for speed purposes
* @return string The javascript tags to be written to the page
*/
public static function render($group = false, $inline_dep = null, $attr = array())
{
$r = static::render_css($group, $inline_dep, $attr);
$r.= static::render_js($group, $inline_dep, $attr);
return $r;
}
/**
* Renders the specific javascript group, or all groups if no group specified.
*
* @param string $group Which group to render. If omitted renders all groups
* @param array $options An array of options to use. Only supported option is 'gen_tags'
* @param bool $inline_dep DEPRECATED. If true, the result is printed inline. If false, is
* written to a file and linked to. In fact, $inline = true also causes
* a cache file to be written for speed purposes
* @return string The javascript tags to be written to the page
*/
public static function render_js($group = false, $options = array(), $attr_dep = array())
{
// Don't force the user to remember that false is used for ommitted non-bool arguments
if (!is_string($group))
$group = false;
if (!is_array($attr_dep))
$attr_dep = array();
// Backwards compat: We used to have $inline where $options currently sits
// So, if $options isn't an array, then assume they're still passing $inline
$inline_dep = null;
if (!is_array($options))
{
$inline_dep = $options;
$options = array();
}
$options += array(
'gen_tags' => true,
);
$file_groups = static::files_to_render('js', $group);
// If not generating tags, return an array of content
$ret = ($options['gen_tags']) ? '' : array();
foreach ($file_groups as $group_name => $file_group)
{
// We used to take $inline as 2nd argument. However, we now use a group option.
// It's easiest if we let $inline override this group option, though.
$inline = ($inline_dep === null) ? static::$groups['js'][$group_name]['inline'] : $inline_dep;
// $attr is also deprecated. If specified, entirely overrides the group option.
$attr = (!count($attr_dep)) ? static::$groups['js'][$group_name]['attr'] : $attr_dep;
// the type attribute is not required for script elements under html5
// @link http://www.w3.org/TR/html5/scripting-1.html#attr-script-type
if (!\Html::$html5)
$attr = array( 'type' => 'text/javascript' ) + $attr;
if (static::$groups['js'][$group_name]['combine'])
{
$filename = static::combine('js', $file_group, static::$groups['js'][$group_name]['min'], $inline);
if (!$inline && static::$show_files && $options['gen_tags'])
{
$ret .= '<!--'.PHP_EOL.'Group: '.$group_name.PHP_EOL.implode('', array_map(function($a){
return "\t".$a['file'].PHP_EOL;
}, $file_group)).'-->'.PHP_EOL;
}
if ($inline)
{
$content = file_get_contents(DOCROOT.static::$cache_path.$filename);
if ($options['gen_tags'])
$ret .= html_tag('script', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
else
$ret[] = $content;
}
else
{
$filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'js');
if ($options['gen_tags'])
$ret .= html_tag('script', array('src' => $filepath,)+$attr, '').PHP_EOL;
else
$ret[] = $filepath;
}
}
else
{
foreach ($file_group as $file)
{
if ($inline)
{
$content = static::load_file($file['file'], 'js');
if ($options['gen_tags'])
$ret .= html_tag('script', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
else
$ret[] = $content;
}
else
{
$remote = (strpos($file['file'], '//') !== false);
$base = ($remote) ? '' : static::$asset_url;
$filepath = $base.static::process_filepath($file['file'], 'js', $remote);
if ($options['gen_tags'])
$ret .= html_tag('script', array('src' => $filepath,)+$attr, '').PHP_EOL;
else
$ret[] = $filepath;
}
}
}
}
return $ret;
}
/**
* Renders the specific css group, or all groups if no group specified.
*
* @param string $group Which group to render. If omitted renders all groups
* @param array $options ANnarray of options to use. Only supported option is 'gen_tags'
* @return string The css tags to be written to the page
*/
public static function render_css($group = false, $options = array(), $attr_dep = array())
{
// Don't force the user to remember that false is used for ommitted non-bool arguments
if (!is_string($group))
$group = false;
if (!is_array($attr_dep))
$attr_dep = array();
// Backwards compat: We used to have $inline where $options currently sits
// So, if $options isn't an array, then assume they're still passing $inline
$inline_dep = null;
if (!is_array($options))
{
$inline_dep = $options;
$options = array();
}
$options += array(
'gen_tags' => true,
);
$file_groups = static::files_to_render('css', $group);
// If not generating tags, return an array of content
$ret = ($options['gen_tags']) ? '' : array();
foreach ($file_groups as $group_name => $file_group)
{
// We used to take $inline as 2nd argument. However, we now use a group option.
// It's easiest if we let $inline override this group option, though.
$inline = ($inline_dep === null) ? static::$groups['css'][$group_name]['inline'] : $inline_dep;
// $attr is also deprecated. If specified, entirely overrides the group option.
$attr = (!count($attr_dep)) ? static::$groups['css'][$group_name]['attr'] : $attr_dep;
// the type attribute is not required for style or link[rel="stylesheet"] elements under html5
// @link http://www.w3.org/TR/html5/links.html#link-type-stylesheet
// @link http://www.w3.org/TR/html5/semantics.html#attr-style-type
if (!\Html::$html5)
$attr = array( 'type' => 'text/css' ) + $attr;
if (static::$groups['css'][$group_name]['combine'])
{
$filename = static::combine('css', $file_group, static::$groups['css'][$group_name]['min'], $inline);
if (!$inline && static::$show_files && $options['gen_tags'])
{
$ret .= '<!--'.PHP_EOL.'Group: '.$group_name.PHP_EOL.implode('', array_map(function($a){
return "\t".$a['file'].PHP_EOL;
}, $file_group)).'-->'.PHP_EOL;
}
if ($inline)
{
$content = file_get_contents(DOCROOT.static::$cache_path.$filename);
// We'll need to fix the uris, unless they were rewritten absolutely to start with
$content = static::css_rewrite_uris($content, static::$cache_path.$filename, \Uri::string());
if ($options['gen_tags'])
$ret .= html_tag('style', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
else
$ret[] = $content;
}
else
{
$filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'css');
if ($options['gen_tags'])
$ret .= html_tag('link', array('rel' => 'stylesheet', 'href' => $filepath)+$attr).PHP_EOL;
else
$ret[] = $filepath;
}
}
else
{
foreach ($file_group as $file)
{
if ($inline)
{
$content = static::load_file($file['file'], 'css', false, \Uri::string());
if ($options['gen_tags'])
$ret .= html_tag('style', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
else
$ret[] = $content;
}
else
{
$remote = (strpos($file['file'], '//') !== false);
$base = ($remote) ? '' : static::$asset_url;
$filepath = $base.static::process_filepath($file['file'], 'css', $remote);
if ($options['gen_tags'])
$ret .= html_tag('link', array('rel' => 'stylesheet', 'href' => $filepath)+$attr).PHP_EOL;
else
$ret[] = $filepath;
}
}
}
}
return $ret;
}
/**
* Figures out where a file should be, based on its namespace and type.
*
* @param string $file The name of the asset to search for
* @param string $asset_type 'css', 'js' or 'img'
* @return string The path to the asset, relative to $asset_url
*/
protected static function find_files($file, $asset_type)
{
$parts = explode('::', $file, 2);
if (!array_key_exists($parts[0], static::$asset_paths))
throw new Casset_Exception("Could not find namespace {$parts[0]}");
$path = static::$asset_paths[$parts[0]]['path'];
$file = $parts[1];
$folder = $file[0] == '/' ? '' : static::$asset_paths[$parts[0]]['dirs'][$asset_type];
$file = ltrim($file, '/');
$remote = (strpos($path, '//') !== false);
if ($remote)
{
// Glob doesn't work on remote locations, so just assume they
// specified a file, not a glob pattern.
// Don't look for the file now either. That'll be done by
// file_get_contents later on, if need be.
return array($path.$folder.$file);
}
else
{
$glob_files = glob($path.$folder.$file);
if (!$glob_files || !count($glob_files))
throw new Casset_Exception("Found no files matching $path$folder$file");
return $glob_files;
}
}
/**
* Given a list of group names, adds to that list, in the appropriate places,
* and groups which are listed as dependencies of those group.
* Duplicate group names are not a problem, as a group is disabled when it's
* rendered.
*
* @param string $type 'js' /or/ 'css'
* @param array $group_names Array of group names to check
* @param int $depth Used by this function to check for potentially infinite recursion
* @return array List of group names with deps resolved
*/
protected static function resolve_deps($type, $group_names, $depth=0)
{
if ($depth > static::$deps_max_depth)
{
throw new Casset_Exception("Reached depth $depth trying to resolve dependencies. ".
"You've probably got some circular ones involving ".implode(',', $group_names).". ".
"If not, adjust the config key deps_max_depth.");
}
// Insert the dep just before what it's a dep for
foreach ($group_names as $i => $group_name)
{
// If the group's already been rendered, bottle
if (in_array($group_name, static::$rendered_groups[$type]))
continue;
// Don't pay attention to bottom-level groups which are disabled
if (!static::$groups[$type][$group_name]['enabled'] && $depth == 0)
continue;
// Otherwise, enable the group. Fairly obvious, as the whole point of
// deps is to render disabled groups
static::asset_enabled($type, $group_name, true);
if (count(static::$groups[$type][$group_name]['deps']))
{
array_splice($group_names, $i, 0, static::resolve_deps($type, static::$groups[$type][$group_name]['deps'], $depth+1));