Complex classes like Requirements_Backend often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Requirements_Backend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 494 | class Requirements_Backend |
||
| 495 | { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Whether to add caching query params to the requests for file-based requirements. |
||
| 499 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 500 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 501 | * while automatically busting this cache every time the file is changed. |
||
| 502 | * |
||
| 503 | * @var bool |
||
| 504 | */ |
||
| 505 | protected $suffixRequirements = true; |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Whether to combine CSS and JavaScript files |
||
| 509 | * |
||
| 510 | * @var bool |
||
| 511 | */ |
||
| 512 | protected $combinedFilesEnabled = true; |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Determine if files should be combined automatically on dev mode. |
||
| 516 | * |
||
| 517 | * By default combined files will not be combined except in test or |
||
| 518 | * live environments. Turning this on will allow for pre-combining of files in development mode. |
||
| 519 | * |
||
| 520 | * @config |
||
| 521 | * @var bool |
||
| 522 | */ |
||
| 523 | private static $combine_in_dev = false; |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Paths to all required JavaScript files relative to docroot |
||
| 527 | * |
||
| 528 | * @var array |
||
| 529 | */ |
||
| 530 | protected $javascript = array(); |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Map of included scripts to array of contained files. |
||
| 534 | * To be used alongside front-end combination mechanisms. |
||
| 535 | * |
||
| 536 | * @var array Map of providing filepath => array(provided filepaths) |
||
| 537 | */ |
||
| 538 | protected $providedJavascript = array(); |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Paths to all required CSS files relative to the docroot. |
||
| 542 | * |
||
| 543 | * @var array |
||
| 544 | */ |
||
| 545 | protected $css = array(); |
||
| 546 | |||
| 547 | /** |
||
| 548 | * All custom javascript code that is inserted into the page's HTML |
||
| 549 | * |
||
| 550 | * @var array |
||
| 551 | */ |
||
| 552 | protected $customScript = array(); |
||
| 553 | |||
| 554 | /** |
||
| 555 | * All custom CSS rules which are inserted directly at the bottom of the HTML <head> tag |
||
| 556 | * |
||
| 557 | * @var array |
||
| 558 | */ |
||
| 559 | protected $customCSS = array(); |
||
| 560 | |||
| 561 | /** |
||
| 562 | * All custom HTML markup which is added before the closing <head> tag, e.g. additional |
||
| 563 | * metatags. |
||
| 564 | * |
||
| 565 | * @var array |
||
| 566 | */ |
||
| 567 | protected $customHeadTags = array(); |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Remembers the file paths or uniquenessIDs of all Requirements cleared through |
||
| 571 | * {@link clear()}, so that they can be restored later. |
||
| 572 | * |
||
| 573 | * @var array |
||
| 574 | */ |
||
| 575 | protected $disabled = array(); |
||
| 576 | |||
| 577 | /** |
||
| 578 | * The file paths (relative to docroot) or uniquenessIDs of any included requirements which |
||
| 579 | * should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example, |
||
| 580 | * to block scripts included by a superclass without having to override entire functions and |
||
| 581 | * duplicate a lot of code. |
||
| 582 | * |
||
| 583 | * Use {@link unblock()} or {@link unblock_all()} to revert changes. |
||
| 584 | * |
||
| 585 | * @var array |
||
| 586 | */ |
||
| 587 | protected $blocked = array(); |
||
| 588 | |||
| 589 | /** |
||
| 590 | * A list of combined files registered via {@link combine_files()}. Keys are the output file |
||
| 591 | * names, values are lists of input files. |
||
| 592 | * |
||
| 593 | * @var array |
||
| 594 | */ |
||
| 595 | protected $combinedFiles = array(); |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Use the JSMin library to minify any javascript file passed to {@link combine_files()}. |
||
| 599 | * |
||
| 600 | * @var bool |
||
| 601 | */ |
||
| 602 | protected $minifyCombinedJSFiles = true; |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Whether or not file headers should be written when combining files |
||
| 606 | * |
||
| 607 | * @var boolean |
||
| 608 | */ |
||
| 609 | protected $writeHeaderComment = true; |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Where to save combined files. By default they're placed in assets/_combinedfiles, however |
||
| 613 | * this may be an issue depending on your setup, especially for CSS files which often contain |
||
| 614 | * relative paths. |
||
| 615 | * |
||
| 616 | * @var string |
||
| 617 | */ |
||
| 618 | protected $combinedFilesFolder = null; |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Put all JavaScript includes at the bottom of the template before the closing <body> tag, |
||
| 622 | * rather than the default behaviour of placing them at the end of the <head> tag. This means |
||
| 623 | * script downloads won't block other HTTP requests, which can be a performance improvement. |
||
| 624 | * |
||
| 625 | * @var bool |
||
| 626 | */ |
||
| 627 | public $writeJavascriptToBody = true; |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
||
| 631 | * |
||
| 632 | * @var boolean |
||
| 633 | */ |
||
| 634 | protected $forceJSToBottom = false; |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Configures the default prefix for combined files. |
||
| 638 | * |
||
| 639 | * This defaults to `_combinedfiles`, and is the folder within the configured asset backend that |
||
| 640 | * combined files will be stored in. If using a backend shared with other systems, it is usually |
||
| 641 | * necessary to distinguish combined files from other assets. |
||
| 642 | * |
||
| 643 | * @config |
||
| 644 | * @var string |
||
| 645 | */ |
||
| 646 | private static $default_combined_files_folder = '_combinedfiles'; |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Flag to include the hash in the querystring instead of the filename for combined files. |
||
| 650 | * |
||
| 651 | * By default the `<hash>` of the source files is appended to the end of the combined file |
||
| 652 | * (prior to the file extension). If combined files are versioned in source control or running |
||
| 653 | * in a distributed environment (such as one where the newest version of a file may not always be |
||
| 654 | * immediately available) then it may sometimes be necessary to disable this. When this is set to true, |
||
| 655 | * the hash will instead be appended via a querystring parameter to enable cache busting, but not in |
||
| 656 | * the filename itself. I.e. `assets/_combinedfiles/name.js?m=<hash>` |
||
| 657 | * |
||
| 658 | * @config |
||
| 659 | * @var bool |
||
| 660 | */ |
||
| 661 | private static $combine_hash_querystring = false; |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @var GeneratedAssetHandler |
||
| 665 | */ |
||
| 666 | protected $assetHandler = null; |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Gets the backend storage for generated files |
||
| 670 | * |
||
| 671 | * @return GeneratedAssetHandler |
||
| 672 | */ |
||
| 673 | public function getAssetHandler() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Set a new asset handler for this backend |
||
| 679 | * |
||
| 680 | * @param GeneratedAssetHandler $handler |
||
| 681 | */ |
||
| 682 | public function setAssetHandler(GeneratedAssetHandler $handler) { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Enable or disable the combination of CSS and JavaScript files |
||
| 688 | * |
||
| 689 | * @param bool $enable |
||
| 690 | */ |
||
| 691 | public function setCombinedFilesEnabled($enable) { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Check if header comments are written |
||
| 697 | * |
||
| 698 | * @return bool |
||
| 699 | */ |
||
| 700 | public function getWriteHeaderComment() { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Flag whether header comments should be written for each combined file |
||
| 706 | * |
||
| 707 | * @param bool $write |
||
| 708 | * @return $this |
||
| 709 | */ |
||
| 710 | public function setWriteHeaderComment($write) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Set the folder to save combined files in. By default they're placed in _combinedfiles, |
||
| 717 | * however this may be an issue depending on your setup, especially for CSS files which often |
||
| 718 | * contain relative paths. |
||
| 719 | * |
||
| 720 | * This must not include any 'assets' prefix |
||
| 721 | * |
||
| 722 | * @param string $folder |
||
| 723 | */ |
||
| 724 | public function setCombinedFilesFolder($folder) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Retrieve the combined files folder prefix |
||
| 730 | * |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | public function getCombinedFilesFolder() { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Set whether to add caching query params to the requests for file-based requirements. |
||
| 742 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 743 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 744 | * while automatically busting this cache every time the file is changed. |
||
| 745 | * |
||
| 746 | * @param bool |
||
| 747 | */ |
||
| 748 | public function setSuffixRequirements($var) { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Check whether we want to suffix requirements |
||
| 754 | * |
||
| 755 | * @return bool |
||
| 756 | */ |
||
| 757 | public function getSuffixRequirements() { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Set whether you want to write the JS to the body of the page rather than at the end of the |
||
| 763 | * head tag. |
||
| 764 | * |
||
| 765 | * @param bool |
||
| 766 | * @return $this |
||
| 767 | */ |
||
| 768 | public function setWriteJavascriptToBody($var) { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Check whether you want to write the JS to the body of the page rather than at the end of the |
||
| 775 | * head tag. |
||
| 776 | * |
||
| 777 | * @return bool |
||
| 778 | */ |
||
| 779 | public function getWriteJavascriptToBody() { |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Forces the JavaScript requirements to the end of the body, right before the closing tag |
||
| 785 | * |
||
| 786 | * @param bool |
||
| 787 | * @return $this |
||
| 788 | */ |
||
| 789 | public function setForceJSToBottom($var) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Check if the JavaScript requirements are written to the end of the body, right before the closing tag |
||
| 796 | * |
||
| 797 | * @return bool |
||
| 798 | */ |
||
| 799 | public function getForceJSToBottom() { |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Check if minify js files should be combined |
||
| 805 | * |
||
| 806 | * @return bool |
||
| 807 | */ |
||
| 808 | public function getMinifyCombinedJSFiles() { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Set if combined js files should be minified |
||
| 814 | * |
||
| 815 | * @param bool $minify |
||
| 816 | * @return $this |
||
| 817 | */ |
||
| 818 | public function setMinifyCombinedJSFiles($minify) { |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Register the given JavaScript file as required. |
||
| 825 | * |
||
| 826 | * @param string $file Relative to docroot |
||
| 827 | * @param array $options List of options. Available options include: |
||
| 828 | * - 'provides' : List of scripts files included in this file |
||
| 829 | * - 'async' : Boolean value to set async attribute to script tag |
||
| 830 | * - 'defer' : Boolean value to set defer attribute to script tag |
||
| 831 | * - 'type' : Override script type= value. |
||
| 832 | */ |
||
| 833 | public function javascript($file, $options = array()) { |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Remove a javascript requirement |
||
| 875 | * |
||
| 876 | * @param string $file |
||
| 877 | */ |
||
| 878 | protected function unsetJavascript($file) { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Gets all scripts that are already provided by prior scripts. |
||
| 884 | * This follows these rules: |
||
| 885 | * - Files will not be considered provided if they are separately |
||
| 886 | * included prior to the providing file. |
||
| 887 | * - Providing files can be blocked, and don't provide anything |
||
| 888 | * - Provided files can't be blocked (you need to block the provider) |
||
| 889 | * - If a combined file includes files that are provided by prior |
||
| 890 | * scripts, then these should be excluded from the combined file. |
||
| 891 | * - If a combined file includes files that are provided by later |
||
| 892 | * scripts, then these files should be included in the combined |
||
| 893 | * file, but we can't block the later script either (possible double |
||
| 894 | * up of file). |
||
| 895 | * |
||
| 896 | * @return array Array of provided files (map of $path => $path) |
||
| 897 | */ |
||
| 898 | public function getProvidedScripts() { |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Returns an array of required JavaScript, excluding blocked |
||
| 924 | * and duplicates of provided files. |
||
| 925 | * |
||
| 926 | * @return array |
||
| 927 | */ |
||
| 928 | public function getJavascript() { |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Gets all javascript, including blocked files. Unwraps the array into a non-associative list |
||
| 938 | * |
||
| 939 | * @return array Indexed array of javascript files |
||
| 940 | */ |
||
| 941 | protected function getAllJavascript() { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Register the given JavaScript code into the list of requirements |
||
| 947 | * |
||
| 948 | * @param string $script The script content as a string (without enclosing <script> tag) |
||
| 949 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 950 | */ |
||
| 951 | public function customScript($script, $uniquenessID = null) { |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Return all registered custom scripts |
||
| 961 | * |
||
| 962 | * @return array |
||
| 963 | */ |
||
| 964 | public function getCustomScripts() { |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Register the given CSS styles into the list of requirements |
||
| 970 | * |
||
| 971 | * @param string $script CSS selectors as a string (without enclosing <style> tag) |
||
| 972 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 973 | */ |
||
| 974 | public function customCSS($script, $uniquenessID = null) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Return all registered custom CSS |
||
| 984 | * |
||
| 985 | * @return array |
||
| 986 | */ |
||
| 987 | public function getCustomCSS() { |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Add the following custom HTML code to the <head> section of the page |
||
| 993 | * |
||
| 994 | * @param string $html Custom HTML code |
||
| 995 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 996 | */ |
||
| 997 | public function insertHeadTags($html, $uniquenessID = null) { |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Return all custom head tags |
||
| 1007 | * |
||
| 1008 | * @return array |
||
| 1009 | */ |
||
| 1010 | public function getCustomHeadTags() { |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Include the content of the given JavaScript file in the list of requirements. Dollar-sign |
||
| 1016 | * variables will be interpolated with values from $vars similar to a .ss template. |
||
| 1017 | * |
||
| 1018 | * @param string $file The template file to load, relative to docroot |
||
| 1019 | * @param string[] $vars The array of variables to interpolate. |
||
| 1020 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 1021 | */ |
||
| 1022 | public function javascriptTemplate($file, $vars, $uniquenessID = null) { |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * Register the given stylesheet into the list of requirements. |
||
| 1038 | * |
||
| 1039 | * @param string $file The CSS file to load, relative to site root |
||
| 1040 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1041 | * (e.g. 'screen,projector') |
||
| 1042 | */ |
||
| 1043 | public function css($file, $media = null) { |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Remove a css requirement |
||
| 1051 | * |
||
| 1052 | * @param string $file |
||
| 1053 | */ |
||
| 1054 | protected function unsetCSS($file) { |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Get the list of registered CSS file requirements, excluding blocked files |
||
| 1060 | * |
||
| 1061 | * @return array Associative array of file to spec |
||
| 1062 | */ |
||
| 1063 | public function getCSS() { |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Gets all CSS files requirements, including blocked |
||
| 1069 | * |
||
| 1070 | * @return array Associative array of file to spec |
||
| 1071 | */ |
||
| 1072 | protected function getAllCSS() { |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Gets the list of all blocked files |
||
| 1078 | * |
||
| 1079 | * @return array |
||
| 1080 | */ |
||
| 1081 | public function getBlocked() { |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Clear either a single or all requirements |
||
| 1087 | * |
||
| 1088 | * Caution: Clearing single rules added via customCSS and customScript only works if you |
||
| 1089 | * originally specified a $uniquenessID. |
||
| 1090 | * |
||
| 1091 | * @param string|int $fileOrID |
||
| 1092 | */ |
||
| 1093 | public function clear($fileOrID = null) { |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Restore requirements cleared by call to Requirements::clear |
||
| 1118 | */ |
||
| 1119 | public function restore() { |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Block inclusion of a specific file |
||
| 1129 | * |
||
| 1130 | * The difference between this and {@link clear} is that the calling order does not matter; |
||
| 1131 | * {@link clear} must be called after the initial registration, whereas {@link block} can be |
||
| 1132 | * used in advance. This is useful, for example, to block scripts included by a superclass |
||
| 1133 | * without having to override entire functions and duplicate a lot of code. |
||
| 1134 | * |
||
| 1135 | * Note that blocking should be used sparingly because it's hard to trace where an file is |
||
| 1136 | * being blocked from. |
||
| 1137 | * |
||
| 1138 | * @param string|int $fileOrID |
||
| 1139 | */ |
||
| 1140 | public function block($fileOrID) { |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Remove an item from the block list |
||
| 1146 | * |
||
| 1147 | * @param string|int $fileOrID |
||
| 1148 | */ |
||
| 1149 | public function unblock($fileOrID) { |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Removes all items from the block list |
||
| 1155 | */ |
||
| 1156 | public function unblockAll() { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 1162 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 1163 | * including a head and body tag. |
||
| 1164 | * |
||
| 1165 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 1166 | * through {@link SSViewer} |
||
| 1167 | * @return string HTML content augmented with the requirements tags |
||
| 1168 | */ |
||
| 1169 | public function includeInHTML($content) { |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * Given a block of HTML, insert the given scripts at the bottom before |
||
| 1240 | * the closing </body> tag |
||
| 1241 | * |
||
| 1242 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1243 | * @param string $content HTML body |
||
| 1244 | * @return string Merged HTML |
||
| 1245 | */ |
||
| 1246 | protected function insertScriptsAtBottom($jsRequirements, $content) { |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Given a block of HTML, insert the given scripts inside the <body></body> |
||
| 1259 | * |
||
| 1260 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1261 | * @param string $content HTML body |
||
| 1262 | * @return string Merged HTML |
||
| 1263 | */ |
||
| 1264 | protected function insertScriptsIntoBody($jsRequirements, $content) { |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Given a block of HTML, insert the given code inside the <head></head> block |
||
| 1295 | * |
||
| 1296 | * @param string $jsRequirements String containing one or more html tags |
||
| 1297 | * @param string $content HTML body |
||
| 1298 | * @return string Merged HTML |
||
| 1299 | */ |
||
| 1300 | protected function insertTagsIntoHead($jsRequirements, $content) { |
||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * Safely escape a literal string for use in preg_replace replacement |
||
| 1311 | * |
||
| 1312 | * @param string $replacement |
||
| 1313 | * @return string |
||
| 1314 | */ |
||
| 1315 | protected function escapeReplacement($replacement) { |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 1321 | * HTTP Response |
||
| 1322 | * |
||
| 1323 | * @param SS_HTTPResponse $response |
||
| 1324 | */ |
||
| 1325 | public function includeInResponse(SS_HTTPResponse $response) { |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Add i18n files from the given javascript directory. SilverStripe expects that the given |
||
| 1356 | * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, |
||
| 1357 | * etc. |
||
| 1358 | * |
||
| 1359 | * @param string $langDir The JavaScript lang directory, relative to the site root, e.g., |
||
| 1360 | * 'framework/javascript/lang' |
||
| 1361 | * @param bool $return Return all relative file paths rather than including them in |
||
| 1362 | * requirements |
||
| 1363 | * @param bool $langOnly Only include language files, not the base libraries |
||
| 1364 | * |
||
| 1365 | * @return array|null All relative files if $return is true, or null otherwise |
||
| 1366 | */ |
||
| 1367 | public function add_i18n_javascript($langDir, $return = false, $langOnly = false) { |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Finds the path for specified file |
||
| 1410 | * |
||
| 1411 | * @param string $fileOrUrl |
||
| 1412 | * @return string|bool |
||
| 1413 | */ |
||
| 1414 | protected function pathForFile($fileOrUrl) { |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Concatenate several css or javascript files into a single dynamically generated file. This |
||
| 1444 | * increases performance by fewer HTTP requests. |
||
| 1445 | * |
||
| 1446 | * The combined file is regenerated based on every file modification time. Optionally a |
||
| 1447 | * rebuild can be triggered by appending ?flush=1 to the URL. |
||
| 1448 | * |
||
| 1449 | * All combined files will have a comment on the start of each concatenated file denoting their |
||
| 1450 | * original position. |
||
| 1451 | * |
||
| 1452 | * CAUTION: You're responsible for ensuring that the load order for combined files is |
||
| 1453 | * retained - otherwise combining JavaScript files can lead to functional errors in the |
||
| 1454 | * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also |
||
| 1455 | * only include each file once across all includes and combinations in a single page load. |
||
| 1456 | * |
||
| 1457 | * CAUTION: Combining CSS Files discards any "media" information. |
||
| 1458 | * |
||
| 1459 | * Example for combined JavaScript: |
||
| 1460 | * <code> |
||
| 1461 | * Requirements::combine_files( |
||
| 1462 | * 'foobar.js', |
||
| 1463 | * array( |
||
| 1464 | * 'mysite/javascript/foo.js', |
||
| 1465 | * 'mysite/javascript/bar.js', |
||
| 1466 | * ), |
||
| 1467 | * array( |
||
| 1468 | * 'async' => true, |
||
| 1469 | * 'defer' => true, |
||
| 1470 | * ) |
||
| 1471 | * ); |
||
| 1472 | * </code> |
||
| 1473 | * |
||
| 1474 | * Example for combined CSS: |
||
| 1475 | * <code> |
||
| 1476 | * Requirements::combine_files( |
||
| 1477 | * 'foobar.css', |
||
| 1478 | * array( |
||
| 1479 | * 'mysite/javascript/foo.css', |
||
| 1480 | * 'mysite/javascript/bar.css', |
||
| 1481 | * ), |
||
| 1482 | * array( |
||
| 1483 | * 'media' => 'print', |
||
| 1484 | * ) |
||
| 1485 | * ); |
||
| 1486 | * </code> |
||
| 1487 | * |
||
| 1488 | * @param string $combinedFileName Filename of the combined file relative to docroot |
||
| 1489 | * @param array $files Array of filenames relative to docroot |
||
| 1490 | * @param array $options Array of options for combining files. Available options are: |
||
| 1491 | * - 'media' : If including CSS Files, you can specify a media type |
||
| 1492 | * - 'async' : If including JavaScript Files, boolean value to set async attribute to script tag |
||
| 1493 | * - 'defer' : If including JavaScript Files, boolean value to set defer attribute to script tag |
||
| 1494 | */ |
||
| 1495 | public function combineFiles($combinedFileName, $files, $options = array()) { |
||
| 1552 | |||
| 1553 | /** |
||
| 1554 | * Return path and type of given combined file |
||
| 1555 | * |
||
| 1556 | * @param string|array $file Either a file path, or an array spec |
||
| 1557 | * @return array array with two elements, path and type of file |
||
| 1558 | */ |
||
| 1559 | protected function parseCombinedFile($file) { |
||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Return all combined files; keys are the combined file names, values are lists of |
||
| 1585 | * associative arrays with 'files', 'type', and 'media' keys for details about this |
||
| 1586 | * combined file. |
||
| 1587 | * |
||
| 1588 | * @return array |
||
| 1589 | */ |
||
| 1590 | public function getCombinedFiles() { |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Includes all combined files, including blocked ones |
||
| 1596 | * |
||
| 1597 | * @return array |
||
| 1598 | */ |
||
| 1599 | protected function getAllCombinedFiles() { |
||
| 1602 | |||
| 1603 | /** |
||
| 1604 | * Clears all combined files |
||
| 1605 | */ |
||
| 1606 | public function deleteAllCombinedFiles() { |
||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * Clear all registered CSS and JavaScript file combinations |
||
| 1615 | */ |
||
| 1616 | public function clearCombinedFiles() { |
||
| 1619 | |||
| 1620 | /** |
||
| 1621 | * Do the heavy lifting involved in combining the combined files. |
||
| 1622 | */ |
||
| 1623 | public function processCombinedFiles() { |
||
| 1688 | |||
| 1689 | /** |
||
| 1690 | * Given a set of files, combine them (as necessary) and return the url |
||
| 1691 | * |
||
| 1692 | * @param string $combinedFile Filename for this combined file |
||
| 1693 | * @param array $fileList List of files to combine |
||
| 1694 | * @param string $type Either 'js' or 'css' |
||
| 1695 | * @return string|null URL to this resource, if there are files to combine |
||
| 1696 | */ |
||
| 1697 | protected function getCombinedFileURL($combinedFile, $fileList, $type) { |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * Given a filename and list of files, generate a new filename unique to these files |
||
| 1751 | * |
||
| 1752 | * @param string $combinedFile |
||
| 1753 | * @param array $fileList |
||
| 1754 | * @return string |
||
| 1755 | */ |
||
| 1756 | protected function hashedCombinedFilename($combinedFile, $fileList) { |
||
| 1762 | |||
| 1763 | /** |
||
| 1764 | * Check if combined files are enabled |
||
| 1765 | * |
||
| 1766 | * @return bool |
||
| 1767 | */ |
||
| 1768 | public function getCombinedFilesEnabled() { |
||
| 1791 | |||
| 1792 | /** |
||
| 1793 | * For a given filelist, determine some discriminating value to determine if |
||
| 1794 | * any of these files have changed. |
||
| 1795 | * |
||
| 1796 | * @param array $fileList List of files |
||
| 1797 | * @return string SHA1 bashed file hash |
||
| 1798 | */ |
||
| 1799 | protected function hashOfFiles($fileList) { |
||
| 1812 | |||
| 1813 | /** |
||
| 1814 | * Registers the given themeable stylesheet as required. |
||
| 1815 | * |
||
| 1816 | * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for, |
||
| 1817 | * and it that doesn't exist and the module parameter is set then a CSS file with that name in |
||
| 1818 | * the module is used. |
||
| 1819 | * |
||
| 1820 | * @param string $name The name of the file - eg '/css/File.css' would have the name 'File' |
||
| 1821 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1822 | * (e.g. 'screen,projector') |
||
| 1823 | */ |
||
| 1824 | public function themedCSS($name, $media = null) { |
||
| 1835 | |||
| 1836 | /** |
||
| 1837 | * Registers the given themeable javascript as required. |
||
| 1838 | * |
||
| 1839 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
| 1840 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
| 1841 | * the module is used. |
||
| 1842 | * |
||
| 1843 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
| 1844 | * @param string $type Comma-separated list of types to use in the script tag |
||
| 1845 | * (e.g. 'text/javascript,text/ecmascript') |
||
| 1846 | */ |
||
| 1847 | public function themedJavascript($name, $type = null) { |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Output debugging information. |
||
| 1865 | */ |
||
| 1866 | public function debug() { |
||
| 1874 | |||
| 1875 | } |
||
| 1876 | |||
| 1892 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.