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 |
||
| 471 | class Requirements_Backend |
||
| 472 | { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Whether to add caching query params to the requests for file-based requirements. |
||
| 476 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 477 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 478 | * while automatically busting this cache every time the file is changed. |
||
| 479 | * |
||
| 480 | * @var bool |
||
| 481 | */ |
||
| 482 | protected $suffixRequirements = true; |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Whether to combine CSS and JavaScript files |
||
| 486 | * |
||
| 487 | * @var bool |
||
| 488 | */ |
||
| 489 | protected $combinedFilesEnabled = true; |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Determine if files should be combined automatically on dev mode. |
||
| 493 | * |
||
| 494 | * By default combined files will not be combined except in test or |
||
| 495 | * live environments. Turning this on will allow for pre-combining of files in development mode. |
||
| 496 | * |
||
| 497 | * @config |
||
| 498 | * @var bool |
||
| 499 | */ |
||
| 500 | private static $combine_in_dev = false; |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Paths to all required JavaScript files relative to docroot |
||
| 504 | * |
||
| 505 | * @var array |
||
| 506 | */ |
||
| 507 | protected $javascript = array(); |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Map of included scripts to array of contained files. |
||
| 511 | * To be used alongside front-end combination mechanisms. |
||
| 512 | * |
||
| 513 | * @var array Map of providing filepath => array(provided filepaths) |
||
| 514 | */ |
||
| 515 | protected $providedJavascript = array(); |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Paths to all required CSS files relative to the docroot. |
||
| 519 | * |
||
| 520 | * @var array |
||
| 521 | */ |
||
| 522 | protected $css = array(); |
||
| 523 | |||
| 524 | /** |
||
| 525 | * All custom javascript code that is inserted into the page's HTML |
||
| 526 | * |
||
| 527 | * @var array |
||
| 528 | */ |
||
| 529 | protected $customScript = array(); |
||
| 530 | |||
| 531 | /** |
||
| 532 | * All custom CSS rules which are inserted directly at the bottom of the HTML <head> tag |
||
| 533 | * |
||
| 534 | * @var array |
||
| 535 | */ |
||
| 536 | protected $customCSS = array(); |
||
| 537 | |||
| 538 | /** |
||
| 539 | * All custom HTML markup which is added before the closing <head> tag, e.g. additional |
||
| 540 | * metatags. |
||
| 541 | * |
||
| 542 | * @var array |
||
| 543 | */ |
||
| 544 | protected $customHeadTags = array(); |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Remembers the file paths or uniquenessIDs of all Requirements cleared through |
||
| 548 | * {@link clear()}, so that they can be restored later. |
||
| 549 | * |
||
| 550 | * @var array |
||
| 551 | */ |
||
| 552 | protected $disabled = array(); |
||
| 553 | |||
| 554 | /** |
||
| 555 | * The file paths (relative to docroot) or uniquenessIDs of any included requirements which |
||
| 556 | * should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example, |
||
| 557 | * to block scripts included by a superclass without having to override entire functions and |
||
| 558 | * duplicate a lot of code. |
||
| 559 | * |
||
| 560 | * Use {@link unblock()} or {@link unblock_all()} to revert changes. |
||
| 561 | * |
||
| 562 | * @var array |
||
| 563 | */ |
||
| 564 | protected $blocked = array(); |
||
| 565 | |||
| 566 | /** |
||
| 567 | * A list of combined files registered via {@link combine_files()}. Keys are the output file |
||
| 568 | * names, values are lists of input files. |
||
| 569 | * |
||
| 570 | * @var array |
||
| 571 | */ |
||
| 572 | protected $combinedFiles = array(); |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Use the JSMin library to minify any javascript file passed to {@link combine_files()}. |
||
| 576 | * |
||
| 577 | * @var bool |
||
| 578 | */ |
||
| 579 | protected $minifyCombinedJSFiles = true; |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Whether or not file headers should be written when combining files |
||
| 583 | * |
||
| 584 | * @var boolean |
||
| 585 | */ |
||
| 586 | protected $writeHeaderComment = true; |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Where to save combined files. By default they're placed in assets/_combinedfiles, however |
||
| 590 | * this may be an issue depending on your setup, especially for CSS files which often contain |
||
| 591 | * relative paths. |
||
| 592 | * |
||
| 593 | * @var string |
||
| 594 | */ |
||
| 595 | protected $combinedFilesFolder = null; |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Put all JavaScript includes at the bottom of the template before the closing <body> tag, |
||
| 599 | * rather than the default behaviour of placing them at the end of the <head> tag. This means |
||
| 600 | * script downloads won't block other HTTP requests, which can be a performance improvement. |
||
| 601 | * |
||
| 602 | * @var bool |
||
| 603 | */ |
||
| 604 | public $writeJavascriptToBody = true; |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
||
| 608 | * |
||
| 609 | * @var boolean |
||
| 610 | */ |
||
| 611 | protected $forceJSToBottom = false; |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Configures the default prefix for combined files. |
||
| 615 | * |
||
| 616 | * This defaults to `_combinedfiles`, and is the folder within the configured asset backend that |
||
| 617 | * combined files will be stored in. If using a backend shared with other systems, it is usually |
||
| 618 | * necessary to distinguish combined files from other assets. |
||
| 619 | * |
||
| 620 | * @config |
||
| 621 | * @var string |
||
| 622 | */ |
||
| 623 | private static $default_combined_files_folder = '_combinedfiles'; |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Flag to include the hash in the querystring instead of the filename for combined files. |
||
| 627 | * |
||
| 628 | * By default the `<hash>` of the source files is appended to the end of the combined file |
||
| 629 | * (prior to the file extension). If combined files are versioned in source control or running |
||
| 630 | * in a distributed environment (such as one where the newest version of a file may not always be |
||
| 631 | * immediately available) then it may sometimes be necessary to disable this. When this is set to true, |
||
| 632 | * the hash will instead be appended via a querystring parameter to enable cache busting, but not in |
||
| 633 | * the filename itself. I.e. `assets/_combinedfiles/name.js?m=<hash>` |
||
| 634 | * |
||
| 635 | * @config |
||
| 636 | * @var bool |
||
| 637 | */ |
||
| 638 | private static $combine_hash_querystring = false; |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @var GeneratedAssetHandler |
||
| 642 | */ |
||
| 643 | protected $assetHandler = null; |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Gets the backend storage for generated files |
||
| 647 | * |
||
| 648 | * @return GeneratedAssetHandler |
||
| 649 | */ |
||
| 650 | public function getAssetHandler() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Set a new asset handler for this backend |
||
| 656 | * |
||
| 657 | * @param GeneratedAssetHandler $handler |
||
| 658 | */ |
||
| 659 | public function setAssetHandler(GeneratedAssetHandler $handler) { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Enable or disable the combination of CSS and JavaScript files |
||
| 665 | * |
||
| 666 | * @param bool $enable |
||
| 667 | */ |
||
| 668 | public function setCombinedFilesEnabled($enable) { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Check if header comments are written |
||
| 674 | * |
||
| 675 | * @return bool |
||
| 676 | */ |
||
| 677 | public function getWriteHeaderComment() { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Flag whether header comments should be written for each combined file |
||
| 683 | * |
||
| 684 | * @param bool $write |
||
| 685 | * @return $this |
||
| 686 | */ |
||
| 687 | public function setWriteHeaderComment($write) { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Set the folder to save combined files in. By default they're placed in _combinedfiles, |
||
| 694 | * however this may be an issue depending on your setup, especially for CSS files which often |
||
| 695 | * contain relative paths. |
||
| 696 | * |
||
| 697 | * This must not include any 'assets' prefix |
||
| 698 | * |
||
| 699 | * @param string $folder |
||
| 700 | */ |
||
| 701 | public function setCombinedFilesFolder($folder) { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Retrieve the combined files folder prefix |
||
| 707 | * |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | public function getCombinedFilesFolder() { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Set whether to add caching query params to the requests for file-based requirements. |
||
| 719 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 720 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 721 | * while automatically busting this cache every time the file is changed. |
||
| 722 | * |
||
| 723 | * @param bool |
||
| 724 | */ |
||
| 725 | public function setSuffixRequirements($var) { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Check whether we want to suffix requirements |
||
| 731 | * |
||
| 732 | * @return bool |
||
| 733 | */ |
||
| 734 | public function getSuffixRequirements() { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Set whether you want to write the JS to the body of the page rather than at the end of the |
||
| 740 | * head tag. |
||
| 741 | * |
||
| 742 | * @param bool |
||
| 743 | * @return $this |
||
| 744 | */ |
||
| 745 | public function setWriteJavascriptToBody($var) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Check whether you want to write the JS to the body of the page rather than at the end of the |
||
| 752 | * head tag. |
||
| 753 | * |
||
| 754 | * @return bool |
||
| 755 | */ |
||
| 756 | public function getWriteJavascriptToBody() { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Forces the JavaScript requirements to the end of the body, right before the closing tag |
||
| 762 | * |
||
| 763 | * @param bool |
||
| 764 | * @return $this |
||
| 765 | */ |
||
| 766 | public function setForceJSToBottom($var) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Check if the JavaScript requirements are written to the end of the body, right before the closing tag |
||
| 773 | * |
||
| 774 | * @return bool |
||
| 775 | */ |
||
| 776 | public function getForceJSToBottom() { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Check if minify js files should be combined |
||
| 782 | * |
||
| 783 | * @return bool |
||
| 784 | */ |
||
| 785 | public function getMinifyCombinedJSFiles() { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Set if combined js files should be minified |
||
| 791 | * |
||
| 792 | * @param bool $minify |
||
| 793 | * @return $this |
||
| 794 | */ |
||
| 795 | public function setMinifyCombinedJSFiles($minify) { |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Register the given JavaScript file as required. |
||
| 802 | * |
||
| 803 | * @param string $file Relative to docroot |
||
| 804 | * @param array $options List of options. Available options include: |
||
| 805 | * - 'provides' : List of scripts files included in this file |
||
| 806 | */ |
||
| 807 | public function javascript($file, $options = array()) { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Remove a javascript requirement |
||
| 818 | * |
||
| 819 | * @param string $file |
||
| 820 | */ |
||
| 821 | protected function unsetJavascript($file) { |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Gets all scripts that are already provided by prior scripts. |
||
| 827 | * This follows these rules: |
||
| 828 | * - Files will not be considered provided if they are separately |
||
| 829 | * included prior to the providing file. |
||
| 830 | * - Providing files can be blocked, and don't provide anything |
||
| 831 | * - Provided files can't be blocked (you need to block the provider) |
||
| 832 | * - If a combined file includes files that are provided by prior |
||
| 833 | * scripts, then these should be excluded from the combined file. |
||
| 834 | * - If a combined file includes files that are provided by later |
||
| 835 | * scripts, then these files should be included in the combined |
||
| 836 | * file, but we can't block the later script either (possible double |
||
| 837 | * up of file). |
||
| 838 | * |
||
| 839 | * @return array Array of provided files (map of $path => $path) |
||
| 840 | */ |
||
| 841 | public function getProvidedScripts() { |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Returns an array of required JavaScript, excluding blocked |
||
| 867 | * and duplicates of provided files. |
||
| 868 | * |
||
| 869 | * @return array |
||
| 870 | */ |
||
| 871 | public function getJavascript() { |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Gets all javascript, including blocked files. Unwraps the array into a non-associative list |
||
| 881 | * |
||
| 882 | * @return array Indexed array of javascript files |
||
| 883 | */ |
||
| 884 | protected function getAllJavascript() { |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Register the given JavaScript code into the list of requirements |
||
| 890 | * |
||
| 891 | * @param string $script The script content as a string (without enclosing <script> tag) |
||
| 892 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 893 | */ |
||
| 894 | public function customScript($script, $uniquenessID = null) { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Return all registered custom scripts |
||
| 904 | * |
||
| 905 | * @return array |
||
| 906 | */ |
||
| 907 | public function getCustomScripts() { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Register the given CSS styles into the list of requirements |
||
| 913 | * |
||
| 914 | * @param string $script CSS selectors as a string (without enclosing <style> tag) |
||
| 915 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 916 | */ |
||
| 917 | public function customCSS($script, $uniquenessID = null) { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Return all registered custom CSS |
||
| 927 | * |
||
| 928 | * @return array |
||
| 929 | */ |
||
| 930 | public function getCustomCSS() { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Add the following custom HTML code to the <head> section of the page |
||
| 936 | * |
||
| 937 | * @param string $html Custom HTML code |
||
| 938 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 939 | */ |
||
| 940 | public function insertHeadTags($html, $uniquenessID = null) { |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Return all custom head tags |
||
| 950 | * |
||
| 951 | * @return array |
||
| 952 | */ |
||
| 953 | public function getCustomHeadTags() { |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Include the content of the given JavaScript file in the list of requirements. Dollar-sign |
||
| 959 | * variables will be interpolated with values from $vars similar to a .ss template. |
||
| 960 | * |
||
| 961 | * @param string $file The template file to load, relative to docroot |
||
| 962 | * @param string[] $vars The array of variables to interpolate. |
||
| 963 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 964 | */ |
||
| 965 | public function javascriptTemplate($file, $vars, $uniquenessID = null) { |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Register the given stylesheet into the list of requirements. |
||
| 981 | * |
||
| 982 | * @param string $file The CSS file to load, relative to site root |
||
| 983 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 984 | * (e.g. 'screen,projector') |
||
| 985 | */ |
||
| 986 | public function css($file, $media = null) { |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Remove a css requirement |
||
| 994 | * |
||
| 995 | * @param string $file |
||
| 996 | */ |
||
| 997 | protected function unsetCSS($file) { |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Get the list of registered CSS file requirements, excluding blocked files |
||
| 1003 | * |
||
| 1004 | * @return array Associative array of file to spec |
||
| 1005 | */ |
||
| 1006 | public function getCSS() { |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Gets all CSS files requirements, including blocked |
||
| 1012 | * |
||
| 1013 | * @return array Associative array of file to spec |
||
| 1014 | */ |
||
| 1015 | protected function getAllCSS() { |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Gets the list of all blocked files |
||
| 1021 | * |
||
| 1022 | * @return array |
||
| 1023 | */ |
||
| 1024 | public function getBlocked() { |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Clear either a single or all requirements |
||
| 1030 | * |
||
| 1031 | * Caution: Clearing single rules added via customCSS and customScript only works if you |
||
| 1032 | * originally specified a $uniquenessID. |
||
| 1033 | * |
||
| 1034 | * @param string|int $fileOrID |
||
| 1035 | */ |
||
| 1036 | public function clear($fileOrID = null) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Restore requirements cleared by call to Requirements::clear |
||
| 1061 | */ |
||
| 1062 | public function restore() { |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Block inclusion of a specific file |
||
| 1072 | * |
||
| 1073 | * The difference between this and {@link clear} is that the calling order does not matter; |
||
| 1074 | * {@link clear} must be called after the initial registration, whereas {@link block} can be |
||
| 1075 | * used in advance. This is useful, for example, to block scripts included by a superclass |
||
| 1076 | * without having to override entire functions and duplicate a lot of code. |
||
| 1077 | * |
||
| 1078 | * Note that blocking should be used sparingly because it's hard to trace where an file is |
||
| 1079 | * being blocked from. |
||
| 1080 | * |
||
| 1081 | * @param string|int $fileOrID |
||
| 1082 | */ |
||
| 1083 | public function block($fileOrID) { |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Remove an item from the block list |
||
| 1089 | * |
||
| 1090 | * @param string|int $fileOrID |
||
| 1091 | */ |
||
| 1092 | public function unblock($fileOrID) { |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Removes all items from the block list |
||
| 1098 | */ |
||
| 1099 | public function unblockAll() { |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 1105 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 1106 | * including a head and body tag. |
||
| 1107 | * |
||
| 1108 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 1109 | * through {@link SSViewer} |
||
| 1110 | * @return string HTML content augmented with the requirements tags |
||
| 1111 | */ |
||
| 1112 | public function includeInHTML($content) { |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Given a block of HTML, insert the given scripts at the bottom before |
||
| 1180 | * the closing </body> tag |
||
| 1181 | * |
||
| 1182 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1183 | * @param string $content HTML body |
||
| 1184 | * @return string Merged HTML |
||
| 1185 | */ |
||
| 1186 | protected function insertScriptsAtBottom($jsRequirements, $content) { |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Given a block of HTML, insert the given scripts inside the <body></body> |
||
| 1199 | * |
||
| 1200 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 1201 | * @param string $content HTML body |
||
| 1202 | * @return string Merged HTML |
||
| 1203 | */ |
||
| 1204 | protected function insertScriptsIntoBody($jsRequirements, $content) { |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Given a block of HTML, insert the given code inside the <head></head> block |
||
| 1235 | * |
||
| 1236 | * @param string $jsRequirements String containing one or more html tags |
||
| 1237 | * @param string $content HTML body |
||
| 1238 | * @return string Merged HTML |
||
| 1239 | */ |
||
| 1240 | protected function insertTagsIntoHead($jsRequirements, $content) { |
||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Safely escape a literal string for use in preg_replace replacement |
||
| 1251 | * |
||
| 1252 | * @param string $replacement |
||
| 1253 | * @return string |
||
| 1254 | */ |
||
| 1255 | protected function escapeReplacement($replacement) { |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 1261 | * HTTP Response |
||
| 1262 | * |
||
| 1263 | * @param SS_HTTPResponse $response |
||
| 1264 | */ |
||
| 1265 | public function includeInResponse(SS_HTTPResponse $response) { |
||
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Add i18n files from the given javascript directory. SilverStripe expects that the given |
||
| 1296 | * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, |
||
| 1297 | * etc. |
||
| 1298 | * |
||
| 1299 | * @param string $langDir The JavaScript lang directory, relative to the site root, e.g., |
||
| 1300 | * 'framework/javascript/lang' |
||
| 1301 | * @param bool $return Return all relative file paths rather than including them in |
||
| 1302 | * requirements |
||
| 1303 | * @param bool $langOnly Only include language files, not the base libraries |
||
| 1304 | * |
||
| 1305 | * @return array|null All relative files if $return is true, or null otherwise |
||
| 1306 | */ |
||
| 1307 | public function add_i18n_javascript($langDir, $return = false, $langOnly = false) { |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Finds the path for specified file |
||
| 1350 | * |
||
| 1351 | * @param string $fileOrUrl |
||
| 1352 | * @return string|bool |
||
| 1353 | */ |
||
| 1354 | protected function pathForFile($fileOrUrl) { |
||
| 1381 | |||
| 1382 | /** |
||
| 1383 | * Concatenate several css or javascript files into a single dynamically generated file. This |
||
| 1384 | * increases performance by fewer HTTP requests. |
||
| 1385 | * |
||
| 1386 | * The combined file is regenerated based on every file modification time. Optionally a |
||
| 1387 | * rebuild can be triggered by appending ?flush=1 to the URL. |
||
| 1388 | * |
||
| 1389 | * All combined files will have a comment on the start of each concatenated file denoting their |
||
| 1390 | * original position. |
||
| 1391 | * |
||
| 1392 | * CAUTION: You're responsible for ensuring that the load order for combined files is |
||
| 1393 | * retained - otherwise combining JavaScript files can lead to functional errors in the |
||
| 1394 | * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also |
||
| 1395 | * only include each file once across all includes and combinations in a single page load. |
||
| 1396 | * |
||
| 1397 | * CAUTION: Combining CSS Files discards any "media" information. |
||
| 1398 | * |
||
| 1399 | * Example for combined JavaScript: |
||
| 1400 | * <code> |
||
| 1401 | * Requirements::combine_files( |
||
| 1402 | * 'foobar.js', |
||
| 1403 | * array( |
||
| 1404 | * 'mysite/javascript/foo.js', |
||
| 1405 | * 'mysite/javascript/bar.js', |
||
| 1406 | * ) |
||
| 1407 | * ); |
||
| 1408 | * </code> |
||
| 1409 | * |
||
| 1410 | * Example for combined CSS: |
||
| 1411 | * <code> |
||
| 1412 | * Requirements::combine_files( |
||
| 1413 | * 'foobar.css', |
||
| 1414 | * array( |
||
| 1415 | * 'mysite/javascript/foo.css', |
||
| 1416 | * 'mysite/javascript/bar.css', |
||
| 1417 | * ) |
||
| 1418 | * ); |
||
| 1419 | * </code> |
||
| 1420 | * |
||
| 1421 | * @param string $combinedFileName Filename of the combined file relative to docroot |
||
| 1422 | * @param array $files Array of filenames relative to docroot |
||
| 1423 | * @param string $media If including CSS Files, you can specify a media type |
||
| 1424 | */ |
||
| 1425 | public function combineFiles($combinedFileName, $files, $media = null) { |
||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * Return path and type of given combined file |
||
| 1481 | * |
||
| 1482 | * @param string|array $file Either a file path, or an array spec |
||
| 1483 | * @return array array with two elements, path and type of file |
||
| 1484 | */ |
||
| 1485 | protected function parseCombinedFile($file) { |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Return all combined files; keys are the combined file names, values are lists of |
||
| 1511 | * associative arrays with 'files', 'type', and 'media' keys for details about this |
||
| 1512 | * combined file. |
||
| 1513 | * |
||
| 1514 | * @return array |
||
| 1515 | */ |
||
| 1516 | public function getCombinedFiles() { |
||
| 1519 | |||
| 1520 | /** |
||
| 1521 | * Includes all combined files, including blocked ones |
||
| 1522 | * |
||
| 1523 | * @return array |
||
| 1524 | */ |
||
| 1525 | protected function getAllCombinedFiles() { |
||
| 1528 | |||
| 1529 | /** |
||
| 1530 | * Clears all combined files |
||
| 1531 | */ |
||
| 1532 | public function deleteAllCombinedFiles() { |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * Clear all registered CSS and JavaScript file combinations |
||
| 1541 | */ |
||
| 1542 | public function clearCombinedFiles() { |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Do the heavy lifting involved in combining the combined files. |
||
| 1548 | */ |
||
| 1549 | public function processCombinedFiles() { |
||
| 1614 | |||
| 1615 | /** |
||
| 1616 | * Given a set of files, combine them (as necessary) and return the url |
||
| 1617 | * |
||
| 1618 | * @param string $combinedFile Filename for this combined file |
||
| 1619 | * @param array $fileList List of files to combine |
||
| 1620 | * @param string $type Either 'js' or 'css' |
||
| 1621 | * @return string|null URL to this resource, if there are files to combine |
||
| 1622 | */ |
||
| 1623 | protected function getCombinedFileURL($combinedFile, $fileList, $type) { |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Given a filename and list of files, generate a new filename unique to these files |
||
| 1677 | * |
||
| 1678 | * @param string $combinedFile |
||
| 1679 | * @param array $fileList |
||
| 1680 | * @return string |
||
| 1681 | */ |
||
| 1682 | protected function hashedCombinedFilename($combinedFile, $fileList) { |
||
| 1688 | |||
| 1689 | /** |
||
| 1690 | * Check if combined files are enabled |
||
| 1691 | * |
||
| 1692 | * @return bool |
||
| 1693 | */ |
||
| 1694 | public function getCombinedFilesEnabled() { |
||
| 1717 | |||
| 1718 | /** |
||
| 1719 | * For a given filelist, determine some discriminating value to determine if |
||
| 1720 | * any of these files have changed. |
||
| 1721 | * |
||
| 1722 | * @param array $fileList List of files |
||
| 1723 | * @return string SHA1 bashed file hash |
||
| 1724 | */ |
||
| 1725 | protected function hashOfFiles($fileList) { |
||
| 1738 | |||
| 1739 | /** |
||
| 1740 | * Registers the given themeable stylesheet as required. |
||
| 1741 | * |
||
| 1742 | * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for, |
||
| 1743 | * and it that doesn't exist and the module parameter is set then a CSS file with that name in |
||
| 1744 | * the module is used. |
||
| 1745 | * |
||
| 1746 | * @param string $name The name of the file - eg '/css/File.css' would have the name 'File' |
||
| 1747 | * @param string $module The module to fall back to if the css file does not exist in the |
||
| 1748 | * current theme. |
||
| 1749 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1750 | * (e.g. 'screen,projector') |
||
| 1751 | */ |
||
| 1752 | public function themedCSS($name, $module = null, $media = null) { |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Output debugging information. |
||
| 1773 | */ |
||
| 1774 | public function debug() { |
||
| 1782 | |||
| 1783 | } |
||
| 1784 | |||
| 1800 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: