Requirements.php 42.5 KB
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
<?php

/**
 * Requirements tracker for JavaScript and CSS.
 *
 * @package framework
 * @subpackage view
 */
class Requirements implements Flushable {

	/**
	 * Triggered early in the request when a flush is requested
	 */
	public static function flush() {
		self::delete_all_combined_files();
	}

	/**
	 * Enable combining of css/javascript files.
	 * 
	 * @param bool $enable
	 */
	public static function set_combined_files_enabled($enable) {
		self::backend()->set_combined_files_enabled($enable);
	}

	/**
	 * Checks whether combining of css/javascript files is enabled.
	 * 
	 * @return bool
	 */
	public static function get_combined_files_enabled() {
		return self::backend()->get_combined_files_enabled();
	}

	/**
	 * Set the relative folder e.g. 'assets' for where to store combined files
	 * 
	 * @param string $folder Path to folder
	 */
	public static function set_combined_files_folder($folder) {
		self::backend()->setCombinedFilesFolder($folder);
	}

	/**
	 * Set whether to add caching query params to the requests for file-based requirements. 
	 * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by
	 * filemtime. This has the benefit of allowing the browser to cache the URL infinitely,
	 * while automatically busting this cache every time the file is changed.
	 *
	 * @param bool
	 */
	public static function set_suffix_requirements($var) {
		self::backend()->set_suffix_requirements($var);
	}

	/**
	 * Check whether we want to suffix requirements
	 *
	 * @return bool
	 */
	public static function get_suffix_requirements() {
		return self::backend()->get_suffix_requirements();
	}

	/**
	 * Instance of the requirements for storage. You can create your own backend to change the 
	 * default JS and CSS inclusion behaviour. 
	 *
	 * @var Requirements_Backend
	 */
	private static $backend = null;

	public static function backend() {
		if(!self::$backend) {
			self::$backend = new Requirements_Backend();
		}
		return self::$backend;
	}

	/**
	 * Setter method for changing the Requirements backend
	 *
	 * @param Requirements_Backend $backend
	 */
	public static function set_backend(Requirements_Backend $backend) {
		self::$backend = $backend;
	}

	/**
	 * Register the given JavaScript file as required.
	 * 
	 * @param string $file Relative to docroot
	 */
	public static function javascript($file) {
		self::backend()->javascript($file);
	}

	/**
	 * Register the given JavaScript code into the list of requirements
	 *
	 * @param string     $script       The script content as a string (without enclosing <script> tag)
	 * @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public static function customScript($script, $uniquenessID = null) {
		self::backend()->customScript($script, $uniquenessID);
	}

	/**
	 * Return all registered custom scripts
	 *
	 * @return array
	 */
	public static function get_custom_scripts() {
		return self::backend()->get_custom_scripts();
	}

	/**
	 * Register the given CSS styles into the list of requirements 
	 *
	 * @param string     $script       CSS selectors as a string (without enclosing <style> tag)
	 * @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public static function customCSS($script, $uniquenessID = null) {
		self::backend()->customCSS($script, $uniquenessID);
	}

	/**
	 * Add the following custom HTML code to the <head> section of the page
	 *
	 * @param string     $html         Custom HTML code
	 * @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public static function insertHeadTags($html, $uniquenessID = null) {
		self::backend()->insertHeadTags($html, $uniquenessID);
	}

	/**
	 * Include the content of the given JavaScript file in the list of requirements. Dollar-sign
	 * variables will be interpolated with values from $vars similar to a .ss template.
	 *
	 * @param string         $file         The template file to load, relative to docroot
	 * @param string[]|int[] $vars         The array of variables to interpolate.
	 * @param string|int     $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public static function javascriptTemplate($file, $vars, $uniquenessID = null) {
		self::backend()->javascriptTemplate($file, $vars, $uniquenessID);
	}

	/**
	 * Register the given stylesheet into the list of requirements.
	 *
	 * @param string $file  The CSS file to load, relative to site root
	 * @param string $media Comma-separated list of media types to use in the link tag 
	 *                      (e.g. 'screen,projector')
	 */
	public static function css($file, $media = null) {
		self::backend()->css($file, $media);
	}

	/**
	 * Registers the given themeable stylesheet as required.
	 *
	 * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for,
	 * and it that doesn't exist and the module parameter is set then a CSS file with that name in 
	 * the module is used.
	 *
	 * @param string $name   The name of the file - eg '/css/File.css' would have the name 'File'
	 * @param string $module The module to fall back to if the css file does not exist in the 
	 *                       current theme.
	 * @param string $media  Comma-separated list of media types to use in the link tag 
	 *                       (e.g. 'screen,projector')
	 */
	public static function themedCSS($name, $module = null, $media = null) {
		return self::backend()->themedCSS($name, $module, $media);
	}

	/**
	 * Clear either a single or all requirements
	 * 
	 * Caution: Clearing single rules added via customCSS and customScript only works if you 
	 * originally specified a $uniquenessID.
	 *
	 * @param string|int $fileOrID
	 */
	public static function clear($fileOrID = null) {
		self::backend()->clear($fileOrID);
	}

