Index: branches/resourceloader/phase3/includes/ResourceLoader.php |
— | — | @@ -0,0 +1,137 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + * http://www.gnu.org/copyleft/gpl.html |
| 18 | + * |
| 19 | + * @author Roan Kattouw |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * TODO: Class description |
| 25 | + */ |
| 26 | +class ResourceLoader { |
| 27 | + /** |
| 28 | + * List of modules. |
| 29 | + * |
| 30 | + * Format: |
| 31 | + * 'modulename' => array( |
| 32 | + * 'script' => 'resources/foo/bar.js', |
| 33 | + * 'loader' => 'resources/foo/loader.js', |
| 34 | + * 'style' => 'resources/foo/bar.css', |
| 35 | + * 'messages' => array( 'messagekey1', 'messagekey2' ) |
| 36 | + * ); |
| 37 | + * 'script' and 'loader' are mandatory. |
| 38 | + */ |
| 39 | + public static $modules = array( |
| 40 | + ); |
| 41 | + |
| 42 | + private $mScripts = array(); |
| 43 | + private $mStyles = array(); |
| 44 | + private $mMessages = array(); |
| 45 | + |
| 46 | + private $mUseJSMin = true; |
| 47 | + private $mUseCSSMin = true; |
| 48 | + private $mUseCSSJanus = true; |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Add a module to the output. This includes the module's |
| 53 | + * JS itself, its style and its messages. |
| 54 | + * @param $module string Module name |
| 55 | + */ |
| 56 | + public function addModule( $module ) { |
| 57 | + $this->mScripts[] = self::$modules[$module]['script']; |
| 58 | + if ( isset( $module['style'] ) ) { |
| 59 | + $this->mStyles[] = self::$modules[$module]['script']; |
| 60 | + } |
| 61 | + if ( isset( $module['messages'] ) ) { |
| 62 | + $this->mMessages = array_merge( $this->mMessages, self::$modules[$module]['messages'] ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function setUseJSMin( $use ) { |
| 67 | + $this->mUseJSMin = $use; |
| 68 | + } |
| 69 | + |
| 70 | + public function setUseCSSMin( $use ) { |
| 71 | + $this->mUseCSSMin = $use; |
| 72 | + } |
| 73 | + |
| 74 | + public function setUseCSSJanus( $use ) { |
| 75 | + $this->mUseCSSJanus = $use; |
| 76 | + } |
| 77 | + |
| 78 | + private function getStyleJS( $styles ) { |
| 79 | + $retval = ''; |
| 80 | + foreach ( $styles as $style ) { |
| 81 | + // TODO: file_get_contents() errors? |
| 82 | + $css = file_get_contents( $style ); |
| 83 | + if ( $this->mUseCSSJanus ) { |
| 84 | + $css = $this->cssJanus( $css ); |
| 85 | + } |
| 86 | + if ( $this->mUseCSSMin ) { |
| 87 | + $css = $this->cssMin( $css ); |
| 88 | + } |
| 89 | + $escCss = Xml::escapeJsString( $css ); |
| 90 | + $retval .= "\$j( 'head' ).append( '<style>$escCSS</style>' );\n"; |
| 91 | + } |
| 92 | + return $retval; |
| 93 | + } |
| 94 | + |
| 95 | + private function getMessagesJS( $messages ) { |
| 96 | + $msgs = array(); |
| 97 | + foreach ( $messages as $message ) { |
| 98 | + $escKey = Xml::escapeJsString( $message ); |
| 99 | + $escValue = Xml::escapeJsString( wfMsg( $message ) ); |
| 100 | + $msgs[] = "'$escKey': '$escValue'"; |
| 101 | + } |
| 102 | + return "mw.addMessages( {\n" . implode( ",\n", $msgs ) . "\n} );\n"; |
| 103 | + } |
| 104 | + |
| 105 | + public function getOutput() { |
| 106 | + $this->mScripts = array_unique( $this->mScripts ); |
| 107 | + $this->mStyles = array_unique( $this->mStyles ); |
| 108 | + $this->mMessages = array_unique( $this->mMessages ); |
| 109 | + $retval = ''; |
| 110 | + |
| 111 | + foreach ( $this->mScripts as $script ) { |
| 112 | + // TODO: file_get_contents() errors? |
| 113 | + $retval .= file_get_contents( $script ); |
| 114 | + } |
| 115 | + $retval .= $this->getStyleJS( $this->mStyles ); |
| 116 | + $retval .= $this->getMessagesJS( $this->mMessages ); |
| 117 | + |
| 118 | + if ( $this->mUseJSMin ) { |
| 119 | + $retval = $this->jsMin( $retval ); |
| 120 | + } |
| 121 | + return $retval; |
| 122 | + } |
| 123 | + |
| 124 | + public function jsMin( $js ) { |
| 125 | + // TODO: Implement |
| 126 | + return $js; |
| 127 | + } |
| 128 | + |
| 129 | + public function cssMin( $css ) { |
| 130 | + // TODO: Implement |
| 131 | + return $css; |
| 132 | + } |
| 133 | + |
| 134 | + public function cssJanus( $css ) { |
| 135 | + // TODO: Implement |
| 136 | + return $css; |
| 137 | + } |
| 138 | +} |
Property changes on: branches/resourceloader/phase3/includes/ResourceLoader.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 139 | + native |