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 | */ |
||
| 832 | public function javascript($file, $options = array()) { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Remove a javascript requirement |
||
| 864 | * |
||
| 865 | * @param string $file |
||
| 866 | */ |
||
| 867 | protected function unsetJavascript($file) { |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Gets all scripts that are already provided by prior scripts. |
||
| 873 | * This follows these rules: |
||
| 874 | * - Files will not be considered provided if they are separately |
||
| 875 | * included prior to the providing file. |
||
| 876 | * - Providing files can be blocked, and don't provide anything |
||
| 877 | * - Provided files can't be blocked (you need to block the provider) |
||
| 878 | * - If a combined file includes files that are provided by prior |
||
| 879 | * scripts, then these should be excluded from the combined file. |
||
| 880 | * - If a combined file includes files that are provided by later |
||
| 881 | * scripts, then these files should be included in the combined |
||
| 882 | * file, but we can't block the later script either (possible double |
||
| 883 | * up of file). |
||
| 884 | * |
||
| 885 | * @return array Array of provided files (map of $path => $path) |
||
| 886 | */ |
||
| 887 | public function getProvidedScripts() { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Returns an array of required JavaScript, excluding blocked |
||
| 913 | * and duplicates of provided files. |
||
| 914 | * |
||
| 915 | * @return array |
||
| 916 | */ |
||
| 917 | public function getJavascript() { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Gets all javascript, including blocked files. Unwraps the array into a non-associative list |
||
| 927 | * |
||
| 928 | * @return array Indexed array of javascript files |
||
| 929 | */ |
||
| 930 | protected function getAllJavascript() { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Register the given JavaScript code into the list of requirements |
||
| 936 | * |
||
| 937 | * @param string $script The script content as a string (without enclosing <script> tag) |
||
| 938 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 939 | */ |
||
| 940 | public function customScript($script, $uniquenessID = null) { |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Return all registered custom scripts |
||
| 950 | * |
||
| 951 | * @return array |
||
| 952 | */ |
||
| 953 | public function getCustomScripts() { |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Register the given CSS styles into the list of requirements |
||
| 959 | * |
||
| 960 | * @param string $script CSS selectors as a string (without enclosing <style> tag) |
||
| 961 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 962 | */ |
||
| 963 | public function customCSS($script, $uniquenessID = null) { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Return all registered custom CSS |
||
| 973 | * |
||
| 974 | * @return array |
||
| 975 | */ |
||
| 976 | public function getCustomCSS() { |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Add the following custom HTML code to the <head> section of the page |
||
| 982 | * |
||
| 983 | * @param string $html Custom HTML code |
||
| 984 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 985 | */ |
||
| 986 | public function insertHeadTags($html, $uniquenessID = null) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Return all custom head tags |
||
| 996 | * |
||
| 997 | * @return array |
||
| 998 | */ |
||
| 999 | public function getCustomHeadTags() { |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Include the content of the given JavaScript file in the list of requirements. Dollar-sign |
||
| 1005 | * variables will be interpolated with values from $vars similar to a .ss template. |
||
| 1006 | * |
||
| 1007 | * @param string $file The template file to load, relative to docroot |
||
| 1008 | * @param string[] $vars The array of variables to interpolate. |
||
| 1009 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 1010 | */ |
||
| 1011 | public function javascriptTemplate($file, $vars, $uniquenessID = null) { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Register the given stylesheet into the list of requirements. |
||
| 1027 | * |
||
| 1028 | * @param string $file The CSS file to load, relative to site root |
||
| 1029 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1030 | * (e.g. 'screen,projector') |
||
| 1031 | */ |
||
| 1032 | public function css($file, $media = null) { |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Remove a css requirement |
||
| 1040 | * |
||
| 1041 | * @param string $file |
||
| 1042 | */ |
||
| 1043 | protected function unsetCSS($file) { |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Get the list of registered CSS file requirements, excluding blocked files |
||
| 1049 | * |
||
| 1050 | * @return array Associative array of file to spec |
||
| 1051 | */ |
||
| 1052 | public function getCSS() { |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Gets all CSS files requirements, including blocked |
||
| 1058 | * |
||
| 1059 | * @return array Associative array of file to spec |
||
| 1060 | */ |
||
| 1061 | protected function getAllCSS() { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Gets the list of all blocked files |
||
| 1067 | * |
||
| 1068 | * @return array |
||
| 1069 | */ |
||
| 1070 | public function getBlocked() { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Clear either a single or all requirements |
||
| 1076 | * |
||
| 1077 | * Caution: Clearing single rules added via customCSS and customScript only works if you |
||
| 1078 | * originally specified a $uniquenessID. |
||
| 1079 | * |
||
| 1080 | * @param string|int $fileOrID |
||
| 1081 | */ |
||
| 1082 | public function clear($fileOrID = null) { |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Restore requirements cleared by call to Requirements::clear |
||
| 1107 | */ |
||
| 1108 | public function restore() { |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Block inclusion of a specific file |
||
| 1118 | * |
||
| 1119 | * The difference between this and {@link clear} is that the calling order does not matter; |
||
| 1120 | * {@link clear} must be called after the initial registration, whereas {@link block} can be |
||
| 1121 | * used in advance. This is useful, for example, to block scripts included by a superclass |
||
| 1122 | * without having to override entire functions and duplicate a lot of code. |
||
| 1123 | * |
||
| 1124 | * Note that blocking should be used sparingly because it's hard to trace where an file is |
||
| 1125 | * being blocked from. |
||
| 1126 | * |
||
| 1127 | * @param string|int $fileOrID |
||
| 1128 | */ |
||
| 1129 | public function block($fileOrID) { |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Remove an item from the block list |
||
| 1135 | * |
||
| 1136 | * @param string|int $fileOrID |
||
| 1137 | */ |
||
| 1138 | public function unblock($fileOrID) { |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Removes all items from the block list |
||
| 1144 | */ |
||
| 1145 | public function unblockAll() { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 1151 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 1152 | * including a head and body tag. |
||
| 1153 | * |
||
| 1154 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 1155 | * through {@link SSViewer} |
||
| 1156 | * @return string HTML content augmented with the requirements tags |
||
| 1157 | */ |
||
| 1158 | public function includeInHTML($content) { |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Given a block of HTML, insert the given scripts at the bottom before |
||
| 1228 | * the closing </body> tag |
||
| 1229 | * |
||
| 1230 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1231 | * @param string $content HTML body |
||
| 1232 | * @return string Merged HTML |
||
| 1233 | */ |
||
| 1234 | protected function insertScriptsAtBottom($jsRequirements, $content) { |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Given a block of HTML, insert the given scripts inside the <body></body> |
||
| 1247 | * |
||
| 1248 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1249 | * @param string $content HTML body |
||
| 1250 | * @return string Merged HTML |
||
| 1251 | */ |
||
| 1252 | protected function insertScriptsIntoBody($jsRequirements, $content) { |
||
| 1280 | |||
| 1281 | /** |
||
| 1282 | * Given a block of HTML, insert the given code inside the <head></head> block |
||
| 1283 | * |
||
| 1284 | * @param string $jsRequirements String containing one or more html tags |
||
| 1285 | * @param string $content HTML body |
||
| 1286 | * @return string Merged HTML |
||
| 1287 | */ |
||
| 1288 | protected function insertTagsIntoHead($jsRequirements, $content) { |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Safely escape a literal string for use in preg_replace replacement |
||
| 1299 | * |
||
| 1300 | * @param string $replacement |
||
| 1301 | * @return string |
||
| 1302 | */ |
||
| 1303 | protected function escapeReplacement($replacement) { |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 1309 | * HTTP Response |
||
| 1310 | * |
||
| 1311 | * @param SS_HTTPResponse $response |
||
| 1312 | */ |
||
| 1313 | public function includeInResponse(SS_HTTPResponse $response) { |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Add i18n files from the given javascript directory. SilverStripe expects that the given |
||
| 1344 | * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, |
||
| 1345 | * etc. |
||
| 1346 | * |
||
| 1347 | * @param string $langDir The JavaScript lang directory, relative to the site root, e.g., |
||
| 1348 | * 'framework/javascript/lang' |
||
| 1349 | * @param bool $return Return all relative file paths rather than including them in |
||
| 1350 | * requirements |
||
| 1351 | * @param bool $langOnly Only include language files, not the base libraries |
||
| 1352 | * |
||
| 1353 | * @return array|null All relative files if $return is true, or null otherwise |
||
| 1354 | */ |
||
| 1355 | public function add_i18n_javascript($langDir, $return = false, $langOnly = false) { |
||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Finds the path for specified file |
||
| 1398 | * |
||
| 1399 | * @param string $fileOrUrl |
||
| 1400 | * @return string|bool |
||
| 1401 | */ |
||
| 1402 | protected function pathForFile($fileOrUrl) { |
||
| 1429 | |||
| 1430 | /** |
||
| 1431 | * Concatenate several css or javascript files into a single dynamically generated file. This |
||
| 1432 | * increases performance by fewer HTTP requests. |
||
| 1433 | * |
||
| 1434 | * The combined file is regenerated based on every file modification time. Optionally a |
||
| 1435 | * rebuild can be triggered by appending ?flush=1 to the URL. |
||
| 1436 | * |
||
| 1437 | * All combined files will have a comment on the start of each concatenated file denoting their |
||
| 1438 | * original position. |
||
| 1439 | * |
||
| 1440 | * CAUTION: You're responsible for ensuring that the load order for combined files is |
||
| 1441 | * retained - otherwise combining JavaScript files can lead to functional errors in the |
||
| 1442 | * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also |
||
| 1443 | * only include each file once across all includes and combinations in a single page load. |
||
| 1444 | * |
||
| 1445 | * CAUTION: Combining CSS Files discards any "media" information. |
||
| 1446 | * |
||
| 1447 | * Example for combined JavaScript: |
||
| 1448 | * <code> |
||
| 1449 | * Requirements::combine_files( |
||
| 1450 | * 'foobar.js', |
||
| 1451 | * array( |
||
| 1452 | * 'mysite/javascript/foo.js', |
||
| 1453 | * 'mysite/javascript/bar.js', |
||
| 1454 | * ), |
||
| 1455 | * array( |
||
| 1456 | * 'async' => true, |
||
| 1457 | * 'defer' => true, |
||
| 1458 | * ) |
||
| 1459 | * ); |
||
| 1460 | * </code> |
||
| 1461 | * |
||
| 1462 | * Example for combined CSS: |
||
| 1463 | * <code> |
||
| 1464 | * Requirements::combine_files( |
||
| 1465 | * 'foobar.css', |
||
| 1466 | * array( |
||
| 1467 | * 'mysite/javascript/foo.css', |
||
| 1468 | * 'mysite/javascript/bar.css', |
||
| 1469 | * ), |
||
| 1470 | * array( |
||
| 1471 | * 'media' => 'print', |
||
| 1472 | * ) |
||
| 1473 | * ); |
||
| 1474 | * </code> |
||
| 1475 | * |
||
| 1476 | * @param string $combinedFileName Filename of the combined file relative to docroot |
||
| 1477 | * @param array $files Array of filenames relative to docroot |
||
| 1478 | * @param array $options Array of options for combining files. Available options are: |
||
| 1479 | * - 'media' : If including CSS Files, you can specify a media type |
||
| 1480 | * - 'async' : If including JavaScript Files, boolean value to set async attribute to script tag |
||
| 1481 | * - 'defer' : If including JavaScript Files, boolean value to set defer attribute to script tag |
||
| 1482 | */ |
||
| 1483 | public function combineFiles($combinedFileName, $files, $options = array()) { |
||
| 1540 | |||
| 1541 | /** |
||
| 1542 | * Return path and type of given combined file |
||
| 1543 | * |
||
| 1544 | * @param string|array $file Either a file path, or an array spec |
||
| 1545 | * @return array array with two elements, path and type of file |
||
| 1546 | */ |
||
| 1547 | protected function parseCombinedFile($file) { |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Return all combined files; keys are the combined file names, values are lists of |
||
| 1573 | * associative arrays with 'files', 'type', and 'media' keys for details about this |
||
| 1574 | * combined file. |
||
| 1575 | * |
||
| 1576 | * @return array |
||
| 1577 | */ |
||
| 1578 | public function getCombinedFiles() { |
||
| 1581 | |||
| 1582 | /** |
||
| 1583 | * Includes all combined files, including blocked ones |
||
| 1584 | * |
||
| 1585 | * @return array |
||
| 1586 | */ |
||
| 1587 | protected function getAllCombinedFiles() { |
||
| 1590 | |||
| 1591 | /** |
||
| 1592 | * Clears all combined files |
||
| 1593 | */ |
||
| 1594 | public function deleteAllCombinedFiles() { |
||
| 1600 | |||
| 1601 | /** |
||
| 1602 | * Clear all registered CSS and JavaScript file combinations |
||
| 1603 | */ |
||
| 1604 | public function clearCombinedFiles() { |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Do the heavy lifting involved in combining the combined files. |
||
| 1610 | */ |
||
| 1611 | public function processCombinedFiles() { |
||
| 1676 | |||
| 1677 | /** |
||
| 1678 | * Given a set of files, combine them (as necessary) and return the url |
||
| 1679 | * |
||
| 1680 | * @param string $combinedFile Filename for this combined file |
||
| 1681 | * @param array $fileList List of files to combine |
||
| 1682 | * @param string $type Either 'js' or 'css' |
||
| 1683 | * @return string|null URL to this resource, if there are files to combine |
||
| 1684 | */ |
||
| 1685 | protected function getCombinedFileURL($combinedFile, $fileList, $type) { |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Given a filename and list of files, generate a new filename unique to these files |
||
| 1739 | * |
||
| 1740 | * @param string $combinedFile |
||
| 1741 | * @param array $fileList |
||
| 1742 | * @return string |
||
| 1743 | */ |
||
| 1744 | protected function hashedCombinedFilename($combinedFile, $fileList) { |
||
| 1750 | |||
| 1751 | /** |
||
| 1752 | * Check if combined files are enabled |
||
| 1753 | * |
||
| 1754 | * @return bool |
||
| 1755 | */ |
||
| 1756 | public function getCombinedFilesEnabled() { |
||
| 1779 | |||
| 1780 | /** |
||
| 1781 | * For a given filelist, determine some discriminating value to determine if |
||
| 1782 | * any of these files have changed. |
||
| 1783 | * |
||
| 1784 | * @param array $fileList List of files |
||
| 1785 | * @return string SHA1 bashed file hash |
||
| 1786 | */ |
||
| 1787 | protected function hashOfFiles($fileList) { |
||
| 1800 | |||
| 1801 | /** |
||
| 1802 | * Registers the given themeable stylesheet as required. |
||
| 1803 | * |
||
| 1804 | * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for, |
||
| 1805 | * and it that doesn't exist and the module parameter is set then a CSS file with that name in |
||
| 1806 | * the module is used. |
||
| 1807 | * |
||
| 1808 | * @param string $name The name of the file - eg '/css/File.css' would have the name 'File' |
||
| 1809 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1810 | * (e.g. 'screen,projector') |
||
| 1811 | */ |
||
| 1812 | public function themedCSS($name, $media = null) { |
||
| 1823 | |||
| 1824 | /** |
||
| 1825 | * Registers the given themeable javascript as required. |
||
| 1826 | * |
||
| 1827 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
| 1828 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
| 1829 | * the module is used. |
||
| 1830 | * |
||
| 1831 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
| 1832 | * @param string $type Comma-separated list of types to use in the script tag |
||
| 1833 | * (e.g. 'text/javascript,text/ecmascript') |
||
| 1834 | */ |
||
| 1835 | public function themedJavascript($name, $type = null) { |
||
| 1850 | |||
| 1851 | /** |
||
| 1852 | * Output debugging information. |
||
| 1853 | */ |
||
| 1854 | public function debug() { |
||
| 1862 | |||
| 1863 | } |
||
| 1864 | |||
| 1880 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.