	/**
	 * Restore requirements cleared by call to Requirements::clear
	 */
	public static function restore() {
		self::backend()->restore();
	}

	/**
	 * Block inclusion of a specific file 
	 * 
	 * The difference between this and {@link clear} is that the calling order does not matter; 
	 * {@link clear} must be called after the initial registration, whereas {@link block} can be
	 * used in advance. This is useful, for example, to block scripts included by a superclass 
	 * without having to override entire functions and duplicate a lot of code.
	 *
	 * Note that blocking should be used sparingly because it's hard to trace where an file is
	 * being blocked from.
	 *
	 * @param string|int $fileOrID
	 */
	public static function block($fileOrID) {
		self::backend()->block($fileOrID);
	}
	
	/**
	 * Remove an item from the block list
	 *
	 * @param string|int $fileOrID
	 */
	public static function unblock($fileOrID) {
		self::backend()->unblock($fileOrID);
	}

	/**
	 * Removes all items from the block list
	 */
	public static function unblock_all() {
		self::backend()->unblock_all();
	}

	/**
	 * Update the given HTML content with the appropriate include tags for the registered
	 * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter,
	 * including a head and body tag.
	 *
	 * @param string $templateFile No longer used, only retained for compatibility
	 * @param string $content      HTML content that has already been parsed from the $templateFile
	 *                             through {@link SSViewer}
	 * @return string HTML content augmented with the requirements tags
	 */
	public static function includeInHTML($templateFile, $content) {
		return self::backend()->includeInHTML($templateFile, $content);
	}

	/**
	 * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given 
	 * HTTP Response
	 * 
	 * @param SS_HTTPResponse $response
	 */
	public static function include_in_response(SS_HTTPResponse $response) {
		return self::backend()->include_in_response($response);
	}

	/**
	 * Add i18n files from the given javascript directory. SilverStripe expects that the given 
	 * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, 
	 * etc.
	 *
	 * @param string $langDir  The JavaScript lang directory, relative to the site root, e.g., 
	 *                         'framework/javascript/lang'
	 * @param bool   $return   Return all relative file paths rather than including them in 
	 *                         requirements
	 * @param bool   $langOnly Only include language files, not the base libraries
	 * 
	 * @return array
	 */
	public static function add_i18n_javascript($langDir, $return = false, $langOnly = false) {
		return self::backend()->add_i18n_javascript($langDir, $return, $langOnly);
	}

	/**
	 * Concatenate several css or javascript files into a single dynamically generated file. This
	 * increases performance by fewer HTTP requests.
	 *
	 * The combined file is regenerated based on every file modification time. Optionally a
	 * rebuild can be triggered by appending ?flush=1 to the URL. If all files to be combined are
	 * JavaScript, we use the external JSMin library to minify the JavaScript.
	 *
	 * All combined files will have a comment on the start of each concatenated file denoting their
	 * original position. For easier debugging, we only minify JavaScript if not in development
	 * mode ({@link Director::isDev()}).
	 *
	 * CAUTION: You're responsible for ensuring that the load order for combined files is
	 * retained - otherwise combining JavaScript files can lead to functional errors in the
	 * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also
	 * only include each file once across all includes and comibinations in a single page load.
	 *
	 * CAUTION: Combining CSS Files discards any "media" information.
	 *
	 * Example for combined JavaScript:
	 * <code>
	 * Requirements::combine_files(
	 *  'foobar.js',
	 *  array(
	 *        'mysite/javascript/foo.js',
	 *        'mysite/javascript/bar.js',
	 *    )
	 * );
	 * </code>
	 *
	 * Example for combined CSS:
	 * <code>
	 * Requirements::combine_files(
	 *  'foobar.css',
	 *    array(
	 *        'mysite/javascript/foo.css',
	 *        'mysite/javascript/bar.css',
	 *    )
	 * );
	 * </code>
	 *
	 * @param string $combinedFileName Filename of the combined file relative to docroot
	 * @param array  $files            Array of filenames relative to docroot
	 * @param string $media
	 *
	 * @return bool|void
	 */
	public static function combine_files($combinedFileName, $files, $media = null) {
		self::backend()->combine_files($combinedFileName, $files, $media);
	}

	/**
	 * Return all combined files; keys are the combined file names, values are lists of 
	 * files being combined.
	 *
	 * @return array
	 */
	public static function get_combine_files() {
		return self::backend()->get_combine_files();
	}

	/**
	 * Delete all dynamically generated combined files from the filesystem
	 *
	 * @param string $combinedFileName If left blank, all combined files are deleted.
	 */
	public static function delete_combined_files($combinedFileName = null) {
		return self::backend()->delete_combined_files($combinedFileName);
	}

	/**
	 * Deletes all generated combined files in the configured combined files directory,
	 * but doesn't delete the directory itself
	 */
	public static function delete_all_combined_files() {
		return self::backend()->delete_all_combined_files();
	}

	/**
	 * Re-sets the combined files definition. See {@link Requirements_Backend::clear_combined_files()}
	 */
	public static function clear_combined_files() {
		self::backend()->clear_combined_files();
	}

