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 |
||
| 480 | class Requirements_Backend |
||
| 481 | { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Whether to add caching query params to the requests for file-based requirements. |
||
| 485 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 486 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 487 | * while automatically busting this cache every time the file is changed. |
||
| 488 | * |
||
| 489 | * @var bool |
||
| 490 | */ |
||
| 491 | protected $suffixRequirements = true; |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Whether to combine CSS and JavaScript files |
||
| 495 | * |
||
| 496 | * @var bool |
||
| 497 | */ |
||
| 498 | protected $combinedFilesEnabled = true; |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Determine if files should be combined automatically on dev mode. |
||
| 502 | * |
||
| 503 | * By default combined files will not be combined except in test or |
||
| 504 | * live environments. Turning this on will allow for pre-combining of files in development mode. |
||
| 505 | * |
||
| 506 | * @config |
||
| 507 | * @var bool |
||
| 508 | */ |
||
| 509 | private static $combine_in_dev = false; |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Paths to all required JavaScript files relative to docroot |
||
| 513 | * |
||
| 514 | * @var array |
||
| 515 | */ |
||
| 516 | protected $javascript = array(); |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Map of included scripts to array of contained files. |
||
| 520 | * To be used alongside front-end combination mechanisms. |
||
| 521 | * |
||
| 522 | * @var array Map of providing filepath => array(provided filepaths) |
||
| 523 | */ |
||
| 524 | protected $providedJavascript = array(); |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Paths to all required CSS files relative to the docroot. |
||
| 528 | * |
||
| 529 | * @var array |
||
| 530 | */ |
||
| 531 | protected $css = array(); |
||
| 532 | |||
| 533 | /** |
||
| 534 | * All custom javascript code that is inserted into the page's HTML |
||
| 535 | * |
||
| 536 | * @var array |
||
| 537 | */ |
||
| 538 | protected $customScript = array(); |
||
| 539 | |||
| 540 | /** |
||
| 541 | * All custom CSS rules which are inserted directly at the bottom of the HTML <head> tag |
||
| 542 | * |
||
| 543 | * @var array |
||
| 544 | */ |
||
| 545 | protected $customCSS = array(); |
||
| 546 | |||
| 547 | /** |
||
| 548 | * All custom HTML markup which is added before the closing <head> tag, e.g. additional |
||
| 549 | * metatags. |
||
| 550 | * |
||
| 551 | * @var array |
||
| 552 | */ |
||
| 553 | protected $customHeadTags = array(); |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Remembers the file paths or uniquenessIDs of all Requirements cleared through |
||
| 557 | * {@link clear()}, so that they can be restored later. |
||
| 558 | * |
||
| 559 | * @var array |
||
| 560 | */ |
||
| 561 | protected $disabled = array(); |
||
| 562 | |||
| 563 | /** |
||
| 564 | * The file paths (relative to docroot) or uniquenessIDs of any included requirements which |
||
| 565 | * should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example, |
||
| 566 | * to block scripts included by a superclass without having to override entire functions and |
||
| 567 | * duplicate a lot of code. |
||
| 568 | * |
||
| 569 | * Use {@link unblock()} or {@link unblock_all()} to revert changes. |
||
| 570 | * |
||
| 571 | * @var array |
||
| 572 | */ |
||
| 573 | protected $blocked = array(); |
||
| 574 | |||
| 575 | /** |
||
| 576 | * A list of combined files registered via {@link combine_files()}. Keys are the output file |
||
| 577 | * names, values are lists of input files. |
||
| 578 | * |
||
| 579 | * @var array |
||
| 580 | */ |
||
| 581 | protected $combinedFiles = array(); |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Use the JSMin library to minify any javascript file passed to {@link combine_files()}. |
||
| 585 | * |
||
| 586 | * @var bool |
||
| 587 | */ |
||
| 588 | protected $minifyCombinedJSFiles = true; |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Whether or not file headers should be written when combining files |
||
| 592 | * |
||
| 593 | * @var boolean |
||
| 594 | */ |
||
| 595 | protected $writeHeaderComment = true; |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Where to save combined files. By default they're placed in assets/_combinedfiles, however |
||
| 599 | * this may be an issue depending on your setup, especially for CSS files which often contain |
||
| 600 | * relative paths. |
||
| 601 | * |
||
| 602 | * @var string |
||
| 603 | */ |
||
| 604 | protected $combinedFilesFolder = null; |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Put all JavaScript includes at the bottom of the template before the closing <body> tag, |
||
| 608 | * rather than the default behaviour of placing them at the end of the <head> tag. This means |
||
| 609 | * script downloads won't block other HTTP requests, which can be a performance improvement. |
||
| 610 | * |
||
| 611 | * @var bool |
||
| 612 | */ |
||
| 613 | public $writeJavascriptToBody = true; |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
||
| 617 | * |
||
| 618 | * @var boolean |
||
| 619 | */ |
||
| 620 | protected $forceJSToBottom = false; |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Configures the default prefix for combined files. |
||
| 624 | * |
||
| 625 | * This defaults to `_combinedfiles`, and is the folder within the configured asset backend that |
||
| 626 | * combined files will be stored in. If using a backend shared with other systems, it is usually |
||
| 627 | * necessary to distinguish combined files from other assets. |
||
| 628 | * |
||
| 629 | * @config |
||
| 630 | * @var string |
||
| 631 | */ |
||
| 632 | private static $default_combined_files_folder = '_combinedfiles'; |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Flag to include the hash in the querystring instead of the filename for combined files. |
||
| 636 | * |
||
| 637 | * By default the `<hash>` of the source files is appended to the end of the combined file |
||
| 638 | * (prior to the file extension). If combined files are versioned in source control or running |
||
| 639 | * in a distributed environment (such as one where the newest version of a file may not always be |
||
| 640 | * immediately available) then it may sometimes be necessary to disable this. When this is set to true, |
||
| 641 | * the hash will instead be appended via a querystring parameter to enable cache busting, but not in |
||
| 642 | * the filename itself. I.e. `assets/_combinedfiles/name.js?m=<hash>` |
||
| 643 | * |
||
| 644 | * @config |
||
| 645 | * @var bool |
||
| 646 | */ |
||
| 647 | private static $combine_hash_querystring = false; |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @var GeneratedAssetHandler |
||
| 651 | */ |
||
| 652 | protected $assetHandler = null; |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Gets the backend storage for generated files |
||
| 656 | * |
||
| 657 | * @return GeneratedAssetHandler |
||
| 658 | */ |
||
| 659 | public function getAssetHandler() { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Set a new asset handler for this backend |
||
| 665 | * |
||
| 666 | * @param GeneratedAssetHandler $handler |
||
| 667 | */ |
||
| 668 | public function setAssetHandler(GeneratedAssetHandler $handler) { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Enable or disable the combination of CSS and JavaScript files |
||
| 674 | * |
||
| 675 | * @param bool $enable |
||
| 676 | */ |
||
| 677 | public function setCombinedFilesEnabled($enable) { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Check if header comments are written |
||
| 683 | * |
||
| 684 | * @return bool |
||
| 685 | */ |
||
| 686 | public function getWriteHeaderComment() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Flag whether header comments should be written for each combined file |
||
| 692 | * |
||
| 693 | * @param bool $write |
||
| 694 | * @return $this |
||
| 695 | */ |
||
| 696 | public function setWriteHeaderComment($write) { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Set the folder to save combined files in. By default they're placed in _combinedfiles, |
||
| 703 | * however this may be an issue depending on your setup, especially for CSS files which often |
||
| 704 | * contain relative paths. |
||
| 705 | * |
||
| 706 | * This must not include any 'assets' prefix |
||
| 707 | * |
||
| 708 | * @param string $folder |
||
| 709 | */ |
||
| 710 | public function setCombinedFilesFolder($folder) { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Retrieve the combined files folder prefix |
||
| 716 | * |
||
| 717 | * @return string |
||
| 718 | */ |
||
| 719 | public function getCombinedFilesFolder() { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Set whether to add caching query params to the requests for file-based requirements. |
||
| 728 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 729 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 730 | * while automatically busting this cache every time the file is changed. |
||
| 731 | * |
||
| 732 | * @param bool |
||
| 733 | */ |
||
| 734 | public function setSuffixRequirements($var) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Check whether we want to suffix requirements |
||
| 740 | * |
||
| 741 | * @return bool |
||
| 742 | */ |
||
| 743 | public function getSuffixRequirements() { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Set whether you want to write the JS to the body of the page rather than at the end of the |
||
| 749 | * head tag. |
||
| 750 | * |
||
| 751 | * @param bool |
||
| 752 | * @return $this |
||
| 753 | */ |
||
| 754 | public function setWriteJavascriptToBody($var) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Check whether you want to write the JS to the body of the page rather than at the end of the |
||
| 761 | * head tag. |
||
| 762 | * |
||
| 763 | * @return bool |
||
| 764 | */ |
||
| 765 | public function getWriteJavascriptToBody() { |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Forces the JavaScript requirements to the end of the body, right before the closing tag |
||
| 771 | * |
||
| 772 | * @param bool |
||
| 773 | * @return $this |
||
| 774 | */ |
||
| 775 | public function setForceJSToBottom($var) { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Check if the JavaScript requirements are written to the end of the body, right before the closing tag |
||
| 782 | * |
||
| 783 | * @return bool |
||
| 784 | */ |
||
| 785 | public function getForceJSToBottom() { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Check if minify js files should be combined |
||
| 791 | * |
||
| 792 | * @return bool |
||
| 793 | */ |
||
| 794 | public function getMinifyCombinedJSFiles() { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Set if combined js files should be minified |
||
| 800 | * |
||
| 801 | * @param bool $minify |
||
| 802 | * @return $this |
||
| 803 | */ |
||
| 804 | public function setMinifyCombinedJSFiles($minify) { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Register the given JavaScript file as required. |
||
| 811 | * |
||
| 812 | * @param string $file Relative to docroot |
||
| 813 | * @param array $options List of options. Available options include: |
||
| 814 | * - 'provides' : List of scripts files included in this file |
||
| 815 | * - 'async' : Boolean value to set async attribute to script tag |
||
| 816 | * - 'defer' : Boolean value to set defer attribute to script tag |
||
| 817 | */ |
||
| 818 | public function javascript($file, $options = array()) { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Remove a javascript requirement |
||
| 850 | * |
||
| 851 | * @param string $file |
||
| 852 | */ |
||
| 853 | protected function unsetJavascript($file) { |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Gets all scripts that are already provided by prior scripts. |
||
| 859 | * This follows these rules: |
||
| 860 | * - Files will not be considered provided if they are separately |
||
| 861 | * included prior to the providing file. |
||
| 862 | * - Providing files can be blocked, and don't provide anything |
||
| 863 | * - Provided files can't be blocked (you need to block the provider) |
||
| 864 | * - If a combined file includes files that are provided by prior |
||
| 865 | * scripts, then these should be excluded from the combined file. |
||
| 866 | * - If a combined file includes files that are provided by later |
||
| 867 | * scripts, then these files should be included in the combined |
||
| 868 | * file, but we can't block the later script either (possible double |
||
| 869 | * up of file). |
||
| 870 | * |
||
| 871 | * @return array Array of provided files (map of $path => $path) |
||
| 872 | */ |
||
| 873 | public function getProvidedScripts() { |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Returns an array of required JavaScript, excluding blocked |
||
| 899 | * and duplicates of provided files. |
||
| 900 | * |
||
| 901 | * @return array |
||
| 902 | */ |
||
| 903 | public function getJavascript() { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Gets all javascript, including blocked files. Unwraps the array into a non-associative list |
||
| 913 | * |
||
| 914 | * @return array Indexed array of javascript files |
||
| 915 | */ |
||
| 916 | protected function getAllJavascript() { |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Register the given JavaScript code into the list of requirements |
||
| 922 | * |
||
| 923 | * @param string $script The script content as a string (without enclosing <script> tag) |
||
| 924 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 925 | */ |
||
| 926 | public function customScript($script, $uniquenessID = null) { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Return all registered custom scripts |
||
| 936 | * |
||
| 937 | * @return array |
||
| 938 | */ |
||
| 939 | public function getCustomScripts() { |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Register the given CSS styles into the list of requirements |
||
| 945 | * |
||
| 946 | * @param string $script CSS selectors as a string (without enclosing <style> tag) |
||
| 947 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 948 | */ |
||
| 949 | public function customCSS($script, $uniquenessID = null) { |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Return all registered custom CSS |
||
| 959 | * |
||
| 960 | * @return array |
||
| 961 | */ |
||
| 962 | public function getCustomCSS() { |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Add the following custom HTML code to the <head> section of the page |
||
| 968 | * |
||
| 969 | * @param string $html Custom HTML code |
||
| 970 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 971 | */ |
||
| 972 | public function insertHeadTags($html, $uniquenessID = null) { |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Return all custom head tags |
||
| 982 | * |
||
| 983 | * @return array |
||
| 984 | */ |
||
| 985 | public function getCustomHeadTags() { |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Include the content of the given JavaScript file in the list of requirements. Dollar-sign |
||
| 991 | * variables will be interpolated with values from $vars similar to a .ss template. |
||
| 992 | * |
||
| 993 | * @param string $file The template file to load, relative to docroot |
||
| 994 | * @param string[] $vars The array of variables to interpolate. |
||
| 995 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 996 | */ |
||
| 997 | public function javascriptTemplate($file, $vars, $uniquenessID = null) { |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Register the given stylesheet into the list of requirements. |
||
| 1013 | * |
||
| 1014 | * @param string $file The CSS file to load, relative to site root |
||
| 1015 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1016 | * (e.g. 'screen,projector') |
||
| 1017 | */ |
||
| 1018 | public function css($file, $media = null) { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Remove a css requirement |
||
| 1026 | * |
||
| 1027 | * @param string $file |
||
| 1028 | */ |
||
| 1029 | protected function unsetCSS($file) { |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Get the list of registered CSS file requirements, excluding blocked files |
||
| 1035 | * |
||
| 1036 | * @return array Associative array of file to spec |
||
| 1037 | */ |
||
| 1038 | public function getCSS() { |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Gets all CSS files requirements, including blocked |
||
| 1044 | * |
||
| 1045 | * @return array Associative array of file to spec |
||
| 1046 | */ |
||
| 1047 | protected function getAllCSS() { |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Gets the list of all blocked files |
||
| 1053 | * |
||
| 1054 | * @return array |
||
| 1055 | */ |
||
| 1056 | public function getBlocked() { |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Clear either a single or all requirements |
||
| 1062 | * |
||
| 1063 | * Caution: Clearing single rules added via customCSS and customScript only works if you |
||
| 1064 | * originally specified a $uniquenessID. |
||
| 1065 | * |
||
| 1066 | * @param string|int $fileOrID |
||
| 1067 | */ |
||
| 1068 | public function clear($fileOrID = null) { |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Restore requirements cleared by call to Requirements::clear |
||
| 1093 | */ |
||
| 1094 | public function restore() { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Block inclusion of a specific file |
||
| 1104 | * |
||
| 1105 | * The difference between this and {@link clear} is that the calling order does not matter; |
||
| 1106 | * {@link clear} must be called after the initial registration, whereas {@link block} can be |
||
| 1107 | * used in advance. This is useful, for example, to block scripts included by a superclass |
||
| 1108 | * without having to override entire functions and duplicate a lot of code. |
||
| 1109 | * |
||
| 1110 | * Note that blocking should be used sparingly because it's hard to trace where an file is |
||
| 1111 | * being blocked from. |
||
| 1112 | * |
||
| 1113 | * @param string|int $fileOrID |
||
| 1114 | */ |
||
| 1115 | public function block($fileOrID) { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Remove an item from the block list |
||
| 1121 | * |
||
| 1122 | * @param string|int $fileOrID |
||
| 1123 | */ |
||
| 1124 | public function unblock($fileOrID) { |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Removes all items from the block list |
||
| 1130 | */ |
||
| 1131 | public function unblockAll() { |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 1137 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 1138 | * including a head and body tag. |
||
| 1139 | * |
||
| 1140 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 1141 | * through {@link SSViewer} |
||
| 1142 | * @return string HTML content augmented with the requirements tags |
||
| 1143 | */ |
||
| 1144 | public function includeInHTML($content) { |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Given a block of HTML, insert the given scripts at the bottom before |
||
| 1214 | * the closing </body> tag |
||
| 1215 | * |
||
| 1216 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1217 | * @param string $content HTML body |
||
| 1218 | * @return string Merged HTML |
||
| 1219 | */ |
||
| 1220 | protected function insertScriptsAtBottom($jsRequirements, $content) { |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Given a block of HTML, insert the given scripts inside the <body></body> |
||
| 1233 | * |
||
| 1234 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1235 | * @param string $content HTML body |
||
| 1236 | * @return string Merged HTML |
||
| 1237 | */ |
||
| 1238 | protected function insertScriptsIntoBody($jsRequirements, $content) { |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Given a block of HTML, insert the given code inside the <head></head> block |
||
| 1269 | * |
||
| 1270 | * @param string $jsRequirements String containing one or more html tags |
||
| 1271 | * @param string $content HTML body |
||
| 1272 | * @return string Merged HTML |
||
| 1273 | */ |
||
| 1274 | protected function insertTagsIntoHead($jsRequirements, $content) { |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Safely escape a literal string for use in preg_replace replacement |
||
| 1285 | * |
||
| 1286 | * @param string $replacement |
||
| 1287 | * @return string |
||
| 1288 | */ |
||
| 1289 | protected function escapeReplacement($replacement) { |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 1295 | * HTTP Response |
||
| 1296 | * |
||
| 1297 | * @param SS_HTTPResponse $response |
||
| 1298 | */ |
||
| 1299 | public function includeInResponse(SS_HTTPResponse $response) { |
||
| 1300 | $this->processCombinedFiles(); |
||
| 1301 | $jsRequirements = array(); |
||
| 1302 | $cssRequirements = array(); |
||
| 1303 | |||
| 1304 | foreach($this->getJavascript() as $file => $attributes) { |
||
| 1305 | $path = $this->pathForFile($file); |
||
| 1306 | if($path) { |
||
| 1307 | $jsRequirements[] = str_replace(',', '%2C', $path); |
||
| 1308 | } |
||
| 1309 | } |
||
| 1310 | |||
| 1311 | if(count($jsRequirements)) { |
||
| 1312 | $response->addHeader('X-Include-JS', implode(',', $jsRequirements)); |
||
| 1313 | } |
||
| 1314 | |||
| 1315 | foreach($this->getCSS() as $file => $params) { |
||
| 1316 | $path = $this->pathForFile($file); |
||
| 1317 | if($path) { |
||
| 1318 | $path = str_replace(',', '%2C', $path); |
||
| 1319 | $cssRequirements[] = isset($params['media']) ? "$path:##:$params[media]" : $path; |
||
| 1320 | } |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | if(count($cssRequirements)) { |
||
| 1324 | $response->addHeader('X-Include-CSS', implode(',', $cssRequirements)); |
||
| 1325 | } |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Add i18n files from the given javascript directory. SilverStripe expects that the given |
||
| 1330 | * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, |
||
| 1331 | * etc. |
||
| 1332 | * |
||
| 1333 | * @param string $langDir The JavaScript lang directory, relative to the site root, e.g., |
||
| 1334 | * 'framework/javascript/lang' |
||
| 1335 | * @param bool $return Return all relative file paths rather than including them in |
||
| 1336 | * requirements |
||
| 1337 | * @param bool $langOnly Only include language files, not the base libraries |
||
| 1338 | * |
||
| 1339 | * @return array|null All relative files if $return is true, or null otherwise |
||
| 1340 | */ |
||
| 1341 | public function add_i18n_javascript($langDir, $return = false, $langOnly = false) { |
||
| 1381 | |||
| 1382 | /** |
||
| 1383 | * Finds the path for specified file |
||
| 1384 | * |
||
| 1385 | * @param string $fileOrUrl |
||
| 1386 | * @return string|bool |
||
| 1387 | */ |
||
| 1388 | protected function pathForFile($fileOrUrl) { |
||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Concatenate several css or javascript files into a single dynamically generated file. This |
||
| 1418 | * increases performance by fewer HTTP requests. |
||
| 1419 | * |
||
| 1420 | * The combined file is regenerated based on every file modification time. Optionally a |
||
| 1421 | * rebuild can be triggered by appending ?flush=1 to the URL. |
||
| 1422 | * |
||
| 1423 | * All combined files will have a comment on the start of each concatenated file denoting their |
||
| 1424 | * original position. |
||
| 1425 | * |
||
| 1426 | * CAUTION: You're responsible for ensuring that the load order for combined files is |
||
| 1427 | * retained - otherwise combining JavaScript files can lead to functional errors in the |
||
| 1428 | * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also |
||
| 1429 | * only include each file once across all includes and combinations in a single page load. |
||
| 1430 | * |
||
| 1431 | * CAUTION: Combining CSS Files discards any "media" information. |
||
| 1432 | * |
||
| 1433 | * Example for combined JavaScript: |
||
| 1434 | * <code> |
||
| 1435 | * Requirements::combine_files( |
||
| 1436 | * 'foobar.js', |
||
| 1437 | * array( |
||
| 1438 | * 'mysite/javascript/foo.js', |
||
| 1439 | * 'mysite/javascript/bar.js', |
||
| 1440 | * ), |
||
| 1441 | * array( |
||
| 1442 | * 'async' => true, |
||
| 1443 | * 'defer' => true, |
||
| 1444 | * ) |
||
| 1445 | * ); |
||
| 1446 | * </code> |
||
| 1447 | * |
||
| 1448 | * Example for combined CSS: |
||
| 1449 | * <code> |
||
| 1450 | * Requirements::combine_files( |
||
| 1451 | * 'foobar.css', |
||
| 1452 | * array( |
||
| 1453 | * 'mysite/javascript/foo.css', |
||
| 1454 | * 'mysite/javascript/bar.css', |
||
| 1455 | * ), |
||
| 1456 | * array( |
||
| 1457 | * 'media' => 'print', |
||
| 1458 | * ) |
||
| 1459 | * ); |
||
| 1460 | * </code> |
||
| 1461 | * |
||
| 1462 | * @param string $combinedFileName Filename of the combined file relative to docroot |
||
| 1463 | * @param array $files Array of filenames relative to docroot |
||
| 1464 | * @param array $options Array of options for combining files. Available options are: |
||
| 1465 | * - 'media' : If including CSS Files, you can specify a media type |
||
| 1466 | * - 'async' : If including JavaScript Files, boolean value to set async attribute to script tag |
||
| 1467 | * - 'defer' : If including JavaScript Files, boolean value to set defer attribute to script tag |
||
| 1468 | */ |
||
| 1469 | public function combineFiles($combinedFileName, $files, $options = array()) { |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * Return path and type of given combined file |
||
| 1529 | * |
||
| 1530 | * @param string|array $file Either a file path, or an array spec |
||
| 1531 | * @return array array with two elements, path and type of file |
||
| 1532 | */ |
||
| 1533 | protected function parseCombinedFile($file) { |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Return all combined files; keys are the combined file names, values are lists of |
||
| 1559 | * associative arrays with 'files', 'type', and 'media' keys for details about this |
||
| 1560 | * combined file. |
||
| 1561 | * |
||
| 1562 | * @return array |
||
| 1563 | */ |
||
| 1564 | public function getCombinedFiles() { |
||
| 1567 | |||
| 1568 | /** |
||
| 1569 | * Includes all combined files, including blocked ones |
||
| 1570 | * |
||
| 1571 | * @return array |
||
| 1572 | */ |
||
| 1573 | protected function getAllCombinedFiles() { |
||
| 1576 | |||
| 1577 | /** |
||
| 1578 | * Clears all combined files |
||
| 1579 | */ |
||
| 1580 | public function deleteAllCombinedFiles() { |
||
| 1586 | |||
| 1587 | /** |
||
| 1588 | * Clear all registered CSS and JavaScript file combinations |
||
| 1589 | */ |
||
| 1590 | public function clearCombinedFiles() { |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Do the heavy lifting involved in combining the combined files. |
||
| 1596 | */ |
||
| 1597 | public function processCombinedFiles() { |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Given a set of files, combine them (as necessary) and return the url |
||
| 1665 | * |
||
| 1666 | * @param string $combinedFile Filename for this combined file |
||
| 1667 | * @param array $fileList List of files to combine |
||
| 1668 | * @param string $type Either 'js' or 'css' |
||
| 1669 | * @return string|null URL to this resource, if there are files to combine |
||
| 1670 | */ |
||
| 1671 | protected function getCombinedFileURL($combinedFile, $fileList, $type) { |
||
| 1722 | |||
| 1723 | /** |
||
| 1724 | * Given a filename and list of files, generate a new filename unique to these files |
||
| 1725 | * |
||
| 1726 | * @param string $combinedFile |
||
| 1727 | * @param array $fileList |
||
| 1728 | * @return string |
||
| 1729 | */ |
||
| 1730 | protected function hashedCombinedFilename($combinedFile, $fileList) { |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Check if combined files are enabled |
||
| 1739 | * |
||
| 1740 | * @return bool |
||
| 1741 | */ |
||
| 1742 | public function getCombinedFilesEnabled() { |
||
| 1765 | |||
| 1766 | /** |
||
| 1767 | * For a given filelist, determine some discriminating value to determine if |
||
| 1768 | * any of these files have changed. |
||
| 1769 | * |
||
| 1770 | * @param array $fileList List of files |
||
| 1771 | * @return string SHA1 bashed file hash |
||
| 1772 | */ |
||
| 1773 | protected function hashOfFiles($fileList) { |
||
| 1786 | |||
| 1787 | /** |
||
| 1788 | * Registers the given themeable stylesheet as required. |
||
| 1789 | * |
||
| 1790 | * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for, |
||
| 1791 | * and it that doesn't exist and the module parameter is set then a CSS file with that name in |
||
| 1792 | * the module is used. |
||
| 1793 | * |
||
| 1794 | * @param string $name The name of the file - eg '/css/File.css' would have the name 'File' |
||
| 1795 | * @param string $module The module to fall back to if the css file does not exist in the |
||
| 1796 | * current theme. |
||
| 1797 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1798 | * (e.g. 'screen,projector') |
||
| 1799 | */ |
||
| 1800 | public function themedCSS($name, $module = null, $media = null) { |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Output debugging information. |
||
| 1821 | */ |
||
| 1822 | public function debug() { |
||
| 1830 | |||
| 1831 | } |
||
| 1832 | |||
| 1848 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.