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 |
||
| 19 | class Requirements_Backend |
||
| 20 | { |
||
| 21 | use Injectable; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Whether to add caching query params to the requests for file-based requirements. |
||
| 25 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 26 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 27 | * while automatically busting this cache every time the file is changed. |
||
| 28 | * |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | protected $suffixRequirements = true; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Whether to combine CSS and JavaScript files |
||
| 35 | * |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $combinedFilesEnabled = true; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Determine if files should be combined automatically on dev mode. |
||
| 42 | * |
||
| 43 | * By default combined files will not be combined except in test or |
||
| 44 | * live environments. Turning this on will allow for pre-combining of files in development mode. |
||
| 45 | * |
||
| 46 | * @config |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private static $combine_in_dev = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Paths to all required JavaScript files relative to docroot |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $javascript = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Map of included scripts to array of contained files. |
||
| 60 | * To be used alongside front-end combination mechanisms. |
||
| 61 | * |
||
| 62 | * @var array Map of providing filepath => array(provided filepaths) |
||
| 63 | */ |
||
| 64 | protected $providedJavascript = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Paths to all required CSS files relative to the docroot. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $css = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * All custom javascript code that is inserted into the page's HTML |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $customScript = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * All custom CSS rules which are inserted directly at the bottom of the HTML <head> tag |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $customCSS = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * All custom HTML markup which is added before the closing <head> tag, e.g. additional |
||
| 89 | * metatags. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $customHeadTags = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Remembers the file paths or uniquenessIDs of all Requirements cleared through |
||
| 97 | * {@link clear()}, so that they can be restored later. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $disabled = array(); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The file paths (relative to docroot) or uniquenessIDs of any included requirements which |
||
| 105 | * should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example, |
||
| 106 | * to block scripts included by a superclass without having to override entire functions and |
||
| 107 | * duplicate a lot of code. |
||
| 108 | * |
||
| 109 | * Use {@link unblock()} or {@link unblock_all()} to revert changes. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | protected $blocked = array(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * A list of combined files registered via {@link combine_files()}. Keys are the output file |
||
| 117 | * names, values are lists of input files. |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $combinedFiles = array(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Use the injected minification service to minify any javascript file passed to {@link combine_files()}. |
||
| 125 | * |
||
| 126 | * @var bool |
||
| 127 | */ |
||
| 128 | protected $minifyCombinedFiles = false; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Whether or not file headers should be written when combining files |
||
| 132 | * |
||
| 133 | * @var boolean |
||
| 134 | */ |
||
| 135 | protected $writeHeaderComment = true; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Where to save combined files. By default they're placed in assets/_combinedfiles, however |
||
| 139 | * this may be an issue depending on your setup, especially for CSS files which often contain |
||
| 140 | * relative paths. |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $combinedFilesFolder = null; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Put all JavaScript includes at the bottom of the template before the closing <body> tag, |
||
| 148 | * rather than the default behaviour of placing them at the end of the <head> tag. This means |
||
| 149 | * script downloads won't block other HTTP requests, which can be a performance improvement. |
||
| 150 | * |
||
| 151 | * @var bool |
||
| 152 | */ |
||
| 153 | public $writeJavascriptToBody = true; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
||
| 157 | * |
||
| 158 | * @var boolean |
||
| 159 | */ |
||
| 160 | protected $forceJSToBottom = false; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Configures the default prefix for combined files. |
||
| 164 | * |
||
| 165 | * This defaults to `_combinedfiles`, and is the folder within the configured asset backend that |
||
| 166 | * combined files will be stored in. If using a backend shared with other systems, it is usually |
||
| 167 | * necessary to distinguish combined files from other assets. |
||
| 168 | * |
||
| 169 | * @config |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | private static $default_combined_files_folder = '_combinedfiles'; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Flag to include the hash in the querystring instead of the filename for combined files. |
||
| 176 | * |
||
| 177 | * By default the `<hash>` of the source files is appended to the end of the combined file |
||
| 178 | * (prior to the file extension). If combined files are versioned in source control or running |
||
| 179 | * in a distributed environment (such as one where the newest version of a file may not always be |
||
| 180 | * immediately available) then it may sometimes be necessary to disable this. When this is set to true, |
||
| 181 | * the hash will instead be appended via a querystring parameter to enable cache busting, but not in |
||
| 182 | * the filename itself. I.e. `assets/_combinedfiles/name.js?m=<hash>` |
||
| 183 | * |
||
| 184 | * @config |
||
| 185 | * @var bool |
||
| 186 | */ |
||
| 187 | private static $combine_hash_querystring = false; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var GeneratedAssetHandler |
||
| 191 | */ |
||
| 192 | protected $assetHandler = null; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var Requirements_Minifier |
||
| 196 | */ |
||
| 197 | protected $minifier = null; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Gets the backend storage for generated files |
||
| 201 | * |
||
| 202 | * @return GeneratedAssetHandler |
||
| 203 | */ |
||
| 204 | public function getAssetHandler() |
||
| 205 | { |
||
| 206 | return $this->assetHandler; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set a new asset handler for this backend |
||
| 211 | * |
||
| 212 | * @param GeneratedAssetHandler $handler |
||
| 213 | */ |
||
| 214 | public function setAssetHandler(GeneratedAssetHandler $handler) |
||
| 215 | { |
||
| 216 | $this->assetHandler = $handler; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Gets the minification service for this backend |
||
| 221 | * |
||
| 222 | * @deprecated 4.0..5.0 |
||
| 223 | * @return Requirements_Minifier |
||
| 224 | */ |
||
| 225 | public function getMinifier() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set a new minification service for this backend |
||
| 232 | * |
||
| 233 | * @param Requirements_Minifier $minifier |
||
| 234 | */ |
||
| 235 | public function setMinifier(Requirements_Minifier $minifier = null) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Enable or disable the combination of CSS and JavaScript files |
||
| 242 | * |
||
| 243 | * @param bool $enable |
||
| 244 | */ |
||
| 245 | public function setCombinedFilesEnabled($enable) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Check if header comments are written |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function getWriteHeaderComment() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Flag whether header comments should be written for each combined file |
||
| 262 | * |
||
| 263 | * @param bool $write |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function setWriteHeaderComment($write) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set the folder to save combined files in. By default they're placed in _combinedfiles, |
||
| 274 | * however this may be an issue depending on your setup, especially for CSS files which often |
||
| 275 | * contain relative paths. |
||
| 276 | * |
||
| 277 | * This must not include any 'assets' prefix |
||
| 278 | * |
||
| 279 | * @param string $folder |
||
| 280 | */ |
||
| 281 | public function setCombinedFilesFolder($folder) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Retrieve the combined files folder prefix |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getCombinedFilesFolder() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set whether to add caching query params to the requests for file-based requirements. |
||
| 301 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 302 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 303 | * while automatically busting this cache every time the file is changed. |
||
| 304 | * |
||
| 305 | * @param bool |
||
| 306 | */ |
||
| 307 | public function setSuffixRequirements($var) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Check whether we want to suffix requirements |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function getSuffixRequirements() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set whether you want to write the JS to the body of the page rather than at the end of the |
||
| 324 | * head tag. |
||
| 325 | * |
||
| 326 | * @param bool |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | public function setWriteJavascriptToBody($var) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check whether you want to write the JS to the body of the page rather than at the end of the |
||
| 337 | * head tag. |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function getWriteJavascriptToBody() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Forces the JavaScript requirements to the end of the body, right before the closing tag |
||
| 348 | * |
||
| 349 | * @param bool |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | public function setForceJSToBottom($var) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check if the JavaScript requirements are written to the end of the body, right before the closing tag |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | public function getForceJSToBottom() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Check if minify files should be combined |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public function getMinifyCombinedFiles() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Set if combined files should be minified |
||
| 380 | * |
||
| 381 | * @param bool $minify |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | public function setMinifyCombinedFiles($minify) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Register the given JavaScript file as required. |
||
| 392 | * |
||
| 393 | * @param string $file Relative to docroot |
||
| 394 | * @param array $options List of options. Available options include: |
||
| 395 | * - 'provides' : List of scripts files included in this file |
||
| 396 | * - 'async' : Boolean value to set async attribute to script tag |
||
| 397 | * - 'defer' : Boolean value to set defer attribute to script tag |
||
| 398 | * - 'type' : Override script type= value. |
||
| 399 | */ |
||
| 400 | public function javascript($file, $options = array()) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Remove a javascript requirement |
||
| 442 | * |
||
| 443 | * @param string $file |
||
| 444 | */ |
||
| 445 | protected function unsetJavascript($file) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Gets all scripts that are already provided by prior scripts. |
||
| 452 | * This follows these rules: |
||
| 453 | * - Files will not be considered provided if they are separately |
||
| 454 | * included prior to the providing file. |
||
| 455 | * - Providing files can be blocked, and don't provide anything |
||
| 456 | * - Provided files can't be blocked (you need to block the provider) |
||
| 457 | * - If a combined file includes files that are provided by prior |
||
| 458 | * scripts, then these should be excluded from the combined file. |
||
| 459 | * - If a combined file includes files that are provided by later |
||
| 460 | * scripts, then these files should be included in the combined |
||
| 461 | * file, but we can't block the later script either (possible double |
||
| 462 | * up of file). |
||
| 463 | * |
||
| 464 | * @return array Array of provided files (map of $path => $path) |
||
| 465 | */ |
||
| 466 | public function getProvidedScripts() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns an array of required JavaScript, excluding blocked |
||
| 493 | * and duplicates of provided files. |
||
| 494 | * |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | public function getJavascript() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Gets all javascript, including blocked files. Unwraps the array into a non-associative list |
||
| 508 | * |
||
| 509 | * @return array Indexed array of javascript files |
||
| 510 | */ |
||
| 511 | protected function getAllJavascript() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Register the given JavaScript code into the list of requirements |
||
| 518 | * |
||
| 519 | * @param string $script The script content as a string (without enclosing <script> tag) |
||
| 520 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 521 | */ |
||
| 522 | public function customScript($script, $uniquenessID = null) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Return all registered custom scripts |
||
| 533 | * |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | public function getCustomScripts() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Register the given CSS styles into the list of requirements |
||
| 543 | * |
||
| 544 | * @param string $script CSS selectors as a string (without enclosing <style> tag) |
||
| 545 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 546 | */ |
||
| 547 | public function customCSS($script, $uniquenessID = null) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Return all registered custom CSS |
||
| 558 | * |
||
| 559 | * @return array |
||
| 560 | */ |
||
| 561 | public function getCustomCSS() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Add the following custom HTML code to the <head> section of the page |
||
| 568 | * |
||
| 569 | * @param string $html Custom HTML code |
||
| 570 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 571 | */ |
||
| 572 | public function insertHeadTags($html, $uniquenessID = null) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Return all custom head tags |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | public function getCustomHeadTags() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Include the content of the given JavaScript file in the list of requirements. Dollar-sign |
||
| 593 | * variables will be interpolated with values from $vars similar to a .ss template. |
||
| 594 | * |
||
| 595 | * @param string $file The template file to load, relative to docroot |
||
| 596 | * @param string[] $vars The array of variables to interpolate. |
||
| 597 | * @param string $uniquenessID A unique ID that ensures a piece of code is only added once |
||
| 598 | */ |
||
| 599 | public function javascriptTemplate($file, $vars, $uniquenessID = null) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Register the given stylesheet into the list of requirements. |
||
| 618 | * |
||
| 619 | * @param string $file The CSS file to load, relative to site root |
||
| 620 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 621 | * (e.g. 'screen,projector') |
||
| 622 | */ |
||
| 623 | public function css($file, $media = null) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Remove a css requirement |
||
| 632 | * |
||
| 633 | * @param string $file |
||
| 634 | */ |
||
| 635 | protected function unsetCSS($file) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Get the list of registered CSS file requirements, excluding blocked files |
||
| 642 | * |
||
| 643 | * @return array Associative array of file to spec |
||
| 644 | */ |
||
| 645 | public function getCSS() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Gets all CSS files requirements, including blocked |
||
| 652 | * |
||
| 653 | * @return array Associative array of file to spec |
||
| 654 | */ |
||
| 655 | protected function getAllCSS() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Gets the list of all blocked files |
||
| 662 | * |
||
| 663 | * @return array |
||
| 664 | */ |
||
| 665 | public function getBlocked() |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Clear either a single or all requirements |
||
| 672 | * |
||
| 673 | * Caution: Clearing single rules added via customCSS and customScript only works if you |
||
| 674 | * originally specified a $uniquenessID. |
||
| 675 | * |
||
| 676 | * @param string|int $fileOrID |
||
| 677 | */ |
||
| 678 | public function clear($fileOrID = null) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Restore requirements cleared by call to Requirements::clear |
||
| 704 | */ |
||
| 705 | public function restore() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Block inclusion of a specific file |
||
| 716 | * |
||
| 717 | * The difference between this and {@link clear} is that the calling order does not matter; |
||
| 718 | * {@link clear} must be called after the initial registration, whereas {@link block} can be |
||
| 719 | * used in advance. This is useful, for example, to block scripts included by a superclass |
||
| 720 | * without having to override entire functions and duplicate a lot of code. |
||
| 721 | * |
||
| 722 | * Note that blocking should be used sparingly because it's hard to trace where an file is |
||
| 723 | * being blocked from. |
||
| 724 | * |
||
| 725 | * @param string|int $fileOrID |
||
| 726 | */ |
||
| 727 | public function block($fileOrID) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Remove an item from the block list |
||
| 734 | * |
||
| 735 | * @param string|int $fileOrID |
||
| 736 | */ |
||
| 737 | public function unblock($fileOrID) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Removes all items from the block list |
||
| 744 | */ |
||
| 745 | public function unblockAll() |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 752 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 753 | * including a head and body tag. |
||
| 754 | * |
||
| 755 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 756 | * through {@link SSViewer} |
||
| 757 | * @return string HTML content augmented with the requirements tags |
||
| 758 | */ |
||
| 759 | public function includeInHTML($content) |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Given a block of HTML, insert the given scripts at the bottom before |
||
| 831 | * the closing </body> tag |
||
| 832 | * |
||
| 833 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 834 | * @param string $content HTML body |
||
| 835 | * @return string Merged HTML |
||
| 836 | */ |
||
| 837 | protected function insertScriptsAtBottom($jsRequirements, $content) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Given a block of HTML, insert the given scripts inside the <body></body> |
||
| 851 | * |
||
| 852 | * @param string $jsRequirements String containing one or more javascript <script /> tags |
||
| 853 | * @param string $content HTML body |
||
| 854 | * @return string Merged HTML |
||
| 855 | */ |
||
| 856 | protected function insertScriptsIntoBody($jsRequirements, $content) |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Given a block of HTML, insert the given code inside the <head></head> block |
||
| 888 | * |
||
| 889 | * @param string $jsRequirements String containing one or more html tags |
||
| 890 | * @param string $content HTML body |
||
| 891 | * @return string Merged HTML |
||
| 892 | */ |
||
| 893 | protected function insertTagsIntoHead($jsRequirements, $content) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Safely escape a literal string for use in preg_replace replacement |
||
| 905 | * |
||
| 906 | * @param string $replacement |
||
| 907 | * @return string |
||
| 908 | */ |
||
| 909 | protected function escapeReplacement($replacement) |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 916 | * HTTP Response |
||
| 917 | * |
||
| 918 | * @param HTTPResponse $response |
||
| 919 | */ |
||
| 920 | public function includeInResponse(HTTPResponse $response) |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Add i18n files from the given javascript directory. SilverStripe expects that the given |
||
| 952 | * directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js, |
||
| 953 | * etc. |
||
| 954 | * |
||
| 955 | * @param string $langDir The JavaScript lang directory, relative to the site root, e.g., |
||
| 956 | * 'framework/javascript/lang' |
||
| 957 | * @param bool $return Return all relative file paths rather than including them in |
||
| 958 | * requirements |
||
| 959 | * |
||
| 960 | * @return array|null All relative files if $return is true, or null otherwise |
||
| 961 | */ |
||
| 962 | public function add_i18n_javascript($langDir, $return = false) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Finds the path for specified file |
||
| 997 | * |
||
| 998 | * @param string $fileOrUrl |
||
| 999 | * @return string|bool |
||
| 1000 | */ |
||
| 1001 | protected function pathForFile($fileOrUrl) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Concatenate several css or javascript files into a single dynamically generated file. This |
||
| 1032 | * increases performance by fewer HTTP requests. |
||
| 1033 | * |
||
| 1034 | * The combined file is regenerated based on every file modification time. Optionally a |
||
| 1035 | * rebuild can be triggered by appending ?flush=1 to the URL. |
||
| 1036 | * |
||
| 1037 | * All combined files will have a comment on the start of each concatenated file denoting their |
||
| 1038 | * original position. |
||
| 1039 | * |
||
| 1040 | * CAUTION: You're responsible for ensuring that the load order for combined files is |
||
| 1041 | * retained - otherwise combining JavaScript files can lead to functional errors in the |
||
| 1042 | * JavaScript logic, and combining CSS can lead to incorrect inheritance. You can also |
||
| 1043 | * only include each file once across all includes and combinations in a single page load. |
||
| 1044 | * |
||
| 1045 | * CAUTION: Combining CSS Files discards any "media" information. |
||
| 1046 | * |
||
| 1047 | * Example for combined JavaScript: |
||
| 1048 | * <code> |
||
| 1049 | * Requirements::combine_files( |
||
| 1050 | * 'foobar.js', |
||
| 1051 | * array( |
||
| 1052 | * 'mysite/javascript/foo.js', |
||
| 1053 | * 'mysite/javascript/bar.js', |
||
| 1054 | * ), |
||
| 1055 | * array( |
||
| 1056 | * 'async' => true, |
||
| 1057 | * 'defer' => true, |
||
| 1058 | * ) |
||
| 1059 | * ); |
||
| 1060 | * </code> |
||
| 1061 | * |
||
| 1062 | * Example for combined CSS: |
||
| 1063 | * <code> |
||
| 1064 | * Requirements::combine_files( |
||
| 1065 | * 'foobar.css', |
||
| 1066 | * array( |
||
| 1067 | * 'mysite/javascript/foo.css', |
||
| 1068 | * 'mysite/javascript/bar.css', |
||
| 1069 | * ), |
||
| 1070 | * array( |
||
| 1071 | * 'media' => 'print', |
||
| 1072 | * ) |
||
| 1073 | * ); |
||
| 1074 | * </code> |
||
| 1075 | * |
||
| 1076 | * @param string $combinedFileName Filename of the combined file relative to docroot |
||
| 1077 | * @param array $files Array of filenames relative to docroot |
||
| 1078 | * @param array $options Array of options for combining files. Available options are: |
||
| 1079 | * - 'media' : If including CSS Files, you can specify a media type |
||
| 1080 | * - 'async' : If including JavaScript Files, boolean value to set async attribute to script tag |
||
| 1081 | * - 'defer' : If including JavaScript Files, boolean value to set defer attribute to script tag |
||
| 1082 | */ |
||
| 1083 | public function combineFiles($combinedFileName, $files, $options = array()) |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Return path and type of given combined file |
||
| 1144 | * |
||
| 1145 | * @param string|array $file Either a file path, or an array spec |
||
| 1146 | * @return array array with two elements, path and type of file |
||
| 1147 | */ |
||
| 1148 | protected function parseCombinedFile($file) |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Return all combined files; keys are the combined file names, values are lists of |
||
| 1175 | * associative arrays with 'files', 'type', and 'media' keys for details about this |
||
| 1176 | * combined file. |
||
| 1177 | * |
||
| 1178 | * @return array |
||
| 1179 | */ |
||
| 1180 | public function getCombinedFiles() |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Includes all combined files, including blocked ones |
||
| 1187 | * |
||
| 1188 | * @return array |
||
| 1189 | */ |
||
| 1190 | protected function getAllCombinedFiles() |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Clears all combined files |
||
| 1197 | */ |
||
| 1198 | public function deleteAllCombinedFiles() |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Clear all registered CSS and JavaScript file combinations |
||
| 1208 | */ |
||
| 1209 | public function clearCombinedFiles() |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Do the heavy lifting involved in combining the combined files. |
||
| 1216 | */ |
||
| 1217 | public function processCombinedFiles() |
||
| 1283 | |||
| 1284 | /** |
||
| 1285 | * Given a set of files, combine them (as necessary) and return the url |
||
| 1286 | * |
||
| 1287 | * @param string $combinedFile Filename for this combined file |
||
| 1288 | * @param array $fileList List of files to combine |
||
| 1289 | * @param string $type Either 'js' or 'css' |
||
| 1290 | * @return string|null URL to this resource, if there are files to combine |
||
| 1291 | */ |
||
| 1292 | protected function getCombinedFileURL($combinedFile, $fileList, $type) |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Given a filename and list of files, generate a new filename unique to these files |
||
| 1359 | * |
||
| 1360 | * @param string $combinedFile |
||
| 1361 | * @param array $fileList |
||
| 1362 | * @return string |
||
| 1363 | */ |
||
| 1364 | protected function hashedCombinedFilename($combinedFile, $fileList) |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Check if combined files are enabled |
||
| 1374 | * |
||
| 1375 | * @return bool |
||
| 1376 | */ |
||
| 1377 | public function getCombinedFilesEnabled() |
||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * For a given filelist, determine some discriminating value to determine if |
||
| 1404 | * any of these files have changed. |
||
| 1405 | * |
||
| 1406 | * @param array $fileList List of files |
||
| 1407 | * @return string SHA1 bashed file hash |
||
| 1408 | */ |
||
| 1409 | protected function hashOfFiles($fileList) |
||
| 1423 | |||
| 1424 | /** |
||
| 1425 | * Registers the given themeable stylesheet as required. |
||
| 1426 | * |
||
| 1427 | * A CSS file in the current theme path name 'themename/css/$name.css' is first searched for, |
||
| 1428 | * and it that doesn't exist and the module parameter is set then a CSS file with that name in |
||
| 1429 | * the module is used. |
||
| 1430 | * |
||
| 1431 | * @param string $name The name of the file - eg '/css/File.css' would have the name 'File' |
||
| 1432 | * @param string $media Comma-separated list of media types to use in the link tag |
||
| 1433 | * (e.g. 'screen,projector') |
||
| 1434 | */ |
||
| 1435 | public function themedCSS($name, $media = null) |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * Registers the given themeable javascript as required. |
||
| 1450 | * |
||
| 1451 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
| 1452 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
| 1453 | * the module is used. |
||
| 1454 | * |
||
| 1455 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
| 1456 | * @param string $type Comma-separated list of types to use in the script tag |
||
| 1457 | * (e.g. 'text/javascript,text/ecmascript') |
||
| 1458 | */ |
||
| 1459 | public function themedJavascript($name, $type = null) |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Output debugging information. |
||
| 1478 | */ |
||
| 1479 | public function debug() |
||
| 1488 | } |
||
| 1489 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.