	/**
	 * Do the heavy lifting involved in combining (and, in the case of JavaScript minifying) the
	 * combined files. 
 	 */
	public static function process_combined_files() {
		return self::backend()->process_combined_files();
	}

	/**
	 * Set whether you want to write the JS to the body of the page rather than at the end of the
	 * head tag.
	 *
	 * @param bool
	 */
	public static function set_write_js_to_body($var) {
		self::backend()->set_write_js_to_body($var);
	}

	/**
	 * Set whether to force the JavaScript to end of the body. Useful if you use inline script tags
	 * that don't rely on scripts included via {@link Requirements::javascript()).
	 *
	 * @param boolean $var If true, force the JavaScript to be included at the bottom of the page
	 */
	public static function set_force_js_to_bottom($var) {
		self::backend()->set_force_js_to_bottom($var);
	}

	/**
	 * Output debugging information
	 */
	public static function debug() {
		return self::backend()->debug();
	}

}

/**
 * @package framework
 * @subpackage view
 */
class Requirements_Backend {

	/**
	 * Whether to add caching query params to the requests for file-based requirements. 
	 * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by
	 * filemtime. This has the benefit of allowing the browser to cache the URL infinitely,
	 * while automatically busting this cache every time the file is changed.
	 * 
	 * @var bool
	 */
	protected $suffix_requirements = true;

	/**
	 * Whether to combine CSS and JavaScript files
	 *
	 * @var bool
	 */
	protected $combined_files_enabled = true;

	/**
	 * Paths to all required JavaScript files relative to docroot
	 *
	 * @var array $javascript
	 */
	protected $javascript = array();

	/**
	 * Paths to all required CSS files relative to the docroot.
	 *
	 * @var array $css
	 */
	protected $css = array();

	/**
	 * All custom javascript code that is inserted into the page's HTML
	 *
	 * @var array $customScript
	 */
	protected $customScript = array();

	/**
	 * All custom CSS rules which are inserted directly at the bottom of the HTML <head> tag
	 *
	 * @var array $customCSS
	 */
	protected $customCSS = array();

	/**
	 * All custom HTML markup which is added before the closing <head> tag, e.g. additional 
	 * metatags.
	 */
	protected $customHeadTags = array();

	/**
	 * Remembers the file paths or uniquenessIDs of all Requirements cleared through 
	 * {@link clear()}, so that they can be restored later.
	 *
	 * @var array $disabled
	 */
	protected $disabled = array();

	/**
	 * The file paths (relative to docroot) or uniquenessIDs of any included requirements which
	 * should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example, 
	 * to block scripts included by a superclass without having to override entire functions and
	 * duplicate a lot of code.
	 * 
	 * Use {@link unblock()} or {@link unblock_all()} to revert changes.
	 *
	 * @var array $blocked
	 */
	protected $blocked = array();

	/**
	 * A list of combined files registered via {@link combine_files()}. Keys are the output file
	 * names, values are lists of input files.
	 *
	 * @var array $combine_files
	 */
	public $combine_files = array();

	/**
	 * Use the JSMin library to minify any javascript file passed to {@link combine_files()}.
	 *
	 * @var bool
	 */
	public $combine_js_with_jsmin = true;

	/**
	 * Whether or not file headers should be written when combining files
	 *
	 * @var boolean
	 */
	public $write_header_comment = true;
	
	/**
	 * Where to save combined files. By default they're placed in assets/_combinedfiles, however
	 * this may be an issue depending on your setup, especially for CSS files which often contain
	 * relative paths. 
	 * 
	 * @var string
	 */
	protected $combinedFilesFolder = null;

	/**
	 * Put all JavaScript includes at the bottom of the template before the closing <body> tag,
	 * rather than the default behaviour of placing them at the end of the <head> tag. This means 
	 * script downloads won't block other HTTP requests, which can be a performance improvement.
	 * 
	 * @var bool
	 */
	public $write_js_to_body = true;

	/**
	 * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already
	 *
	 * @var boolean
	 */
	protected $force_js_to_bottom = false;

	/**
	 * Enable or disable the combination of CSS and JavaScript files
	 * 
	 * @param $enable
	 */
	public function set_combined_files_enabled($enable) {
		$this->combined_files_enabled = (bool) $enable;
	}

	/**
	 * Check whether file combination is enabled.
	 * 
	 * @return bool
	 */
	public function get_combined_files_enabled() {
		return $this->combined_files_enabled;
	}

	/**
	 * Set the folder to save combined files in. By default they're placed in assets/_combinedfiles,
	 * however this may be an issue depending on your setup, especially for CSS files which often 
	 * contain relative paths.
	 * 
	 * @param string $folder
	 */
	public function setCombinedFilesFolder($folder) {
		$this->combinedFilesFolder = $folder;
	}

	/**
	 * @return string Folder relative to the webroot
	 */
	public function getCombinedFilesFolder() {
		return ($this->combinedFilesFolder) ? $this->combinedFilesFolder : ASSETS_DIR . '/_combinedfiles';
	}

	/**
	 * Set whether to add caching query params to the requests for file-based requirements.
	 * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by
	 * filemtime. This has the benefit of allowing the browser to cache the URL infinitely,
	 * while automatically busting this cache every time the file is changed.
	 *
	 * @param bool
	 */
	public function set_suffix_requirements($var) {
		$this->suffix_requirements = $var;
	}

	/**
	 * Check whether we want to suffix requirements
	 *
	 * @return bool
	 */
	public function get_suffix_requirements() {
		return $this->suffix_requirements;
	}

	/**
	 * Set whether you want to write the JS to the body of the page rather than at the end of the
	 * head tag.
	 *
	 * @param bool
	 */
	public function set_write_js_to_body($var) {
		$this->write_js_to_body = $var;
	}

	/**
	 * Forces the JavaScript requirements to the end of the body, right before the closing tag
	 *
	 * @param bool
	 */
	public function set_force_js_to_bottom($var) {
		$this->force_js_to_bottom = $var;
	}
	
	/**
	 * Register the given JavaScript file as required.
	 *
	 * @param string $file Relative to docroot
	 */
	public function javascript($file) {
		$this->javascript[$file] = true;
	}

	/**
	 * Returns an array of all required JavaScript
	 *
	 * @return array
	 */
	public function get_javascript() {
		return array_keys(array_diff_key($this->javascript, $this->blocked));
	}

	/**
	 * Register the given JavaScript code into the list of requirements
	 *
	 * @param string     $script       The script content as a string (without enclosing <script> tag)
	 * @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public function customScript($script, $uniquenessID = null) {
		if($uniquenessID) $this->customScript[$uniquenessID] = $script;
		else $this->customScript[] = $script;

		$script .= "\n";
	}

	/**
	 * Return all registered custom scripts
	 *
	 * @return array
	 */
	public function get_custom_scripts() {
		$requirements = "";

		if($this->customScript) {
			foreach($this->customScript as $script) {
				$requirements .= "$script\n";
			}
		}

		return $requirements;
	}

	/**
	 * Register the given CSS styles into the list of requirements
	 *
	 * @param string     $script       CSS selectors as a string (without enclosing <style> tag)
	 * @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public function customCSS($script, $uniquenessID = null) {
		if($uniquenessID) $this->customCSS[$uniquenessID] = $script;
		else $this->customCSS[] = $script;
	}

	/**
	 * Add the following custom HTML code to the <head> section of the page
	 *
	 * @param string     $html         Custom HTML code
	 * @param string|int $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public function insertHeadTags($html, $uniquenessID = null) {
		if($uniquenessID) $this->customHeadTags[$uniquenessID] = $html;
		else $this->customHeadTags[] = $html;
	}

	/**
	 * Include the content of the given JavaScript file in the list of requirements. Dollar-sign
	 * variables will be interpolated with values from $vars similar to a .ss template.
	 *
	 * @param string         $file         The template file to load, relative to docroot
	 * @param string[]|int[] $vars         The array of variables to interpolate.
	 * @param string|int     $uniquenessID A unique ID that ensures a piece of code is only added once
	 */
	public function javascriptTemplate($file, $vars, $uniquenessID = null) {
		$script = file_get_contents(Director::getAbsFile($file));
		$search = array();
		$replace = array();

		if($vars) foreach($vars as $k => $v) {
			$search[] = '$' . $k;
			$replace[] = str_replace("\\'","'", Convert::raw2js($v));
		}

		$script = str_replace($search, $replace, $script);
		$this->customScript($script, $uniquenessID);
	}

	/**
	 * Register the given stylesheet into the list of requirements.
	 *
	 * @param string $file  The CSS file to load, relative to site root
	 * @param string $media Comma-separated list of media types to use in the link tag
	 *                      (e.g. 'screen,projector')
	 */
	public function css($file, $media = null) {
		$this->css[$file] = array(
			"media" => $media
		);
	}

	/**
	 * Get the list of registered CSS file requirements, excluding blocked files
	 * 
	 * @return array
	 */
	public function get_css() {
		return array_diff_key($this->css, $this->blocked);
	}

	/**
	 * Clear either a single or all requirements
	 *
	 * Caution: Clearing single rules added via customCSS and customScript only works if you
	 * originally specified a $uniquenessID.
	 *
	 * @param string|int $fileOrID
	 */
	public function clear($fileOrID = null) {
		if($fileOrID) {
			foreach(array('javascript','css', 'customScript', 'customCSS', 'customHeadTags') as $type) {
				if(isset($this->{$type}[$fileOrID])) {
					$this->disabled[$type][$fileOrID] = $this->{$type}[$fileOrID];
					unset($this->{$type}[$fileOrID]);
				}
			}
		} else {
			$this->disabled['javascript'] = $this->javascript;
			$this->disabled['css'] = $this->css;
			$this->disabled['customScript'] = $this->customScript;
			$this->disabled['customCSS'] = $this->customCSS;
			$this->disabled['customHeadTags'] = $this->customHeadTags;

			$this->javascript = array();
			$this->css = array();
			$this->customScript = array();
			$this->customCSS = array();
			$this->customHeadTags = array();
		}
	}

	/**
	 * Restore requirements cleared by call to Requirements::clear
	 */
	public function restore() {
		$this->javascript = $this->disabled['javascript'];
		$this->css = $this->disabled['css'];
		$this->customScript = $this->disabled['customScript'];
		$this->customCSS = $this->disabled['customCSS'];
		$this->customHeadTags = $this->disabled['customHeadTags'];
	}
	/**
	 * Block inclusion of a specific file
	 *
	 * The difference between this and {@link clear} is that the calling order does not matter;
	 * {@link clear} must be called after the initial registration, whereas {@link block} can be
	 * used in advance. This is useful, for example, to block scripts included by a superclass
	 * without having to override entire functions and duplicate a lot of code.
	 *
	 * Note that blocking should be used sparingly because it's hard to trace where an file is
	 * being blocked from.
	 *
	 * @param string|int $fileOrID
	 */
	public function block($fileOrID) {
		$this->blocked[$fileOrID] = $fileOrID;
	}

	/**
	 * Remove an item from the block list
	 *
	 * @param string|int $fileOrID
	 */
	public function unblock($fileOrID) {
		if(isset($this->blocked[$fileOrID])) unset($this->blocked[$fileOrID]);
	}

	/**
	 * Removes all items from the block list
	 */
	public function unblock_all() {
		$this->blocked = array();
	}

	/**
	 * Update the given HTML content with the appropriate include tags for the registered
	 * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter,
	 * including a head and body tag.
	 *
	 * @param string $templateFile No longer used, only retained for compatibility
	 * @param string $content      HTML content that has already been parsed from the $templateFile
	 *                             through {@link SSViewer}
	 * @return string HTML content augmented with the requirements tags
	 */
	public function includeInHTML($templateFile, $content) {
		if(
			(strpos($content, '</head>') !== false || strpos($content, '</head ') !== false)
			&& ($this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags)
		) {
			$requirements = '';
			$jsRequirements = '';

			// Combine files - updates $this->javascript and $this->css
			$this->process_combined_files();

			foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
				$path = Convert::raw2xml($this->path_for_file($file));
				if($path) {
					$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
				}
			}

			// Add all inline JavaScript *after* including external files they might rely on
			if($this->customScript) {
				foreach(array_diff_key($this->customScript,$this->blocked) as $script) {
					$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n";
					$jsRequirements .= "$script\n";
					$jsRequirements .= "\n//]]>\n</script>\n";
				}
			}

			foreach(array_diff_key($this->css,$this->blocked) as $file => $params) {
				$path = Convert::raw2xml($this->path_for_file($file));
				if($path) {
					$media = (isset($params['media']) && !empty($params['media']))
						? " media=\"{$params['media']}\"" : "";
					$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n";
				}
			}

			foreach(array_diff_key($this->customCSS, $this->blocked) as $css) {
				$requirements .= "<style type=\"text/css\">\n$css\n</style>\n";
			}

			foreach(array_diff_key($this->customHeadTags,$this->blocked) as $customHeadTag) {
				$requirements .= "$customHeadTag\n";
			}

			if ($this->force_js_to_bottom) {
				// Remove all newlines from code to preserve layout
				$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);

				// Forcefully put the scripts at the bottom of the body instead of before the first
				// script tag.
				$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
				
				// Put CSS at the bottom of the head
				$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);				
			} elseif($this->write_js_to_body) {
				// Remove all newlines from code to preserve layout
				$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
				
				// If your template already has script tags in the body, then we try to put our script
				// tags just before those. Otherwise, we put it at the bottom.
				$p2 = stripos($content, '<body');
				$p1 = stripos($content, '<script', $p2);
				
				$commentTags = array();
				$canWriteToBody = ($p1 !== false)
					&&
					// Check that the script tag is not inside a html comment tag
					!(
						preg_match('/.*(?|(<!--)|(-->))/U', $content, $commentTags, 0, $p1)
						&& 
						$commentTags[1] == '-->'
					);

				if($canWriteToBody) {
					$content = substr($content,0,$p1) . $jsRequirements . substr($content,$p1);
				} else {
					$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
				}

				// Put CSS at the bottom of the head
				$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
			} else {
				$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content);
				$content = preg_replace("/(<\/head>)/i", $jsRequirements . "\\1", $content);
			}
		}

		return $content;
	}

	/**
	 * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given
	 * HTTP Response
	 *
	 * @param SS_HTTPResponse $response
	 */
	public function include_in_response(SS_HTTPResponse $response) {
		$this->process_combined_files();
		$jsRequirements = array();
		$cssRequirements = array();

		foreach(array_diff_key($this->javascript, $this->blocked) as $file => $dummy) {
			$path = $this->path_for_file($file);
			if($path) {
				$jsRequirements[] = str_replace(',', '%2C', $path);
			}
		}

		$response->addHeader('X-Include-JS', implode(',', $jsRequirements));

		foreach(array_diff_key($this->css,$this->blocked) as $file => $params) {
			$path = $this->path_for_file($file);
			if($path) {
				$path = str_replace(',', '%2C', $path);
				$cssRequirements[] = isset($params['media']) ? "$path:##:$params[media]" : $path;
			}
		}

		$response->addHeader('X-Include-CSS', implode(',', $cssRequirements));
	}

	/**
	 * Add i18n files from the given javascript directory. SilverStripe expects that the given
	 * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js,
	 * etc.
	 *
	 * @param string $langDir  The JavaScript lang directory, relative to the site root, e.g.,
	 *                         'framework/javascript/lang'
	 * @param bool   $return   Return all relative file paths rather than including them in
	 *                         requirements
	 * @param bool   $langOnly Only include language files, not the base libraries
	 *
	 * @return array
	 */
	public function add_i18n_javascript($langDir, $return = false, $langOnly = false) {
		$files = array();
		$base = Director::baseFolder() . '/';
		if(i18n::config()->js_i18n) {
			// Include i18n.js even if no languages are found.  The fact that
			// add_i18n_javascript() was called indicates that the methods in
			// here are needed.
			if(!$langOnly) $files[] = FRAMEWORK_DIR . '/javascript/i18n.js';

			if(substr($langDir,-1) != '/') $langDir .= '/';

			$candidates = array(
				'en.js',
				'en_US.js',
				i18n::get_lang_from_locale(i18n::default_locale()) . '.js',
				i18n::default_locale() . '.js',
				i18n::get_lang_from_locale(i18n::get_locale()) . '.js',
				i18n::get_locale() . '.js',
			);
			foreach($candidates as $candidate) {
				if(file_exists($base . DIRECTORY_SEPARATOR . $langDir . $candidate)) {
					$files[] = $langDir . $candidate;
				}
			}
		} else {
			// Stub i18n implementation for when i18n is disabled.
			if(!$langOnly) $files[] = FRAMEWORK_DIR . '/javascript/i18nx.js';
		}

		if($return) {
			return $files;
		} else {
			foreach($files as $file) $this->javascript($file);
		}
	}

	/**
	 * Finds the path for specified file
	 *
	 * @param string $fileOrUrl
	 * @return string|bool
	 */
	protected function path_for_file($fileOrUrl) {
		if(preg_match('{^//|http[s]?}', $fileOrUrl)) {
			return $fileOrUrl;
		} elseif(Director::fileExists($fileOrUrl)) {
			$filePath = preg_replace('/\?.*/', '', Director::baseFolder() . '/' . $fileOrUrl);
			$prefix = Director::baseURL();
			$mtimesuffix = "";
			$suffix = '';
			if($this->suffix_requirements) {
				$mtimesuffix = "?m=" . filemtime($filePath);
				$suffix = '&';
			}
			if(strpos($fileOrUrl, '?') !== false) {
				if (strlen($suffix) == 0) {
					$suffix = '?';
				}
				$suffix .= substr($fileOrUrl, strpos($fileOrUrl, '?')+1);
				$fileOrUrl = substr($fileOrUrl, 0, strpos($fileOrUrl, '?'));
			} else {
				$suffix = '';
			}
			return "{$prefix}{$fileOrUrl}{$mtimesuffix}{$suffix}";
		} else {
			return false;
		}
	}

	/**
	 * Concatenate several css or javascript files into a single dynamically generated file. This 
	 * increases performance by fewer HTTP requests.
	 *
	 * The combined file is regenerated based on every file modification time. Optionally a 
	 * rebuild can be triggered by appending ?flush=1 to the URL. If all files to be combined are 
	 * JavaScript, we use the external JSMin library to minify the JavaScript. This can be 
	 * controlled using {@link $combine_js_with_jsmin}.
	 *
	 * All combined files will have a comment on the start of each concatenated file denoting their 
	 * original position. For easier debugging, we only minify JavaScript if not in development
	 * mode ({@link Director::isDev()}).
	 *
	 * CAUTION: You're responsible for ensuring that the load order for combined files is 
	 * retained - otherwise combining JavaScript files can lead to functional errors in the 
	 * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also
	 * only include each file once across all includes and combinations in a single page load.
	 *
	 * CAUTION: Combining CSS Files discards any "media" information.
	 *
	 * Example for combined JavaScript:
	 * <code>
	 * Requirements::combine_files(
	 *  'foobar.js',
	 *  array(
	 *        'mysite/javascript/foo.js',
	 *        'mysite/javascript/bar.js',
	 *    )
	 * );
	 * </code>
	 *
	 * Example for combined CSS:
	 * <code>
	 * Requirements::combine_files(
	 *  'foobar.css',
	 *    array(
	 *        'mysite/javascript/foo.css',
	 *        'mysite/javascript/bar.css',
	 *    )
	 * );
	 * </code>
	 *
	 * @param string $combinedFileName Filename of the combined file relative to docroot
	 * @param array  $files            Array of filenames relative to docroot
	 * @param string $media
	 * 
	 * @return bool|void
	 */
	public function combine_files($combinedFileName, $files, $media = null) {
		// duplicate check
		foreach($this->combine_files as $_combinedFileName => $_files) {
			$duplicates = array_intersect($_files, $files);
			if($duplicates && $combinedFileName != $_combinedFileName) {
				user_error("Requirements_Backend::combine_files(): Already included files " . implode(',', $duplicates)
					. " in combined file '{$_combinedFileName}'", E_USER_NOTICE);
				return false;
			}
		}
		foreach($files as $index=>$file) {
			if(is_array($file)) {
				// Either associative array path=>path type=>type or numeric 0=>path 1=>type
				// Otherwise, assume path is the first item
				if (isset($file['type']) && in_array($file['type'], array('css', 'javascript', 'js'))) {
					switch ($file['type']) {
						case 'css':
							$this->css($file['path'], $media);
							break;
						default:
							$this->javascript($file['path']);
							break;
					}
					$files[$index] = $file['path'];
				} elseif (isset($file[1]) && in_array($file[1], array('css', 'javascript', 'js'))) {
					switch ($file[1]) {
						case 'css':
							$this->css($file[0], $media);
							break;
						default:
							$this->javascript($file[0]);
							break;
					}
					$files[$index] = $file[0];
				} else {
					$file = array_shift($file);
				}
			}
			if (!is_array($file)) {
				if(substr($file, -2) == 'js') {
					$this->javascript($file);
				} elseif(substr($file, -3) == 'css') {
					$this->css($file, $media);
				} else {
					user_error("Requirements_Backend::combine_files(): Couldn't guess file type for file '$file', "
						. "please specify by passing using an array instead.", E_USER_NOTICE);
				}
			}
		}
		$this->combine_files[$combinedFileName] = $files;
	}

	/**
	 * Return all combined files; keys are the combined file names, values are lists of
	 * files being combined.
	 *
	 * @return array
	 */
	public function get_combine_files() {
		return $this->combine_files;
	}

	/**
	 * Delete all dynamically generated combined files from the filesystem
	 *
	 * @param string $combinedFileName If left blank, all combined files are deleted.
	 */
	public function delete_combined_files($combinedFileName = null) {
		$combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files;
		$combinedFolder = ($this->getCombinedFilesFolder()) ?
			(Director::baseFolder() . '/' . $this->combinedFilesFolder) : Director::baseFolder();
		foreach($combinedFiles as $combinedFile => $sourceItems) {
			$filePath = $combinedFolder . '/' . $combinedFile;
			if(file_exists($filePath)) {
				unlink($filePath);
			}
		}
	}

	/**
	 * Deletes all generated combined files in the configured combined files directory,
	 * but doesn't delete the directory itself.
	 */
	public function delete_all_combined_files() {
		$combinedFolder = $this->getCombinedFilesFolder();
		if(!$combinedFolder) return false;

		$path = Director::baseFolder() . '/' . $combinedFolder;
		if(file_exists($path)) {
			Filesystem::removeFolder($path, true);
		}
	}

	/**
	 * Clear all registered CSS and JavaScript file combinations
	 */
	public function clear_combined_files() {
		$this->combine_files = array();
	}

	/**
	 * Do the heavy lifting involved in combining (and, in the case of JavaScript minifying) the
	 * combined files.
	 */
	public function process_combined_files() {
		// The class_exists call prevents us loading SapphireTest.php (slow) just to know that
		// SapphireTest isn't running :-)
		if(class_exists('SapphireTest', false)) $runningTest = SapphireTest::is_running_test();
		else $runningTest = false;

		if((Director::isDev() && !$runningTest && !isset($_REQUEST['combine'])) || !$this->combined_files_enabled) {
			return;
		}

		// Make a map of files that could be potentially combined
		$combinerCheck = array();
		foreach($this->combine_files as $combinedFile => $sourceItems) {
			foreach($sourceItems as $sourceItem) {
				if(isset($combinerCheck[$sourceItem]) && $combinerCheck[$sourceItem] != $combinedFile){
					user_error("Requirements_Backend::process_combined_files - file '$sourceItem' appears in two " .
						"combined files:" .	" '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING);
				}
				$combinerCheck[$sourceItem] = $combinedFile;

			}
		}

		// Work out the relative URL for the combined files from the base folder
		$combinedFilesFolder = ($this->getCombinedFilesFolder()) ? ($this->getCombinedFilesFolder() . '/') : '';

		// Figure out which ones apply to this request
		$combinedFiles = array();
		$newJSRequirements = array();
		$newCSSRequirements = array();
		foreach($this->javascript as $file => $dummy) {
			if(isset($combinerCheck[$file])) {
				$newJSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
				$combinedFiles[$combinerCheck[$file]] = true;
			} else {
				$newJSRequirements[$file] = true;
			}
		}

		foreach($this->css as $file => $params) {
			if(isset($combinerCheck[$file])) {
				// Inherit the parameters from the last file in the combine set.
				$newCSSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = $params;
				$combinedFiles[$combinerCheck[$file]] = true;
			} else {
				$newCSSRequirements[$file] = $params;
			}
		}

		// Process the combined files
		$base = Director::baseFolder() . '/';
		foreach(array_diff_key($combinedFiles, $this->blocked) as $combinedFile => $dummy) {
			$fileList = $this->combine_files[$combinedFile];
			$combinedFilePath = $base . $combinedFilesFolder . '/' . $combinedFile;


			// Make the folder if necessary
			if(!file_exists(dirname($combinedFilePath))) {
				Filesystem::makeFolder(dirname($combinedFilePath));
			}

			// If the file isn't writeable, don't even bother trying to make the combined file and return. The
			// files will be included individually instead. This is a complex test because is_writable fails
			// if the file doesn't exist yet.
			if((file_exists($combinedFilePath) && !is_writable($combinedFilePath))
				|| (!file_exists($combinedFilePath) && !is_writable(dirname($combinedFilePath)))
			) {
				user_error("Requirements_Backend::process_combined_files(): Couldn't create '$combinedFilePath'",
					E_USER_WARNING);
				return false;
			}

			// Determine if we need to build the combined include
			if(file_exists($combinedFilePath)) {
				// file exists, check modification date of every contained file
				$srcLastMod = 0;
				foreach($fileList as $file) {
					if(file_exists($base . $file)) {
						$srcLastMod = max(filemtime($base . $file), $srcLastMod);
					}
				}
				$refresh = $srcLastMod > filemtime($combinedFilePath);
			} else {
				// File doesn't exist, or refresh was explicitly required
				$refresh = true;
			}

			if(!$refresh) continue;

			$failedToMinify = false;
			$combinedData = "";
			foreach(array_diff($fileList, $this->blocked) as $file) {
				$fileContent = file_get_contents($base . $file);
				
				try{
					$fileContent = $this->minifyFile($file, $fileContent);
				}catch(Exception $e){
					$failedToMinify = true;
				}

				if ($this->write_header_comment) {
					// Write a header comment for each file for easier identification and debugging. The semicolon between each file is required for jQuery to be combined properly and protects against unterminated statements.
					$combinedData .= "/****** FILE: $file *****/\n";
				}

				$combinedData .= $fileContent . "\n";
			}

			$successfulWrite = false;
			$fh = fopen($combinedFilePath, 'wb');
			if($fh) {
				if(fwrite($fh, $combinedData) == strlen($combinedData)) $successfulWrite = true;
				fclose($fh);
				unset($fh);
			}
			
			if($failedToMinify){
				// Failed to minify, use unminified files instead. This warning is raised at the end to allow code execution
				// to complete in case this warning is caught inside a try-catch block. 
				user_error('Failed to minify '.$file.', exception: '.$e->getMessage(), E_USER_WARNING);
			}

			// Unsuccessful write - just include the regular JS files, rather than the combined one
			if(!$successfulWrite) {
				user_error("Requirements_Backend::process_combined_files(): Couldn't create '$combinedFilePath'",
					E_USER_WARNING);
				continue;
			}
		}

		// Note: Alters the original information, which means you can't call this method repeatedly - it will behave
		// differently on the subsequent calls
		$this->javascript = $newJSRequirements;
		$this->css = $newCSSRequirements;
	}

	/**
	 * Minify the given $content according to the file type indicated in $filename
	 * 
	 * @param string $filename
	 * @param string $content
	 * @return string
	 */
	protected function minifyFile($filename, $content) {
		// if we have a javascript file and jsmin is enabled, minify the content
		$isJS = stripos($filename, '.js');
		if($isJS && $this->combine_js_with_jsmin) {
			require_once('thirdparty/jsmin/jsmin.php');

			increase_time_limit_to();
			$content = JSMin::minify($content);
		}
		$content .= ($isJS ? ';' : '') . "\n";
		return $content;
	}

	/**
	 * Registers the given themeable stylesheet as required.
	 *
	 * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for,
	 * and it that doesn't exist and the module parameter is set then a CSS file with that name in
	 * the module is used.
	 *
	 * @param string $name   The name of the file - eg '/css/File.css' would have the name 'File'
	 * @param string $module The module to fall back to if the css file does not exist in the
	 *                       current theme.
	 * @param string $media  Comma-separated list of media types to use in the link tag
	 *                       (e.g. 'screen,projector')
	 */
	public function themedCSS($name, $module = null, $media = null) {
		$theme = SSViewer::get_theme_folder();
		$project = project();
		$absbase = BASE_PATH . DIRECTORY_SEPARATOR;
		$abstheme = $absbase . $theme;
		$absproject = $absbase . $project;
		$css = "/css/$name.css";
		
		if(file_exists($absproject . $css)) {
			$this->css($project . $css, $media);
		} elseif($module && file_exists($abstheme . '_' . $module.$css)) {
			$this->css($theme . '_' . $module . $css, $media);
		} elseif(file_exists($abstheme . $css)) {
			$this->css($theme . $css, $media);
		} elseif($module) {
			$this->css($module . $css, $media);
		}
	}

	/**
	 * Output debugging information.
	 */
	public function debug() {
		Debug::show($this->javascript);
		Debug::show($this->css);
		Debug::show($this->customCSS);
		Debug::show($this->customScript);
		Debug::show($this->customHeadTags);
		Debug::show($this->combine_files);
	}

}